What is the GOMAXPROCS default value?
Asked Answered
U

4

67

Is it guaranteed that GOMAXPROCS is set to 1 when the environment variable of the same name is not set?

This code shows the value:

package main

import (
    "runtime"
    "fmt"
)

func getGOMAXPROCS() int {
    return runtime.GOMAXPROCS(0)
}

func main() {
    fmt.Printf("GOMAXPROCS is %d\n", getGOMAXPROCS())
}

and running it like this:

$ GOMAXPROCS= go run max.go 
GOMAXPROCS is 1

shows that it is 1 in this case, but I am looking for some confirmation here.

Undersheriff answered 25/7, 2013 at 9:16 Comment(0)
M
76

UPDATE 2018: By default, Go programs run with GOMAXPROCS set to the number of cores available; in prior releases it defaulted to 1.

Starting from Go 1.5, the default value is the number of cores. You only need to explicitly set it if you are not okay with this in newer Go versions.


No, there's no guarantee about what the default is; even though all known implementations use the value '1'. If your code, in absence of the environment variable, requires a specific default value then you should set it in code. Additionally:

GOMAXPROCS sets the maximum number of CPUs that can be executing simultaneously and returns the previous setting. If n < 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.

(Emphasis mine)

Martica answered 25/7, 2013 at 9:22 Comment(5)
GOMAXPROCS shouldn't go away until Go 2.0 at the earliest, otherwise it would break the Go 1 guarantee: that no API changes will be made that break builds or change standard library behavior unless it's a bug fix. Since GOMAXPROCS is a bit odd and affects the runtime directly it may become a suggestion rather than a command, but it won't suddenly disappear on you in Go 1.2 or anything.Imbed
@Jsor If GOMAXPROCS was to change the behavior of something it would sound like a bug to me?Suppositious
@inf It wouldn't be a bug, because it doesn't effect the semantics of the language (that is, the final result). The only time it can affect the semantics is if you're playing with undefined/illegal behavior like race conditions to begin with. Think of GOMAXPROCS like inline directives in C++, the compiler is allowed to ignore it because sometimes it knows better than you, but it also doesn't affect the final result unless you're doing really weird things. GOMAXPROCS is a runtime directive, but the same applies (though there are considerations to be made for things like OpenGL).Imbed
@Jsor Ah ok, that was a misunderstanding then. I thought you you meant that changing the default would be a break the stability guarantee and had the same thoughts that you just described.Suppositious
This answer is out of date as of Go 1.5. See laike9m's answer for the currently correct answer.Coven
J
45

As Go 1.5 Release Notes says

By default, Go programs run with GOMAXPROCS set to the number of cores available; in prior releases it defaulted to 1.

So starting from Go 1.5, the default value should be the number of cores.

Jansen answered 31/10, 2015 at 12:0 Comment(2)
What will happened if GOMAXPROCS > number of coresFeminacy
You could end up with higher CPU Load (tasks fighting for CPU time).Bringingup
P
14

Starting with Go 1.5, GOMAXPROCS is set to number of CPUs available by default. However, you can explicitly set it using GOMAXPROCS environment variable or by calling runtime.GOMAXPROCS.

https://docs.google.com/document/d/1At2Ls5_fhJQ59kDK2DFVhFu3g5mATSXqqV5QrxinasI/preview?sle=true

Pucker answered 28/5, 2015 at 21:40 Comment(1)
Note this is (currently) only a proposal and even if accepted the default setting may be reverted back to 1 if there are unforeseen issues..Socialistic
A
-2
package main

import (
    "fmt"
    "runtime"
)

func main() {
    fmt.Println(runtime.GOMAXPROCS(runtime.NumCPU() - 1))
}

test code

this will output your current thread's, always add this to have a control of ur threading and don't rely on someones defauls.

package main
        
import (
    "fmt"
    "runtime"
)

func main() {
    runtime.GOMAXPROCS(runtime.NumCPU() - 1)
    // and your code goes brrrrrrrrrrrrr....
}

and for those who will ask why -1

GOMAXPROCS sets the maximum number of CPUs that can be executing simultaneously and returns the previous setting. It defaults to the value of runtime.NumCPU. If n < 1, it does not change the current setting. This call will go away when the scheduler improves.

link on original.

utility is our all.

Annoyance answered 16/8, 2022 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.