OpenMp只执行一个线程

时间:2017-09-23 10:56:24

标签: c openmp

我设置了4个线程。Screen1但是只有一个线程在并行区域中执行(查看“id”)。 Screen2
可用的线程(我使用了omp_get_max_threads()) 有什么问题?

int main(int argc, char** argv)
{
  srand(time(NULL));
  int P = atoi(argv[1]);

omp_set_num_threads(4);
#pragma parallel for
  for( int i = 0; i < 1000000; i++ ) {
    printf("%d\n",omp_get_max_threads());
    printf("Num Threads:%d ",omp_get_num_threads());
    printf("id:%d\n",omp_get_thread_num());
  }
  while(1);
  const uint num_elements = 79;
  int* data = (int*) malloc(sizeof(int) * num_elements);
  for( uint i = 0; i < num_elements; i++ ) {
    data[i] = rand() % 100 - 50;
  }
  int* buffer = (int*) malloc(sizeof(int) * num_elements);
  PrintArray(data,0,num_elements);
  int* res = MergeSort(data,buffer,0,num_elements - 1,8);
  PrintArray(res,0,num_elements);

  return 0;
}

2 个答案:

答案 0 :(得分:1)

错误是:#pragma parallel for。

正确的指示是&#34; #pragma omp并行为&#34;

答案 1 :(得分:0)

printf("%d\n",omp_get_max_threads());
printf("Num Threads:%d ",omp_get_num_threads());
printf("id:%d\n",omp_get_thread_num());

请将这三个printf放入一个printf中。简单。然后从for循环中删除{}括号, 它测试了,它有效。我只是通过智能手机在这里写作。我将稍后添加到github基本样本。