-
Notifications
You must be signed in to change notification settings - Fork 0
/
bandwidth-test-statistics.cu
255 lines (226 loc) · 8.25 KB
/
bandwidth-test-statistics.cu
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <cuda_runtime.h>
#include <vector>
#include <iostream>
#include <iomanip>
#include <string>
#include <stdexcept>
#include <sstream>
#include <cassert>
typedef float real_t;
size_t searchPageLockedLimit( size_t m = 0xffff, size_t M = 0x3fffffff ) {
assert( m <= M );
const size_t h = ( M + m ) / 2;
if( h == m || h > size_t(1E9) ) return h;
void* p = 0;
if( cudaHostAlloc( &p, h, cudaHostAllocDefault ) == cudaSuccess ) {
cudaFreeHost( p );
return searchPageLockedLimit( h, M );
}
return searchPageLockedLimit( m, h );
}
size_t findPow2PageLockedLimit( size_t s = 65536 ) {
void* p = 0;
cudaError_t e = cudaSuccess;
while( e == cudaSuccess && s > 0 ) {
e = cudaHostAlloc( &p, s, cudaHostAllocDefault );
if( e == cudaSuccess ) {
cudaFreeHost( p );
return s;
}
s >>= 1;
}
return s;
}
template < typename T >
T strTo( const char* str ) {
if( !str ) throw std::runtime_error( "strTo<T> - NULL srting");
std::istringstream is( str );
T v = T();
is >> v;
return v;
}
const double GB = 1024 * 1024 * 1024;
double GBs( size_t bytes, double tms ) {
return ( bytes / GB ) / ( tms / 1000 );
}
cudaEvent_t start = cudaEvent_t();
cudaEvent_t stop = cudaEvent_t();
cudaError_t memCpy( void* target, const void* src, size_t bytes, cudaMemcpyKind flags, const char* msg ) {
float elapsed = 0.f;
cudaError_t e = cudaEventRecord( start ); if( e != cudaSuccess ) return e;
e = cudaMemcpy( target, src, bytes, flags ); if( e != cudaSuccess ) return e;
e = cudaEventRecord( stop ); if( e != cudaSuccess ) return e;
e = cudaEventSynchronize( stop ); if( e != cudaSuccess ) return e;
e = cudaEventElapsedTime( &elapsed, start, stop ); if( e != cudaSuccess ) return e;
std::cout << bytes << ',' << msg << ',' << GBs( bytes, elapsed ) << ',' << elapsed << std::endl;
return e;
}
size_t nextStep( size_t prev, size_t val, bool mul ) {
return mul ? val * prev : prev + val;
}
int main(int argc, char** argv ) {
size_t beginSize = 0;
size_t endSize = 0;
size_t step = 0;
bool mul = false;
int dev_id = 0;
if( argc < 4 ) {
std::cout << "usage: " << argv[ 0 ] << " <min num elements> <max num elements> <step> [device id]\n";
return 0;
} else {
beginSize = strTo< size_t >( argv[ 1 ] );
endSize = strTo< size_t >( argv[ 2 ] );
step = strTo< size_t >( argv[ 3 ] );
if( argc > 4 ) dev_id = strTo< int >( argv[ 4 ] );
}
if( cudaSetDevice( dev_id ) != cudaSuccess ) {
std::cerr << "Cannot set device " << dev_id << std::endl;
return 1;
}
cudaSetDeviceFlags( cudaDeviceMapHost );
#if 0
//size_t pll = findPow2PageLockedLimit( 1024 * 1024 * 1024 );
pll = searchPageLockedLimit( pll, 2 * pll );
std::cout << "Max power of two page-lockable size (<= 1GB): "
<< double( pll ) / ( 1024 * 1024 ) << "MB" << std::endl;
#endif
cudaEventCreate( &start );
cudaEventCreate( &stop );
for( size_t s = beginSize; s < endSize; s = nextStep( s, step, mul ) ) {
const size_t NUM_ELEMENTS = s;
const size_t SIZE = NUM_ELEMENTS * sizeof( real_t );
{
real_t* dev_in, *dev_out;
cudaMalloc( &dev_in, SIZE );
cudaMalloc( &dev_out, SIZE );
memCpy( dev_out, dev_in, SIZE, cudaMemcpyDeviceToDevice, "Device to Device");
cudaFree( dev_in );
cudaFree( dev_out );
}
//
{
real_t *dev;
std::vector< real_t > host( NUM_ELEMENTS );
cudaMalloc( &dev, SIZE );
memCpy( dev, &host[ 0 ], SIZE, cudaMemcpyHostToDevice, "Host to Device" );
memCpy( &host[ 0 ], dev, SIZE, cudaMemcpyDeviceToHost, "Device to host" );
cudaFree( dev );
}
//
{
real_t *dev, *host;
cudaMalloc( &dev, SIZE );
const cudaError_t e = cudaHostAlloc( &host, SIZE, cudaHostAllocDefault /*same as cudaMallocHost*/ );
if( e != cudaSuccess ) {
std::cerr << "Error - cudaHostAlloc" << std::endl;
return 1;
}
memCpy( dev, host, SIZE, cudaMemcpyHostToDevice, "Host to Device - cudaHostAllocDefault" );
memCpy( host, dev, SIZE, cudaMemcpyDeviceToHost, "Device to Host - cudaHostAllocDefault" );
cudaFree( dev );
cudaFreeHost( host );
}
//
{
real_t *dev, *host;
cudaMalloc( &dev, SIZE );
const cudaError_t e = cudaHostAlloc( &host, SIZE, cudaHostAllocPortable );
if( e != cudaSuccess ) {
std::cerr << "Error - cudaHostAlloc" << std::endl;
return 1;
}
memCpy( dev, host, SIZE, cudaMemcpyHostToDevice, "Host to Device - cudaHostAllocPortable" );
memCpy( host, dev, SIZE, cudaMemcpyDeviceToHost, "Device to Host - cudaHostAllocPortable" );
cudaFree( dev );
cudaFreeHost( host );
}
//
{
real_t *dev, *host;
cudaMalloc( &dev, SIZE );
const cudaError_t e = cudaHostAlloc( &host, SIZE, cudaHostAllocMapped );
if( e != cudaSuccess ) {
std::cerr << "Error - cudaHostAlloc - mapped" << std::endl;
return 1;
}
memCpy( dev, host, SIZE, cudaMemcpyHostToDevice, "Host to Device - cudaHostAllocMapped" );
memCpy( host, dev, SIZE, cudaMemcpyDeviceToHost, "Device to Host - cudaHostAllocMapped" );
cudaFree( dev );
cudaFreeHost( host );
}
//
{
real_t *dev, *host, *mapped_host;
cudaMalloc( &dev, SIZE );
const cudaError_t e = cudaHostAlloc( &host, SIZE, cudaHostAllocMapped );
if( e != cudaSuccess ) {
std::cerr << "Error - cudaHostAlloc - mapped" << std::endl;
return 1;
}
cudaHostGetDevicePointer( &mapped_host, host, 0 /*has to be zero "for now" */ );
memCpy( dev, mapped_host, SIZE, cudaMemcpyDeviceToDevice, "Device(on host) to Device - cudaHostAllocMapped" );
memCpy( mapped_host, dev, SIZE, cudaMemcpyDeviceToDevice, "Device to Device(on host) - cudaHostAllocMapped" );
cudaFree( dev );
cudaFreeHost( host );
}
//
{
real_t *dev, *host;
cudaMalloc( &dev, SIZE );
const cudaError_t e = cudaHostAlloc( &host, SIZE, cudaHostAllocWriteCombined );
if( e != cudaSuccess ) {
std::cerr << "Error - cudaHostAlloc - write combining" << std::endl;
return 1;
}
memCpy( dev, host, SIZE, cudaMemcpyHostToDevice, "Host to Device - cudaHostAllocWriteCombined" );
memCpy( host, dev, SIZE, cudaMemcpyDeviceToHost, "Device to Host - cudaHostAllocWriteCombined" );
cudaFree( dev );
cudaFreeHost( host );
}
//
{
real_t *dev, *host, *mapped_host;
cudaMalloc( &dev, SIZE );
const cudaError_t e = cudaHostAlloc( &host, SIZE, cudaHostAllocMapped | cudaHostAllocWriteCombined );
if( e != cudaSuccess ) {
std::cerr << "Error - cudaHostAlloc - mapped | write combining" << std::endl;
return 1;
}
cudaHostGetDevicePointer( &mapped_host, host, 0 /*has to be zero "for now" */ );
memCpy( dev, mapped_host, SIZE, cudaMemcpyDeviceToDevice, "Device(on host) to Device - cudaHostAllocMapped | cudaHostAllocWriteCombined" );
memCpy( mapped_host, dev, SIZE, cudaMemcpyDeviceToDevice, "Device to Device(on host) - cudaHostAllocMapped | cudaHostAllocWriteCombined" );
cudaFree( dev );
cudaFreeHost( host );
}
#if CUDART_VERSION >= 4000
//
{
float elapsed = float();
real_t *dev, *mapped_host;
cudaMalloc( &dev, SIZE );
std::vector< real_t > host( NUM_ELEMENTS );
const cudaError_t e = cudaHostRegister( &host[ 0 ], SIZE, cudaHostAllocMapped );
if( e != cudaSuccess ) {
std::cerr << "Error - cudaHostRegister - mapped" << std::endl;
return 1;
}
cudaHostGetDevicePointer( &mapped_host, &host[ 0 ], 0 /*has to be zero "for now" */ );
cudaEventRecord( start );
cudaMemcpy( dev, mapped_host, SIZE, cudaMemcpyDeviceToDevice );
cudaEventRecord( stop );
cudaEventSynchronize( stop );
cudaEventElapsedTime( &elapsed, start, stop );
std::cout << "\nHost to device - cudaHostRegister - mapped: " << GBs( NUM_ELEMENTS, elapsed ) << std::endl;
cudaEventRecord( start );
cudaMemcpy( &host[ 0 ], dev, SIZE, cudaMemcpyDeviceToHost );
cudaEventRecord( stop );
cudaEventSynchronize( stop );
cudaEventElapsedTime( &elapsed, start, stop );
std::cout << "\nDevice to host - cudaHostRegister - mapped: " << GBs( NUM_ELEMENTS, elapsed ) << std::endl;
cudaFree( dev );
cudaHostUnregister( &host[ 0 ] );
}
#endif
}
return 0;
}