一个 goroutine 是否一定在不同的 CPU 上运行?

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

Does a goroutine necessarily run on a different CPU?

问题

以下是翻译好的内容:

以下摘录来自https://go.dev/doc/effective_go#parallel。

我们在循环中独立地启动这些片段,每个CPU一个。它们可以以任何顺序完成,但这并不重要;我们只需在启动所有goroutine后通过从通道中读取来计算完成信号。

const numCPU = 4 // CPU核心数

func (v Vector) DoAll(u Vector) {
    c := make(chan int, numCPU)  // 缓冲区大小可选,但是合理。
    for i := 0; i < numCPU; i++ {
        go v.DoSome(i*len(v)/numCPU, (i+1)*len(v)/numCPU, u, c)
    }
    // 清空通道。
    for i := 0; i < numCPU; i++ {
        <-c    // 等待一个任务完成
    }
    // 全部完成。
}

为什么文章中指定了"每个CPU一个"?多个goroutine不需要在不同的CPU上执行。实际上,子节的最后一段提醒读者并不要混淆并发(concurrency)和并行(parallelism)的概念:

请确保不要混淆并发(concurrency)——将程序结构化为独立执行的组件——和并行(parallelism)——在多个CPU上为了效率而并行执行计算。

英文:

The following excerpt is from https://go.dev/doc/effective_go#parallel.

> We launch the pieces independently in a loop, one per CPU. They can complete in any order but it doesn't matter; we just count the completion signals by draining the channel after launching all the goroutines.

const numCPU = 4 // number of CPU cores

func (v Vector) DoAll(u Vector) {
    c := make(chan int, numCPU)  // Buffering optional but sensible.
    for i := 0; i &lt; numCPU; i++ {
        go v.DoSome(i*len(v)/numCPU, (i+1)*len(v)/numCPU, u, c)
    }
    // Drain the channel.
    for i := 0; i &lt; numCPU; i++ {
        &lt;-c    // wait for one task to complete
    }
    // All done.
}

Why does the article specify "one per CPU"? Multiple goroutines need not be executed on different CPUs. In fact, the last paragraph in the sub-section reminds the reader that concurrency is not parallelism:

> Be sure not to confuse the ideas of concurrency—structuring a program as independently executing components—and parallelism—executing calculations in parallel for efficiency on multiple CPUs.

答案1

得分: 2

一个 goroutine 是否一定在不同的 CPU 上运行?

不一定,但可能会。

这里没有什么特别的。

为什么文章中指定“每个 CPU 一个”?

它本可以说 5 或者 2。实际上这里没有任何重要的隐藏信息。这只是一个例子,而不是 goroutine 调度的规范。

英文:

> Does a goroutine necessarily run on a different CPU?

No, but it might.

Nothing to see here.

> why the article specifies "one per CPU"

It could have said 5 or 2. Really there is nothing of importance hidden here. This is just an example, not the specification of goroutine scheduling.

答案2

得分: 0

为什么文章要指定“每个CPU一个”?多个goroutine不需要在不同的CPU上执行。实际上,子节的最后一段提醒读者并发不等于并行。

goroutine与操作系统线程(M:N)映射,并且每个goroutine一次只能使用一个线程,但无法预测它是否使用一个CPU来执行所有4个(GOMAXPROCS)goroutine,或者使用2个CPU、3个CPU或所有4个CPU。这取决于许多因素,而所有这些复杂性都被Go运行时隐藏起来。

英文:

> Why does the article specify "one per CPU"? Multiple goroutines need
> not be executed on different CPUs. In fact, the last paragraph in the
> sub-section reminds the reader that concurrency is not parallelism:

goroutines are mapped with OS threads (M:N) and each goroutine can use a maximum of one thread at a time but you can not predict whether it is using the 1 CPU to execute all the 4 (GOMAXPROCS) goutines or it is use 2 cpu or 3 cpu or all 4 cpu.It is depending upon many factor's and all these complexity is hidden by go runtime

huangapple
  • 本文由 发表于 2022年6月27日 21:33:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/72773006.html
匿名

发表评论

匿名网友

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

确定