CUDA函数中的计算精度很高

时间:2018-12-18 09:00:59

标签: matlab cuda double parameter-passing mex

当我使用CUDA将高精度双精度数传递给MEXFILE函数时,我遇到了一个未知的问题,结果是不同的。请看下面的例子:

    // includes, system
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//include, mexFunction
#include "mex.h"
#include "gpu/mxGPUArray.h"

__global__ void calculate( double i_2j,  double i_2jm1,  double inj,  double injm1,  double h,  double a,  double* g)
{

    //*g = (0.0000000006253315801371527100000000000000 - 0.0000000000000000000000000000000000000000)/0.0003125000000000000100000000000000000000 - 0.5*1543.2000000000000000000000000000000000000000*(0.0000000012966958634259257000000000000000 + 0.0000000012966958634259257000000000000000);
    *g = (i_2j - i_2jm1)/h - 0.5*a*(inj + injm1);
}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{

    double i_2j;
    double i_2jm1;
    double inj;
    double injm1;
    double h;
    double a;

    i_2j = (double)mxGetScalar(prhs[0]);
    i_2jm1 = (double)mxGetScalar(prhs[1]);
    inj = (double)mxGetScalar(prhs[2]);
    injm1 = (double)mxGetScalar(prhs[3]);
    h = (double)mxGetScalar(prhs[4]);
    a = (double)mxGetScalar(prhs[5]);


    double g = 10;
    double *d_g;
    cudaMalloc (&d_g, sizeof( double));
    calculate<<<1,1>>>(i_2j,i_2jm1,inj,injm1, h, a, d_g);
    cudaMemcpy(&g, d_g, sizeof(double), cudaMemcpyDeviceToHost);
    printf ("%.40f \n", g);

}

当我调用函数: check_pre(0.0000000006253315801371527100000000000000,0.0000000000000000000000000000000000000000,0.0000000012966958634259257000000000000000,0.0000000012966958634259257000000000000000, 0.0003125000000000000100000000000000000000, 1543.2000000000000000000000000000000000000000)

我得到的结果是:0.0000000000000000000001934886220713464500

但是,当我将确切的数字放入函数中时,得到的结果是0,与MATLAB相比,它应该为零,我想我的问题是当我将double值传递给函数时。我可以做些什么以获得正确的结果吗?

0 个答案:

没有答案