Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.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++ 在pragma parallel指令中将数组声明为共享变量并稳定代码_C++_C_Openmp - Fatal编程技术网

C++ 在pragma parallel指令中将数组声明为共享变量并稳定代码

C++ 在pragma parallel指令中将数组声明为共享变量并稳定代码,c++,c,openmp,C++,C,Openmp,我一直在尝试将使用一定数量的项计算级数的和值并行化到使用块分配的处理器。 在这个程序中,我正在生成算术序列,希望将数组作为共享变量传递到pragma中,并尝试重新构造pragma parallel指令。 我是OPENMP-C新手。请帮助我如何将数组值作为共享变量插入并稳定代码。我附上下面的代码 #include <stdio.h> #include <stdlib.h> #include <omp.h> int main (int argc, char *a

我一直在尝试将使用一定数量的项计算级数的和值并行化到使用块分配的处理器。 在这个程序中,我正在生成算术序列,希望将数组作为共享变量传递到pragma中,并尝试重新构造pragma parallel指令。 我是OPENMP-C新手。请帮助我如何将数组值作为共享变量插入并稳定代码。我附上下面的代码

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

int main (int argc, char *argv[])
{
  int rank, comm_sz;
  int number, i, first, difference, global_sum1, global_sum, nprocs, step, local_sum1, local_n;
  int* a;
  int BLOCK_LOW, BLOCK_HIGH;
  double t0, t1;

  comm_sz = atoi(argv[1]);
  first = atoi(argv[2]);
 difference  = atoi(argv[3]);
  number = atoi(argv[4]);
  omp_set_num_threads (comm_sz);
  rank = omp_get_thread_num();
  a = (int*) malloc (n*sizeof(int));
  printf("comm_sz=%d, first=%d, difference=%d, number of terms=%d\n",comm_sz, first, difference, number);

 for(i=1; i <= number; i++){
  a[i-1] = first + (i-1)*difference;
  printf("a[%d]=%d\n",i-1,a[i]);
  }

  for(i=0; i < number; i++){
  printf("a[%d]=%d\n",i,a[i]);}


  t0 = omp_get_wtime();

  #pragma omp parallel omp_set_num_threads(comm_sz, number, comm_sz, first, difference, global_sum1)
  {
    BLOCK_LOW = (rank * number)/comm_sz;
    BLOCK_HIGH = ((rank+1) * number)/comm_sz;

  #pragma omp parallel while private(i, local_sum1)
    //int local_sum1 = 0;
    i=BLOCK_LOW;
    while( i < BLOCK_HIGH )
    { 
      printf("%d, %d\n",BLOCK_LOW,BLOCK_HIGH);
      local_sum1 = local_sum1 + a[i];
      i++;
    }
    //global_sum1 = global_sum1 + local_sum1;

    #pragma omp while reduction(+:sum1)
    i=0;
    for (i < comm_sz) {
      global_sum1 = global_sum1 + local_sum1;
      i++;
    }
  }

  step = 2*first + (n-1)*difference;
    sum = 0.5*n*step;
     printf("sum is %d\n", global_sum );

  t1 = omp_get_wtime();
  printf("Estimate of pi: %7.5f\n", global_sum1);
  printf("Time: %7.2f\n", t1-t0);
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
国际排名,comm_sz;
整数,i,first,difference,global_sum1,global_sum,nprocs,step,local_sum1,local_n;
int*a;
int BLOCK_低,BLOCK_高;
双t0,t1;
comm_sz=atoi(argv[1]);
第一个=atoi(argv[2]);
差异=atoi(argv[3]);
编号=atoi(argv[4]);
omp_set_num_线程(comm_sz);
rank=omp_get_thread_num();
a=(int*)malloc(n*sizeof(int));
printf(“comm_sz=%d,first=%d,difference=%d,术语数量=%d\n”,comm_sz,first,difference,number);

对于(i=1;i您的代码中有几个错误。我试图推断您想要做什么。因此,根据我的理解,我重写了您的代码。 以下是我的建议:

int main (int argc, char *argv[])
{
  int comm_sz, number, i, first, difference, global_sum, step;
  int* a;
  double t0, t1, sum;

  comm_sz     = atoi(argv[1]);
  first       = atoi(argv[2]);
  difference  = atoi(argv[3]);
  number      = atoi(argv[4]);

  omp_set_num_threads (comm_sz);

  a = (int*) malloc (number*sizeof(int)); 
  printf("comm_sz=%d, first=%d, difference=%d, number of terms=%d\n",comm_sz, first, difference, number);

  for(i=0; i < number; i++){
    a[i] = first + (i)*difference;
    printf("a[%d]=%d\n",i,a[i]);
  }

  t0 = omp_get_wtime();

  global_sum = 0;

  #pragma omp parallel for private(i) reduction(+:global_sum)
  for (i=0; i < number; i++){
      global_sum += a[i];
  }

  step = 2*first + (number-1)*difference;
  sum = 0.5*number*step;

  t1 = omp_get_wtime();

  printf("sum is %d\n", global_sum);
  printf("Estimate of pi: %7.5f\n", sum);
  printf("Time: %7.2f\n", t1-t0);
}
intmain(intargc,char*argv[])
{
int comm_sz,number,i,first,difference,global_sum,step;
int*a;
双t0,t1,和;
comm_sz=atoi(argv[1]);
第一个=atoi(argv[2]);
差异=atoi(argv[3]);
编号=atoi(argv[4]);
omp_set_num_线程(comm_sz);
a=(int*)malloc(number*sizeof(int));
printf(“comm_sz=%d,first=%d,difference=%d,术语数量=%d\n”,comm_sz,first,difference,number);
对于(i=0;i