gcc - 使用 GSL 和 OpenMP 编译

标签 gcc openmp gsl

在编译/编写 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/

相关文章:

具有 GSL、LAPACK 或 CBLAS 等数学库的 C++ 性能与具有 R 函数的 Rinside 的 C++ 相比?

python - Yocto,安装 Numpy

c - C中的函数指针和内存地址

gcc - 代码:: block ,ld.exe错误 "Cannot find -lgomp"

c++ - 在矩阵乘法中使用 C++2011 线程而不是 OpenMP 时出现异常加速

使用 O3 优化级别编译 GSL

c++ - "already a friend"警告什么时候有用?

c++ - 更改所有子类中父类(super class)变量的值

fortran - OpenMP 和 gfortran 中的嵌套并行区域

cygwin - 在 Windows 中构建 GSL(GNU 科学库)以与 VS2005 一起使用