Proper Thread Balancing Function?

杀马特。学长 韩版系。学妹 提交于 2021-01-07 01:30:42

问题


Consider a parallel program written in OpenMP. Suppose there is a loop that iterates from 0 to N-1. Suppose that there are num_threads threads. Suppose you want to divide the loop iterations so that thread 0 computes the first N/num_threads iterations, thread 1 computes the next N/num_threads iterations, etc. If N is divisible by num_threads and rank is the thread's id (from rank 0 to num_threads-1), then each thread can execute the following code to get its loop start and end index:

int mystart=N/num_threads*rank;
int myend=N/num_threads*(rank+1)-1;
for (int i=mystart; i<=myend; ++i) {
//work would be done here
}

However, if N is not divisible by num_threads, the above strategy will not work. Write a function named start_index that takes three int arguments: N, num_threads, and rank. The function should return the loop start index for that thread based on N and num_threads. To make the allocation as even as possible, no thread should need to compute more than one iteration more than any other thread and the number of iterations for thread i should never be fewer than the number of iterations for thread i+1. If the total number of threads is passed as the rank argument, the function should return N. Therefore, the above code can be translated to:

int mystart=start_index(N,num_threads,rank);
int myend=start_index(N,num_threads,rank+1)-1;
for (int i=mystart; i<=myend; ++i) {
//work would be done here
}

As a specific example, if N=10 and num_threads=4 then:

start_index(N,num_threads,0); //should return 0
start_index(N,num_threads,1); //should return 3
start_index(N,num_threads,2); //should return 6
start_index(N,num_threads,3); //should return 8
start_index(N,num_threads,4); //should return 10

Note that the first two threads, threads 0 and 1, are each responsible for 3 iterations and the remaining threads, threads 2 and 3, are each responsible for 2 iterations of the loop. You can assume that all the arguments will be positive values, and that N>num_threads, and that the value passed as the rank argument will be <= num_threads

来源:https://stackoverflow.com/questions/64900875/proper-thread-balancing-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!