49

Goroutines are light-weight processes that are automatically time-sliced onto one or more operating system threads by the Go runtime. (This is a really cool feature of Go!)

Suppose I have a concurrent application like a webserver. There is plenty of stuff happening concurrently in my hypothetical program, without much non-concurrent (Amdahl's Law) ratio.

It seems that the default number of operating system threads in use is currently 1. Does this mean that only one CPU core gets used?

If I start my program with

runtime.GOMAXPROCS(runtime.NumCPU())

will that give reasonably efficient use of all the cores on my PC?

Is there any "parallel slackness" benefit from having even more OS threads in use, e.g. via some heuristic

runtime.GOMAXPROCS(runtime.NumCPU() * 2)

?

3 Answers 3

78

From the Go FAQ:

Why doesn't my multi-goroutine program use multiple CPUs?

You must set the GOMAXPROCS shell environment variable or use the similarly-named function of the runtime package to allow the run-time support to utilize more than one OS thread.

Programs that perform parallel computation should benefit from an increase in GOMAXPROCS. However, be aware that concurrency is not parallelism.

(UPDATE 8/28/2015: Go 1.5 is set to make the default value of GOMAXPROCS the same as the number of CPUs on your machine, so this shouldn't be a problem anymore)

And

Why does using GOMAXPROCS > 1 sometimes make my program slower?

It depends on the nature of your program. Problems that are intrinsically sequential cannot be sped up by adding more goroutines. Concurrency only becomes parallelism when the problem is intrinsically parallel.

In practical terms, programs that spend more time communicating on channels than doing computation will experience performance degradation when using multiple OS threads. This is because sending data between threads involves switching contexts, which has significant cost. For instance, the prime sieve example from the Go specification has no significant parallelism although it launches many goroutines; increasing GOMAXPROCS is more likely to slow it down than to speed it up.

Go's goroutine scheduler is not as good as it needs to be. In future, it should recognize such cases and optimize its use of OS threads. For now, GOMAXPROCS should be set on a per-application basis.

In short: it is very difficult to make Go use "efficient use of all your cores". Simply spawning a billion goroutines and increasing GOMAXPROCS is just as likely to degrade your performance as speed it up because it will be switching thread contexts all the time. If you have a large program that is parallelizable, then increasing GOMAXPROCS to the number of parallel components works fine. If you have a parallel problem embedded in a largely non-parallel program, it may speed up, or you may have to make creative use of functions like runtime.LockOSThread() to ensure the runtime distributes everything correctly (generally speaking Go just dumbly spreads currently non-blocking Goroutines haphazardly and evenly among all active threads).

Also, GOMAXPROCS is the number of CPU cores to use, if it's greater than NumCPU I'm fairly sure that it simply clamps to NumCPU. GOMAXPROCS isn't strictly equal to the number of threads. I'm not 100% sure of exactly when the runtime decides to spawn new threads, but one instance is when the number of blocking goroutines using runtime.LockOSThread() is greater than or equal to GOMAXPROCs -- it will spawn more threads than cores so it can keep the rest of the program running sanely.

Basically, it's quite simple to increase GOMAXPROCS and make go use all cores of your CPU. It's quite another thing at this point in Go's development to actually get it to smartly and efficiently use all cores of your CPU, requiring a lot of program design and finagling to get right.

4
  • I know what runtime says, I was told differently here: groups.google.com/forum/#!topic/golang-nuts/z_7F6EpcCLY Maybe it's implementation dependent?
    – Linear
    Jul 26, 2013 at 10:23
  • Ah! That's a useful thread. "There's an argument for the Go scheduler always keeping a spare thread around for short-term requests. If no running goroutine has given up control in the last N milliseconds, fork off a new thread, rather than waiting for the compute-bound ones to finish. One thread per CPU isn't a good rule if there are compute-bound tasks." So, a few excess threads would be advisable in this case.
    – Rick-777
    Jul 26, 2013 at 20:38
  • @Rick-777, What you said in quotes: is that a fact or advocation for a new feature? Feb 18, 2016 at 8:25
  • Suppose I have 1,000 independent tasks and want to know how many goroutines I should use to tackle those tasks. GOMAXPROCS? GOMAXPROCS - 1?
    – Seth
    Apr 8, 2017 at 15:05
4

This question cannot be answered, it is much too broad.

Take your problem, your algorithm and your workload and measure what is best for this combination.

Nobody can answer a question like "Is there any heuristic that adding twice as much salt to my lunch will make it taste better?" as this depends on the lunch (tomatoes benefit much more from salt than strawberries) your taste and how much salt there is already. Try it.

On more: runtime.GOMAXPROCS(runtime.NumCPU()) has achieved cult status but controlling the number of threads by setting the GOMAXPROCS environment variable from the outside might be the much better option.

1
  • 1
    Surely people have acquired experience from writing webservers and similar highly-concurrent apps?
    – Rick-777
    Jul 26, 2013 at 20:31
3

runtime.GOMAXPROCS() sets the number of (virtual) CPU cores that your program can use simultaneously. Allowing Go to use more CPU cores than you actually have won't help, as your system only has so many CPU cores.

In order to run in more than one thread, your program has to have several goroutines, typically function calls with go someFunc(). If your program doesn't start any additional goroutines it will naturally run in only one thread no matter how many CPUs/cores you allow it to use.

Check out this and the following exercises on how to create goroutines.

2
  • 2
    Allowing Go to use more OS threads than there are CPUs may sometimes reduce the latency because there could be more "ready to run" if any of the threads should get blocked, however briefly.
    – Rick-777
    Jul 26, 2013 at 20:30
  • @Rick-777: Sure. Setting runtime.GOMAXPROCS doesn't change the number of threads that your program can run, however. It changes the number of CPUs on which those threads can run. Check out runtime.NumGoroutine to see how many threads/goroutines your program has spawned.
    – sjakobi
    Jul 26, 2013 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.