14

I am new to Google Go (Golang). My question is related to this post What exactly does runtime.Gosched do?. The structure of code is as copied below. My question, is that when I change the number of processor in GOMAXPROCS, how do I verify how many processors it is running on. When I do 'top', it shows a.out process which consumes 100% or less resources even when GOMAXPROCS is more than 1. I would be grateful for your help.

package main

import (
    "fmt"
    "runtime"
    "sync"
)

var wg sync.WaitGroup

func doTasks() {
    fmt.Println(" Doing task ")
    for ji := 1; ji < 100000000; ji++ {
        for io := 1; io < 10; io++ {
            //Some computations
        }
    }
    runtime.Gosched()

    wg.Done()
}

func main() {
    wg.Add(1)
    runtime.GOMAXPROCS(1) // or 2 or 4
    go doTasks()
    doTasks()
    wg.Wait()
}
Community
  • 1
  • 1
user984260
  • 3,401
  • 5
  • 25
  • 38
  • What OS you're testing this on? Also it would be helpful to know which CPU you use. I can't reproduce the behaviour. – nemo Nov 05 '12 at 15:48
  • cat /etc/*-release returns: Red Hat Enterprise Linux Workstation release 6.3 (Santiago). OS is linux. CPU is Intel(R) Xeon(R) CPU, X5460 @ 3.16GHz. NumCPU returns 8. – user984260 Nov 05 '12 at 15:59
  • @nemo The program runs perfectly fine. Only I was curious how to see the number of processors on which it is running – user984260 Nov 05 '12 at 16:00
  • 2
    It seems that your curly brackets are not paired (func doTasks() ). This is important because the place of runtime.Gosched() being called matters... – Song Gao Nov 05 '12 at 17:54
  • 4
    Please make some attempt to format your code reasonably. You can paste it into play.golang.org and click `format` and it will do this for you. You will notice it will fail because your code isn't valid go. It's easier to help you when your code is valid. – Dustin Nov 06 '12 at 02:10
  • 3
    I'm not sure about `top` (I rarely touch it these days) but `htop` by default shows you all the LWPs (light-weight processes, that is, OS threads) running in the context of each process. Certainly that is *not* the number of *running* threads but at least it would give you insight on whether the Go runtime decided to create another thread(s) for your program. – kostix Nov 06 '12 at 08:19
  • Thanks. I have reformatted the code. – user984260 Nov 06 '12 at 16:28

2 Answers2

36

The largest number of logical CPUs the process can be running on at a given time is no more than the minimum of runtime.GOMAXPROCS(0) and runtime.NumCPU().

func MaxParallelism() int {
    maxProcs := runtime.GOMAXPROCS(0)
    numCPU := runtime.NumCPU()
    if maxProcs < numCPU {
        return maxProcs
    }
    return numCPU
}
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
  • +1 this looks like a correct solution (or at least interesting). But do you have some sources about the absence of better and more durable ways ? – Denys Séguret Nov 06 '12 at 13:37
  • Not sure if it's just me, but MaxParallelism returns 0 on my 2011 Macbook Air and 8 core Intel server running Debian 7. My workaround is to use runtime.NumCPU which returns 4 for MBA (2 threads on each of 2 cores) and 8 CPU on server (has 8 non-hyperthread core XEON). Testing done with Go 1.2. – Peter Krnjevic Feb 06 '14 at 06:52
  • @PeterKrnjevic: I can't reproduce this. Macbook air 10.9, go 1.2. – Matt Joiner Feb 14 '14 at 02:14
  • @MattJoiner: running this http://play.golang.org/p/IFnlTmTD6Q on my Macbook Air 10.9.1, with go1.2rc2, prints 1. Same on Debian server with go1.2 linux/amd64. Could it be 64bit related? Curious how I got 0 before, though in that case I used your MaxParallelism in a larger program - most likely pebcak. – Peter Krnjevic Feb 14 '14 at 06:54
  • @PeterKrnjevic: I'm not sure. The Macbook was 64bit also. For runtime.NumCPU to work, but not GOMAXPROCS would suggest an error in the Go runtime. Update to a stable release and check it doesn't occur. – Matt Joiner Feb 14 '14 at 13:11
  • @MattJoiner: Okay, found the answer: GOMAXPROCS the function returns the value of the environment variable of the same name. If undefined, GOMAXPROCS defaults to 1. For details see: http://stackoverflow.com/questions/17853831/what-is-the-gomaxprocs-default-value. Note: "This call (GOMAXPROCS) will go away when the scheduler improves" – Peter Krnjevic Feb 14 '14 at 18:09
2

The number of cores can be inquired by http://golang.org/pkg/runtime/#NumCPU.

The documentation says: "NumCPU returns the number of logical CPUs on the local machine."

zzzz
  • 87,403
  • 16
  • 175
  • 139
  • 1
    Thanks. However, I wanted to ask, how to know the number of CPUs, on which current go program is running. This number is less than or equal to NumCPU. – user984260 Nov 05 '12 at 15:33