在编译/编写 makefile 方面,我不是最好的。
我正在尝试编写一个同时使用 GSL 和 OpenMP 的程序。
我单独使用 GSL 和 OpenMP 没有问题,但使用两者时遇到问题。例如,我可以编译GSL程序 http://www.gnu.org/software/gsl/manual/html_node/An-Example-Program.html 通过输入
$gcc -c Bessel.c
$gcc Bessel.o -lgsl -lgslcblas -lm
$./a.out
并且它有效。
我还能够编译使用我在此处找到的 OpenMP 的程序: Starting a thread for each inner loop in OpenMP
在这种情况下我输入了
$gcc -fopenmp test_omp.c
$./a.out
我得到了我想要的(我拥有的所有 4 个线程都已使用)。
但是,当我简单地编写一个结合这两个代码的程序时
#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>
#include <omp.h>
int
main (void)
{
double x = 5.0;
double y = gsl_sf_bessel_J0 (x);
printf ("J0(%g) = %.18e\n", x, y);
int dimension = 4;
int i = 0;
int j = 0;
#pragma omp parallel private(i, j)
for (i =0; i < dimension; i++)
for (j = 0; j < dimension; j++)
printf("i=%d, jjj=%d, thread = %d\n", i, j, omp_get_thread_num());
return 0;
}
然后我尝试编译以输入
$gcc -c Bessel_omp_test.c
$gcc Bessel_omp_test.o -fopenmp -lgsl -lgslcblas -lm
$./a.out
GSL 部分可以工作(计算 Bessel 函数),但 OpenMP 部分仅使用一个线程。我不确定这里出了什么问题......
最佳答案
您错过了 OpenMP 部分中的工作共享指令 for
。应该是:
// Just in case GSL modifies the number of threads
omp_set_num_threads(omp_get_max_threads());
omp_set_dynamic(0);
#pragma omp parallel for private(i, j)
for (i =0; i < dimension; i++)
for (j = 0; j < dimension; j++)
printf("i=%d, jjj=%d, thread = %d\n", i, j, omp_get_thread_num());
编辑:总结下面评论中的讨论,OP 在编译阶段未能提供 -fopenmp
。这导致 GCC 无法识别 OpenMP 指令,因此无法生成并行代码。
关于gcc - 使用 GSL 和 OpenMP 编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10519718/