-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdagSimCL.cpp
324 lines (260 loc) · 10 KB
/
dagSimCL.cpp
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdint.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <limits.h>
#include <CL/cl.h>
using namespace std;
#define MEM_SIZE (128)
#define MAX_SOURCE_SIZE (0x100000)
#define GRID_SIZE 8192
#define BLOCK_SIZE 256
#define MEGABYTE (1024 * 1024)
#define STEP 128
#define START 128
#define THREADS_PER_HASH 8
#define ACCESSES 64
#define FNV_PRIME 0x01000193
#define CHUNK_SIZE (256 * MEGABYTE)
#define fnv(x,y) ((x) * FNV_PRIME ^(y))
#if RAND_MAX == INT_MAX
#define random_uint() (2 * rand() + (rand() & 0x1))
#elif RAND_MAX == SHRT_MAX
#define random_uint() (4 * (rand() * RAND_MAX + rand()) + (rand() & 0xff))
#endif
#define CL_CHECK(_expr) \
do { \
cl_int _err = _expr; \
if (_err == CL_SUCCESS) \
break; \
fprintf(stderr, "OpenCL Error: '%s' returned %d!\n", #_expr, (int)_err); \
abort(); \
} while (0)
#define CL_CHECK2(_err) \
do { \
\
if (_err == CL_SUCCESS) \
break; \
fprintf(stderr, "OpenCL Error: returned %d!\n", (int)_err); \
abort(); \
} while (0)
#define CL_CHECK_ERR(_expr) \
({ \
cl_int _err = CL_INVALID_VALUE; \
auto _ret = _expr; \
if (_err != CL_SUCCESS) { \
fprintf(stderr, "OpenCL Error: '%s' returned %d!\n", #_expr, (int)_err); \
abort(); \
} \
_ret; \
})
void pfn_notify(const char *errinfo, const void *private_info, size_t cb, void *user_data)
{
fprintf(stderr, "OpenCL Error (via pfn_notify): %s\n", errinfo);
}
static void addDefinition(string& _source, char const* _id, unsigned _value)
{
char buf[256];
sprintf(buf, "#define %s %uu\n", _id, _value);
_source.insert(_source.begin(), buf, buf + strlen(buf));
}
int main(int argc, char *argv[])
{
unsigned int max_buffer_size;
unsigned int buffer_size;
unsigned int chunk_size;
printf("Genoil's DAGGER simulator\n");
printf("=========================\n\n");
printf("usage:\n");
printf("dagSimCL <c> <m> <d> <p>\n");
printf("c: chunk size. Defaults to 256\n");
printf("m: max size. Defaults to max GPU RAM or 4096\n");
printf("d: device id. Defaults to 0\n");
printf("p: platform id. Defaults to 0\n\n");
cl_platform_id platforms[100];
cl_device_id devices[100];
cl_uint platforms_n = 0;
int platform_id = NULL;
int device_id = NULL;
CL_CHECK(clGetPlatformIDs(100, platforms, &platforms_n));
char strbuf[10240];
printf("%d OpenCL platform(s) found: \n", platforms_n);
for (int i = 0; i < platforms_n; i++)
{
CL_CHECK(clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, 10240, strbuf, NULL));
printf("%d: %s\n", i, strbuf);
cl_uint devices_n = 0;
cl_int ret = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU, 100, devices, &devices_n);
if (ret == CL_SUCCESS) {
printf(" %d OpenCL device(s) found on platform:\n", devices_n);
for (int j = 0; j < devices_n; j++)
{
cl_ulong buf_ulong;
CL_CHECK(clGetDeviceInfo(devices[j], CL_DEVICE_NAME, sizeof(strbuf), strbuf, NULL));
printf("%d: %s\n", j, strbuf);
CL_CHECK(clGetDeviceInfo(devices[j], CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(buf_ulong), &buf_ulong, NULL));
cl_uint buf_cmd = atoi(argv[2]);
if (argc > 2 && buf_cmd * MEGABYTE <= buf_ulong && buf_cmd < 4096) {
buf_ulong = buf_cmd * MEGABYTE;
}
if (buf_ulong > CL_UINT_MAX) {
max_buffer_size = CL_UINT_MAX;
}
else{
max_buffer_size = buf_ulong;
}
if (argc > 1 && atoi(argv[1]) != 0) {
chunk_size = atoi(argv[1]) * MEGABYTE;
}
else {
chunk_size = 256 * MEGABYTE;
}
}
if (argc > 4 && atoi(argv[4]) == i) {
platform_id = atoi(argv[4]);
}
else {
platform_id = i;
}
if (argc <= 3) {
device_id = 0;
}
else if (argc > 3 && atoi(argv[3]) < devices_n) {
device_id = atoi(argv[3]);
}
else if (argc > 3 && atoi(argv[3]) >= devices_n) {
printf(" Invalid device id specified, using first device.\n");
device_id = 0;
}
}
else {
printf(" No GPU devices found on platform %d.\n", i);
if (argc > 4 && atoi(argv[4]) == i) {
if (platforms_n > 1) {
printf(" Try specifying a different platform id. ");
}
printf("Exiting.\n");
exit(-1);
}
}
}
if (platforms_n == 0)
exit(-1);
printf("\nUsing %dMB chunks\n", chunk_size/MEGABYTE);
printf("Using device %d on platform %d\n", device_id, platform_id);
unsigned int * buffer = (unsigned int *)malloc(max_buffer_size);
printf("Generating pseudo-DAG of size %u bytes... (will take a minute)\n", max_buffer_size);
srand(time(NULL));
for (unsigned int i = 0; i < max_buffer_size / 4; i++) {
buffer[i] = random_uint();
}
unsigned int target;
cl_context_properties contextProperties[] =
{
CL_CONTEXT_PLATFORM,
(cl_context_properties)platforms[platform_id],
0
};
cl_int _err = CL_INVALID_VALUE;
cl_context context = clCreateContext(contextProperties, 1, &devices[device_id], NULL, NULL, &_err);
CL_CHECK2(_err);
ifstream inFile;
inFile.open("./dagSim.cl");
stringstream strStream;
strStream << inFile.rdbuf();
string code = strStream.str();
printf("Loaded kernel source from %s\n", "./dagSim.cl");
addDefinition(code, "GROUP_SIZE", BLOCK_SIZE);
addDefinition(code, "ACCESSES", ACCESSES);
const char * c = code.c_str();
const size_t l = code.length();
cl_program program;
program = clCreateProgramWithSource(context, 1, &c, &l, &_err);
if (clBuildProgram(program, 1, devices, "", NULL, NULL) != CL_SUCCESS) {
clGetProgramBuildInfo(program, devices[device_id], CL_PROGRAM_BUILD_LOG, sizeof(strbuf), strbuf, NULL);
fprintf(stderr, "CL Compilation failed:\n%s", strbuf);
abort();
}
cl_kernel kernel;
kernel = clCreateKernel(program, "dagSim", &_err);
CL_CHECK2(_err);
cl_command_queue queue;
queue = clCreateCommandQueue(context, devices[0], CL_QUEUE_PROFILING_ENABLE, &_err);
cl_mem num_results = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(uint32_t), NULL, &_err);
unsigned int max_chunks = max_buffer_size / chunk_size;
cl_mem * dag = new cl_mem[max_chunks];
unsigned int num_dag_pages;
filebuf csvfile;
csvfile.open("results.csv", std::ios::out);
ostream csvdata(&csvfile);
csvdata.imbue(std::locale(""));
csvdata << "DAG size (MB)\tBandwidth (GB/s)\tHashrate (MH/s)" << endl;
unsigned int full_chunks, rest_size, chunk;
full_chunks = max_buffer_size / chunk_size;
rest_size = max_buffer_size % chunk_size;
for (chunk = 0; chunk < full_chunks; chunk++) {
dag[chunk] = clCreateBuffer(context, CL_MEM_READ_ONLY, chunk_size, NULL, &_err);
}
if (_err != CL_SUCCESS) {
printf("Out of memory. Bailing.\n");
exit(0);
}
if (rest_size > 0)
dag[full_chunks] = clCreateBuffer(context, CL_MEM_READ_ONLY, chunk_size, NULL, &_err);
if (_err != CL_SUCCESS) {
printf("Out of memory. Bailing.\n");
exit(0);
}
for (buffer_size = START * MEGABYTE; buffer_size < max_buffer_size; buffer_size += (STEP * MEGABYTE)) {
printf("Running kernel with %dMB DAG...\n", buffer_size / MEGABYTE);
target = random_uint();
num_dag_pages = buffer_size / 128;
CL_CHECK(clSetKernelArg(kernel, 0, sizeof(target), &target));
CL_CHECK(clSetKernelArg(kernel, 1, sizeof(num_results), &num_results));
CL_CHECK(clSetKernelArg(kernel, 2, sizeof(num_dag_pages), &num_dag_pages));
CL_CHECK(clSetKernelArg(kernel, 3, sizeof(dag[0]), &dag[0]));
for (chunk = 0; chunk < full_chunks; chunk++)
{
_err = clEnqueueWriteBuffer(queue, dag[chunk], CL_TRUE, 0, chunk_size, buffer + chunk * chunk_size, NULL, NULL, NULL);
}
if (rest_size > 0)
{
_err = clEnqueueWriteBuffer(queue, dag[full_chunks], CL_TRUE, 0, rest_size, buffer + full_chunks * chunk_size, NULL, NULL, NULL);
}
if (_err != CL_SUCCESS) {
printf("Out of memory. Bailing.\n");
break;
}
clFinish(queue);
size_t g = GRID_SIZE * BLOCK_SIZE;
size_t w = BLOCK_SIZE;
cl_event e;
CL_CHECK(clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &g, &w, NULL, NULL, &e));
clWaitForEvents(1, &e);
cl_ulong time_start, time_end;
double total_time;
clGetEventProfilingInfo(e, CL_PROFILING_COMMAND_START, sizeof(time_start), &time_start, NULL);
clGetEventProfilingInfo(e, CL_PROFILING_COMMAND_END, sizeof(time_end), &time_end, NULL);
total_time = time_end - time_start;
double ms = total_time / 1000000.0;
double hashes = GRID_SIZE * BLOCK_SIZE;
double hashrate = hashes / (1000.0 *ms);
double bandwidth = (1000.0f / ms) * 16 * hashes * THREADS_PER_HASH * ACCESSES / static_cast<float>(1 << 30);
printf("Execution time:\t\t%0f ms\n", ms);
printf("Approximate hashrate:\t%0.1f MH/s\n", hashrate);
printf("Achieved bandwith:\t%0.1f GB/s\n\n", bandwidth);
csvdata << buffer_size / MEGABYTE << "\t" << bandwidth << "\t" << hashrate << endl;
}
for (chunk = 0; chunk < full_chunks; chunk++)
CL_CHECK(clReleaseMemObject(dag[chunk]));
if (rest_size > 0)
CL_CHECK(clReleaseMemObject(dag[full_chunks]));
printf("Writing CSV file\n");
csvfile.close();
}