英文:
Recommend method to parallelize a multipass-algorithm
问题
我正在为一种自定义视频格式(QTC)编写一个解码器和编码器。解码过程包括多个阶段,每个阶段的输出都传递给下一个阶段:
- 反序列化输入流
- 使用范围编码器生成符号序列
- 从符号流生成图像流
- 将图像流序列化为输出格式
第三和第四步几乎占据了所有的处理时间,第三步大约占35%,第四步大约占60%,第一步和最后一步相对简单。
在并行运行这四个步骤时,推荐和惯用的方法是什么?我主要关注如何处理各个部分之间的通信。我计划为第二步和第三步分别使用一个Goroutine,并使用带缓冲的通道将它们连接起来。这样做对吗?
英文:
I am writing a de- and encoder for a custom video format (QTC). The decoding process consists of multiple stages, where the output of each stage is passed to the next stage:
- deserialize the input stream
- generate a sequence of symbols with a range coder
- generate a stream of images from the symbol stream
- serialize the image stream into an output format
Steps three and four take up almost all of the processing time, step three takes roughly 35% and step four takes about 60%, the first and the last step are rather trivial.
What is the recommend and ideomatic way to runs the four steps in parallel? I am mostly interested in how to handle the communication between the parts. I plan to use one Goroutine for step two and one for step three, the routines are connected with a buffered channel. Is this the right way?
答案1
得分: 3
对于一些任务,通过谨慎使用互斥锁来“保护”“共享”数据结构可能更容易,但在Go中,使用缓冲通道是“标准”方法,对于你的任务来说,这似乎也是正确的解决方案。
你在这里提问是因为你遇到了什么问题吗?
英文:
For some tasks having a "shared" data structure "protected" by careful use of mutexes is easier, but a buffered channel would be the "standard" way to do this in Go and for your task it sounds like the right solution, too.
Are you running into any problems with it since you are asking here?
答案2
得分: 0
不要过于思考。只是写一些代码。Go代码很容易改变 - 编译器会帮助你。写一些测试/基准来保持诚实。
Alex
英文:
Do not over think. Just write some code. Go code is easy to change - compiler holds your hand. Write some tests / benchmarks to keep yourself honest.
Alex
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论