gpt4 book ai didi

c - Openmp C 编译错误

转载 作者:行者123 更新时间:2023-11-30 20:24:46 26 4
gpt4 key购买 nike

我是 openmp 和 c 的新手。我正在尝试编译一个基本程序。我遇到一些问题。有人可以帮助引导我走向正确的方向吗?谢谢!

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

int main() {

#pragma omp parallel for private(i, a) num_threads(3)
for (i=0; i<5; i++)
{
a[i] = i + 1;
printf(“Thread %d has a value of a = %d for i = %d\n”,
omp_get_thread_num(), a[i], i);
} /*end of parallel for */

}

最佳答案

嗯,获取实际的错误消息会有所帮助...

以防万一,您是否在某处声明了 ai ?您是否为您的编译器启用了 OpenMP 支持?

我确实在代码中添加了这些并自己编译了它。这是它给出的内容:

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

int main() {
int a[5], i;
#pragma omp parallel for private(i, a) num_threads(3)
for (i=0; i<5; i++)
{
a[i] = i + 1;
printf("Thread %d has a value of a = %d for i = %d\n",
omp_get_thread_num(), a[i], i);
} /*end of parallel for */

}

结果是

$ gcc -fopenmp sample_omp.c 
$ ./a.out
Thread 2 has a value of a = 5 for i = 4
Thread 1 has a value of a = 3 for i = 2
Thread 1 has a value of a = 4 for i = 3
Thread 0 has a value of a = 1 for i = 0
Thread 0 has a value of a = 2 for i = 1

关于c - Openmp C 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32665576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com