1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Parallel for algorithm.
12 // the thread's iteration space [32lsb, 32msb)
20 byte pad
[CacheLineSize
];
24 runtime_parforalloc(uint32 nthrmax
)
28 // The ParFor object is followed by CacheLineSize padding
29 // and then nthrmax ParForThread.
30 desc
= (ParFor
*)runtime_malloc(sizeof(ParFor
) + CacheLineSize
+ nthrmax
* sizeof(ParForThread
));
31 desc
->thr
= (ParForThread
*)((byte
*)(desc
+1) + CacheLineSize
);
32 desc
->nthrmax
= nthrmax
;
37 runtime_parforsetup(ParFor
*desc
, uint32 nthr
, uint32 n
, bool wait
, const FuncVal
*body
)
42 if(desc
== nil
|| nthr
== 0 || nthr
> desc
->nthrmax
|| body
== nil
) {
43 runtime_printf("desc=%p nthr=%d count=%d body=%p\n", desc
, nthr
, n
, body
);
44 runtime_throw("parfor: invalid args");
58 for(i
=0; i
<nthr
; i
++) {
59 begin
= (uint64
)n
*i
/ nthr
;
60 end
= (uint64
)n
*(i
+1) / nthr
;
61 pos
= &desc
->thr
[i
].pos
;
62 if(((uintptr
)pos
& 7) != 0)
63 runtime_throw("parforsetup: pos is not aligned");
64 *pos
= (uint64
)begin
| (((uint64
)end
)<<32);
69 runtime_parfordo(ParFor
*desc
)
72 uint32 tid
, begin
, end
, begin2
, try, victim
, i
;
73 uint64
*mypos
, *victimpos
, pos
, newpos
;
75 void (*bodyfn
)(ParFor
*, uint32
);
78 // Obtain 0-based thread index.
79 tid
= runtime_xadd(&desc
->thrseq
, 1) - 1;
80 if(tid
>= desc
->nthr
) {
81 runtime_printf("tid=%d nthr=%d\n", tid
, desc
->nthr
);
82 runtime_throw("parfor: invalid tid");
86 bodyfn
= (void (*)(ParFor
*, uint32
))(void*)body
->fn
;
88 // If single-threaded, just execute the for serially.
90 for(i
=0; i
<desc
->cnt
; i
++)
91 __builtin_call_with_static_chain (bodyfn(desc
, i
), body
);
99 // While there is local work,
100 // bump low index and execute the iteration.
101 pos
= runtime_xadd64(mypos
, 1);
102 begin
= (uint32
)pos
-1;
103 end
= (uint32
)(pos
>>32);
105 __builtin_call_with_static_chain(bodyfn(desc
, begin
), body
);
111 // Out of work, need to steal something.
114 // If we don't see any work for long enough,
115 // increment the done counter...
116 if(try > desc
->nthr
*4 && !idle
) {
118 runtime_xadd(&desc
->done
, 1);
120 // ...if all threads have incremented the counter,
122 if(desc
->done
+ !idle
== desc
->nthr
) {
124 runtime_xadd(&desc
->done
, 1);
127 // Choose a random victim for stealing.
128 victim
= runtime_fastrand1() % (desc
->nthr
-1);
131 victimpos
= &desc
->thr
[victim
].pos
;
133 // See if it has any work.
134 pos
= runtime_atomicload64(victimpos
);
136 end
= (uint32
)(pos
>>32);
142 runtime_xadd(&desc
->done
, -1);
145 begin2
= begin
+ (end
-begin
)/2;
146 newpos
= (uint64
)begin
| (uint64
)begin2
<<32;
147 if(runtime_cas64(victimpos
, pos
, newpos
)) {
153 // Has successfully stolen some work.
155 runtime_throw("parfor: should not be idle");
156 runtime_atomicstore64(mypos
, (uint64
)begin
| (uint64
)end
<<32);
158 me
->nstealcnt
+= end
-begin
;
162 if(try < desc
->nthr
) {
164 } else if (try < 4*desc
->nthr
) {
166 runtime_procyield(20);
167 // If a caller asked not to wait for the others, exit now
168 // (assume that most work is already done at this point).
169 } else if (!desc
->wait
) {
171 runtime_xadd(&desc
->done
, 1);
173 } else if (try < 6*desc
->nthr
) {
183 runtime_xadd64(&desc
->nsteal
, me
->nsteal
);
184 runtime_xadd64(&desc
->nstealcnt
, me
->nstealcnt
);
185 runtime_xadd64(&desc
->nprocyield
, me
->nprocyield
);
186 runtime_xadd64(&desc
->nosyield
, me
->nosyield
);
187 runtime_xadd64(&desc
->nsleep
, me
->nsleep
);
195 // For testing from Go.
197 runtime_parforiters(ParFor
*desc
, uintptr tid
, uintptr
*start
, uintptr
*end
)
199 *start
= (uint32
)desc
->thr
[tid
].pos
;
200 *end
= (uint32
)(desc
->thr
[tid
].pos
>>32);