TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Go concurrency isn't parallelism: Real world lessons with Monte Carlo sims

57 pointsby soroushjpover 10 years ago

16 comments

bkeroackover 10 years ago
TLDR: Adjust GOMAXPROCS if you want a speedup from multiple goroutines.<p><a href="http://golang.org/pkg/runtime/" rel="nofollow">http:&#x2F;&#x2F;golang.org&#x2F;pkg&#x2F;runtime&#x2F;</a><p>&quot;GOMAXPROCS sets the maximum number of CPUs that can be executing simultaneously and returns the previous setting. If n &lt; 1, it does not change the current setting. The number of logical CPUs on the local machine can be queried with NumCPU. This call will go away when the scheduler improves.&quot;<p>It will be nice when this requirement is eliminated.
评论 #9014506 未加载
评论 #9014924 未加载
spullaraover 10 years ago
I think that the default of single threaded is going to bite them hard in the long run. I&#x27;ve already discovered libraries that were never tested with MAXGOPROCS &gt; 1 that are not thread safe. They should default it to at least 2 to make sure these bugs are shaken out.
评论 #9014540 未加载
评论 #9015031 未加载
评论 #9028081 未加载
评论 #9014526 未加载
tshadwellover 10 years ago
Isn&#x27;t this a misleading title? The article is essentially the author forgetting to set GOMAXPROCS, not really a lack of parallelism in Go.
评论 #9015822 未加载
评论 #9016809 未加载
评论 #9016731 未加载
intortusover 10 years ago
Another error the author made is adding to a sync.WaitGroup in a different goroutine than the one that waits. This is another rookie mistake that go test -race would probably catch.
评论 #9014980 未加载
评论 #9014597 未加载
评论 #9016811 未加载
tux1968over 10 years ago
OT: The way we use the terms parallel and concurrent in computer science seems completely backward to me. The dictionary says &quot;concurrent&quot; means at the same time, and parallel lines need not be drawn at the same moment...<p>Yet in CS we talk of things being concurrent even if they&#x27;re executed as cooperative threads on a single core and parallel only applies when executing concurrently (at the same time).
评论 #9015069 未加载
评论 #9014965 未加载
replicantover 10 years ago
Unrelated question, isn&#x27;t it a bad idea to update the seed for every sample?
评论 #9014546 未加载
评论 #9014560 未加载
评论 #9014550 未加载
评论 #9014428 未加载
评论 #9016814 未加载
SixSigmaover 10 years ago
No-one has ever claimed it is, in fact they specifically tell you it isn&#x27;t, multiple times<p>A Jan 2013 Go lang blog post<p><a href="http://blog.golang.org/concurrency-is-not-parallelism" rel="nofollow">http:&#x2F;&#x2F;blog.golang.org&#x2F;concurrency-is-not-parallelism</a><p>reminding people of the Jan 2012 talk Rob Pike did on the subject<p><a href="https://talks.golang.org/2012/waza.slide" rel="nofollow">https:&#x2F;&#x2F;talks.golang.org&#x2F;2012&#x2F;waza.slide</a><p>Feb 2011 : Rob Pike on Parallelism and Concurrency in Programming Languages<p><a href="http://www.infoq.com/interviews/pike-concurrency" rel="nofollow">http:&#x2F;&#x2F;www.infoq.com&#x2F;interviews&#x2F;pike-concurrency</a><p>I&#x27;ll skip all the intermediate steps from there back to<p>Tony Hoare<p><a href="http://www.usingcsp.com/" rel="nofollow">http:&#x2F;&#x2F;www.usingcsp.com&#x2F;</a>
评论 #9016813 未加载
cubanoover 10 years ago
I have just started a Go project for a core aspect of my business, so this information was well timed, for me at least, and gave me a quick overview of concurrency&#x2F;parallelism in Go.<p>I will be developing both concurrent and parallel threads in my app, so this was very enlightening.<p>Thanks to the author for his clear writing style and efforts to educate.
pbnjayover 10 years ago
Its a decent intro, but I think instead of just jumping into parallel code, its better to read the docs before hand. There are plenty of references to GOMAXPROCS and the thread safety of math&#x2F;rand (and most of the stdlib).
评论 #9016815 未加载
omniover 10 years ago
This is a somewhat trivial suggestion, but it would be much more clear to your readers what the speedup was if you aligned the values in the benchmark results.
dschiptsovover 10 years ago
<a href="http://joearms.github.io/2013/04/05/concurrent-and-parallel-programming.html" rel="nofollow">http:&#x2F;&#x2F;joearms.github.io&#x2F;2013&#x2F;04&#x2F;05&#x2F;concurrent-and-parallel-...</a><p>Parallel, at least in English, means to have nothing in common with others. On most OSes it is a <i>process</i> affined to a dedicated CPU which is configured to access a dedicated physical I&#x2F;O device only. Everything else is concurrent.
nickbaumanover 10 years ago
Parallelism is like hunting elephants. The languages we use like Java, Ruby, Python and C++, for example, give you weapons for the hunt on the level of a very strong toothpick at best. Go has apparently upgraded the situation to a 3&quot; pocket knife. Clojure to the level of a proper spear. But we need languages that allow us to completely avoid hunting elephants in the first place.
kid0m4nover 10 years ago
Have sent a PR handling all the wrinkles: <a href="https://github.com/soroushjp/go-parallelism-monte-carlo-demo/pull/2" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;soroushjp&#x2F;go-parallelism-monte-carlo-demo...</a><p>* There is a race condition where wg.Wait() might execute before the wg.Add(1) runs * The wait group was not necessary as we are already waiting for all the results from the channel
评论 #9016816 未加载
soroushjpover 10 years ago
Thanks for all the feedback everyone. Incorporated everything to make the code as good as possible, want readers to learn as much as possible. Just to be clear, this article was my way of highlighting Rob Pike&#x27;s point that concurrency <i>isn&#x27;t</i> parallelism, not to make it seem as if Go didn&#x27;t support or was falsely claiming true parallelism.
wpetersonover 10 years ago
It looks like you didn&#x27;t allow for using more than a single process by setting GOMAXPROCS.<p>Additionally, it looks like you&#x27;re re-seeding your random engine inside your sample loop, which is very slow. You only need to seed the engine outside the loop at the beginning.
评论 #9016817 未加载
zzzcpanover 10 years ago
I wouldn&#x27;t call this &quot;real world&quot;. In a real world you are better off distributing these kind of tasks across multiple machines. Multicore parallelism per se is overrated and overhyped.