gpt4 book ai didi

gcc - 使用 GSL 和 OpenMP 编译

转载 作者:行者123 更新时间:2023-12-04 05:47:14 27 4
gpt4 key购买 nike

在编译/编写 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 部分仅使用一个线程。我不确定这里出了什么问题......

最佳答案

您错过了工作共享指令 for在您的 OpenMP 部分。它应该是:

// 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/

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