首页 > 解决方案 > OpenMP C 程序说明

问题描述

如果要从并行范围之外访问私有化变量的线程局部值,可以将它们写回全局声明的辅助数组。这是 OpenMP 书籍中描述的方式

(2017 - Bertil Schmidt、Jorge Gonzalez-Dominguez、Christian Hundt、Moritz Schlarb - 并行编程_ 概念与实践-Morgan Kaufmann)

作者想出了这个程序——

int main () { 

    // maximum number of threads and auxiliary memory  

    int num = omp_get_max_threads(); 

    int * aux = new int[num]; 

    int i = 1; // we pass this via copy by value 

    #pragma omp parallel firstprivate(i) num_threads(num) { 

        // get the thread identifier j 

        int j = omp_get_thread_num(); 

        i += j; 

        aux[j] = i; 

    } 

    for(k=0; k<num; k++) 

        printf("%d \n", aux[k]); 

} 

在 Mac 上试过 -->

错误信息

这里有什么问题?

标签: cgccopenmp

解决方案


new不是 的运算符Cnew是为了C++C你可以用其他功能分配内存,比如malloc

正如您在错误中看到的那样,它说new undeclared因为该运算符未在中定义C。(就像使用未知名称一样)

所以它希望你在这里添加一些东西,new int[num];因为它是这样的

unknown_name/*add something like ;*/ int[num];

而不是new,你可以使用这样的东西:

int *aux=malloc(sizeof(int)*num);

推荐阅读