|
| 1 | +#include <cstdio> |
| 2 | + |
| 3 | +#ifdef USE_NVTX |
| 4 | +#include "nvToolsExt.h" |
| 5 | + |
| 6 | +const uint32_t colors[] = { 0x0000ff00, 0x000000ff, 0x00ffff00, 0x00ff00ff, 0x0000ffff, 0x00ff0000, 0x00ffffff }; |
| 7 | +const int num_colors = sizeof(colors)/sizeof(uint32_t); |
| 8 | + |
| 9 | +#define START_RANGE(name,cid) { \ |
| 10 | + int color_id = cid; \ |
| 11 | + color_id = color_id%num_colors;\ |
| 12 | + nvtxEventAttributes_t eventAttrib = {0}; \ |
| 13 | + eventAttrib.version = NVTX_VERSION; \ |
| 14 | + eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE; \ |
| 15 | + eventAttrib.colorType = NVTX_COLOR_ARGB; \ |
| 16 | + eventAttrib.color = colors[color_id]; \ |
| 17 | + eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII; \ |
| 18 | + eventAttrib.message.ascii = name; \ |
| 19 | + nvtxRangePushEx(&eventAttrib); \ |
| 20 | +} |
| 21 | +#define END_RANGE nvtxRangePop(); |
| 22 | +#else |
| 23 | +#define START_RANGE(name,cid) |
| 24 | +#define END_RANGE |
| 25 | +#endif |
| 26 | + |
| 27 | +__global__ void init_data_kernel( int n, double* x) |
| 28 | +{ |
| 29 | + int i = blockIdx.x * blockDim.x + threadIdx.x; |
| 30 | + if ( i < n ) |
| 31 | + { |
| 32 | + x[i] = n - i; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +__global__ void daxpy_kernel(int n, double a, double * x, double * y) |
| 38 | +{ |
| 39 | + int i = blockIdx.x*blockDim.x + threadIdx.x; |
| 40 | + if (i < n) |
| 41 | + { |
| 42 | + y[i] = a*x[i] + y[i]; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +__global__ void check_results_kernel( int n, double correctvalue, double * x ) |
| 47 | +{ |
| 48 | + int i = blockIdx.x*blockDim.x + threadIdx.x; |
| 49 | + if (i < n) |
| 50 | + { |
| 51 | + if ( x[i] != correctvalue ) |
| 52 | + { |
| 53 | + printf("ERROR at index = %d, expected = %f, actual: %f\n",i,correctvalue,x[i]); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +void init_host_data( int n, double * x ) |
| 59 | +{ |
| 60 | + START_RANGE("init_host_data",1) |
| 61 | + for (int i=0; i<n; ++i) |
| 62 | + { |
| 63 | + x[i] = i; |
| 64 | + } |
| 65 | + END_RANGE |
| 66 | +} |
| 67 | + |
| 68 | +void init_data(int n, double* x, double* x_d, double* y_d) |
| 69 | +{ |
| 70 | + START_RANGE("init_data",2) |
| 71 | + cudaStream_t copy_stream; |
| 72 | + cudaStream_t compute_stream; |
| 73 | + cudaStreamCreate(©_stream); |
| 74 | + cudaStreamCreate(&compute_stream); |
| 75 | + |
| 76 | + cudaMemcpyAsync( x_d, x, n*sizeof(double), cudaMemcpyDefault, copy_stream ); |
| 77 | + init_data_kernel<<<ceil(n/256),256,0,compute_stream>>>(n, y_d); |
| 78 | + |
| 79 | + cudaStreamSynchronize(copy_stream); |
| 80 | + cudaStreamSynchronize(compute_stream); |
| 81 | + |
| 82 | + cudaStreamDestroy(compute_stream); |
| 83 | + cudaStreamDestroy(copy_stream); |
| 84 | + END_RANGE |
| 85 | +} |
| 86 | + |
| 87 | +void daxpy(int n, double a, double* x_d, double* y_d) |
| 88 | +{ |
| 89 | + START_RANGE("daxpy",3) |
| 90 | + daxpy_kernel<<<ceil(n/256),256>>>(n,a,x_d,y_d); |
| 91 | + cudaDeviceSynchronize(); |
| 92 | + END_RANGE |
| 93 | +} |
| 94 | + |
| 95 | +void check_results( int n, double correctvalue, double* x_d ) |
| 96 | +{ |
| 97 | + START_RANGE("check_results",4) |
| 98 | + check_results_kernel<<<ceil(n/256),256>>>(n,correctvalue,x_d); |
| 99 | + END_RANGE |
| 100 | +} |
| 101 | + |
| 102 | +void run_test(int n) |
| 103 | +{ |
| 104 | + START_RANGE("run_test",0) |
| 105 | + double* x; |
| 106 | + double* x_d; |
| 107 | + double* y_d; |
| 108 | + cudaSetDevice(0); |
| 109 | + cudaMallocHost((void**) &x, n*sizeof(double)); |
| 110 | + cudaMalloc((void**)&x_d,n*sizeof(double)); |
| 111 | + cudaMalloc((void**)&y_d,n*sizeof(double)); |
| 112 | + |
| 113 | + init_host_data(n, x); |
| 114 | + |
| 115 | + init_data(n,x,x_d,y_d); |
| 116 | + |
| 117 | + daxpy(n,1.0,x_d,y_d); |
| 118 | + |
| 119 | + check_results(n, n, y_d); |
| 120 | + |
| 121 | + cudaFree(y_d); |
| 122 | + cudaFree(x_d); |
| 123 | + cudaFreeHost(x); |
| 124 | + cudaDeviceSynchronize(); |
| 125 | + END_RANGE |
| 126 | +} |
| 127 | + |
| 128 | +int main() |
| 129 | +{ |
| 130 | + int n = 1<<22; |
| 131 | + run_test(n); |
| 132 | + return 0; |
| 133 | +} |
0 commit comments