]> Cypherpunks repositories - gostls13.git/commitdiff
add runtime.GOMAXPROCS, allowing a program to, in effect, set $GOMAXPROCS
authorRob Pike <r@golang.org>
Thu, 6 Aug 2009 20:07:05 +0000 (13:07 -0700)
committerRob Pike <r@golang.org>
Thu, 6 Aug 2009 20:07:05 +0000 (13:07 -0700)
R=rsc
DELTA=29  (28 added, 1 deleted, 0 changed)
OCL=32829
CL=32837

src/pkg/runtime/extern.go
src/pkg/runtime/proc.c
test/bench/spectral-norm-parallel.go

index 1f6561394c5625567301b787a8e933cafe8f3af2..d002e48136fcff16e702c5d7234d74b003897577 100644 (file)
@@ -40,3 +40,6 @@ func LockOSThread()
 // If the calling goroutine has not called LockOSThread, UnlockOSThread is a no-op.
 func UnlockOSThread()
 
+// GOMAXPROCS sets the maximum number of CPUs that can be executing
+// simultaneously.   This call will go away when the scheduler improves.
+func GOMAXPROCS(n int)
index 3c8ef54fe73303f3e5c67dfdc6ca75ade10526de..47356851b9ea2bf6df096e002b8f5259905c13e7 100644 (file)
@@ -807,6 +807,29 @@ runtime·LockOSThread(void)
        g->lockedm = m;
 }
 
+// delete when scheduler is stronger
+void
+runtime·GOMAXPROCS(int32 n)
+{
+       if(n < 1)
+               n = 1;
+
+       lock(&sched);
+       sched.gomaxprocs = n;
+       sched.mcpumax = n;
+       // handle fewer procs
+       while(sched.mcpu > sched.mcpumax) {
+               noteclear(&sched.stopped);
+               sched.waitstop = 1;
+               unlock(&sched);
+               notesleep(&sched.stopped);
+               lock(&sched);
+       }
+       // handle more procs
+       matchmg();
+       unlock(&sched);
+}
+
 void
 runtime·UnlockOSThread(void)
 {
@@ -821,4 +844,3 @@ runtime·mid(uint32 ret)
        ret = m->id;
        FLUSH(&ret);
 }
-
index 0de2273ee706bbc9df390a99300cfa3a68efada6..9e7f33885f7cbf941b05d2fe7cbffac3d8c5956c 100644 (file)
@@ -40,6 +40,7 @@ import (
        "flag";
        "fmt";
        "math";
+       "runtime";
 )
 
 var n = flag.Int("n", 2000, "count")
@@ -92,6 +93,7 @@ func (v Vec) ATimesTransp(u Vec) {
 
 func main() {
        flag.Parse();
+       runtime.GOMAXPROCS(*nCPU);
        N := *n;
        u := make(Vec, N);
        for i := 0; i < N; i++ {