Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用OpenMP使用2个函数调用并行化代码_C_Multithreading_Parallel Processing_Openmp - Fatal编程技术网

C 使用OpenMP使用2个函数调用并行化代码

C 使用OpenMP使用2个函数调用并行化代码,c,multithreading,parallel-processing,openmp,C,Multithreading,Parallel Processing,Openmp,我想并行化一个代码部分,它使用OpenMP执行2个函数调用。我尝试使用如下“sections”参数: int func(int *V1, int *V2, int length){ int result=0; int i; for(i=0;i<length;i++){ result = result + V1[i] + V2[i]; } return result; } int main(){ omp_set_num_threads(32); #p

我想并行化一个代码部分,它使用OpenMP执行2个函数调用。我尝试使用如下“sections”参数:

int func(int *V1, int *V2, int length){
  int result=0;
  int i;

  for(i=0;i<length;i++){
    result = result + V1[i] + V2[i];
  }
  return result;
}

int main(){

  omp_set_num_threads(32);
  #pragma omp parallel sections
  {
    #pragma omp section
    {
      result1 = func(array_A,array_B,1000000);
    }
    #pragma omp section
    {
      result2 = func(array_X,array_Y,2000000);
    }
  }
}
int func(int*V1,int*V2,int-length){
int结果=0;
int i;

对于(i=0;i不使用节。不设置线程数(使用默认值)。执行以下操作:

#include <stdlib.h>   
int func(int *V1, int *V2, int length) {
    int result=0;
    int i;
    #pragma omp parallel for reduction(+:result)
    for(i=0;i<length;i++) {
        result += V1[i] + V2[i];
    }
    return result;
}

int main(){
    int result1, result2;
    int *array_A, *array_B, *array_X, *array_Y;
    array_A = malloc(sizeof(int)*1000000);
    array_B = malloc(sizeof(int)*1000000);
    array_X = malloc(sizeof(int)*2000000);
    array_Y = malloc(sizeof(int)*2000000);

    result1 = func(array_A,array_B,1000000);
    result2 = func(array_X,array_Y,2000000);
    //now do something with result1 and result2
    return 0;
}
#包括
int func(int*V1,int*V2,int长度){
int结果=0;
int i;
#pragma omp并行用于缩减(+:结果)

对于(i=0;i谢谢你,结果会更好。但是,每个函数不可能同时使用16个线程?我认为执行会更快。@user1702964,我不明白为什么在一个函数上运行16个线程,在另一个函数上运行16个线程会更快。理想情况下(忽略缓存、超线程,…),如果您使用32个线程运行函数两次,或同时使用16个线程并行运行两个函数,则两种方法应同时完成。如果您担心性能,则函数具有依赖链,请尝试展开循环几次。我已运行了您的程序,它使用8个线程达到性能峰值,因此最好的解决方案是每个函数使用8个线程。我举了两个函数调用的例子,但在我的例子中,我调用了3次该函数。@user1702964,这可能是错误的结论。你有什么样的系统?它有什么样的英特尔或AMD处理器?它有多少个物理和逻辑处理器?我正在运行它在一个有48个CPU(24个物理*2个逻辑)的AMD上,每个核心1个线程。如果我使用你的代码,我首先用8个处理器func(数组a,数组B,1000000)(当时40个处理器什么都不做)执行,然后result2=func(数组X,数组Y,2000000)有8个处理器。我认为一次运行这两个功能会更快。用OpenMP可以做到吗?
void foo(int *V1, int *V2, int length1, int *V3, int *V4, int length2) {
    int result1, result2;
    result1=0; result2=0;
    #pragma omp parallel
    {
        int i, ithread, nthreads, start, finish, result_private, *a1, *a2;
        ithread = omp_get_thread_num(); nthreads = omp_get_num_threads();
        if(ithread<nthreads/2) {
            start = ithread*length1/(nthreads/2);
            finish = (ithread+1)*length1/(nthreads/2);
            a1 = V1; a2 = V2;          
        }
        else {
            start  = (ithread - nthreads/2)*length2/(nthreads - nthreads/2);
            finish = (ithread+1 - nthreads/2)*length2/(nthreads - nthreads/2);
            a1 = V3; a2 = V4;
        }
        result_private = 0;
        #pragma omp for nowait
        for(i=start; i<finish; i++) {
            result_private += a1[i] + a2[i];
        }
        #pragma omp critical
        {
            if(ithread<nthreads/2) {
                result1 += result_private;
            }
            else {
                result2 += result_private;
            }
        }
    }
}