Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // this will split the work between all available threads
- // if chunk_count is -1, default number of chunks will be computed
- void JobQue::parallel_for ( int from, int to, const JobChunk & chunk, int chunk_count, int step )
- {
- if ( from >= to )
- return;
- // so the idea here is that if say 1 thread is busy
- // we don't pay 2x time. smaller chunks are more evenly distributed
- if ( chunk_count==-1 )
- chunk_count = mThreadCount * 4;
- // adjust step
- step = max ( ( to - from + 1 ) / chunk_count, step );
- int numChunks = (to - from + step) / step;
- // if only one chunk
- if ( numChunks==1 )
- {
- chunk(from, to);
- return;
- }
- // results
- int onMainThread = max ( (numChunks + mThreadCount) / (mThreadCount+1), 1 );
- int onThreads = numChunks - onMainThread;
- vector<JobStatus> status(onThreads);
- // que parallel ones
- for ( int ch = 0; ch < onThreads; ++ch )
- {
- int i0 = from + ch * step;
- int i1 = min(i0 + step, to);
- push([=,&chunk,&status](){
- PROFILE_BLOCKI("Main","PFor",0xff3f3f3f);
- chunk(i0, i1);
- status[ch].Notify();
- });
- }
- // run the tail
- for ( int ch = onThreads; ch < numChunks; ++ch )
- {
- int i0 = from + ch * step;
- int i1 = min(i0 + step, to);
- chunk(i0, i1);
- }
- // and wait for them (but last one)
- {
- PROFILE_BLOCKI("Main","WaitForPFor",0xffff0000);
- for ( int i=0; i<onThreads; ++i)
- status[i].Wait();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement