Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:23:52+00:00 2026-06-14T11:23:52+00:00

I’m working on a statistical application containing approximately 10 – 30 million floating point

  • 0

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)?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:23:53+00:00Added an answer on June 14, 2026 at 11:23 am

    UPDATE GPU Version

    __global__ void hash (float *largeFloatingPointArray,int largeFloatingPointArraySize, int *dictionary, int size, int num_blocks)
    {
        int x = (threadIdx.x + blockIdx.x * blockDim.x); // Each thread of each block will
        float y;                                         // compute one (or more) floats
        int noOfOccurrences = 0;
        int a;
        
        while( x < size )            // While there is work to do each thread will:
        {
            dictionary[x] = 0;       // Initialize the position in each it will work
            noOfOccurrences = 0;    
    
            for(int j = 0 ;j < largeFloatingPointArraySize; j ++) // Search for floats
            {                                                     // that are equal 
                                                                 // to it assign float
               y = largeFloatingPointArray[j];  // Take a candidate from the floats array 
               y *= 10000;                      // e.g if y = 0.0001f;
               a = y + 0.5;                     // a = 1 + 0.5 = 1;
               if (a == x) noOfOccurrences++;    
            }                                      
                                                        
            dictionary[x] += noOfOccurrences; // Update in the dictionary 
                                              // the number of times that the float appears 
    
        x += blockDim.x * gridDim.x;  // Update the position here the thread will work
        }
    }
    

    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 value appears in the array and then puts this value in a dictionary along with the number of times it has appeared.

    You can use sorted map, instead of the unordered_map that I used.

    Heres the code:

    #include <stdio.h>
    #include <stdlib.h>
    #include "cuda.h"
    #include <algorithm>
    #include <string>
    #include <iostream>
    #include <tr1/unordered_map>
    
    
    typedef std::tr1::unordered_map<float, int> Mymap;
    
    
    void generator(float *data, long int size)
    {
        float LO = 0.0;
        float HI = 100.0;
        
        for(long int i = 0; i < size; i++)
            data[i] = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
    }
    
    void print_array(float *data, long int size)
    {
    
        for(long int i = 2; i < size; i++)
            printf("%f\n",data[i]);
        
    }
    
    std::tr1::unordered_map<float, int> fill_dict(float *data, int size)
    {
        float previous = data[0];
        int count = 1;
        std::tr1::unordered_map<float, int> dict;
        
        for(long int i = 1; i < size; i++)
        {
            if(previous == data[i])
                count++;
            else
            {
              dict.insert(Mymap::value_type(previous,count));
              previous = data[i];
              count = 1;         
            }
            
        }
        dict.insert(Mymap::value_type(previous,count)); // add the last member
        return dict;
        
    }
    
    void printMAP(std::tr1::unordered_map<float, int> dict)
    {
       for(std::tr1::unordered_map<float, int>::iterator i = dict.begin(); i != dict.end(); i++)
      {
         std::cout << "key(string): " << i->first << ", value(int): " << i->second << std::endl;
       }
    }
    
    
    int main(int argc, char** argv)
    {
      int size = 1000000; 
      if(argc > 1) size = atoi(argv[1]);
      printf("Size = %d",size);
      
      float data[size];
      using namespace __gnu_cxx;
      
      std::tr1::unordered_map<float, int> dict;
      
      generator(data,size);
      
      sort(data, data + size);
      dict = fill_dict(data,size);
      
      return 0;
    }
    

    If you have the library thrust installed in you machine your should use this:

    #include <thrust/sort.h>
    thrust::sort(data, data + size);
    

    instead of this

    sort(data, data + size);
    

    For sure it will be faster.

    Original Post

    I’m working on a statistical application which has a large array
    containing 10 – 30 millions of floating point values.

    Is it possible (and does it make sense) to utilize a GPU to speed up
    such calculations?

    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 6 array each one with 500,000 doubles, for a total of 3 Millions doubles (22 MB).

    So if you are planning to put 30 Million floating points, which is about 114 MB of 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 25 hours to complete while the GPU version took 45 Minutes. 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:

    __global__ void add(double *fx, double *fy, double *fz,
                        double *x, double *y, double *z,...){
       
         int pos = (threadIdx.x + blockIdx.x * blockDim.x); 
          
         ...
         
         while(pos < particles)
         {
         
          for (i = 0; i < particles; i++)
          {
                  if(//inside of the same radius)
                    {
                     // calculate force
                    } 
           }
         pos += blockDim.x * gridDim.x;  
         }        
      }
    

    A simple example of a code in CUDA could be the sum of two 2D arrays:

    In C:

    for(int i = 0; i < N; i++)
        c[i] = a[i] + b[i]; 
    

    In CUDA:

    __global__ add(int *c, int *a, int*b, int N)
    {
      int pos = (threadIdx.x + blockIdx.x)
      for(; i < N; pos +=blockDim.x)
          c[pos] = a[pos] + b[pos];
    }
    

    In CUDA you basically took each for iteration and assigned to each thread,

    1) threadIdx.x + blockIdx.x*blockDim.x;
    

    Each block has an ID from 0 to N-1 (N the number maximum of blocks) and each block has a 'X' number of threads with an ID from 0 to X-1.

    1. Gives you the for loop iteration that each thread will compute based on its ID and the block ID which 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 10 threads and N=40, the:

    Thread 0 Block 0 will execute pos 0
    Thread 1 Block 0 will execute pos 1
    ...
    Thread 9 Block 0 will execute pos 9
    Thread 0 Block 1 will execute pos 10
    ....
    Thread 9 Block 1 will execute pos 19
    Thread 0 Block 0 will execute pos 20
    ...
    Thread 0 Block 1 will execute pos 30
    Thread 9 Block 1 will execute pos 39
    

    Looking at your current code, I have made this draft of what your code could look like in CUDA:

    __global__ hash (float *largeFloatingPointArray, int *dictionary)
        // You can turn the dictionary in one array of int
        // here each position will represent the float
        // Since  x = 0f; x < 100f; x += 0.0001f
        // you can associate each x to different position
        // in the dictionary:
    
        // pos 0 have the same meaning as 0f;
        // pos 1 means float 0.0001f
        // pos 2 means float 0.0002f ect.
        // Then you use the int of each position 
        // to count how many times that "float" had appeared 
    
    
       int x = blockIdx.x;  // Each block will take a different x to work
        float y;
        
    while( x < 1000000) // x < 100f (for incremental step of 0.0001f)
    {
        int noOfOccurrences = 0;
        float z = converting_int_to_float(x); // This function will convert the x to the
                                              // float like you use (x / 0.0001)
    
        // each thread of each block
        // will takes the y from the array of largeFloatingPointArray
        
        for(j = threadIdx.x; j < largeFloatingPointArraySize; j += blockDim.x)
        {
            y = largeFloatingPointArray[j];
            if (z == y)
            {
                noOfOccurrences++;
            }
        }
        if(threadIdx.x == 0) // Thread master will update the values
          atomicAdd(&dictionary[x], noOfOccurrences);
        __syncthreads();
    }
    

    You have to use atomicAdd because different threads from different blocks may write/read noOfOccurrences concurrently, 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:

    • Volume I: Introduction to CUDA Programming
    • Getting started with CUDA
    • CUDA Resources List

    Take a look on the last item, you will find many link to learn CUDA.

    OpenCL: OpenCL Tutorials | MacResearch

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.