Runtime.
--
Aram Hăvărneanu
You can set this at runtime
runtime.GOMAXPROCS(runtime.NumCPU())
Where NumCPU() is the number of logical cores/cpus available.
$ GOMAXPROCS=72 go run cpu.go
ENV says GOMAXPROCS: :72:
runtime says MAXPROCS = 72
On Mon, Mar 12, 2012 at 22:57, Hotei <hote...@gmail.com> wrote:
> $ ./envprint
> ENV says GOMAXPROCS: :: <-- empty string
> runtime says MAXPROCS = 6
--
matt kane's brain
http://hydrogenproject.com
Kyle,Are you sure? Looks like the latest runtime knows how many CPUs without an explict ENV setting.
func main() {fmt.Printf("ENV says GOMAXPROCS: :%s: \n", os.Getenv("GOMAXPROCS")
fmt.Printf("runtime says MAXPROCS = %d \n", runtime.NumCPU())
After about an hour of experimentation here's what I found: If I want to use a specific number of 'cores', I MUST set that number in the program with:runtime.GOMAXPROCS(n)This seems to enforce a limit on the number of CPUs active, regardless of how many goroutines might be called to run on them.Sensible values for n range from 1 up to the number of cores in my AMD CPU ( x 2 for Intel with active HT). Setting it to 1 will make some programs faster, but most will benefit from higher numbers - providing more cores are available of course.I can choose to set n using the GOMAXPROCS environment variable, but I have to do that explicitly by converting it to an int n and then calling the runtime.GOMAXPROCS(n) for it to "stick".
I can choose to let n "default", which on my system makes it use the value of runtime.NumCPU(). Others as noted in posts above have reported different results for their systems.
This applies to my Ubuntu 11.04/AMD weekly 2012-3-4 for sure and ? others.
This seems to enforce a limit on the number of CPUs active, regardless of how many goroutines might be called to run on them.
Setting it to 1 will make some programs faster, but most will benefit from higher numbers - providing more cores are available of course.
I can choose to set n using the GOMAXPROCS environment variable, but I have to do that explicitly by converting it to an int n and then calling the runtime.GOMAXPROCS(n) for it to "stick".
I can choose to let n "default", which on my system makes it use the value of runtime.NumCPU(). Others as noted in posts above have reported different results for their systems.
If there is no GOMAXPROCS variable in the environment, the default value on all systems is 1. The source code is at http://r60.golang.org/src/pkg/runtime/proc.c, line 195.
This applies to my Ubuntu 11.04/AMD weekly 2012-3-4 for sure and ? others.If you are counting OS threads, note that Go run-time may create more OS threads than the value returned by runtime.GOMAXPROCS(-1).
For me the key is that "GOMAXPROCS" inside the program, the "GOMAXPROCS=n" environment variable, and "NumCPU()" can represent 3 distinct, potentially non-overlapping entities.
My informal understanding has always been that GOMAXPROCS is the
number of operating system threads that the go runtime will multiplex
goroutines onto. In addition, the go runtime may have a number of
other threads, related to cgo, or polling, or other potentially
blocking things. Maybe that helps