Commit 86d5377e727ffcfe8ac0c78a282453aa35a5b498

Con Kolivas 2012-08-23T10:48:15

Use atomic ops to never miss a nonce on opencl kernels, including nonce==0, also allowing us to make the output buffer smaller.

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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
diff --git a/diablo120724.cl b/diablo120724.cl
index 4b64c30..4687c5b 100644
--- a/diablo120724.cl
+++ b/diablo120724.cl
@@ -62,7 +62,7 @@ void search(
     const uint c1_plus_k5, const uint b1_plus_k6,
     const uint state0, const uint state1, const uint state2, const uint state3,
     const uint state4, const uint state5, const uint state6, const uint state7,
-    __global uint * output)
+    volatile __global uint * output)
 {
 
   z ZA[930];
@@ -1242,33 +1242,50 @@ void search(
     
     ZA[924] = (ZCh(ZA[922], ZA[920], ZA[918]) + ZA[923]) + ZR26(ZA[922]);
     
-#define FOUND (0x800)
-#define NFLAG (0x7FF)
+#define FOUND (0x0F)
 
 #if defined(VECTORS4)
 	bool result = any(ZA[924] == 0x136032EDU);
 
 	if (result) {
-		if (ZA[924].x == 0x136032EDU)
-			output[FOUND] = output[NFLAG & Znonce.x] =  Znonce.x;
-		if (ZA[924].y == 0x136032EDU)
-			output[FOUND] = output[NFLAG & Znonce.y] =  Znonce.y;
-		if (ZA[924].z == 0x136032EDU)
-			output[FOUND] = output[NFLAG & Znonce.z] =  Znonce.z;
-		if (ZA[924].w == 0x136032EDU)
-			output[FOUND] = output[NFLAG & Znonce.w] =  Znonce.w;
+		uint found;
+
+		if (ZA[924].x == 0x136032EDU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = Znonce.x;
+		}
+		if (ZA[924].y == 0x136032EDU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = Znonce.y;
+		}
+		if (ZA[924].z == 0x136032EDU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = Znonce.z;
+		}
+		if (ZA[924].w == 0x136032EDU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = Znonce.w;
+		}
 	}
 #elif defined(VECTORS2)
 	bool result = any(ZA[924] == 0x136032EDU);
 
 	if (result) {
-		if (ZA[924].x == 0x136032EDU)
-			output[FOUND] = output[NFLAG & Znonce.x] =  Znonce.x;
-		if (ZA[924].y == 0x136032EDU)
-			output[FOUND] = output[NFLAG & Znonce.y] =  Znonce.y;
+		uint found;
+
+		if (ZA[924].x == 0x136032EDU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = Znonce.x;
+		}
+		if (ZA[924].y == 0x136032EDU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = Znonce.y;
+		}
 	}
 #else
-	if (ZA[924] == 0x136032EDU)
-		output[FOUND] = output[NFLAG & Znonce] =  Znonce;
+	if (ZA[924] == 0x136032EDU) {
+		uint found = atomic_add(&output[FOUND], 1);
+		output[found] = Znonce;
+	}
 #endif
 }
diff --git a/diakgcn120724.cl b/diakgcn120724.cl
index 7dd73fb..d27674f 100644
--- a/diakgcn120724.cl
+++ b/diakgcn120724.cl
@@ -48,7 +48,7 @@ __kernel
 			const uint state0A, const uint state0B,
 			const uint state1A, const uint state2A, const uint state3A, const uint state4A,
 			const uint state5A, const uint state6A, const uint state7A,
-			__global uint * output)
+			volatile __global uint * output)
 {
 	u V[8];
 	u W[16];
@@ -571,17 +571,46 @@ __kernel
 
 	V[7] += V[3] + W[12] + ch(V[0], V[1], V[2]) + rotr26(V[0]);
 
-#define FOUND (0x800)
-#define NFLAG (0x7FF)
+#define FOUND (0x0F)
 
 #ifdef VECTORS4
-	if ((V[7].x == 0x136032edU) ^ (V[7].y == 0x136032edU) ^ (V[7].z == 0x136032edU) ^ (V[7].w == 0x136032edU))
-		output[FOUND] = output[NFLAG & nonce.x] = (V[7].x == 0x136032edU) ? nonce.x : ((V[7].y == 0x136032edU) ? nonce.y : ((V[7].z == 0x136032edU) ? nonce.z : nonce.w));
+	if ((V[7].x == 0x136032edU) ^ (V[7].y == 0x136032edU) ^ (V[7].z == 0x136032edU) ^ (V[7].w == 0x136032edU)) {
+		uint found;
+
+		if (V[7].x == 0x136032edU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = nonce.x;
+		}
+		if (V[7].y == 0x136032edU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = nonce.y;
+		}
+		if (V[7].z == 0x136032edU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = nonce.z;
+		}
+		if (V[7].w == 0x136032edU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = nonce.w;
+		}
+	}
 #elif defined VECTORS2
-	if ((V[7].x == 0x136032edU) + (V[7].y == 0x136032edU))
-		output[FOUND] = output[NFLAG & nonce.x] = (V[7].x == 0x136032edU) ? nonce.x : nonce.y;
+	if ((V[7].x == 0x136032edU) + (V[7].y == 0x136032edU)) {
+		uint found;
+
+		if (V[7].x == 0x136032edU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = nonce.x;
+		}
+		if (V[7].y == 0x136032edU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = nonce.y;
+		}
+	}
 #else
-	if (V[7] == 0x136032edU)
-		output[FOUND] = output[NFLAG & nonce] = nonce;
+	if (V[7] == 0x136032edU) {
+		uint found = atomic_add(&output[FOUND], 1);
+		output[found] = nonce;
+	}
 #endif
 }
diff --git a/driver-opencl.c b/driver-opencl.c
index a8744e2..6883ada 100644
--- a/driver-opencl.c
+++ b/driver-opencl.c
@@ -1511,7 +1511,7 @@ static int64_t opencl_scanhash(struct thr_info *thr, struct work *work,
 	if (hashes > gpu->max_hashes)
 		gpu->max_hashes = hashes;
 
-	/* MAXBUFFERS entry is used as a flag to say nonces exist */
+	/* FOUND entry is used as a counter to say how many nonces exist */
 	if (thrdata->res[FOUND]) {
 		/* Clear the buffer again */
 		status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
diff --git a/findnonce.c b/findnonce.c
index 9980a70..d557f17 100644
--- a/findnonce.c
+++ b/findnonce.c
@@ -172,6 +172,7 @@ struct pc_data {
 	struct work *work;
 	uint32_t res[MAXBUFFERS];
 	pthread_t pth;
+	int found;
 };
 
 static void send_sha_nonce(struct pc_data *pcd, cl_uint nonce)
@@ -237,32 +238,22 @@ static void send_scrypt_nonce(struct pc_data *pcd, uint32_t nonce)
 static void *postcalc_hash(void *userdata)
 {
 	struct pc_data *pcd = (struct pc_data *)userdata;
-	struct thr_info *thr = pcd->thr;
-	int entry = 0, nonces = 0;
+	unsigned int entry = 0;
 
 	pthread_detach(pthread_self());
 
-	for (entry = 0; entry < FOUND; entry++) {
+	for (entry = 0; entry < pcd->res[FOUND]; entry++) {
 		uint32_t nonce = pcd->res[entry];
 
-		if (nonce) {
-			applog(LOG_DEBUG, "OCL NONCE %u", nonce);
-			if (opt_scrypt)
-				send_scrypt_nonce(pcd, nonce);
-			else
-				send_sha_nonce(pcd, nonce);
-			nonces++;
-		}
+		applog(LOG_DEBUG, "OCL NONCE %u found in slot %d", nonce, entry);
+		if (opt_scrypt)
+			send_scrypt_nonce(pcd, nonce);
+		else
+			send_sha_nonce(pcd, nonce);
 	}
 
 	free(pcd);
 
-	if (unlikely(!nonces)) {
-		applog(LOG_DEBUG, "No nonces found! Error in OpenCL code?");
-		hw_errors++;
-		thr->cgpu->hw_errors++;
-	}
-
 	return NULL;
 }
 
diff --git a/findnonce.h b/findnonce.h
index ce69569..610f6f8 100644
--- a/findnonce.h
+++ b/findnonce.h
@@ -4,10 +4,9 @@
 #include "config.h"
 
 #define MAXTHREADS (0xFFFFFFFEULL)
-#define MAXBUFFERS (0xFFF)
+#define MAXBUFFERS (0x10)
 #define BUFFERSIZE (sizeof(uint32_t) * MAXBUFFERS)
-#define FOUND (0x800)
-/* #define NFLAG (0x7FF) Just for reference */
+#define FOUND (0x0F)
 
 #ifdef HAVE_OPENCL
 extern void precalc_hash(dev_blk_ctx *blk, uint32_t *state, uint32_t *data);
diff --git a/mkinstalldirs b/mkinstalldirs
deleted file mode 100755
index 55d537f..0000000
--- a/mkinstalldirs
+++ /dev/null
@@ -1,162 +0,0 @@
-#! /bin/sh
-# mkinstalldirs --- make directory hierarchy
-
-scriptversion=2009-04-28.21; # UTC
-
-# Original author: Noah Friedman <friedman@prep.ai.mit.edu>
-# Created: 1993-05-16
-# Public domain.
-#
-# This file is maintained in Automake, please report
-# bugs to <bug-automake@gnu.org> or send patches to
-# <automake-patches@gnu.org>.
-
-nl='
-'
-IFS=" ""	$nl"
-errstatus=0
-dirmode=
-
-usage="\
-Usage: mkinstalldirs [-h] [--help] [--version] [-m MODE] DIR ...
-
-Create each directory DIR (with mode MODE, if specified), including all
-leading file name components.
-
-Report bugs to <bug-automake@gnu.org>."
-
-# process command line arguments
-while test $# -gt 0 ; do
-  case $1 in
-    -h | --help | --h*)         # -h for help
-      echo "$usage"
-      exit $?
-      ;;
-    -m)                         # -m PERM arg
-      shift
-      test $# -eq 0 && { echo "$usage" 1>&2; exit 1; }
-      dirmode=$1
-      shift
-      ;;
-    --version)
-      echo "$0 $scriptversion"
-      exit $?
-      ;;
-    --)                         # stop option processing
-      shift
-      break
-      ;;
-    -*)                         # unknown option
-      echo "$usage" 1>&2
-      exit 1
-      ;;
-    *)                          # first non-opt arg
-      break
-      ;;
-  esac
-done
-
-for file
-do
-  if test -d "$file"; then
-    shift
-  else
-    break
-  fi
-done
-
-case $# in
-  0) exit 0 ;;
-esac
-
-# Solaris 8's mkdir -p isn't thread-safe.  If you mkdir -p a/b and
-# mkdir -p a/c at the same time, both will detect that a is missing,
-# one will create a, then the other will try to create a and die with
-# a "File exists" error.  This is a problem when calling mkinstalldirs
-# from a parallel make.  We use --version in the probe to restrict
-# ourselves to GNU mkdir, which is thread-safe.
-case $dirmode in
-  '')
-    if mkdir -p --version . >/dev/null 2>&1 && test ! -d ./--version; then
-      echo "mkdir -p -- $*"
-      exec mkdir -p -- "$@"
-    else
-      # On NextStep and OpenStep, the 'mkdir' command does not
-      # recognize any option.  It will interpret all options as
-      # directories to create, and then abort because '.' already
-      # exists.
-      test -d ./-p && rmdir ./-p
-      test -d ./--version && rmdir ./--version
-    fi
-    ;;
-  *)
-    if mkdir -m "$dirmode" -p --version . >/dev/null 2>&1 &&
-       test ! -d ./--version; then
-      echo "mkdir -m $dirmode -p -- $*"
-      exec mkdir -m "$dirmode" -p -- "$@"
-    else
-      # Clean up after NextStep and OpenStep mkdir.
-      for d in ./-m ./-p ./--version "./$dirmode";
-      do
-        test -d $d && rmdir $d
-      done
-    fi
-    ;;
-esac
-
-for file
-do
-  case $file in
-    /*) pathcomp=/ ;;
-    *)  pathcomp= ;;
-  esac
-  oIFS=$IFS
-  IFS=/
-  set fnord $file
-  shift
-  IFS=$oIFS
-
-  for d
-  do
-    test "x$d" = x && continue
-
-    pathcomp=$pathcomp$d
-    case $pathcomp in
-      -*) pathcomp=./$pathcomp ;;
-    esac
-
-    if test ! -d "$pathcomp"; then
-      echo "mkdir $pathcomp"
-
-      mkdir "$pathcomp" || lasterr=$?
-
-      if test ! -d "$pathcomp"; then
-	errstatus=$lasterr
-      else
-	if test ! -z "$dirmode"; then
-	  echo "chmod $dirmode $pathcomp"
-	  lasterr=
-	  chmod "$dirmode" "$pathcomp" || lasterr=$?
-
-	  if test ! -z "$lasterr"; then
-	    errstatus=$lasterr
-	  fi
-	fi
-      fi
-    fi
-
-    pathcomp=$pathcomp/
-  done
-done
-
-exit $errstatus
-
-# Local Variables:
-# mode: shell-script
-# sh-indentation: 2
-# eval: (add-hook 'write-file-hooks 'time-stamp)
-# time-stamp-start: "scriptversion="
-# time-stamp-format: "%:y-%02m-%02d.%02H"
-# time-stamp-time-zone: "UTC"
-# time-stamp-end: "; # UTC"
-# End:
diff --git a/phatk120724.cl b/phatk120724.cl
index 0f60443..cf5eb09 100644
--- a/phatk120724.cl
+++ b/phatk120724.cl
@@ -164,7 +164,7 @@ void search(	const uint state0, const uint state1, const uint state2, const uint
 						const uint PreW18, const uint PreW19,
 						const uint PreW31, const uint PreW32,
 						
-						__global uint * output)
+						volatile __global uint * output)
 {
 
 
@@ -387,31 +387,48 @@ void search(	const uint state0, const uint state1, const uint state2, const uint
 	W[117] += W[108] + Vals[3] + Vals[7] + P2(124) + P1(124) + Ch((Vals[0] + Vals[4]) + (K[59] + W(59+64)) + s1(64+59)+ ch(59+64),Vals[1],Vals[2]) -
 		(-(K[60] + H[7]) - S1((Vals[0] + Vals[4]) + (K[59] + W(59+64))  + s1(64+59)+ ch(59+64)));
 
-#define FOUND (0x800)
-#define NFLAG (0x7FF)
+#define FOUND (0x0F)
 
 #ifdef VECTORS4
 	bool result = W[117].x & W[117].y & W[117].z & W[117].w;
 	if (!result) {
-		if (!W[117].x)
-			output[FOUND] = output[NFLAG & W[3].x] = W[3].x;
-		if (!W[117].y)
-			output[FOUND] = output[NFLAG & W[3].y] = W[3].y;
-		if (!W[117].z)
-			output[FOUND] = output[NFLAG & W[3].z] = W[3].z;
-		if (!W[117].w)
-			output[FOUND] = output[NFLAG & W[3].w] = W[3].w;
+		uint found;
+
+		if (!W[117].x) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = W[3].x;
+		}
+		if (!W[117].y) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = W[3].y;
+		}
+		if (!W[117].z) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = W[3].z;
+		}
+		if (!W[117].w) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = W[3].w;
+		}
 	}
 #elif defined VECTORS2
 	bool result = W[117].x & W[117].y;
 	if (!result) {
-		if (!W[117].x)
-			output[FOUND] = output[NFLAG & W[3].x] = W[3].x;
-		if (!W[117].y)
-			output[FOUND] = output[NFLAG & W[3].y] = W[3].y;
+		uint found;
+
+		if (!W[117].x) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = W[3].x;
+		}
+		if (!W[117].y) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = W[3].y;
+		}
 	}
 #else
-	if (!W[117])
-		output[FOUND] = output[NFLAG & W[3]] = W[3];
+	if (!W[117]) {
+		uint found = atomic_add(&output[FOUND], 1);
+		output[found] = W[3];
+	}
 #endif
 }
diff --git a/poclbm120724.cl b/poclbm120724.cl
index 3e8b994..a02413b 100644
--- a/poclbm120724.cl
+++ b/poclbm120724.cl
@@ -80,7 +80,7 @@ void search(const uint state0, const uint state1, const uint state2, const uint 
 	const uint D1A, const uint C1addK5, const uint B1addK6,
 	const uint W16addK16, const uint W17addK17,
 	const uint PreVal4addT1, const uint Preval0,
-	__global uint * output)
+	volatile __global uint * output)
 {
 	u Vals[24];
 	u *W = &Vals[8];
@@ -1311,43 +1311,46 @@ Vals[1]+=(rotr(W[9],17)^rotr(W[9],19)^(W[9]>>10U));
 Vals[1]+=K[59];
 Vals[1]+=Vals[5];
 
-#define FOUND (0x800)
-#define NFLAG (0x7FF)
+Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
+Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
+Vals[2]+=W[12];
+Vals[2]+=(rotr(W[13],7)^rotr(W[13],18)^(W[13]>>3U));
+Vals[2]+=W[5];
+Vals[2]+=(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U));
+Vals[2]+=Vals[0];
+Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+
+#define FOUND (0x0F)
 
 #if defined(VECTORS2) || defined(VECTORS4)
-	Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
-	Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
-	Vals[2]+=W[12];
-	Vals[2]+=(rotr(W[13],7)^rotr(W[13],18)^(W[13]>>3U));
-	Vals[2]+=W[5];
-	Vals[2]+=(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U));
-	Vals[2]+=Vals[0];
-	Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-	Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
 
 	if (any(Vals[2] == 0x136032edU)) {
-		if (Vals[2].x == 0x136032edU)
-			output[FOUND] = output[NFLAG & nonce.x] = nonce.x;
-		if (Vals[2].y == 0x136032edU)
-			output[FOUND] = output[NFLAG & nonce.y] = nonce.y;
+		uint found;
+
+		if (Vals[2].x == 0x136032edU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = nonce.x;
+		}
+		if (Vals[2].y == 0x136032edU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = nonce.y;
+		}
 #if defined(VECTORS4)
-		if (Vals[2].z == 0x136032edU)
-			output[FOUND] = output[NFLAG & nonce.z] = nonce.z;
-		if (Vals[2].w == 0x136032edU)
-			output[FOUND] = output[NFLAG & nonce.w] = nonce.w;
+		if (Vals[2].z == 0x136032edU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = nonce.z;
+		}
+		if (Vals[2].w == 0x136032edU) {
+			found = atomic_add(&output[FOUND], 1);
+			output[found] = nonce.w;
+		}
 #endif
 	}
 #else
-	if ((Vals[2]+
-		Ma(Vals[6],Vals[5],Vals[7])+
-		(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22))+
-		W[12]+
-		(rotr(W[13],7)^rotr(W[13],18)^(W[13]>>3U))+
-		W[5]+
-		(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U))+
-		Vals[0]+
-		(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25))+
-		ch(Vals[1],Vals[4],Vals[3])) == 0x136032edU)
-			output[FOUND] = output[NFLAG & nonce] =  nonce;
+	if (Vals[2] == 0x136032edU) {
+		uint found = atomic_add(&output[FOUND], 1);
+		output[found] = nonce;
+	}
 #endif
 }
diff --git a/scrypt120724.cl b/scrypt120724.cl
index d38f6a5..7390d2c 100644
--- a/scrypt120724.cl
+++ b/scrypt120724.cl
@@ -682,12 +682,11 @@ void scrypt_core(uint4 X[8], __global uint4*restrict lookup)
 	unshittify(X);
 }
 
-#define FOUND (0x800)
-#define NFLAG (0x7FF)
+#define FOUND (0x0F)
 
 __attribute__((reqd_work_group_size(WORKSIZE, 1, 1)))
 __kernel void search(__global const uint4 * restrict input,
-__global uint*restrict output, __global uint4*restrict padcache,
+volatile __global uint*restrict output, __global uint4*restrict padcache,
 const uint4 midstate0, const uint4 midstate16, const uint target)
 {
 	uint gid = get_global_id(0);
@@ -721,9 +720,11 @@ const uint4 midstate0, const uint4 midstate16, const uint target)
 	SHA256_fixed(&tmp0,&tmp1);
 	SHA256(&ostate0,&ostate1, tmp0, tmp1, (uint4)(0x80000000U, 0U, 0U, 0U), (uint4)(0U, 0U, 0U, 0x300U));
 
-	bool found = (EndianSwap(ostate1.w) <= target);
-	if (found)
-		output[FOUND] = output[NFLAG & gid] = gid;
+	bool result = (EndianSwap(ostate1.w) <= target);
+	if (result) {
+		uint found = atomic_add(&output[FOUND], 1);
+		output[found] = gid;
+	}
 }
 
 /*-