I’m working on a statistical application containing approximately 10 – 30 million floating point values in an array.
Several methods performing different, but independent, calculations on the array in nested loops, for example:
Dictionary<float, int> noOfNumbers = new Dictionary<float, int>();
for (float x = 0f; x < 100f; x += 0.0001f) {
int noOfOccurrences = 0;
foreach (float y in largeFloatingPointArray) {
if (x == y) {
noOfOccurrences++;
}
}
noOfNumbers.Add(x, noOfOccurrences);
}
The current application is written in C#, runs on an Intel CPU and needs several hours to complete. I have no knowledge of GPU programming concepts and APIs, so my questions are:
- Is it possible (and does it make sense) to utilize a GPU to speed up such calculations?
- If yes: Does anyone know any tutorial or got any sample code (programming language doesn’t matter)?
UPDATE GPU Version
This one I just tested for smaller inputs, because I am testing in my laptop. Nevertheless, it is working, but more tests are needed.
UPDATE Sequential Version
I just did this naive version that executes your algorithm for an array with 30,000,000 element in less than 20 seconds (including the time taken by function that generates the data).
This naive version first sorts your array of floats. Afterward, will go through the sorted array and check the number of times a given
valueappears in the array and then puts this value in a dictionary along with the number of times it has appeared.You can use
sortedmap, instead of theunordered_mapthat I used.Heres the code:
If you have the library thrust installed in you machine your should use this:
instead of this
For sure it will be faster.
Original Post
Yes, it is. A month ago, I ran an entirely Molecular Dynamic simulation on a GPU. One of the kernels, which calculated the force between pairs of particles, received as parameter
6array each one with500,000doubles, for a total of3Millions doubles(22 MB).So if you are planning to put
30Million floating points, which is about114 MBof global Memory, it will not be a problem.In your case, can the number of calculations be an issue? Based on my experience with the Molecular Dynamic (MD), I would say no. The sequential MD version takes about
25hours to complete while the GPU version took45Minutes. You said your application took a couple hours, also based in your code example it looks softer than the MD.Here’s the force calculation example:
A simple example of a code in CUDA could be the sum of two 2D arrays:
In C:
In CUDA:
In CUDA you basically took each for iteration and assigned to each thread,
Each block has an
IDfrom0toN-1(N the number maximum of blocks) and each block has a'X'number of threads with anIDfrom0toX-1.IDand the blockIDwhich the thread is in; the blockDim.x is the number of threads that a block has.So if you have 2 blocks each one with
10threads andN=40, the:Looking at your current code, I have made this draft of what your code could look like in CUDA:
You have to use
atomicAddbecause different threads from different blocks may write/readnoOfOccurrencesconcurrently, so you have to ensure mutual exclusion.This is just one approach; you can even assign the iterations of the outer loop to the threads instead of the blocks.
Tutorials
The Dr Dobbs Journal series CUDA: Supercomputing for the masses by Rob Farmer is excellent and covers just about everything in its fourteen installments. It also starts rather gently and is therefore fairly beginner-friendly.
and anothers:
Take a look on the last item, you will find many link to learn CUDA.
OpenCL: OpenCL Tutorials | MacResearch