贝塞尔函数在CUDA上的某些epsilon

时间:2013-04-07 14:32:09

标签: function cuda bessel-functions

我尝试使用CUDA执行贝塞尔函数(J0(x)为例)。 继承人的公式 enter image description here

我尝试将结果置于某个epsilon值内。所以这是代码

    __device__ void Bessel_j0(int totalBlocks, int totalThreads, float z, float epsilon, float* result){
            int n = 1;
            *result = 0;

            bool epsilonFlag = true;

            int idx_start;
            int idx_end;

            while(epsilonFlag == true){ 
                initThreadBounds(&idx_start, &idx_end, n, totalBlocks, totalThreads);
                float a_k;
                for (int k = idx_start; k < idx_end; k++) {
                    a_k = m_power((-0.25 * z * z), k)/(m_factorial(k) * m_factorial(k)); 
                    *result += a_k;
                }
                if(a_k < epsilon){
                        epsilonFlag = false;
                }
                n++;
            }
        }

__global__ void J0(int totalBlocks, int totalThreads,  float x, float* result){
        float res = 0;

        Bessel_j0(totalBlocks, totalThreads, 10, 0.01, &res);
        result[(blockIdx.x*totalThreads + threadIdx.x)] = res;
}

__host__ void J0test(){

    const int blocksNum = 32;
    const int threadNum = 32;

    float   *device_resultf; //для устройства
    float   host_resultf[threadNum*blocksNum]   ={0};


    cudaMalloc((void**) &device_resultf, sizeof(float)*threadNum*blocksNum);

    J0<<<blocksNum, threadNum>>>(blocksNum, threadNum, 10, device_resultf); 
    cudaThreadSynchronize();

    cudaMemcpy(host_resultf, device_resultf, sizeof(float)*threadNum*blocksNum, cudaMemcpyDeviceToHost);

    float sum = 0;

    for (int i = 0; i != blocksNum*threadNum; ++i) {
        sum += host_resultf[i];
        printf ("result in %i cell = %f \n", i, host_resultf[i]);
    }
    printf ("Bessel res = %f \n", sum);
    cudaFree(device_resultf);
}
int main(int argc, char* argv[])
{
    J0test();   
}

当我运行时,它出现黑屏,Windows说nVidia驱动程序没有响应,它恢复了它。在控制台输出中,host_resultf数组中只有零。怎么了?如何在某些epsilon中执行正确的函数?

1 个答案:

答案 0 :(得分:1)

这不太可能,但可能是您的内核执行达到了允许的内核执行时间限制。您的代码未显示迭代次数的上限。可能会发生从未到达epsilon并且您的内核继续执行超出时间限制的情况。这site可以提供帮助。

在所有情况下,我都会为epsilon循环添加一个上限,永远不要让代码在没有限制迭代次数的情况下运行。