推荐并行化多通道算法的方法

huangapple go评论83阅读模式
英文:

Recommend method to parallelize a multipass-algorithm

问题

我正在为一种自定义视频格式(QTC)编写一个解码器和编码器。解码过程包括多个阶段,每个阶段的输出都传递给下一个阶段:

  1. 反序列化输入流
  2. 使用范围编码器生成符号序列
  3. 从符号流生成图像流
  4. 将图像流序列化为输出格式

第三和第四步几乎占据了所有的处理时间,第三步大约占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:

  1. deserialize the input stream
  2. generate a sequence of symbols with a range coder
  3. generate a stream of images from the symbol stream
  4. 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

huangapple
  • 本文由 发表于 2013年4月7日 04:43:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/15855822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定