并行for循环外的私有变量 [英] private variable outside parallel for-loop

查看:110
本文介绍了并行for循环外的私有变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道每个线程在for循环中使用了多少时间.我希望time_taken对于每个线程都是私有的,以便他们可以在其中附加自己的时间.最好的转换,我想要每个线程的总时间,而不是while循环中每次迭代的时间.

I want to know how much time each thread is using in the for loop. I want time_taken to be private for each thread so they can append their own time there. Best cast i would like the total time for each thread, instead of the time for each iteration in the while-loop.

double time_taken = 0.0;

while(delta >= epsilon) {
    delta = 0.0;

    double wtime = omp_get_wtime();

    #pragma omp parallel for reduction(+:delta)
    for (i = 0; i < workSize; i++) { 
        #do some work and change delta
    }
    time_taken += omp_get_wtime() - wtime
    printf("time taken by thread %d: %f\n", omp_get_thread_num(), time_taken);
}

我试图像这样在while循环中将time_taken设为私有:

I've tried to make time_taken private inside the while-loop like this:

double time_taken = 0.0;

while(delta >= epsilon) {
    delta = 0.0;

    #pragma omp private(time_taken)
    {
        double wtime = omp_get_wtime();

        #pragma omp parallel for reduction(+:delta)
        for (i = 0; i < workSize; i++) { 
            #do some work and change delta
        }
        time_taken += opm_get_wtime() - wtime
        printf("time taken by thread %d: %f\n", omp_get_thread_num(), time_taken);
    }
}

我也研究过使用threadprivate,但是我不知道如何正确使用它.

I've also looked into using threadprivate, but I can't figure out how to use it right.

推荐答案

您可以尝试以下代码.这样会分配一个大小等于线程数的指针数组,然后每个线程都分配私有内存,即没有错误共享.

You could try something like the code below. This allocates an array of pointers with a size equal to the number of threads and then each thread allocates private memory i.e. no false sharing.

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
  double delta = 20, epsilon = 0;
  double **time_taken; 
  omp_set_dynamic(0);

  #pragma omp parallel
  {
    int it = omp_get_thread_num();
    #pragma omp single
    time_taken = malloc(sizeof *time_taken * omp_get_num_threads());
    time_taken[it] = malloc(sizeof *time_taken[omp_get_thread_num()]);
    *time_taken[it] = 0.0;
  }

  double sum = 0;
  while(delta >= epsilon) {
    #pragma omp parallel
    {
      int it = omp_get_thread_num();
      *time_taken[it] -= omp_get_wtime();
      #pragma omp for reduction(+: sum)
      for(int i= 0 ; i<2000000000; i++) sum += 1.0*i, delta -=1;
      *time_taken[it] += omp_get_wtime();
    }
  }
  printf("sum %f\n", sum);

  #pragma omp parallel
  {
    int it = omp_get_thread_num();
    printf("time taken by thread %d: %f\n", it, *time_taken[it]);
    free(time_taken[omp_get_thread_num()]);
    #pragma omp barrier
    #pragma omp single
    free(time_taken);
  }
}

这篇关于并行for循环外的私有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆