apow-cli 0.9.1 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,518 @@
1
+ /*
2
+ * APoW GPU Nonce Grinder — CUDA (RTX 4090) v5: Daemon Mode
3
+ *
4
+ * Grinds nonces for up to 8 addresses simultaneously in one kernel launch.
5
+ * Threads are partitioned across addresses — all addresses grind in parallel.
6
+ * Found nonces are streamed to stdout as they're discovered.
7
+ *
8
+ * One-shot mode (backwards compatible):
9
+ * Usage: ./grinder-cuda <challenge_hex> <target_hex> <addr1_hex> [addr2_hex] ...
10
+ *
11
+ * Daemon mode (persistent — eliminates CUDA init overhead per grind):
12
+ * Usage: ./grinder-cuda --daemon
13
+ * Stdin: <challenge_hex> <target_hex> [W<weight>] <addr1_hex> ...\n
14
+ * ABORT\n (cancel current grind, wait for next)
15
+ * Stdout: F <addr_index> <nonce_decimal> <elapsed_seconds>
16
+ * D <total_attempts> <elapsed_seconds>
17
+ * READY\n (daemon ready for next challenge)
18
+ *
19
+ * Build: nvcc grinder-cuda.cu -o grinder-cuda -std=c++17 -O3 -arch=sm_89
20
+ */
21
+
22
+ #include <cstdio>
23
+ #include <cstdlib>
24
+ #include <cstring>
25
+ #include <cstdint>
26
+ #include <ctime>
27
+ #include <chrono>
28
+ #include <unistd.h>
29
+ #include <sys/select.h>
30
+
31
+ #define MAX_ADDRS 8
32
+ #define MAX_LINE 4096
33
+
34
+ // ── Keccak round constants ──
35
+
36
+ __constant__ uint64_t RC[24] = {
37
+ 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
38
+ 0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
39
+ 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
40
+ 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
41
+ 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
42
+ 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
43
+ 0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
44
+ 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
45
+ };
46
+
47
+ __device__ __forceinline__ uint64_t rotl64(uint64_t x, int n) {
48
+ return (x << n) | (x >> (64 - n));
49
+ }
50
+
51
+ __device__ __forceinline__ uint64_t bswap64(uint64_t x) {
52
+ uint32_t lo = (uint32_t)x;
53
+ uint32_t hi = (uint32_t)(x >> 32);
54
+ return ((uint64_t)__byte_perm(lo, 0, 0x0123) << 32) |
55
+ (uint64_t)__byte_perm(hi, 0, 0x0123);
56
+ }
57
+
58
+ #define CHI(a0, a1, a2, a3, a4) { \
59
+ uint64_t c0=a0, c1=a1, c2=a2, c3=a3, c4=a4; \
60
+ a0 = c0 ^ (~c1 & c2); \
61
+ a1 = c1 ^ (~c2 & c3); \
62
+ a2 = c2 ^ (~c3 & c4); \
63
+ a3 = c3 ^ (~c4 & c0); \
64
+ a4 = c4 ^ (~c0 & c1); \
65
+ }
66
+
67
+ // ── Structures ──
68
+
69
+ struct GrindParams {
70
+ uint64_t pre_state[MAX_ADDRS][25]; // Pre-absorbed state per address
71
+ uint64_t target_be[4]; // Same target for all
72
+ uint64_t start_nonce;
73
+ uint32_t num_addrs;
74
+ uint32_t threads_per_addr[MAX_ADDRS]; // Per-address thread count (for weighting)
75
+ uint32_t thread_offset[MAX_ADDRS]; // Cumulative offset: sum of threads_per_addr[0..i-1]
76
+ };
77
+
78
+ struct GrindResult {
79
+ uint32_t found[MAX_ADDRS]; // Per-address found flag
80
+ uint64_t nonce[MAX_ADDRS]; // Per-address winning nonce
81
+ };
82
+
83
+ // ── GPU kernel: threads partitioned across addresses ──
84
+
85
+ __global__ void grind_kernel(const GrindParams* __restrict__ params, GrindResult* result) {
86
+ uint64_t tid = (uint64_t)blockIdx.x * blockDim.x + threadIdx.x;
87
+
88
+ // Find which address this thread belongs to via cumulative offsets
89
+ uint32_t addr_idx = 0;
90
+ uint32_t n = params->num_addrs;
91
+ for (uint32_t i = 0; i < n; i++) {
92
+ if (tid < params->thread_offset[i] + params->threads_per_addr[i]) {
93
+ addr_idx = i;
94
+ break;
95
+ }
96
+ }
97
+
98
+ uint64_t local_tid = tid - params->thread_offset[addr_idx];
99
+ if (local_tid >= params->threads_per_addr[addr_idx]) return;
100
+ if (result->found[addr_idx]) return;
101
+
102
+ uint64_t nonce = params->start_nonce + tid; // Global unique nonce per thread
103
+
104
+ // Load pre-absorbed state for this address
105
+ const uint64_t* ps = params->pre_state[addr_idx];
106
+ uint64_t s00 = ps[0], s01 = ps[1], s02 = ps[2], s03 = ps[3], s04 = ps[4],
107
+ s05 = ps[5], s06 = ps[6], s07 = ps[7], s08 = ps[8], s09 = ps[9],
108
+ s10 = ps[10], s11 = ps[11], s12 = ps[12], s13 = ps[13], s14 = ps[14],
109
+ s15 = ps[15], s16 = ps[16], s17 = ps[17], s18 = ps[18], s19 = ps[19],
110
+ s20 = ps[20], s21 = ps[21], s22 = ps[22], s23 = ps[23], s24 = ps[24];
111
+
112
+ // XOR nonce into words 9 and 10
113
+ uint32_t nhi = (uint32_t)(nonce >> 32);
114
+ uint32_t nlo = (uint32_t)(nonce);
115
+ s09 ^= (uint64_t)__byte_perm(nhi, 0, 0x0123) << 32;
116
+ s10 ^= (uint64_t)__byte_perm(nlo, 0, 0x0123);
117
+
118
+ // ── Keccak-f1600: 24 rounds ──
119
+ for (int r = 0; r < 24; r++) {
120
+ uint64_t bc0 = s00^s05^s10^s15^s20, bc1 = s01^s06^s11^s16^s21,
121
+ bc2 = s02^s07^s12^s17^s22, bc3 = s03^s08^s13^s18^s23,
122
+ bc4 = s04^s09^s14^s19^s24;
123
+ uint64_t t0 = bc4^rotl64(bc1,1), t1 = bc0^rotl64(bc2,1),
124
+ t2 = bc1^rotl64(bc3,1), t3 = bc2^rotl64(bc4,1),
125
+ t4 = bc3^rotl64(bc0,1);
126
+ s00^=t0; s05^=t0; s10^=t0; s15^=t0; s20^=t0;
127
+ s01^=t1; s06^=t1; s11^=t1; s16^=t1; s21^=t1;
128
+ s02^=t2; s07^=t2; s12^=t2; s17^=t2; s22^=t2;
129
+ s03^=t3; s08^=t3; s13^=t3; s18^=t3; s23^=t3;
130
+ s04^=t4; s09^=t4; s14^=t4; s19^=t4; s24^=t4;
131
+
132
+ uint64_t tmp, t = s01;
133
+ tmp=s10; s10=rotl64(t, 1); t=tmp; tmp=s07; s07=rotl64(t, 3); t=tmp;
134
+ tmp=s11; s11=rotl64(t, 6); t=tmp; tmp=s17; s17=rotl64(t,10); t=tmp;
135
+ tmp=s18; s18=rotl64(t,15); t=tmp; tmp=s03; s03=rotl64(t,21); t=tmp;
136
+ tmp=s05; s05=rotl64(t,28); t=tmp; tmp=s16; s16=rotl64(t,36); t=tmp;
137
+ tmp=s08; s08=rotl64(t,45); t=tmp; tmp=s21; s21=rotl64(t,55); t=tmp;
138
+ tmp=s24; s24=rotl64(t, 2); t=tmp; tmp=s04; s04=rotl64(t,14); t=tmp;
139
+ tmp=s15; s15=rotl64(t,27); t=tmp; tmp=s23; s23=rotl64(t,41); t=tmp;
140
+ tmp=s19; s19=rotl64(t,56); t=tmp; tmp=s13; s13=rotl64(t, 8); t=tmp;
141
+ tmp=s12; s12=rotl64(t,25); t=tmp; tmp=s02; s02=rotl64(t,43); t=tmp;
142
+ tmp=s20; s20=rotl64(t,62); t=tmp; tmp=s14; s14=rotl64(t,18); t=tmp;
143
+ tmp=s22; s22=rotl64(t,39); t=tmp; tmp=s09; s09=rotl64(t,61); t=tmp;
144
+ tmp=s06; s06=rotl64(t,20); t=tmp; s01=rotl64(t,44);
145
+
146
+ CHI(s00,s01,s02,s03,s04); CHI(s05,s06,s07,s08,s09);
147
+ CHI(s10,s11,s12,s13,s14); CHI(s15,s16,s17,s18,s19);
148
+ CHI(s20,s21,s22,s23,s24);
149
+
150
+ s00 ^= RC[r];
151
+ }
152
+
153
+ // ── Compare hash < target ──
154
+ uint64_t h, tgt;
155
+ h = bswap64(s00); tgt = params->target_be[0];
156
+ if (h > tgt) return; if (h < tgt) goto found;
157
+ h = bswap64(s01); tgt = params->target_be[1];
158
+ if (h > tgt) return; if (h < tgt) goto found;
159
+ h = bswap64(s02); tgt = params->target_be[2];
160
+ if (h > tgt) return; if (h < tgt) goto found;
161
+ h = bswap64(s03); tgt = params->target_be[3];
162
+ if (h > tgt) return; if (h < tgt) goto found;
163
+ return;
164
+
165
+ found:
166
+ if (atomicCAS(&result->found[addr_idx], 0u, 1u) == 0u) {
167
+ result->nonce[addr_idx] = nonce;
168
+ }
169
+ }
170
+
171
+ // ── Host code ──
172
+
173
+ static int hex2byte(char c) {
174
+ if (c >= '0' && c <= '9') return c - '0';
175
+ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
176
+ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
177
+ return -1;
178
+ }
179
+
180
+ static int parse_hex(const char* hex, uint8_t* out, int len) {
181
+ if (hex[0] == '0' && hex[1] == 'x') hex += 2;
182
+ for (int i = 0; i < len; i++) {
183
+ int hi = hex2byte(hex[i*2]);
184
+ int lo = hex2byte(hex[i*2+1]);
185
+ if (hi < 0 || lo < 0) return -1;
186
+ out[i] = (hi << 4) | lo;
187
+ }
188
+ return 0;
189
+ }
190
+
191
+ // Check if stdin has data available (non-blocking)
192
+ static bool stdin_has_data() {
193
+ fd_set fds;
194
+ struct timeval tv = {0, 0}; // 0 timeout = non-blocking
195
+ FD_ZERO(&fds);
196
+ FD_SET(STDIN_FILENO, &fds);
197
+ return select(STDIN_FILENO + 1, &fds, nullptr, nullptr, &tv) > 0;
198
+ }
199
+
200
+ // Parse a challenge line into components. Returns number of addresses parsed, or -1 on error.
201
+ static int parse_challenge_line(const char* line,
202
+ uint8_t challenge[32], uint8_t target[32],
203
+ uint8_t addresses[][20], int weights[], int max_addrs) {
204
+ // Tokenize the line (copy it since strtok modifies)
205
+ char buf[MAX_LINE];
206
+ strncpy(buf, line, MAX_LINE - 1);
207
+ buf[MAX_LINE - 1] = '\0';
208
+
209
+ char* tokens[MAX_ADDRS + 10]; // challenge + target + up to MAX_ADDRS * 2 (weight + addr)
210
+ int ntokens = 0;
211
+ char* tok = strtok(buf, " \t\r\n");
212
+ while (tok && ntokens < (int)(sizeof(tokens)/sizeof(tokens[0]))) {
213
+ tokens[ntokens++] = tok;
214
+ tok = strtok(nullptr, " \t\r\n");
215
+ }
216
+
217
+ if (ntokens < 3) return -1; // Need at least challenge + target + 1 addr
218
+
219
+ if (parse_hex(tokens[0], challenge, 32) < 0) return -1;
220
+ if (parse_hex(tokens[1], target, 32) < 0) return -1;
221
+
222
+ int num_addrs = 0;
223
+ int next_weight = 1;
224
+ for (int i = 2; i < ntokens && num_addrs < max_addrs; i++) {
225
+ if (tokens[i][0] == 'W' || tokens[i][0] == 'w') {
226
+ next_weight = atoi(tokens[i] + 1);
227
+ if (next_weight < 1) next_weight = 1;
228
+ if (next_weight > 4) next_weight = 4;
229
+ continue;
230
+ }
231
+ if (parse_hex(tokens[i], addresses[num_addrs], 20) < 0) return -1;
232
+ weights[num_addrs] = next_weight;
233
+ next_weight = 1;
234
+ num_addrs++;
235
+ }
236
+ return num_addrs;
237
+ }
238
+
239
+ // Compute pre-absorbed keccak state for each address
240
+ static void compute_pre_states(uint64_t pre_states[][25], const uint8_t challenge[32],
241
+ const uint8_t addresses[][20], int num_addrs) {
242
+ for (int a = 0; a < num_addrs; a++) {
243
+ uint8_t input[136] = {0};
244
+ memcpy(input, challenge, 32);
245
+ memcpy(input + 32, addresses[a], 20);
246
+ input[84] = 0x01;
247
+ input[135] = 0x80;
248
+
249
+ memset(pre_states[a], 0, sizeof(uint64_t) * 25);
250
+ for (int i = 0; i < 17; i++) {
251
+ uint64_t w = 0;
252
+ for (int b = 0; b < 8; b++)
253
+ w |= (uint64_t)input[i*8 + b] << (b * 8);
254
+ pre_states[a][i] = w;
255
+ }
256
+ }
257
+ }
258
+
259
+ // Compute target as big-endian uint64_t[4]
260
+ static void compute_target_be(uint64_t target_be[4], const uint8_t target[32]) {
261
+ for (int i = 0; i < 4; i++) {
262
+ uint64_t w = 0;
263
+ for (int b = 0; b < 8; b++)
264
+ w = (w << 8) | target[i*8 + b];
265
+ target_be[i] = w;
266
+ }
267
+ }
268
+
269
+ // Configure dispatch (weighted thread allocation across addresses)
270
+ static void configure_dispatch(GrindParams* params, const int weights[], int num_addrs,
271
+ uint64_t* out_batch_size, int* out_grid_size) {
272
+ const int BLOCK_SIZE = 256;
273
+ const uint64_t RAW_BATCH = 1ULL << 26; // 64M
274
+
275
+ int total_weight = 0;
276
+ for (int a = 0; a < num_addrs; a++) total_weight += weights[a];
277
+
278
+ uint64_t batch_size = 0;
279
+ uint32_t cum_offset = 0;
280
+ for (int a = 0; a < num_addrs; a++) {
281
+ uint64_t tpa = (RAW_BATCH * weights[a] / total_weight / BLOCK_SIZE) * BLOCK_SIZE;
282
+ if (tpa < (uint64_t)BLOCK_SIZE) tpa = BLOCK_SIZE;
283
+ params->threads_per_addr[a] = (uint32_t)tpa;
284
+ params->thread_offset[a] = cum_offset;
285
+ cum_offset += (uint32_t)tpa;
286
+ batch_size += tpa;
287
+ }
288
+
289
+ *out_batch_size = batch_size;
290
+ *out_grid_size = (int)(batch_size / BLOCK_SIZE);
291
+ }
292
+
293
+ // Run the grind loop. Returns true if completed (found or exhausted), false if aborted.
294
+ static bool run_grind(GrindParams* params, GrindResult* result,
295
+ int num_addrs, uint64_t baseNonce,
296
+ uint64_t BATCH_SIZE, int GRID_SIZE, int BLOCK_SIZE,
297
+ bool check_abort) {
298
+ const int MAX_BATCHES = 1 << 20;
299
+ bool found_all[MAX_ADDRS] = {false};
300
+ int found_count = 0;
301
+
302
+ auto t0 = std::chrono::steady_clock::now();
303
+ uint64_t totalAttempts = 0;
304
+
305
+ for (int batch = 0; batch < MAX_BATCHES && found_count < num_addrs; batch++) {
306
+ params->start_nonce = baseNonce + (uint64_t)batch * BATCH_SIZE;
307
+
308
+ // Reset unfound addresses
309
+ for (int a = 0; a < num_addrs; a++) {
310
+ if (!found_all[a]) {
311
+ result->found[a] = 0;
312
+ result->nonce[a] = 0;
313
+ }
314
+ }
315
+
316
+ grind_kernel<<<GRID_SIZE, BLOCK_SIZE>>>(params, result);
317
+ cudaDeviceSynchronize();
318
+
319
+ totalAttempts += BATCH_SIZE;
320
+
321
+ // Check for newly found nonces — stream to stdout immediately
322
+ for (int a = 0; a < num_addrs; a++) {
323
+ if (!found_all[a] && result->found[a]) {
324
+ auto t1 = std::chrono::steady_clock::now();
325
+ double elapsed = std::chrono::duration<double>(t1 - t0).count();
326
+ printf("F %d %llu %.3f\n", a,
327
+ (unsigned long long)result->nonce[a], elapsed);
328
+ fflush(stdout);
329
+ found_all[a] = true;
330
+ found_count++;
331
+ }
332
+ }
333
+
334
+ // In daemon mode, check for ABORT between batches
335
+ if (check_abort && stdin_has_data()) {
336
+ char abort_buf[MAX_LINE];
337
+ if (fgets(abort_buf, sizeof(abort_buf), stdin)) {
338
+ // Trim
339
+ char* p = abort_buf;
340
+ while (*p == ' ' || *p == '\t') p++;
341
+ if (strncmp(p, "ABORT", 5) == 0) {
342
+ auto t1 = std::chrono::steady_clock::now();
343
+ double elapsed = std::chrono::duration<double>(t1 - t0).count();
344
+ printf("D %llu %.3f\n", (unsigned long long)totalAttempts, elapsed);
345
+ fflush(stdout);
346
+ return false; // Aborted
347
+ }
348
+ }
349
+ }
350
+ }
351
+
352
+ auto t1 = std::chrono::steady_clock::now();
353
+ double elapsed = std::chrono::duration<double>(t1 - t0).count();
354
+ printf("D %llu %.3f\n", (unsigned long long)totalAttempts, elapsed);
355
+ fflush(stdout);
356
+ return true;
357
+ }
358
+
359
+ // ── Daemon mode: persistent CUDA process ──
360
+ static int run_daemon() {
361
+ const int BLOCK_SIZE = 256;
362
+
363
+ // Allocate unified memory ONCE
364
+ GrindParams* params;
365
+ GrindResult* result;
366
+ cudaMallocManaged(&params, sizeof(GrindParams));
367
+ cudaMallocManaged(&result, sizeof(GrindResult));
368
+
369
+ srand((unsigned)time(nullptr) ^ (unsigned)getpid());
370
+
371
+ // Warmup: launch a tiny kernel to initialize CUDA context fully
372
+ params->num_addrs = 0;
373
+ params->start_nonce = 0;
374
+ grind_kernel<<<1, 1>>>(params, result);
375
+ cudaDeviceSynchronize();
376
+
377
+ fprintf(stderr, "CUDA daemon ready\n");
378
+ printf("READY\n");
379
+ fflush(stdout);
380
+
381
+ // Main loop: read challenges from stdin
382
+ char line[MAX_LINE];
383
+ while (fgets(line, sizeof(line), stdin)) {
384
+ // Trim leading whitespace
385
+ char* p = line;
386
+ while (*p == ' ' || *p == '\t') p++;
387
+
388
+ // Skip empty lines
389
+ if (*p == '\n' || *p == '\r' || *p == '\0') continue;
390
+
391
+ // Ignore stray ABORT between challenges
392
+ if (strncmp(p, "ABORT", 5) == 0) continue;
393
+
394
+ // Parse challenge line
395
+ uint8_t challenge[32], target[32];
396
+ uint8_t addresses[MAX_ADDRS][20];
397
+ int weights[MAX_ADDRS];
398
+
399
+ int num_addrs = parse_challenge_line(p, challenge, target, addresses, weights, MAX_ADDRS);
400
+ if (num_addrs <= 0) {
401
+ fprintf(stderr, "Invalid challenge line, skipping\n");
402
+ printf("READY\n");
403
+ fflush(stdout);
404
+ continue;
405
+ }
406
+
407
+ // Compute pre-states
408
+ uint64_t pre_states[MAX_ADDRS][25];
409
+ compute_pre_states(pre_states, challenge, addresses, num_addrs);
410
+
411
+ uint64_t target_be[4];
412
+ compute_target_be(target_be, target);
413
+
414
+ // Load into GPU params
415
+ memcpy(params->pre_state, pre_states, sizeof(pre_states));
416
+ memcpy(params->target_be, target_be, sizeof(target_be));
417
+ params->num_addrs = num_addrs;
418
+
419
+ // Configure dispatch
420
+ uint64_t BATCH_SIZE;
421
+ int GRID_SIZE;
422
+ configure_dispatch(params, weights, num_addrs, &BATCH_SIZE, &GRID_SIZE);
423
+
424
+ // Random base nonce per challenge
425
+ uint64_t baseNonce = ((uint64_t)rand() << 32) | (uint64_t)rand();
426
+
427
+ // Run grind (with abort checking)
428
+ run_grind(params, result, num_addrs, baseNonce, BATCH_SIZE, GRID_SIZE, BLOCK_SIZE, true);
429
+
430
+ // Signal ready for next challenge
431
+ printf("READY\n");
432
+ fflush(stdout);
433
+ }
434
+
435
+ cudaFree(params);
436
+ cudaFree(result);
437
+ return 0;
438
+ }
439
+
440
+ // ── One-shot mode (original behavior) ──
441
+ static int run_oneshot(int argc, char* argv[]) {
442
+ const int BLOCK_SIZE = 256;
443
+
444
+ uint8_t challenge[32], target[32];
445
+ if (parse_hex(argv[1], challenge, 32) < 0 || parse_hex(argv[2], target, 32) < 0) {
446
+ fprintf(stderr, "Invalid hex input\n");
447
+ return 1;
448
+ }
449
+
450
+ int num_addrs = 0;
451
+ uint8_t addresses[MAX_ADDRS][20];
452
+ int weights[MAX_ADDRS];
453
+ int next_weight = 1;
454
+
455
+ for (int i = 3; i < argc && num_addrs < MAX_ADDRS; i++) {
456
+ if (argv[i][0] == 'W' || argv[i][0] == 'w') {
457
+ next_weight = atoi(argv[i] + 1);
458
+ if (next_weight < 1) next_weight = 1;
459
+ if (next_weight > 4) next_weight = 4;
460
+ continue;
461
+ }
462
+ if (parse_hex(argv[i], addresses[num_addrs], 20) < 0) {
463
+ fprintf(stderr, "Invalid address hex: %s\n", argv[i]);
464
+ return 1;
465
+ }
466
+ weights[num_addrs] = next_weight;
467
+ next_weight = 1;
468
+ num_addrs++;
469
+ }
470
+
471
+ // Pre-absorb state
472
+ uint64_t pre_states[MAX_ADDRS][25];
473
+ compute_pre_states(pre_states, challenge, addresses, num_addrs);
474
+
475
+ uint64_t target_be[4];
476
+ compute_target_be(target_be, target);
477
+
478
+ // Allocate unified memory
479
+ GrindParams* params;
480
+ GrindResult* result;
481
+ cudaMallocManaged(&params, sizeof(GrindParams));
482
+ cudaMallocManaged(&result, sizeof(GrindResult));
483
+
484
+ memcpy(params->pre_state, pre_states, sizeof(pre_states));
485
+ memcpy(params->target_be, target_be, sizeof(target_be));
486
+ params->num_addrs = num_addrs;
487
+
488
+ srand((unsigned)time(nullptr) ^ (unsigned)getpid());
489
+ uint64_t baseNonce = ((uint64_t)rand() << 32) | (uint64_t)rand();
490
+
491
+ // Configure dispatch
492
+ uint64_t BATCH_SIZE;
493
+ int GRID_SIZE;
494
+ configure_dispatch(params, weights, num_addrs, &BATCH_SIZE, &GRID_SIZE);
495
+
496
+ // Run grind (no abort checking in one-shot mode)
497
+ bool found = run_grind(params, result, num_addrs, baseNonce, BATCH_SIZE, GRID_SIZE, BLOCK_SIZE, false);
498
+
499
+ cudaFree(params);
500
+ cudaFree(result);
501
+ return found ? 0 : 1;
502
+ }
503
+
504
+ int main(int argc, char* argv[]) {
505
+ // Check for --daemon flag
506
+ if (argc == 2 && strcmp(argv[1], "--daemon") == 0) {
507
+ return run_daemon();
508
+ }
509
+
510
+ // One-shot mode (backwards compatible)
511
+ if (argc < 4) {
512
+ fprintf(stderr, "Usage: %s <challenge_hex> <target_hex> [W<weight>] <addr1_hex> ...\n", argv[0]);
513
+ fprintf(stderr, " %s --daemon\n", argv[0]);
514
+ return 1;
515
+ }
516
+
517
+ return run_oneshot(argc, argv);
518
+ }