private variable outside parallel for-loop

断了今生、忘了曾经 提交于 2019-12-11 04:43:15

问题


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

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

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


回答1:


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


来源:https://stackoverflow.com/questions/52742036/private-variable-outside-parallel-for-loop

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