Newer
Older
Tomáš Oberhuber
committed
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
/***************************************************************************
tnl-benchmarks.h - description
-------------------
begin : Jan 27, 2010
copyright : (C) 2010 by Tomas Oberhuber
email : tomas.oberhuber@fjfi.cvut.cz
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef TNLBENCHMARKS_H_
#define TNLBENCHMARKS_H_
#include <core/mfuncs.h>
#include <core/tnlTimerCUDA.h>
template< class T >
bool transferBenchmark( const int size,
double& host_to_host_band_width,
double& host_to_device_band_width,
double& device_to_host_band_width,
double& device_to_device_band_width )
{
tnlLongVector< T > host_vector( "transferBenchmark:host-vector", size );
tnlLongVector< T > host_vector2( "transferBenchmark:host-vector-2", size );
tnlLongVector< T, tnlCuda > device_vector( "transferBenchmark:device-vector", size );
tnlLongVector< T, tnlCuda > device_vector2( "transferBenchmark:device-vector-2", size );
for( int i = 0; i < size; i ++ )
host_vector[ i ] = i + 1;
const long int cycles = 100;
long int bytes = cycles * size * sizeof( int );
long int mega_byte = 1 << 20;
tnlTimerCUDA timer;
timer. Reset();
for( int i = 0; i < cycles; i ++ )
if( ! host_vector2. copyFrom( host_vector ) )
return false;
double time = timer. GetTime();
double giga_byte = ( double ) ( 1 << 30 );
host_to_host_band_width = bytes / giga_byte / time;
cout << "Transfering " << bytes / mega_byte << " MB from HOST to HOST took " << time << " seconds. Bandwidth is " << host_to_host_band_width << " GB/s." << endl;
timer. Reset();
for( int i = 0; i < cycles; i ++ )
if( ! device_vector. copyFrom( host_vector ) )
return false;
time = timer. GetTime();
host_to_device_band_width = bytes / giga_byte / time;
cout << "Transfering " << bytes / mega_byte << " MB from HOST to DEVICE took " << time << " seconds. Bandwidth is " << host_to_device_band_width << " GB/s." << endl;
timer. Reset();
for( int i = 0; i < cycles; i ++ )
if( ! host_vector2. copyFrom( device_vector ) )
return false;
time = timer. GetTime();
device_to_host_band_width = bytes / giga_byte / time;
cout << "Transfering " << bytes / mega_byte << " MB from DEVICE to HOST took " << time << " seconds. Bandwidth is " << device_to_host_band_width << " GB/s." << endl;
timer. Reset();
for( int i = 0; i < cycles; i ++ )
if( ! device_vector2. copyFrom( device_vector ) )
return false;
time = timer. GetTime();
// Since we read and write tha data back we process twice as many bytes.
bytes *= 2;
device_to_device_band_width = bytes / giga_byte / time;
cout << "Transfering " << bytes / mega_byte << " MB from DEVICE to DEVICE took " << time << " seconds. Bandwidth is " << device_to_device_band_width << " GB/s." << endl;
}
template< class T >
void tnlCPUReductionSum( const tnlLongVector< T >& host_vector,
T& sum )
{
const T* data = host_vector. Data();
const int size = host_vector. getSize();
sum = 0.0;
for( int i = 0; i < size; i ++ )
sum += data[ i ];
};
template< class T >
void tnlCPUReductionMin( const tnlLongVector< T >& host_vector,
T& min )
{
const T* data = host_vector. Data();
const int size = host_vector. getSize();
//tnlAssert( data );
min = data[ 0 ];
for( int i = 1; i < size; i ++ )
min = :: Min( min, data[ i ] );
};
template< class T >
void tnlCPUReductionMax( const tnlLongVector< T >& host_vector,
T& max )
{
const T* data = host_vector. Data();
const int size = host_vector. getSize();
//tnlAssert( data );
max = data[ 0 ];
for( int i = 1; i < size; i ++ )
max = :: Max( max, data[ i ] );
};
template< class T >
void reductionBenchmark( const int size,
const int algorithm )
{
tnlLongVector< T > host_vector( "reductionBenchmark:host-vector", size );
tnlLongVector< T, tnlCuda > device_vector( "reductionBenchmark:device-vector", size );
tnlLongVector< T, tnlCuda > device_aux( "reductionBenchmark:device-aux", size / 2 );
for( int i = 0; i < size; i ++ )
host_vector[ i ] = i + 1;
device_vector. copyFrom( host_vector );
T sum, min, max;
const long int reducing_cycles( 10 );
tnlTimerCUDA timer;
timer. Reset();
for( int i = 0; i < reducing_cycles; i ++ )
{
switch( algorithm )
{
case 0: // reduction on CPU
tnlCPUReductionSum( host_vector, sum );
tnlCPUReductionMin( host_vector, sum );
tnlCPUReductionMax( host_vector, sum );
case 1:
tnlCUDASimpleReduction1Sum( size,
device_vector. Data(),
sum,
device_aux. Data() );
tnlCUDASimpleReduction1Min( size,
device_vector. Data(),
min,
device_aux. Data() );
tnlCUDASimpleReduction1Max( size,
device_vector. Data(),
max,
device_aux. Data() );
break;
case 2:
tnlCUDASimpleReduction2Sum( size,
device_vector. Data(),
sum,
device_aux. Data() );
tnlCUDASimpleReduction2Min( size,
device_vector. Data(),
min,
device_aux. Data() );
tnlCUDASimpleReduction2Max( size,
device_vector. Data(),
max,
device_aux. Data() );
break;
case 3:
tnlCUDASimpleReduction3Sum( size,
device_vector. Data(),
sum,
device_aux. Data() );
tnlCUDASimpleReduction3Min( size,
device_vector. Data(),
min,
device_aux. Data() );
tnlCUDASimpleReduction3Max( size,
device_vector. Data(),
max,
device_aux. Data() );
break;
case 4:
tnlCUDASimpleReduction4Sum( size,
device_vector. Data(),
sum,
device_aux. Data() );
tnlCUDASimpleReduction4Min( size,
device_vector. Data(),
min,
device_aux. Data() );
tnlCUDASimpleReduction4Max( size,
device_vector. Data(),
max,
device_aux. Data() );
break;
case 5:
tnlCUDASimpleReduction5Sum( size,
device_vector. Data(),
sum,
device_aux. Data() );
tnlCUDASimpleReduction5Min( size,
device_vector. Data(),
min,
device_aux. Data() );
tnlCUDASimpleReduction5Max( size,
device_vector. Data(),
max,
device_aux. Data() );
break;
default:
tnlCUDAReductionSum( size,
device_vector. Data(),
sum,
device_aux. Data() );
tnlCUDAReductionMin( size,
device_vector. Data(),
min,
device_aux. Data() );
tnlCUDAReductionMax( size,
device_vector. Data(),
max,
device_aux. Data() );
}
}
const double time = timer. GetTime();
double giga_byte = ( double ) ( 1 << 30 );
long int mega_byte = 1 << 20;
long int bytes_reduced = size * sizeof( T ) * reducing_cycles * 3;
const double reduction_band_width = bytes_reduced / giga_byte / time;
cout << "Reducing " << bytes_reduced / mega_byte
<< " MB on DEVICE using algorithm " << algorithm
<< " took " << time
<< " seconds. Bandwidth is " << reduction_band_width
<< " GB/s." << endl;
}
#endif /* TNLBENCHMARKS_H_ */