-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.cu
186 lines (151 loc) · 4.57 KB
/
Test.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
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
#include <stdio.h>
#include <assert.h>
#include <iostream>
#define CUDA_CHECK(f) do { \
cudaError_t e = f; \
if (e != cudaSuccess) { \
printf("Cuda failure %s:%d: '%s'\n", __FILE__, __LINE__, cudaGetErrorString(e)); \
exit(1); \
} \
} while (0)
namespace UVMConsistency {
#define UVMSPACE volatile
#define START 0
#define GPU_START_WRITE 1
#define GPU_START_READ 2
#define CPU_LOAD 3
#define GPU_FINISH_WRITE 4
#define GPU_FINISH_READ 5
#define FINISH 6
#define NUM_SHARED 100
typedef unsigned long long int ulli;
__device__ bool is_arr_full(UVMSPACE int *arr) {
int count = 0;
for (int i = 0; i < NUM_SHARED; i++) {
count += arr[i];
}
return count == NUM_SHARED;
}
__device__ bool check_consistency(UVMSPACE int *arr) {
// Read shared memory page - sequentially
for (int i = 0; i < NUM_SHARED - 1; i++) {
if (arr[i + 1] > arr[i]) { // arr[i] == 0 and arr[i + 1] == 1 ==> Inconsistency
return true;
}
}
return false;
}
__device__ void Reader_Thread(UVMSPACE int *arr, UVMSPACE int *finished) {
// Wait for CPU
while (*finished < GPU_START_READ);
while (!is_arr_full(arr)) {
// Check if an inconsistency exists in the array
if (check_consistency(arr)) {
printf("Found Inconsistency !\n");
return;
}
}
printf("No Inconsistency Found\n");
// GPU finished - CPU can finish
*finished = GPU_FINISH_READ;
// Wait for CPU to finish
while (*finished != FINISH);
}
__device__ void Writer_Thread(UVMSPACE int *arr, UVMSPACE int *finished) {
// Wait for CPU
while (*finished < GPU_START_WRITE);
// Loop and execute writes on shared memory page - sequentially
for (int i = 0; i < NUM_SHARED; i++) {
// For Consistency Check
arr[i] = 1;
// __threadfence_system();
}
// GPU finished - CPU can finish
*finished = GPU_FINISH_WRITE;
// Wait for CPU to finish
while (*finished != FINISH);
}
__global__ void GPU_UVM_Writer_Kernel(UVMSPACE int *arr, UVMSPACE int *finished) {
if (threadIdx.x == 0) {
Writer_Thread(arr, finished);
} else {
Reader_Thread(arr, finished);
}
__syncthreads();
}
class Consistency {
private: // Constructor & Destructor
Consistency() {
CUDA_CHECK(cudaMallocManaged(&arr, sizeof(int) * NUM_SHARED));
memset((void *) arr, 0, sizeof(int) * NUM_SHARED);
CUDA_CHECK(cudaMallocManaged(&finished, sizeof(int)));
memset((void *) finished, START, sizeof(int));
// Writing all the changes of UM to GPU
__sync_synchronize();
}
~Consistency() {
CUDA_CHECK(cudaFree((int *) arr));
CUDA_CHECK(cudaFree((int *) finished));
}
private: // Logic
bool is_arr_full() {
int count = 0;
for (int i = 0; i < NUM_SHARED; i++) {
count += arr[i];
}
return count == NUM_SHARED;
}
bool check_consistency(UVMSPACE int *arr) {
// Read shared memory page - sequentially
for (int i = 0; i < NUM_SHARED - 1; i++) {
// int v2 = arr[i + 1];
// int v1 = arr[i];
if (arr[i + 1] > arr[i]) { // arr[i] == 0 and arr[i + 1] == 1 ==> Inconsistency
return true;
}
}
return false;
}
void launch_task() {
// Start GPU task
GPU_UVM_Writer_Kernel<<<1,2>>>(arr, finished);
// GPU can start
*finished = GPU_START_WRITE;
*finished = GPU_START_READ;
}
// void check_consistency() {
// // While writes have not finished
// while (!is_arr_full()) {
// // Check if an inconsistency exists in the array
// if (check_consistency(arr)) {
// ::std::cout << "Found Inconsistency !" << ::std::endl;
// return;
// }
// }
// ::std::cout << "No Inconsistency Found" << ::std::endl;
// }
void finish_task() {
while (*finished < GPU_FINISH_READ);
// Task is over
*finished = FINISH;
CUDA_CHECK(cudaDeviceSynchronize());
}
public:
static void start() {
Consistency consistency;
// Start kernel
consistency.launch_task();
// Check GPU consistency
// consistency.check_consistency();
// Finish task for CPU and GPU
consistency.finish_task();
}
private:
UVMSPACE int *arr;
UVMSPACE int *finished;
};
} // UVMConsistency namespace
int main() {
UVMConsistency::Consistency::start();
return 0;
}