简体   繁体   English

OpenMP“未使用的变量”编译错误

[英]OpenMP 'unused variable' compiling error

i tried to write a c++ programm using openmp for parallelization. 我试图编写一个使用openmp进行并行化的c ++程序。 Unfortunately i get a compiling error which i dont understand. 不幸的是,我收到了一个我不理解的编译错误。 I have listed the g++ command, the problematic lines of code and the error message. 我列出了g ++命令,有问题的代码行和错误消息。 If i missed to give important information please let me know. 如果我错过重要信息,请告诉我。

g++ -o Pogramm -Wall -fopenmp Programm.cpp

#pragma omp parallel
int id,nths,tnbr;
id=omp_get_thread_num();
nths=omp_get_num_thread();

Tree.cpp:52:7: warning: unused variable 'id' [-Wunused-variable] Tree.cpp:52:7:警告:未使用的变量'id'[-Wunused-variable]

error: 'id' was not declared in this scope id=omp_get_thread_num(); 错误:在此范围内未声明'id'id = omp_get_thread_num();

Can someone tell me why 'id' ist not undeclared? 有人可以告诉我为什么不声明'id'ist吗?

According to your code, the scope of the parallel region, which is the scope where you define id, only includes the subsequent line, ie the line where you define id. 根据您的代码,并行区域的范围(即您定义id的范围)仅包括后续行,即您定义id的行。 Therefore, when you use the id variable outside, you get an undefined variable error. 因此,在外部使用id变量时,会出现未定义的变量错误。 Besides, you are also getting an unused id variable warning because you are not using it in the parallel region (where you could use it). 此外,您还收到未使用的id变量警告,因为未在并行区域(可以使用该区域)中使用它。

Most probably you just forgot to add the curly braces to enlarge the scope to be parallelized alltogether, ie 很可能您只是忘了添加花括号来扩大将要并行化的范围,即

#pragma omp parallel
{
  int id,nths,tnbr;
  id=omp_get_thread_num();
  nths=omp_get_num_thread();
  ...
}

A minimal working example is: 一个最小的工作示例是:

#include<iostream>
#include<omp.h>
using namespace std;
int main() {
#pragma omp parallel
    {
        int id,nths,tnbr;
        id=omp_get_thread_num();
        nths=omp_get_num_threads();
        cout << "id, nths: " << id << nths << endl;
    }
    return 0;
}

This can be successfully compiled, eg using g++ v. 4.8.5 可以成功编译,例如使用g ++ v.8.5.5。

g++ main.cpp -fopenmp -Wall

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

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