-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPLOCBuilderKernels.cu
More file actions
executable file
·671 lines (505 loc) · 17.9 KB
/
Copy pathPLOCBuilderKernels.cu
File metadata and controls
executable file
·671 lines (505 loc) · 17.9 KB
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/**
* \file PLOCBuilderKernels.cu
* \author Daniel Meister
* \date 2017/01/23
* \brief PLOCBuilder kernels soruce file.
*/
#include "PLOCBuilderKernels.h"
#include "CudaBVHUtil.cuh"
using namespace FW;
extern "C" __global__ void computeSceneBox(
const int threads,
const int numberOfVertices
) {
// Thread index.
const int threadIndex = blockDim.x * blockIdx.x + threadIdx.x;
// Bounding box within the thread.
AABB box; Vec3f vertex;
for (int vertexIndex = threadIndex; vertexIndex < numberOfVertices; vertexIndex += threads) {
vertexFromTexture(vertexIndex, vertex);
box.grow(vertex);
}
// Cache.
__shared__ float cache[3 * PLOC_REDUCTION_BLOCK_THREADS];
Vec3f * bound = (Vec3f*)cache;
// Min.
bound[threadIdx.x] = box.min();
bound[threadIdx.x] = min(bound[threadIdx.x], bound[threadIdx.x ^ 1]);
bound[threadIdx.x] = min(bound[threadIdx.x], bound[threadIdx.x ^ 2]);
bound[threadIdx.x] = min(bound[threadIdx.x], bound[threadIdx.x ^ 4]);
bound[threadIdx.x] = min(bound[threadIdx.x], bound[threadIdx.x ^ 8]);
bound[threadIdx.x] = min(bound[threadIdx.x], bound[threadIdx.x ^ 16]);
__syncthreads();
if ((threadIdx.x & 32) == 0) bound[threadIdx.x] = min(bound[threadIdx.x], bound[threadIdx.x ^ 32]);
__syncthreads();
if ((threadIdx.x & 64) == 0) bound[threadIdx.x] = min(bound[threadIdx.x], bound[threadIdx.x ^ 64]);
__syncthreads();
if ((threadIdx.x & 128) == 0) bound[threadIdx.x] = min(bound[threadIdx.x], bound[threadIdx.x ^ 128]);
// Update global bounding box.
if (threadIdx.x == 0) {
atomicMin(&sceneBox[0], bound[threadIdx.x].x);
atomicMin(&sceneBox[1], bound[threadIdx.x].y);
atomicMin(&sceneBox[2], bound[threadIdx.x].z);
}
// Max.
bound[threadIdx.x] = box.max();
bound[threadIdx.x] = max(bound[threadIdx.x], bound[threadIdx.x ^ 1]);
bound[threadIdx.x] = max(bound[threadIdx.x], bound[threadIdx.x ^ 2]);
bound[threadIdx.x] = max(bound[threadIdx.x], bound[threadIdx.x ^ 4]);
bound[threadIdx.x] = max(bound[threadIdx.x], bound[threadIdx.x ^ 8]);
bound[threadIdx.x] = max(bound[threadIdx.x], bound[threadIdx.x ^ 16]);
__syncthreads();
if ((threadIdx.x & 32) == 0) bound[threadIdx.x] = max(bound[threadIdx.x], bound[threadIdx.x ^ 32]);
__syncthreads();
if ((threadIdx.x & 64) == 0) bound[threadIdx.x] = max(bound[threadIdx.x], bound[threadIdx.x ^ 64]);
__syncthreads();
if ((threadIdx.x & 128) == 0) bound[threadIdx.x] = max(bound[threadIdx.x], bound[threadIdx.x ^ 128]);
// Update global bounding box.
if (threadIdx.x == 0) {
atomicMax(&sceneBox[3], bound[threadIdx.x].x);
atomicMax(&sceneBox[4], bound[threadIdx.x].y);
atomicMax(&sceneBox[5], bound[threadIdx.x].z);
}
}
extern "C" __global__ void computeMortonCodes30(
const int threads,
const int numberOfTriangles,
Vec4f * nodeBoxesMin,
Vec4f * nodeBoxesMax,
int * triangleIndices,
unsigned int * mortonCodes
) {
// Thread index.
const int threadIndex = blockDim.x * blockIdx.x + threadIdx.x;
// Scene box.
AABB _sceneBox = *(AABB*)sceneBoxConst;
Vec3f scale = 1.0f / (_sceneBox.max() - _sceneBox.min());
for (int triangleIndex = threadIndex; triangleIndex < numberOfTriangles; triangleIndex += threads) {
// Triangle.
Vec3f v0, v1, v2;
verticesFromTexture(triangleIndex, v0, v1, v2);
// Box.
AABB box;
box.grow(v0);
box.grow(v1);
box.grow(v2);
// Node box
const int nodeIndex = triangleIndex + numberOfTriangles - 1;
nodeBoxesMin[nodeIndex] = Vec4f(box.min(), 0.0f);
nodeBoxesMax[nodeIndex] = Vec4f(box.max(), 0.0f);
// Triangle index, node index and Morton code.
triangleIndices[triangleIndex] = triangleIndex;
mortonCodes[triangleIndex] = mortonCode((box.midPoint() - _sceneBox.min()) * scale);
}
}
extern "C" __global__ void computeMortonCodes60(
const int threads,
const int numberOfTriangles,
Vec4f * nodeBoxesMin,
Vec4f * nodeBoxesMax,
int * triangleIndices,
unsigned long long * mortonCodes
) {
// Thread index.
const int threadIndex = blockDim.x * blockIdx.x + threadIdx.x;
// Scene box.
AABB _sceneBox = *(AABB*)sceneBoxConst;
Vec3f scale = 1.0f / (_sceneBox.max() - _sceneBox.min());
for (int triangleIndex = threadIndex; triangleIndex < numberOfTriangles; triangleIndex += threads) {
// Triangle.
Vec3f v0, v1, v2;
verticesFromTexture(triangleIndex, v0, v1, v2);
// Box.
AABB box;
box.grow(v0);
box.grow(v1);
box.grow(v2);
// Node box
const int nodeIndex = triangleIndex + numberOfTriangles - 1;
nodeBoxesMin[nodeIndex] = Vec4f(box.min(), 0.0f);
nodeBoxesMax[nodeIndex] = Vec4f(box.max(), 0.0f);
// Triangle index, node index and Morton code.
triangleIndices[triangleIndex] = triangleIndex;
mortonCodes[triangleIndex] = mortonCode64((box.midPoint() - _sceneBox.min()) * scale);
}
}
extern "C" __global__ void setupClusters(
const int threads,
const int numberOfTriangles,
int * nodeLeftIndices,
int * nodeRightIndices,
int * nodeSizes,
int * nodeIndices
) {
// Thread index.
const int threadIndex = blockDim.x * blockIdx.x + threadIdx.x;
for (int triangleIndex = threadIndex; triangleIndex < numberOfTriangles; triangleIndex += threads) {
// Node.
const int nodeIndex = triangleIndex + numberOfTriangles - 1;
nodeLeftIndices[nodeIndex] = triangleIndex;
nodeRightIndices[nodeIndex] = triangleIndex + 1;
nodeSizes[nodeIndex] = 1;
// Node index.
nodeIndices[triangleIndex] = nodeIndex;
}
}
extern "C" __global__ void generateNeighboursCached(
const int numberOfClusters,
const int radius,
float * neighbourDistances,
int * neighbourIndices,
Vec4f * nodeBoxesMin,
Vec4f * nodeBoxesMax,
int * nodeIndices
) {
// Shared memory cache.
__shared__ char cache[sizeof(AABB)* 2 * PLOC_GEN_BLOCK_THREADS];
AABB * boxes = ((AABB*)cache) + blockDim.x / 2;
// Thread index.
const int threadIndex = blockDim.x * blockIdx.x + threadIdx.x;
// Block offset.
const int blockOffset = blockDim.x * blockIdx.x;
// Load boxes.
for (int neighbourIndex = int(threadIdx.x) - radius; neighbourIndex < int(blockDim.x) + radius; neighbourIndex += blockDim.x) {
// Cluster index.
int clusterIndex = neighbourIndex + blockOffset;
// Valid threads.
if (clusterIndex >= 0 && clusterIndex < numberOfClusters) {
// Node index.
int nodeIndex = nodeIndices[clusterIndex];
// Cluster bounding box.
const Vec4f boxMin = nodeBoxesMin[nodeIndex];
const Vec4f boxMax = nodeBoxesMax[nodeIndex];
boxes[neighbourIndex] = AABB(Vec3f(boxMin.x, boxMin.y, boxMin.z), Vec3f(boxMax.x, boxMax.y, boxMax.z));
}
// Dummy large boxes.
else {
boxes[neighbourIndex] = AABB(Vec3f(-FW_F32_MAX), Vec3f(FW_F32_MAX));
}
}
// Sync.
__syncthreads();
// Nearest neighbour.
int minIndex = -1;
float minDistance = FW_F32_MAX;
// Cluster box.
AABB box = boxes[threadIdx.x];
// Search left.
for (int neighbourIndex = int(threadIdx.x) - radius; neighbourIndex < int(threadIdx.x); ++neighbourIndex) {
// Box.
AABB neighbourBox = boxes[neighbourIndex];
// Grow.
neighbourBox.grow(box);
// Surface area.
const float distance = neighbourBox.area();
// Update distance.
if (minDistance > distance) {
minIndex = blockOffset + neighbourIndex;
minDistance = distance;
} /*else if (minDistance == distance) {
minIndex = FW::min(minIndex, blockOffset + neighbourIndex);
}*/
}
// Search right.
for (int neighbourIndex = threadIdx.x + 1; neighbourIndex < threadIdx.x + radius + 1; ++neighbourIndex) {
// Box.
AABB neighbourBox = boxes[neighbourIndex];
// Grow.
neighbourBox.grow(box);
// Surface area.
const float distance = neighbourBox.area();
// Update distance.
if (minDistance > distance) {
minIndex = blockOffset + neighbourIndex;
minDistance = distance;
} /*else if (minDistance == distance) {
minIndex = FW::min(minIndex, blockOffset + neighbourIndex);
}*/
}
// Save proposal.
if (threadIndex < numberOfClusters) {
const int nodeIndex = nodeIndices[threadIndex];
neighbourDistances[nodeIndex] = minDistance;
neighbourIndices[nodeIndex] = minIndex;
}
}
extern "C" __global__ void generateNeighbours(
const int numberOfClusters,
const int radius,
float * neighbourDistances,
int * neighbourIndices,
Vec4f * nodeBoxesMin,
Vec4f * nodeBoxesMax,
int * nodeIndices
) {
// Thread index.
const int clusterIndex = blockDim.x * blockIdx.x + threadIdx.x;
if (clusterIndex < numberOfClusters) {
// Node index.
const int nodeIndex = nodeIndices[clusterIndex];
// Cluster bounding box.
const Vec4f boxMin = nodeBoxesMin[nodeIndex];
const Vec4f boxMax = nodeBoxesMax[nodeIndex];
AABB box = AABB(Vec3f(boxMin.x, boxMin.y, boxMin.z), Vec3f(boxMax.x, boxMax.y, boxMax.z));
// Nearest neighbour.
int minIndex = -1;
float minDistance = FW_F32_MAX;
// Search left.
for (int neighbourIndex = FW::max(0, clusterIndex - radius); neighbourIndex < clusterIndex; ++neighbourIndex) {
// Neighbour node index.
const int neighbourNodeIndex = nodeIndices[neighbourIndex];
// Box.
Vec4f neighbourBoxMin = nodeBoxesMin[neighbourNodeIndex];
Vec4f neighbourBoxMax = nodeBoxesMax[neighbourNodeIndex];
AABB neighbourBox = AABB(Vec3f(neighbourBoxMin.x, neighbourBoxMin.y, neighbourBoxMin.z),
Vec3f(neighbourBoxMax.x, neighbourBoxMax.y, neighbourBoxMax.z));
neighbourBox.grow(box);
// Surface area.
const float distance = neighbourBox.area();
// Update distance.
if (minDistance > distance) {
minDistance = distance;
minIndex = neighbourIndex;
}
}
// Search right.
for (int neighbourIndex = clusterIndex + 1; neighbourIndex < FW::min(numberOfClusters, clusterIndex + radius + 1); ++neighbourIndex) {
// Neighbour node index.
const int neighbourNodeIndex = nodeIndices[neighbourIndex];
// Box.
Vec4f neighbourBoxMin = nodeBoxesMin[neighbourNodeIndex];
Vec4f neighbourBoxMax = nodeBoxesMax[neighbourNodeIndex];
AABB neighbourBox = AABB(Vec3f(neighbourBoxMin.x, neighbourBoxMin.y, neighbourBoxMin.z),
Vec3f(neighbourBoxMax.x, neighbourBoxMax.y, neighbourBoxMax.z));
neighbourBox.grow(box);
// Surface area.
const float distance = neighbourBox.area();
// Update distance.
if (minDistance > distance) {
minDistance = distance;
minIndex = neighbourIndex;
}
}
// Save proposal.
neighbourDistances[nodeIndex] = minDistance;
neighbourIndices[nodeIndex] = minIndex;
}
}
extern "C" __global__ void merge(
const int numberOfClusters,
const int nodeOffset,
int * neighbourIndices,
int * nodeIndices0,
int * nodeIndices1,
int * nodeParentIndices,
int * nodeLeftIndices,
int * nodeRightIndices,
int * nodeSizes,
Vec4f * nodeBoxesMin,
Vec4f * nodeBoxesMax
) {
// Thread index.
const int clusterIndex = blockDim.x * blockIdx.x + threadIdx.x;
// Warp thread index.
const int warpThreadIndex = threadIdx.x & (WARP_THREADS - 1);
if (clusterIndex < numberOfClusters) {
// Merging flag.
bool merging = false;
// Neighbour indices.
const int leftNodeIndex = nodeIndices0[clusterIndex];
const int neighbourIndex = neighbourIndices[leftNodeIndex];
const int rightNodeIndex = nodeIndices0[neighbourIndex];
const int neighbourNeighbourIndex = neighbourIndices[rightNodeIndex];
// Merge only mutually paired clusters.
if (clusterIndex == neighbourNeighbourIndex) {
if (clusterIndex < neighbourIndex) merging = true;
}
// Just copy the node index.
else {
nodeIndices1[clusterIndex] = leftNodeIndex;
}
// Prefix scan.
const unsigned int warpBallot = __ballot(merging);
const int warpCount = __popc(warpBallot);
const int warpIndex = __popc(warpBallot & ((1u << warpThreadIndex) - 1));
// Add count of components to the global counter.
int warpOffset;
if (warpThreadIndex == 0)
warpOffset = atomicAdd(&prefixScanOffset, warpCount);
// Exchange offset between threads.
warpOffset = __shfl(warpOffset, 0);
// Node index.
const int nodeIndex = nodeOffset - warpOffset - warpIndex;
// Merge.
if (merging) {
// Box min.
Vec4f leftNodeBoxMin = nodeBoxesMin[leftNodeIndex];
Vec4f rightNodeBoxMin = nodeBoxesMin[rightNodeIndex];
leftNodeBoxMin.x = fminf(leftNodeBoxMin.x, rightNodeBoxMin.x);
leftNodeBoxMin.y = fminf(leftNodeBoxMin.y, rightNodeBoxMin.y);
leftNodeBoxMin.z = fminf(leftNodeBoxMin.z, rightNodeBoxMin.z);
nodeBoxesMin[nodeIndex] = leftNodeBoxMin;
// Box max.
Vec4f leftNodeBoxMax = nodeBoxesMax[leftNodeIndex];
Vec4f rightNodeBoxMax = nodeBoxesMax[rightNodeIndex];
leftNodeBoxMax.x = fmaxf(leftNodeBoxMax.x, rightNodeBoxMax.x);
leftNodeBoxMax.y = fmaxf(leftNodeBoxMax.y, rightNodeBoxMax.y);
leftNodeBoxMax.z = fmaxf(leftNodeBoxMax.z, rightNodeBoxMax.z);
nodeBoxesMax[nodeIndex] = leftNodeBoxMax;
// Children.
const int nodeSize = nodeSizes[leftNodeIndex] + nodeSizes[rightNodeIndex];
// Parent indices.
nodeParentIndices[leftNodeIndex] = nodeIndex;
nodeParentIndices[rightNodeIndex] = nodeIndex;
// Node.
nodeSizes[nodeIndex] = nodeSize;
nodeLeftIndices[nodeIndex] = leftNodeIndex;
nodeRightIndices[nodeIndex] = rightNodeIndex;
// Update node index.
nodeIndices1[clusterIndex] = nodeIndex;
nodeIndices1[neighbourIndex] = -1;
}
}
}
extern "C" __global__ void localPrefixScan(
const int numberOfClusters,
int * nodeIndices,
int * threadOffsets,
int * blockOffsets
) {
// Thread index.
const int threadIndex = blockDim.x * blockIdx.x + threadIdx.x;
// Cache.
__shared__ volatile int blockCache[2 * PLOC_SCAN_BLOCK_THREADS];
// Read value.
int threadValue = 0;
if (threadIndex < numberOfClusters)
threadValue = nodeIndices[threadIndex] >= 0;
// Block scan.
int blockSum = threadValue;
blockScan<PLOC_SCAN_BLOCK_THREADS>(blockSum, blockCache);
blockSum -= threadValue;
// Write value.
if (threadIndex < numberOfClusters)
threadOffsets[threadIndex] = blockSum;
// Write block value.
if (threadIdx.x == 0)
blockOffsets[blockIdx.x] = blockCache[2 * PLOC_SCAN_BLOCK_THREADS - 1];
}
extern "C" __global__ void globalPrefixScan(
const int numberOfBlocks,
int * blockOffsets
) {
// Block end.
const int blockEnd = divCeil(numberOfBlocks, PLOC_SCAN_BLOCK_THREADS) * PLOC_SCAN_BLOCK_THREADS;
// Cache.
__shared__ volatile int blockCache[2 * PLOC_SCAN_BLOCK_THREADS];
if (blockIdx.x == 0) {
// Block offset.
int blockOffset = 0;
for (int blockIndex = threadIdx.x; blockIndex < blockEnd; blockIndex += PLOC_SCAN_BLOCK_THREADS) {
// Read value.
int blockValue = 0;
if (blockIndex < numberOfBlocks)
blockValue = blockOffsets[blockIndex];
// Block scan.
int blockSum = blockValue;
blockScan<PLOC_SCAN_BLOCK_THREADS>(blockSum, blockCache);
blockSum -= blockValue;
// Write value.
if (blockIndex < numberOfBlocks)
blockOffsets[blockIndex] = blockSum + blockOffset;
// Update block offset.
blockOffset += blockCache[2 * PLOC_SCAN_BLOCK_THREADS - 1];
}
}
}
extern "C" __global__ void compact(
const int numberOfClusters,
int * nodeIndices0,
int * nodeIndices1,
int * blockOffsets,
int * threadOffsets
) {
// Thread index.
const int threadIndex = blockDim.x * blockIdx.x + threadIdx.x;
// Only valid clusters.
if (threadIndex < numberOfClusters) {
// Compact.
const int nodeIndex = nodeIndices0[threadIndex];
const int newClusterIndex = blockOffsets[blockIdx.x] + threadOffsets[threadIndex];
if (nodeIndex >= 0)
nodeIndices1[newClusterIndex] = nodeIndex;
}
}
extern "C" __global__ void woopifyTriangles(
const int threads,
const int numberOfTriangles,
int * triangleIndices,
Vec4f * triWoopsA,
Vec4f * triWoopsB,
Vec4f * triWoopsC
) {
// Thread index.
const int threadIndex = blockDim.x * blockIdx.x + threadIdx.x;
// Woop's matrix.
Mat4f im;
for (int triangleIndex = threadIndex; triangleIndex < numberOfTriangles; triangleIndex += threads) {
// Triangle.
Vec3f v0, v1, v2;
verticesFromTexture(triangleIndices[triangleIndex], v0, v1, v2);
// Woopify triangle.
im.setCol(0, Vec4f(v0 - v2, 0.0f));
im.setCol(1, Vec4f(v1 - v2, 0.0f));
im.setCol(2, Vec4f(cross(v0 - v2, v1 - v2), 0.0f));
im.setCol(3, Vec4f(v2, 1.0f));
im = invert(im);
triWoopsA[triangleIndex] = Vec4f(im(2, 0), im(2, 1), im(2, 2), -im(2, 3));
triWoopsB[triangleIndex] = im.getRow(0);
triWoopsC[triangleIndex] = im.getRow(1);
}
}
extern "C" __global__ void computeCost(
const int threads,
const int numberOfNodes,
const float sceneBoxArea,
const float ct,
const float ci,
CudaBVHNode * nodes
) {
// Thread index.
const int threadIndex = blockDim.x * blockIdx.x + threadIdx.x;
// Cost.
float _cost = 0.0f;
for (int nodeIndex = threadIndex; nodeIndex < numberOfNodes; nodeIndex += threads) {
CudaBVHNode node = nodes[nodeIndex];
float P = node.getSurfaceArea() / sceneBoxArea;
// Leaf.
if (node.isLeaf()) {
_cost += ci * P * node.getSize();
}
// Interior node.
else {
_cost += ct * P;
}
}
// Cache.
__shared__ volatile float cache[PLOC_REDUCTION_BLOCK_THREADS];
// Cost reduction.
cache[threadIdx.x] = _cost;
cache[threadIdx.x] += cache[threadIdx.x ^ 1];
cache[threadIdx.x] += cache[threadIdx.x ^ 2];
cache[threadIdx.x] += cache[threadIdx.x ^ 4];
cache[threadIdx.x] += cache[threadIdx.x ^ 8];
cache[threadIdx.x] += cache[threadIdx.x ^ 16];
__syncthreads();
if ((threadIdx.x & 32) == 0) cache[threadIdx.x] += cache[threadIdx.x ^ 32];
__syncthreads();
if ((threadIdx.x & 64) == 0) cache[threadIdx.x] += cache[threadIdx.x ^ 64];
__syncthreads();
if ((threadIdx.x & 128) == 0) cache[threadIdx.x] += cache[threadIdx.x ^ 128];
// Update total cost.
if (threadIdx.x == 0) {
atomicAdd(&cost, cache[threadIdx.x]);
}
}