【问题标题】:Why is my parallelized for loop giving a different output?为什么我的并行化 for 循环会给出不同的输出?
【发布时间】:2021-04-24 04:50:43
【问题描述】:

我这样声明我的线程:

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;
}

我不明白为什么输出不同?

我已经创建了线程,引用了我想要处理的函数,并将其实现到我的旧循环中。

【问题讨论】:

  • 您是否已初始化所有变量?我不会问,但既然你还没有发布minimal reproducible example,我不得不问。 (例如A2 是如何创建的,即int **A2 = NULL;, then malloc'd` 一些内存,或intA2[X][Y];。这些都是未初始化变量的示例。)

标签: arrays c parallel-processing pthreads 2d


【解决方案1】:

我目前在您的代码中看到两个小问题:

1。 您正在设置ending_element = starting_element + local - 1, 但是在循环中,条件是i &lt; ending_element

你应该把它改成ending_element = starting_element + local, 或将循环中的条件更改为i &lt;= 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 &lt; ending_element 更改为(i &lt; ending_element) &amp;&amp; (i &lt; P)

【讨论】:

    猜你喜欢
    • 2020-07-07
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 2023-02-21
    • 1970-01-01
    相关资源
    最近更新 更多