OpenMP多线程并行编程

时间:2016-11-16 11:19:51

标签: c++ c multithreading parallel-processing openmp

我的并行程序与串行程序相比具有相同的时间,我不知道为什么(对于大数据,小数据43都是7.3,因此没有加速)。但我猜它是由printf引起的?但我老师问我继续使用printf。 另外还有一种方法可以并行输入程序吗?因为它也需要很长时间。 (有一些技巧,比如读取后删除数字等,xD)

void input(int ***a, int set){
 int i,j;
 FILE *f;
switch(set){
    case 1: f = fopen("dataset1.txt","r");break;
    case 2: f = fopen("dataset2.txt","r");break;
    case 3: f = fopen("dataset3.txt","r");break;
    case 4: f = fopen("dataset4.txt","r");break;
    case 5: f = fopen("dataset5.txt","r");break;
    case 6: f = fopen("dataset6.txt","r");break;
    case 7: f = fopen("dataset7.txt","r");break;
    case 8: f = fopen("dataset8.txt","r");break;
    case 9: f = fopen("dataset9.txt","r");break;
    case 10: f = fopen("dataset10.txt","r");break;
}
for(i=0;i<s;i++)
    for(j=0;j<s;j++)
        fscanf(f,"%d ",&(*a)[i][j]);
fclose(f);
}

serial code:
    void rangeCheck(int **a){
      int i,j;
      for(i=0;i<s;i++)
        for(j=0;j<s;j++)
         if(a[i][j]>=min && a[i][j]<=max)
           printf("(%d,%d) ",i,j);
    }

parallel code:
    void rangeCheck(const int **a){
      int i,chunk,id,p=4;
      chunk = (s%p==0) ? s/p : s/p+1;
      #pragma omp parallel private(i,id)
      {
        id = omp_get_thread_num();
        #pragma omp for schedule (static,chunk)
          for(i=0;i<s;i++){
            int j;
            for(j=0;j<s;j++)
              if(a[i][j]>=min && a[i][j]<=max)
                printf("(%d,%d) ",i,j);
          }
      }
}

0 个答案:

没有答案