Announcement

Collapse
No announcement yet.

Mergesort - 2^n int array - blocks + threads

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mergesort - 2^n int array - blocks + threads

    PHP Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    #include <fstream>

    using namespace std;

    // standard merge function
    __device__ void merge (int first_arrayint size1int second_arrayint size2){

        
    int i,j,k;
        
    int third_array;
        
    //cudaMalloc ((void**)&third_array, (size1 + size2) * sizeof(int));
        
    third_array = (int *) malloc((size1 size2) * sizeof(int));

        for(
    i=0j=0k=-; (i<size1) && (j<size2) ;){
            if ( *(
    first_array i) <= *(second_array j)  ){
                ++
    k;
                *(
    third_array k) =  *(first_array i);
                ++
    i;
            } else {
                ++
    k;
                *(
    third_array k) =  *(second_array j);
                ++
    j;
            }
        }
        if( 
    i>= size1 ){
            for(;
    j<size2;){
                ++
    k;
                *(
    third_array k) =  *(second_array j);
                ++
    j;
            }
        } else {
            for(;
    i<size1;){
                ++
    k;
                *(
    third_array k) =  *(first_array i);
                ++
    i;
            }
        }


        for(
    i=0i<size1 ;++i){
            *(
    first_array i) = *(third_array i);
        }
        
    j=i;
        for(
    i=0i<size2 ;++i){
            *(
    second_array i) = *(third_array j);
        }
        
    //cudaFree (third_array) ;
        
    free (third_array) ;
    }


    __global__ void mergesort(int pointer,int sizeint factor){

        
    int id = ( blockIdx.blockDim.x) + threadIdx.x;
        
    int pointer_to_pass NULL;
        for (;
    id< (size/factor); id += gridDim.blockDim.x){
            
    pointer_to_pass pointer + ( id factor); // pointer to pass to each merge
            
    mergepointer_to_pass, (factor/2), pointer_to_pass + (factor/2), (factor/2) );
        }
    }




    int mainvoid ) {
        
    /*
        fstream fileout ("data.txt",fstream::out); // file to write the non-ordered and ordered array
        fstream fileout_bin ("data_bin.txt",fstream::out | fstream::binary ); // ordered array in binary mode
        */

        
    srand (time(NULL));

        
    int n 65536;
        
    int numeri = new int[n];

        for (
    int i =0in; ++i){
        *(
    numeri i) = rand() +  rand() + rand() + rand() +  rand() + rand() ;  //fill the array with random data
        
    }

        
    /*
        fileout << "pre" << endl; // print to file the non-ordered array
        for(int i = 0;i<n;++i){
        fileout << numeri[i]<< endl;
        }
        */
        


        
    intd_numeri;
        
    cudaMalloc((void **)&d_numeri,n*sizeof(int)); //allocating n*sizeof(int) bytes on gpu ram

        
    cudaMemcpy(d_numeri,numeri,n*sizeof(int),cudaMemcpyHostToDevice); // copy array from host to gpu

        
    for(int i i<=;i=i*2) {
            
    mergesort<<<128,128>>>(d_numeri,n,i); // launch instances of mergesort and run in parallel
        
    }

        
    cudaMemcpy(numeri,d_numeri,n*sizeof(int),cudaMemcpyDeviceToHost); // copy the ordered array from device to host
        
        /*
        fileout << "after" << endl; // write in file the ordered array
        for(int i = 0;i<n;++i){
        fileout << numeri[i]<< endl;
        }

        fileout_bin.write((char *) numeri, n * sizeof(int)); // write to file the ordered array in binary mode
        */


        
    return 0;

    | VFX Artist, C++ Programmer, HW Overclocker | Web: xgiovio.com Email: xgiovio@gmail.com Twitter: @xgiovio
Working...
X

Google Profile


My name is Giovanni Di Grezia, but people call me xgiovio.

Here is my homepage:.

I'm a VFX Artist and Software Developer.

Giovanni Di Grezia