-
Notifications
You must be signed in to change notification settings - Fork 23
/
GpuKang.cpp
515 lines (469 loc) · 13.8 KB
/
GpuKang.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// This file is a part of RCKangaroo software
// (c) 2024, RetiredCoder (RC)
// License: GPLv3, see "LICENSE.TXT" file
// https://github.com/RetiredC
#include <iostream>
#include "cuda_runtime.h"
#include "cuda.h"
#include "GpuKang.h"
cudaError_t cuSetGpuParams(TKparams Kparams, u64* _jmp2_table);
void CallGpuKernelGen(TKparams Kparams);
void CallGpuKernelABC(TKparams Kparams);
void AddPointsToList(u32* data, int cnt, u64 ops_cnt);
int RCGpuKang::CalcKangCnt()
{
Kparams.BlockCnt = mpCnt;
Kparams.BlockSize = IsOldGpu ? 512 : 256;
Kparams.GroupCnt = IsOldGpu ? 64 : 24;
return Kparams.BlockSize* Kparams.GroupCnt* Kparams.BlockCnt;
}
//executes in main thread
bool RCGpuKang::Prepare(EcPoint _PntToSolve, int _Range, int _DP, EcJMP* _EcJumps1, EcJMP* _EcJumps2, EcJMP* _EcJumps3)
{
PntToSolve = _PntToSolve;
Range = _Range;
DP = _DP;
EcJumps1 = _EcJumps1;
EcJumps2 = _EcJumps2;
EcJumps3 = _EcJumps3;
StopFlag = false;
Failed = false;
u64 total_mem = 0;
memset(dbg, 0, sizeof(dbg));
memset(SpeedStats, 0, sizeof(SpeedStats));
cur_stats_ind = 0;
cudaError_t err;
err = cudaSetDevice(CudaIndex);
if (err != cudaSuccess)
return false;
Kparams.BlockCnt = mpCnt;
Kparams.BlockSize = IsOldGpu ? 512 : 256;
Kparams.GroupCnt = IsOldGpu ? 64 : 24;
KangCnt = Kparams.BlockSize * Kparams.GroupCnt * Kparams.BlockCnt;
Kparams.KangCnt = KangCnt;
Kparams.DP = DP;
Kparams.KernelA_LDS_Size = 64 * JMP_CNT + 16 * Kparams.BlockSize;
Kparams.KernelB_LDS_Size = 64 * JMP_CNT;
Kparams.KernelC_LDS_Size = 96 * JMP_CNT;
//allocate gpu mem
u64 size;
if (!IsOldGpu)
{
//L2
int L2size = Kparams.KangCnt * (3 * 32);
total_mem += L2size;
err = cudaMalloc((void**)&Kparams.L2, L2size);
if (err != cudaSuccess)
{
printf("GPU %d, Allocate L2 memory failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
size = L2size;
if (size > persistingL2CacheMaxSize)
size = persistingL2CacheMaxSize;
err = cudaDeviceSetLimit(cudaLimitPersistingL2CacheSize, size); // set max allowed size for L2
//persisting for L2
cudaStreamAttrValue stream_attribute;
stream_attribute.accessPolicyWindow.base_ptr = Kparams.L2;
stream_attribute.accessPolicyWindow.num_bytes = size;
stream_attribute.accessPolicyWindow.hitRatio = 1.0;
stream_attribute.accessPolicyWindow.hitProp = cudaAccessPropertyPersisting;
stream_attribute.accessPolicyWindow.missProp = cudaAccessPropertyStreaming;
err = cudaStreamSetAttribute(NULL, cudaStreamAttributeAccessPolicyWindow, &stream_attribute);
if (err != cudaSuccess)
{
printf("GPU %d, cudaStreamSetAttribute failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
}
size = MAX_DP_CNT * GPU_DP_SIZE + 16;
total_mem += size;
err = cudaMalloc((void**)&Kparams.DPs_out, size);
if (err != cudaSuccess)
{
printf("GPU %d Allocate GpuOut memory failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
size = KangCnt * 96;
total_mem += size;
err = cudaMalloc((void**)&Kparams.Kangs, size);
if (err != cudaSuccess)
{
printf("GPU %d Allocate pKangs memory failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
total_mem += JMP_CNT * 96;
err = cudaMalloc((void**)&Kparams.Jumps1, JMP_CNT * 96);
if (err != cudaSuccess)
{
printf("GPU %d Allocate Jumps1 memory failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
total_mem += JMP_CNT * 96;
err = cudaMalloc((void**)&Kparams.Jumps2, JMP_CNT * 96);
if (err != cudaSuccess)
{
printf("GPU %d Allocate Jumps1 memory failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
total_mem += JMP_CNT * 96;
err = cudaMalloc((void**)&Kparams.Jumps3, JMP_CNT * 96);
if (err != cudaSuccess)
{
printf("GPU %d Allocate Jumps3 memory failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
size = 2 * (u64)KangCnt * STEP_CNT;
total_mem += size;
err = cudaMalloc((void**)&Kparams.JumpsList, size);
if (err != cudaSuccess)
{
printf("GPU %d Allocate JumpsList memory failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
size = (u64)KangCnt * (16 * DPTABLE_MAX_CNT + sizeof(u32)); //we store 16bytes of X
total_mem += size;
err = cudaMalloc((void**)&Kparams.DPTable, size);
if (err != cudaSuccess)
{
printf("GPU %d Allocate DPTable memory failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
size = mpCnt * Kparams.BlockSize * sizeof(u64);
total_mem += size;
err = cudaMalloc((void**)&Kparams.L1S2, size);
if (err != cudaSuccess)
{
printf("GPU %d Allocate L1S2 memory failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
size = (u64)KangCnt * MD_LEN * (2 * 32);
total_mem += size;
err = cudaMalloc((void**)&Kparams.LastPnts, size);
if (err != cudaSuccess)
{
printf("GPU %d Allocate LastPnts memory failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
size = (u64)KangCnt * MD_LEN * sizeof(u64);
total_mem += size;
err = cudaMalloc((void**)&Kparams.LoopTable, size);
if (err != cudaSuccess)
{
printf("GPU %d Allocate LastPnts memory failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
total_mem += 1024;
err = cudaMalloc((void**)&Kparams.dbg_buf, 1024);
if (err != cudaSuccess)
{
printf("GPU %d Allocate dbg_buf memory failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
size = sizeof(u32) * KangCnt + 8;
total_mem += size;
err = cudaMalloc((void**)&Kparams.LoopedKangs, size);
if (err != cudaSuccess)
{
printf("GPU %d Allocate LoopedKangs memory failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
DPs_out = (u32*)malloc(MAX_DP_CNT * GPU_DP_SIZE);
//jmp1
u64* buf = (u64*)malloc(JMP_CNT * 96);
for (int i = 0; i < JMP_CNT; i++)
{
memcpy(buf + i * 12, EcJumps1[i].p.x.data, 32);
memcpy(buf + i * 12 + 4, EcJumps1[i].p.y.data, 32);
memcpy(buf + i * 12 + 8, EcJumps1[i].dist.data, 32);
}
err = cudaMemcpy(Kparams.Jumps1, buf, JMP_CNT * 96, cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
printf("GPU %d, cudaMemcpy Jumps1 failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
free(buf);
//jmp2
buf = (u64*)malloc(JMP_CNT * 96);
u64* jmp2_table = (u64*)malloc(JMP_CNT * 64);
for (int i = 0; i < JMP_CNT; i++)
{
memcpy(buf + i * 12, EcJumps2[i].p.x.data, 32);
memcpy(jmp2_table + i * 8, EcJumps2[i].p.x.data, 32);
memcpy(buf + i * 12 + 4, EcJumps2[i].p.y.data, 32);
memcpy(jmp2_table + i * 8 + 4, EcJumps2[i].p.y.data, 32);
memcpy(buf + i * 12 + 8, EcJumps2[i].dist.data, 32);
}
err = cudaMemcpy(Kparams.Jumps2, buf, JMP_CNT * 96, cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
printf("GPU %d, cudaMemcpy Jumps2 failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
free(buf);
err = cuSetGpuParams(Kparams, jmp2_table);
if (err != cudaSuccess)
{
free(jmp2_table);
printf("GPU %d, cuSetGpuParams failed: %s!\r\n", CudaIndex, cudaGetErrorString(err));
return false;
}
free(jmp2_table);
//jmp3
buf = (u64*)malloc(JMP_CNT * 96);
for (int i = 0; i < JMP_CNT; i++)
{
memcpy(buf + i * 12, EcJumps3[i].p.x.data, 32);
memcpy(buf + i * 12 + 4, EcJumps3[i].p.y.data, 32);
memcpy(buf + i * 12 + 8, EcJumps3[i].dist.data, 32);
}
err = cudaMemcpy(Kparams.Jumps3, buf, JMP_CNT * 96, cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
printf("GPU %d, cudaMemcpy Jumps3 failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
free(buf);
printf("GPU %d: allocated %llu MB, %d kangaroos. OldGpuMode: %s\r\n", CudaIndex, total_mem / (1024 * 1024), KangCnt, IsOldGpu ? "Yes" : "No");
return true;
}
void RCGpuKang::Release()
{
free(RndPnts);
free(DPs_out);
cudaFree(Kparams.LoopedKangs);
cudaFree(Kparams.dbg_buf);
cudaFree(Kparams.LoopTable);
cudaFree(Kparams.LastPnts);
cudaFree(Kparams.L1S2);
cudaFree(Kparams.DPTable);
cudaFree(Kparams.JumpsList);
cudaFree(Kparams.Jumps3);
cudaFree(Kparams.Jumps2);
cudaFree(Kparams.Jumps1);
cudaFree(Kparams.Kangs);
cudaFree(Kparams.DPs_out);
if (!IsOldGpu)
cudaFree(Kparams.L2);
}
void RCGpuKang::Stop()
{
StopFlag = true;
}
void RCGpuKang::GenerateRndDistances()
{
for (int i = 0; i < KangCnt; i++)
{
EcInt d;
if (i < KangCnt / 3)
d.RndBits(Range - 4); //TAME kangs
else
{
d.RndBits(Range - 1);
d.data[0] &= 0xFFFFFFFFFFFFFFFE; //must be even
}
memcpy(RndPnts[i].priv, d.data, 24);
}
}
bool RCGpuKang::Start()
{
if (Failed)
return false;
cudaError_t err;
err = cudaSetDevice(CudaIndex);
if (err != cudaSuccess)
return false;
HalfRange.Set(1);
HalfRange.ShiftLeft(Range - 1);
PntHalfRange = ec.MultiplyG(HalfRange);
NegPntHalfRange = PntHalfRange;
NegPntHalfRange.y.NegModP();
PntA = ec.AddPoints(PntToSolve, NegPntHalfRange);
PntB = PntA;
PntB.y.NegModP();
RndPnts = (TPointPriv*)malloc(KangCnt * 96);
GenerateRndDistances();
/*
//we can calc start points on CPU
for (int i = 0; i < KangCnt; i++)
{
EcInt d;
memcpy(d.data, RndPnts[i].priv, 24);
d.data[3] = 0;
d.data[4] = 0;
EcPoint p = ec.MultiplyG(d);
memcpy(RndPnts[i].x, p.x.data, 32);
memcpy(RndPnts[i].y, p.y.data, 32);
}
for (int i = KangCnt / 3; i < 2 * KangCnt / 3; i++)
{
EcPoint p;
p.LoadFromBuffer64((u8*)RndPnts[i].x);
p = ec.AddPoints(p, PntA);
p.SaveToBuffer64((u8*)RndPnts[i].x);
}
for (int i = 2 * KangCnt / 3; i < KangCnt; i++)
{
EcPoint p;
p.LoadFromBuffer64((u8*)RndPnts[i].x);
p = ec.AddPoints(p, PntB);
p.SaveToBuffer64((u8*)RndPnts[i].x);
}
//copy to gpu
err = cudaMemcpy(Kparams.Kangs, RndPnts, KangCnt * 96, cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
printf("GPU %d, cudaMemcpy failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
/**/
//but it's faster to calc then on GPU
u8 buf_PntA[64], buf_PntB[64];
PntA.SaveToBuffer64(buf_PntA);
PntB.SaveToBuffer64(buf_PntB);
for (int i = 0; i < KangCnt; i++)
{
if (i < KangCnt / 3)
memset(RndPnts[i].x, 0, 64);
else
if (i < 2 * KangCnt / 3)
memcpy(RndPnts[i].x, buf_PntA, 64);
else
memcpy(RndPnts[i].x, buf_PntB, 64);
}
//copy to gpu
err = cudaMemcpy(Kparams.Kangs, RndPnts, KangCnt * 96, cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
printf("GPU %d, cudaMemcpy failed: %s\n", CudaIndex, cudaGetErrorString(err));
return false;
}
CallGpuKernelGen(Kparams);
err = cudaMemset(Kparams.L1S2, 0, mpCnt * Kparams.BlockSize * 8);
if (err != cudaSuccess)
return false;
cudaMemset(Kparams.dbg_buf, 0, 1024);
cudaMemset(Kparams.LoopTable, 0, KangCnt * MD_LEN * sizeof(u64));
return true;
}
#ifdef DEBUG_MODE
int RCGpuKang::Dbg_CheckKangs()
{
int kang_size = mpCnt * Kparams.BlockSize * Kparams.GroupCnt * 96;
u64* kangs = (u64*)malloc(kang_size);
cudaError_t err = cudaMemcpy(kangs, Kparams.Kangs, kang_size, cudaMemcpyDeviceToHost);
int res = 0;
for (int i = 0; i < KangCnt; i++)
{
EcPoint Pnt, p;
Pnt.LoadFromBuffer64((u8*)&kangs[i * 12 + 0]);
EcInt dist;
dist.Set(0);
memcpy(dist.data, &kangs[i * 12 + 8], 24);
bool neg = false;
if (dist.data[2] >> 63)
{
neg = true;
memset(((u8*)dist.data) + 24, 0xFF, 16);
dist.Neg();
}
p = ec.MultiplyG_Fast(dist);
if (neg)
p.y.NegModP();
if (i < KangCnt / 3)
p = p;
else
if (i < 2 * KangCnt / 3)
p = ec.AddPoints(PntA, p);
else
p = ec.AddPoints(PntB, p);
if (!p.IsEqual(Pnt))
res++;
}
free(kangs);
return res;
}
#endif
extern u32 gTotalErrors;
//executes in separate thread
void RCGpuKang::Execute()
{
cudaSetDevice(CudaIndex);
if (!Start())
{
gTotalErrors++;
return;
}
#ifdef DEBUG_MODE
u64 iter = 1;
#endif
cudaError_t err;
while (!StopFlag)
{
u64 t1 = GetTickCount64();
cudaMemset(Kparams.DPs_out, 0, 4);
cudaMemset(Kparams.DPTable, 0, KangCnt * sizeof(u32));
cudaMemset(Kparams.LoopedKangs, 0, 8);
CallGpuKernelABC(Kparams);
int cnt;
err = cudaMemcpy(&cnt, Kparams.DPs_out, 4, cudaMemcpyDeviceToHost);
if (err != cudaSuccess)
{
printf("GPU %d, CallGpuKernel failed: %s\r\n", CudaIndex, cudaGetErrorString(err));
gTotalErrors++;
break;
}
if (cnt >= MAX_DP_CNT)
{
cnt = MAX_DP_CNT;
printf("GPU %d, gpu DP buffer overflow, some points lost, increase DP value!\r\n", CudaIndex);
}
u64 pnt_cnt = (u64)KangCnt * STEP_CNT;
if (cnt)
{
err = cudaMemcpy(DPs_out, Kparams.DPs_out + 4, cnt * GPU_DP_SIZE, cudaMemcpyDeviceToHost);
if (err != cudaSuccess)
{
gTotalErrors++;
break;
}
AddPointsToList(DPs_out, cnt, (u64)KangCnt * STEP_CNT);
}
//dbg
cudaMemcpy(dbg, Kparams.dbg_buf, 1024, cudaMemcpyDeviceToHost);
u32 lcnt;
cudaMemcpy(&lcnt, Kparams.LoopedKangs, 4, cudaMemcpyDeviceToHost);
//printf("GPU %d, Looped: %d\r\n", CudaIndex, lcnt);
u64 t2 = GetTickCount64();
u64 tm = t2 - t1;
if (!tm)
tm = 1;
int cur_speed = (int)(pnt_cnt / (tm * 1000));
//printf("GPU %d kernel time %d ms, speed %d MH\r\n", CudaIndex, (int)tm, cur_speed);
SpeedStats[cur_stats_ind] = cur_speed;
cur_stats_ind = (cur_stats_ind + 1) % STATS_WND_SIZE;
#ifdef DEBUG_MODE
if ((iter % 300) == 0)
{
int corr_cnt = Dbg_CheckKangs();
if (corr_cnt)
{
printf("DBG: GPU %d, KANGS CORRUPTED: %d\r\n", CudaIndex, corr_cnt);
gTotalErrors++;
}
else
printf("DBG: GPU %d, ALL KANGS OK!\r\n", CudaIndex);
}
iter++;
#endif
}
Release();
}
int RCGpuKang::GetStatsSpeed()
{
int res = SpeedStats[0];
for (int i = 1; i < STATS_WND_SIZE; i++)
res += SpeedStats[i];
return res / STATS_WND_SIZE;
}