Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// GPU_check.cu
// get and test GPU information
// (c) MIT CBA Neil Gershenfeld 7/12/20
//
#include <iostream>
#include <chrono>
using namespace std;
__global__ void add_array(float* dbuf0,int npts) {
int index = blockIdx.x*blockDim.x+threadIdx.x;
int nindex = gridDim.x*blockDim.x;
int start = (index/float(nindex))*npts;
int stop = ((index+1)/float(nindex))*npts;
for (int i = start; i < stop; ++i)
for (int j = 0; j < npts; ++j)
dbuf0[i] += 1.0;
}
__global__ void add_arrays(float* dbuf0,int npts,int gpu,int ngpus) {
int index = blockIdx.x*blockDim.x+threadIdx.x;
int nindex = gridDim.x*blockDim.x;
int start = (gpu+index/float(nindex))*(npts/float(ngpus));
int stop = (gpu+(index+1)/float(nindex))*(npts/float(ngpus));
for (int i = start; i < stop; ++i)
for (int j = 0; j < npts; ++j)
dbuf0[i] += 1.0;
}
__global__ void nop(float* dbuf0,int npts,int gpu,int ngpus) {
return;
}
int main(int argc, char** argv) {
int ngpus;
int npts = 5e6;
int grid = 1024;
int block = 1024;
//
// check peers
//
cudaGetDeviceCount(&ngpus);
if (ngpus > 1)
printf("peer access:\n");
for (int i = 0; i < ngpus; ++i) {
for (int j = 0; j < ngpus; ++j) {
int result;
if (j != i) {
cudaDeviceCanAccessPeer(&result,i,j);
printf(" from %d to %d: ",i,j);
if (result == 1)
printf("yes\n");
else
printf("no\n");
}
}
}
//
// list GPUs
//
if (ngpus > 1)
printf("GPUs:\n");
else
printf("GPU:\n");
for (int i = 0; i < ngpus; ++i) {
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop,i);
printf(" number: %d\n",i);
printf(" name: %s\n",prop.name);
printf(" global memory: %lu\n",prop.totalGlobalMem);
printf(" max grid size: %d\n",prop.maxGridSize[0]);
printf(" max threads per block: %d\n",prop.maxThreadsPerBlock);
printf(" max threads dimension: %d\n",prop.maxThreadsDim[0]);
printf(" multiprocessor count: %d\n",prop.multiProcessorCount);
printf(" max threads per multiprocessor: %d\n",prop.maxThreadsPerMultiProcessor);
}
//
// time CPU to GPU
//
printf("copy %d floats from CPU to GPU\n",npts);
cudaSetDevice(0);
float *cbuf,*cbufp,*dbuf0;
cbuf = new float[npts];
cudaMallocHost(&cbufp,npts*sizeof(float));
cudaMalloc(&dbuf0,npts*sizeof(float));
auto t0 = chrono::high_resolution_clock::now();
cudaMemcpy(dbuf0,cbuf,npts*sizeof(float),cudaMemcpyDefault);
cudaDeviceSynchronize();
auto t1 = chrono::high_resolution_clock::now();
float dt = chrono::duration_cast<std::chrono::microseconds>(t1-t0).count();
t0 = chrono::high_resolution_clock::now();
cudaMemcpy(dbuf0,cbufp,npts*sizeof(float),cudaMemcpyDefault);
cudaDeviceSynchronize();
t1 = chrono::high_resolution_clock::now();
float dt1 = chrono::duration_cast<std::chrono::microseconds>(t1-t0).count();
printf(" %f us, %g B/s\n",dt,1e6*npts*sizeof(float)/dt);
printf(" %f us, %g B/s pinned\n",dt1,1e6*npts*sizeof(float)/dt1);
//
// time GPU to GPU
//
if (ngpus > 1) {
float* dbufs[ngpus];
int result;
printf("copy %d floats from GPU to GPU 0:\n",npts);
for (int i = 1; i <= (ngpus-1); ++i) {
cudaSetDevice(i);
cudaMalloc(&dbufs[i],npts*sizeof(float));
cudaDeviceCanAccessPeer(&result,i,0);
if (result != 0)
cudaDeviceEnablePeerAccess(0,0);
auto t0 = chrono::high_resolution_clock::now();
cudaMemcpy(dbuf0,dbufs[i],npts*sizeof(float),cudaMemcpyDefault);
cudaDeviceSynchronize();
auto t1 = chrono::high_resolution_clock::now();
float dt = chrono::duration_cast<std::chrono::microseconds>(t1-t0).count();
printf(" GPU %d: %f us, %g B/s\n",i,dt,1e6*npts*sizeof(float)/dt);
}
}
//
// time Flops
//
printf("add %dx%d floats:\n",npts,npts);
cudaSetDevice(0);
t0 = chrono::high_resolution_clock::now();
add_array<<<grid,block>>>(dbuf0,npts);
cudaDeviceSynchronize();
t1 = chrono::high_resolution_clock::now();
dt = chrono::duration_cast<std::chrono::microseconds>(t1-t0).count();
printf(" %f s, %f G/s\n",dt/1e6,npts*(npts/dt)/1000.0);
//
// time peer Flops
//
if (ngpus > 1) {
int result;
cudaMemset(dbuf0,0,npts*sizeof(float));
printf("peer add %dx%d GPU 0 floats:\n",npts,npts);
for (int i = 1; i < ngpus; ++i) {
cudaDeviceCanAccessPeer(&result,i,0);
if (result != 0) {
cudaSetDevice(i);
t0 = chrono::high_resolution_clock::now();
add_array<<<grid,block>>>(dbuf0,npts);
cudaDeviceSynchronize();
t1 = chrono::high_resolution_clock::now();
dt = chrono::duration_cast<std::chrono::microseconds>(t1-t0).count();
printf(" GPU %d: %f s, %f G/s\n",i,dt/1e6,npts*(npts/dt)/1000.0);
}
}
}
//
// time parallel peer Flops
//
if (ngpus > 1) {
int result;
int count = 1;
cudaMemset(dbuf0,0,npts*sizeof(float));
printf("parallel peer add %dx%d GPU 0 floats:\n",npts,npts);
t0 = chrono::high_resolution_clock::now();
cudaSetDevice(0);
add_arrays<<<grid,block>>>(dbuf0,npts,0,ngpus);
for (int i = 1; i < ngpus; ++i) {
cudaDeviceCanAccessPeer(&result,i,0);
if (result != 0) {
++count;
cudaSetDevice(i);
add_arrays<<<grid,block>>>(dbuf0,npts,i,ngpus);
}
}
cudaSetDevice(0);
cudaDeviceSynchronize();
for (int i = 1; i < ngpus; ++i) {
cudaDeviceCanAccessPeer(&result,i,0);
if (result != 0) {
cudaSetDevice(i);
cudaDeviceSynchronize();
}
}
t1 = chrono::high_resolution_clock::now();
dt = chrono::duration_cast<std::chrono::microseconds>(t1-t0).count();
printf(" %d GPUS: %f s, %f G/s\n",count,dt/1e6,(count*npts/float(ngpus))*(npts/dt)/1000.0);
}
//
// check for errors
//
cudaError err;
err = cudaGetLastError();
if (cudaSuccess != err)
printf("error: %s\n",cudaGetErrorString(err));
//
// return
//
return 0;
}