Simplest Possible Example to Show GPU Outperform CPU Using CUDA

后端 未结 4 748
悲哀的现实
悲哀的现实 2020-12-28 15:24

I am looking for the most concise amount of code possible that can be coded both for a CPU (using g++) and a GPU (using nvcc) for which the GPU consistently outperforms the

4条回答
  •  臣服心动
    2020-12-28 15:47

    For reference, I made a similar example with time measurements. With GTX 660, the GPU speedup was 24X where its operation includes data transfers in addition to actual computation.

    #include "cuda_runtime.h"
    #include "device_launch_parameters.h"
    
    #include 
    #include 
    
    #define N (1024*1024)
    #define M (10000)
    #define THREADS_PER_BLOCK 1024
    
    void serial_add(double *a, double *b, double *c, int n, int m)
    {
        for(int index=0;index>>( d_a, d_b, d_c );
    
        cudaMemcpy( c, d_c, size, cudaMemcpyDeviceToHost );
    
    
        printf( "c[0] = %d\n",0,c[0] );
        printf( "c[%d] = %d\n",N-1, c[N-1] );
    
    
        free(a);
        free(b);
        free(c);
        cudaFree( d_a );
        cudaFree( d_b );
        cudaFree( d_c );
    
        end = clock();
        float time2 = ((float)(end-start))/CLOCKS_PER_SEC;
        printf("CUDA: %f seconds, Speedup: %f\n",time2, time1/time2);
    
        return 0;
    } 
    

提交回复
热议问题