我正在尝试将一个简单的数值分析代码(梯形规则数值积分)转换为可以在启用了 CUDA 的 GPU 上运行的代码。那里有很多文献,但看起来比这里需要的要复杂得多!我目前的代码是:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define N 1000
double function(double);
int main(void)
{
int i;
double lower_bound, upper_bound, h, ans;
printf("Please enter the lower and upper bounds: ");
scanf(" %lf %lf", &lower_bound, &upper_bound);
h = (upper - lower) / N;
ans = (function(lower) + function(upper)) / 2.0;
for (i = 1; i < N; ++i) {
ans += function(i * h);
}
printf("The integral is: %.20lf\n", h * ans));
return 0;
}
double function(double x)
{
return sin(x);
}
这运行良好,直到 N 变得非常大。我已经使用更快的 openMP 实现了一个实现,但我认为了解一点 CUDA 也会很方便。有没有人对从哪里开始或是否有一种无痛的方法来转换此代码有任何建议?非常感谢,杰克。