1

Running on a multi-core machine, I have bunch of go routines waiting on a channel for CPU intensive tasks. What is the optimal number of Go routines I should use in order to achieve maximum throughput of #tasks/second. Should it be equivalent to the number of cores or be proportional to the number of cores, or something else?

2
  • 2
    This totally depends on the task you're performing are they independent, do they need to be certain order or batch etc, have a look at GOMAXPROCS this might be useful for you Commented Dec 25, 2017 at 6:03
  • 1
    The only way to know is to benchmark multiple configurations of your specific code, and on your specific hardware, and on your specific OS, and under your specific load, and see which works best. Commented Dec 25, 2017 at 9:49

2 Answers 2

5

I think you are missing a point of goroutines, they are not OS threads and you should not care about their number (until you reach something like a million goroutine). Making less or more of them barely changes the performance as Go runtime will take care of scheduling them on real OS threads.

The number of real OS threads is controled by GOMAXPROCS (you could set it with programming or as an environment variable). It's default to the number of cores on your machine.

The point is only active goroutines are scheduled on OS threads and inactive ones (like one is waiting on a socket) are not taking any CPU resource.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. So I guess the answer is that it does not matter how many go routines I have. As long as I have GOMAXPROCS set to the number of CPUs available I would have to trust Go scheduler to fully utilize the CPUs.
@JoeLin If you have a lot of CPU intensive goroutines and GOMAXPROCS equal to your CPU count it will utilize all of your CPU. You may want to benchmark producing less goroutines if you are sure that they don't block to minimize switching cost but my speculation is it doesn't matter much.
Starting Go 1.5 GOMAXPROCS is already set equal to the number of cores available on the system(golang.org/doc/go1.5#introduction) and using as many goroutines is not going to consume all of the CPU's, it's just that they are mapping to as many threads running on the CPU's. The OS scheduler is still going to do it's work. Also the GOMAXPROCS function call will go away (golang.org/pkg/runtime/#GOMAXPROCS) eventually. You are better off benchmarking to find out what works best for you.
3

Since your tasks are CPU bound (rather than IO bound) you probably wouldn't benefit from having too many goroutines for sure (since they would hardly block, it may only add a switching overhead between them). If your all your tasks finish in almost the same time, then benchmarking with few numbers (of goroutines) which are multiples of GOMAXPROCS may not be a bad idea to start with.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.