简体   繁体   English

当“并行化”多个 for 循环时,OpenMP `无效的控制谓词`

[英]OpenMP ` invalid controlling predicate` when “parallelizing” more than one for loops

I 'm trying to create a parallel block in openMP with multiple for loops inside.我正在尝试在 openMP 中创建一个并行块,其中包含多个 for 循环。 What I've done so far (or at least the important parts i guess, i can post the whole program if neccessary):到目前为止我所做的(或者至少是我猜的重要部分,如果有必要我可以发布整个程序):

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
int main(int argc, char**argv) {

  [...]

  int i, j, N=200, num_t=2;

  [...]
  M = (int) round(M);
  [...]
  double **u = malloc(N * sizeof(double));
  for (i = 0; i < N; i++){
    u[i] = (double*) malloc(M * sizeof(double));
  }

  /* Initialize time and space arrays */
  double *x, *t;
  x = (double*) malloc(N * sizeof(double));
  t = (double*) malloc(M * sizeof(double));
  
#pragma omp parallel num_threads(num_t) private(i,j) shared(x,t,u,dt,dx,N,M,c) default(none)
  {
#pragma omp for 
  for (i = 0; i < N; i++)
    x[i] =  0 + i * dx;
 
#pragma omp for 
  for (i = 0; i < M; i++)
    t[i] = 0 + i * dt;

  /* Initial Conditions */
  #pragma omp for
  for (i = 0; i < N; i++) {
    [do stuff with `u` array]
  }
  /* boundary conditions. */
  #pragma omp for
  for (j = 0; j < M; j++) {
    [more stuff with `u` array]
  }

#pragma omp for collapse(2)
  for (j = 0; j< M - 1; j++) {
    for(i = 1; i < N - 1; i ++) {
      [even more stuff with `u` array].
    }
 }
}

And why I try to compile this with gcc -Wall -fopenmp -fsanitize=address exe1.c -o t1_1 -lm以及为什么我尝试使用gcc -Wall -fopenmp -fsanitize=address exe1.c -o t1_1 -lm来编译它

I get the following error我收到以下错误

exe1.c:56:15: error: invalid controlling predicate
   for (i = 0; i < M; i++)
               ^
exe1.c:67:15: error: invalid controlling predicate
   for (j = 0; j < M; j++) {
               ^
exe1.c:73:15: error: invalid controlling predicate
   for (j = 0; j< M - 1; j++) {

The only way I 've managed to actually compile this, is by ending the parallel block (not sure if this is the correct terminology) after the first #pragma omp for :我设法实际编译它的唯一方法是在第一个#pragma omp for之后结束并行块(不确定这是否是正确的术语):

#pragma omp parallel num_threads(num_t) private(i,j) shared(x,t,u,dt,dx,N,M,c) default(none)
  {
#pragma omp for 
  for (i = 0; i < N; i++)
    x[i] =  0 + i * dx;
}

My gcc version is gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 and openMP #define _OPENMP 201511 .我的 gcc 版本是gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0和 openMP #define _OPENMP 201511

Now, I 've read lots of posts here about this error comming up ( invalid controlling predicate ) but most of them seems to come from not conforming to a "Canonical Loop Form".现在,我在这里阅读了很多关于此错误的帖子( invalid controlling predicate ),但其中大多数似乎来自不符合“规范循环形式”。 Think my code does conform.认为我的代码确实符合。 What could be the issue?可能是什么问题? Thank you:)谢谢:)

You are getting errors in loops involving M probably because M is not of an integer type.您在涉及M的循环中遇到错误可能是因为M不是 integer 类型。 A clue in favor of that assumption is that you are rounding its value at some point:支持该假设的线索是您在某个时候对其值进行四舍五入:

M = (int) round(M);

You cannot use floating-point variables in canonical loop forms.您不能在规范循环 forms 中使用浮点变量。 So this doesn't work:所以这不起作用:

#pragma omp for 
  for (i = 0; i < M; i++)
    t[i] = 0 + i * dt;

but this should work:但这应该有效:

#pragma omp for 
  for (i = 0; i < (int)M; i++)
    t[i] = 0 + i * dt;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM