Jump to content

I have a for loop which is can be made parallel. I need to apply affinity scheduling to it. Essentially I am not using the standard parallel for implementation:

#pragma omp parallel for

I am explicitly assigning iterations to threads by changing the upper and lower bounds of the for loop to relate to the thread number:

#define N 500
/* main code ommitted */ 
#pragma omp parallel /* Declarations of shared, private variables will be here */
{
int num_threads = omp_get_num_threads();
int threadnum = omp_get_thread_num();
int start = N*threadnum/num_threads;
int end = N*(threadnum+1)/num_threads;
  for (i=start; i<end; i++){
     /* LOOP */
  }         
}

This means each thread is initialised with n/num_threads iterations. From this I need to split this up into chunks of 1/num_threads. When a thread has completed all it's chunks for it's local set it finds the thread with the most remaining chunks and begins executing those. I need to find a way to implement this. And I'm not sure where to begin. I don't know how to split the iterations into chunks manually. 

Link to comment
https://linustechtips.com/topic/914276-openmp-programming-schedule/
Share on other sites

Link to post
Share on other sites

5 minutes ago, gabrielcarvfer said:

To assign the number of threads in a parallel for, you can use num_threads(x) to divide the work in x threads. 

If you want to manually assign this kind of stuff, basically defining the work-stealing scheme, you should be using pthreads.

I can assign the number of threads at run time using export. I need to be able to manually assign iterations to threads and then make those threads dynamically switch to another chunk when it completes all of it's assigned chunks. I haven't used pthreads before. 

Link to post
Share on other sites

22 hours ago, gabrielcarvfer said:

Yes, but I'm telling you that you won't be able to do that manually with OpenMP, as it already takes care of that and can use a job-stealing scheme (GCC seems to use one). 

In pthreads, you create the threads yourself, send jobs to them and implement the job stealing. You will need to use locks to control access to shared variables and it can be really painful if you're learning concurrent programming.

How can I go about implementing a job stealing scheme within OpenMP? I'm able to split the work up between the threads, but I need to find a way to allow one thread to steal the work from another. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×