-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdiverge2.cu
159 lines (110 loc) · 3.2 KB
/
diverge2.cu
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "repeat.h"
/* THIS KERNEL HANGS */
__global__ void kdiverge2_test1 (unsigned int *ts, unsigned int* out, int p1, int p2, int its)
{
int t1 = p1;
int t2 = p2;
unsigned int tid = blockIdx.x*blockDim.x + threadIdx.x;
__shared__ volatile unsigned int sharedvar;
sharedvar = 0;
__syncthreads();
while (sharedvar != tid);
sharedvar++;
out[0] = (t1 + t2);
}
/* THIS KERNEL HANGS */
__global__ void kdiverge2_test2 (unsigned int *ts, unsigned int* out, int p1, int p2, int its)
{
int t1 = p1;
int t2 = p2;
unsigned int tid = blockIdx.x*blockDim.x + threadIdx.x;
__shared__ volatile unsigned int sharedvar;
sharedvar = 0;
__syncthreads();
label0: if (sharedvar != tid) goto label0;
else sharedvar++;
out[0] = (t1 + t2);
}
__global__ void kdiverge2_test3 (unsigned int *ts, unsigned int* out, int p1, int p2, int its)
{
int t1 = p1;
int t2 = p2;
unsigned int tid = blockIdx.x*blockDim.x + threadIdx.x;
__shared__ volatile unsigned int sharedvar;
sharedvar = 0;
__syncthreads();
label0: if (sharedvar != tid)
{
if (t1 > 0) goto label0;
}
else
sharedvar++;
out[0] = (t1 + t2);
}
/* THIS KERNEL HANGS */
__global__ void kdiverge2_test4 (unsigned int *ts, unsigned int* out, int p1, int p2, int its)
{
int t1 = p1;
int t2 = p2;
unsigned int tid = blockIdx.x*blockDim.x + threadIdx.x;
__shared__ volatile unsigned int sharedvar;
sharedvar = 0;
__syncthreads();
while (sharedvar != tid);
atomicAdd((unsigned int *)&sharedvar, 1);
out[0] = (t1 + t2);
}
/* THIS KERNEL HANGS */
__global__ void kdiverge2_test5 (unsigned int *ts, unsigned int* out, int p1, int p2, int its)
{
int t1 = p1;
int t2 = p2;
unsigned int tid = blockIdx.x*blockDim.x + threadIdx.x;
__shared__ unsigned int sharedvar;
sharedvar = 0;
__syncthreads();
while (sharedvar != tid);
atomicAdd((unsigned int *)&sharedvar, 1);
out[0] = (t1 + t2);
}
void measure_diverge2()
{
unsigned int ts[1024]; // ts, output from kernel. Two elements used per thread.
unsigned int *d_ts;
unsigned int *d_out; // Unused memory for storing output
dim3 Db = dim3(32 * 2);
dim3 Dg = dim3(1,1,1);
// Allocate device array.
cudaError_t errorcode;
if (cudaSuccess != (errorcode = cudaMalloc((void**)&d_ts, sizeof(ts))))
{
printf ("cudaMalloc failed %s:%d\n", __FILE__, __LINE__);
printf (" %s\n", cudaGetErrorString(errorcode));
return;
}
if (cudaSuccess != (errorcode = cudaMalloc((void**)&d_out, 4)))
{
printf ("cudaMalloc failed %s:%d\n", __FILE__, __LINE__);
return;
}
errorcode = cudaGetLastError();
printf ("Running divergence tests ...\n");
Db.x = 32;
// Not runnning kdiverge2_test1, 2, 4, and 5 because they hang the system.
printf("kdiverge2_test3: ");
kdiverge2_test3 <<<Dg, Db>>>(d_ts, d_out, 4, 6, 100);
cudaThreadSynchronize();
if (cudaSuccess != (errorcode = cudaGetLastError()))
printf("failed. %s\n", cudaGetErrorString(errorcode));
cudaMemcpy(ts, d_ts, sizeof(ts), cudaMemcpyDeviceToHost);
if (cudaSuccess != (errorcode = cudaGetLastError()))
printf("failed. %s\n", cudaGetErrorString(errorcode));
printf(" PASS.\n");
printf("\n");
cudaFree(d_ts);
cudaFree(d_out);
}