doukong1391 2016-04-08 05:43
浏览 74
已采纳

使用GOMAXPROCS进行并行编程

I have seen people setting runtime.GOMAXPROCS to runtime.NumCPU() to enable parallel processing in go. Official documentation doesn't say anything about the upper bound of GOMAXPROCS; can I set this to any arbitrary positive integer or should it always be less than eq. to NumCPU value?

I tried setting it to a number that's greater than the # of logical cores, my code works just fine

  • 写回答

2条回答 默认 最新

  • dongyan7851 2016-04-08 05:55
    关注

    The max is 256; but note, it will not error if you set it higher.

    https://golang.org/src/runtime/debug.go?s=534:560#L7

    12  // GOMAXPROCS sets the maximum number of CPUs that can be executing
    13  // simultaneously and returns the previous setting.  If n < 1, it does not
    14  // change the current setting.
    15  // The number of logical CPUs on the local machine can be queried with NumCPU.
    16  // This call will go away when the scheduler improves.
    17  func GOMAXPROCS(n int) int {
    18      if n > _MaxGomaxprocs {
    19          n = _MaxGomaxprocs
    20      }
    21      lock(&sched.lock)
    22      ret := int(gomaxprocs)
    23      unlock(&sched.lock)
    24      if n <= 0 || n == ret {
    25          return ret
    26      }
    27  
    28      stopTheWorld("GOMAXPROCS")
    29  
    30      // newprocs will be processed by startTheWorld
    31      newprocs = int32(n)
    32  
    33      startTheWorld()
    34      return ret
    35  }
    

    Line 19 sets the total number to _MaxGomaxprocs.

    Which is...

    https://golang.org/src/runtime/runtime2.go?h=_MaxGomaxprocs#L407

    const (
        // The max value of GOMAXPROCS.
        // There are no fundamental restrictions on the value.
        _MaxGomaxprocs = 1 << 8
    )
    

    That bitshift in binary form is 100000000 and 1 is an int which on 64bit systems Go makes that an Int64, which means the max is 256. (32bit systems with int in Go would be int32, but same value of 256)

    Now, as far as setting this to more than your number of cores - it all depends on your workload and CPU context switching that is happening (e.g. how many locks does your go program force to happen, or do you use mutex.Lock() everywhere, etc).

    Remember, Golang abstracts away the context switching if it needs to happen or not.

    Benchmarking is your friend here. If your app runs 10,000 goroutines with little to no cpu context switching (following good design patterns), then yes bump that sucker to 256 and let it ride. If you app chokes and creates a lot of CPU wait-time with context switching/threads, then set it to 8 or 4 even to give it room to breath with all that mutex locking going on.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法