簡體   English   中英

為什么我的並行化 for 循環會給出不同的 output?

[英]Why is my parallelized for loop giving a different output?

我這樣聲明我的線程:

for (thread_num = 0; thread_num < NUM_THREADS; thread_num++) //for each thread do
        pthread_create(&thread_handles[thread_num], NULL, gemver_default, (void*)thread_num); //create and run the thread. The thread will run the gemver_default. The thread_num will be passed as input to the gemver_default().


    for (thread_num = 0; thread_num < NUM_THREADS; thread_num++) //for each thread do
        pthread_join(thread_handles[thread_num], NULL); //wait for the thread to finish

然后我的 pthread 循環:

unsigned short int gemver_default(void * thread_num) {
    long int my_thread_num = (long int)thread_num; //store the input of the function to my_thread_num
    

    int local = P / NUM_THREADS; //the number of array elements that each thread must compute their sqrt
    
    int starting_element = my_thread_num * local; //first array element to be computed by this thread
    int ending_element = starting_element + local - 1; //last array element to be computed by this thread
    
    for (i = starting_element; i < ending_element; i++)
                for (j = 0; j < local; j++)
                    A2[i][j] += u1[i] * v1[j] + u2[i] * v2[j];
    

}

然后我原來的循環:

unsigned short int gemver_default() {

    //this is the loop to parallelize
    for (int i = 0; i < P; i++)
        for (int j = 0; j < P; j++)
            A2[i][j] += u1[i] * v1[j] + u2[i] * v2[j];

    return 0;
}

我不明白為什么輸出不同?

我已經創建了線程,引用了我想要處理的 function,並將其實現到我的舊循環中。

我目前在您的代碼中看到兩個小問題:

1.您正在設置ending_element = starting_element + local - 1 ,但在循環中條件是i < ending_element

您應該將其更改為ending_element = starting_element + local ,或將循環中的 ondition 更改為i <= ending_element

2. 如果 P 可以被NUM_THREADS整除而沒有余數,使用P / NUM_THREADS可以正常工作,但如果不是,那么您的線程將不會覆蓋從 0 到 P 的所有索引。例如,如果P = 14NUM_THREADS = 5 ,那么P / NUM_THREADS = 2 ,您的線程將只處理索引 0 到 9,忽略索引 10 到 13。

解決這個問題:可以設置local = P / NUM_THREADS + 1 ,將循環中的條件從i < ending_element(i < ending_element) && (i < P)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM