-
Notifications
You must be signed in to change notification settings - Fork 344
/
privatization
37 lines (31 loc) · 1.24 KB
/
privatization
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
__global__ void histogram_update_naive(int *data, int *hist, int data_size, int hist_size) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < data_size) {
int data_value = data[idx];
if (data_value < hist_size) {
atomicAdd(&hist[data_value], 1); // Potential contention point
}
}
}
__global__ void histogram_update_privatized(int *data, int *hist, int data_size, int hist_size) {
extern __shared__ int private_hist[];
int idx = blockIdx.x * blockDim.x + threadIdx.x;
int thread_id = threadIdx.x;
// Initialize private histogram in shared memory
for (int i = thread_id; i < hist_size; i += blockDim.x) {
private_hist[i] = 0;
}
__syncthreads(); // Ensure all threads have initialized their private histograms
// Update private histogram
if (idx < data_size) {
int data_value = data[idx];
if (data_value < hist_size) {
atomicAdd(&private_hist[data_value], 1);
}
}
__syncthreads(); // Ensure all threads have finished updating the private histogram
// Reduce private histograms into global histogram
for (int i = thread_id; i < hist_size; i += blockDim.x) {
atomicAdd(&hist[i], private_hist[i]);
}
}