开发者

Converting a simple C code into a CUDA code

I'm trying to convert a simple numerical analysis code (trapezium rule numerical integration) into something that will run on my CUDA enabled GPU. There is alot of literature out there but it all seems far more complex than what is required here! My current code is:

#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);
}

This runs well until N becomes very large. I've made an implementation with openMP which is faster but I think it will be handy to know a little about CUDA too. Ha开发者_Go百科s anyone got any suggestions about where to start or if there is a painless way to convert this code? Many Thanks, Jack.


It's the loop that would have to be distributed to parallel threads. You can calculate a unique index for each thread (idx = 0...N-1). Each thread merely calculates its individual part of the integral and stores the answer in its position in a common array (intgrl[idx]). You then sum everything up using a procedure called a parallel scan or gather. There are examples in the NVIDIA cuda examples. The easiest way would be to use the Thrust library. You simply tell it "add up these values" and it calculates the fastest method.


You could get rid of the multiplication :D

   double nomul = h;
   for (i = 1; i < N; ++i) {
      ans += function(nomul);
      nomul += h;
   }


First, go ahead and install CUDA on your computer. After that, try to run some of the examples available on the SDK. They may look a little complicated at first sight, but don't worry, there are tons of CUDA "Hello World" examples on the web.

If you're looking for something fancier, you could try compiling this project (you'll need to install OpenCV), which converts an image to its grayscale representation (it has files to compile on Windows/Linux/Mac OS X, so its worth taking a look if you need help to compile your projects).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜