繁体   English   中英

GSL螺纹安全问题

[英]GSL thread safety problems

在使用函数指针时,GSL是否存在线程安全问题? 附带的openmp代码使用gsl的gsl_integration_qng函数在各种c值的1 <= x <= 2范围内积分了f(x)=-(c + x)^ {-1}。 并行版本比串行版本运行慢得多。 我怀疑这与功能指针&fx有关。 有没有人有这个问题的经验? 提前致谢!

#include<cstdlib>
#include <gsl/gsl_integration.h>
#include<cstdio>
#include<omp.h>

double fx(double x,void *p);
double evalintegral(double c);

using namespace std;

int main(int argc, char *argv[])
{

    // numerically integrate the function f(x) = -(c+x)^{-1} between 1 and 2
    int Ncs = atoi(argv[1]);
    int Nreps = atoi(argv[2]);

    printf("Ncs=%d, Nreps=%d.\n",Ncs,Nreps);

    int i, j;
    double tempF;

    double *cs = new double[Ncs];
    double dc = 1 / (double)(Ncs-1);

    for (i = 0; i < Ncs; i++)
    {
        cs[i] = dc*(double)i;
    }

    printf("Began integrations.\n");

#pragma omp parallel for default(none)\
shared(Nreps,Ncs,cs)\
private(i,j,tempF)
    for (i = 0; i < Nreps; i++)
    {
        for (j = 0; j < Ncs; j++)
        {
            tempF = evalintegral(cs[j]);
        }
    }

    delete[] cs;

    printf("Finished integrations.\n");

    return 0;

}

double fx(double x, void *p)
{
    double *c = (double*) p;
    return -1 / (*c + x);
}

double evalintegral(double c)
{
    double *ptr_c = new double[1];
    ptr_c[0] = c;
    gsl_function Fquad;
    Fquad.params = ptr_c;
    Fquad.function = &fx;

    size_t quadneval;
    double quadres, quaderr;

    gsl_integration_qng(&Fquad,1,2,1e-10,1e-6,&quadres,&quaderr,&quadneval);

    delete[] ptr_c;

    return quadres;
}

我使用GSL积分例程遇到了同样的问题。 正如@mrx_hk提到的那样,问题在于重用了工作空间对象 就我而言,我存储:

gsl_integration_workspace * w_ = gsl_integration_workspace_alloc(1200);

作为类成员变量,并在每次集成调用时重新使用存储,这导致了竞争状况。 解决方案是为每个线程 (在我的情况下为每个集成调用) 创建一个工作区对象

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM