-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
179 lines (151 loc) · 4.96 KB
/
Copy pathmain.cpp
File metadata and controls
179 lines (151 loc) · 4.96 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
#ifdef __NVCC__
#include "cuda_global_linear_id.cuh"
#endif // __NVCC__
#include "index_left_fold.hpp"
#include <algorithm>
#include <vector>
#include <random>
#include <array>
#include <cassert>
#if __has_include (<mdspan>)
#include <mdspan>
#endif
// nvcc -x cu -std=c++17 main.cpp
// g++ -std=c++17 main.cpp
// clang++ -std=c++17 main.cpp
#ifdef __NVCC__
__global__ void incr(unsigned *p)
{
p[cuda_global_linear_id<3>()]++;
p[cuda_size_index<3>().index]++;
}
template <unsigned dims>
__global__ void incrT(unsigned *p)
{
p[cuda_global_linear_id<dims>()]++;
p[cuda_size_index<dims>().index]++;
}
template <unsigned dims>
__global__ void bid_store(unsigned *p)
{
const unsigned bid = index_left_fold(gridDim.z, blockIdx.z,
gridDim.y, blockIdx.y,
gridDim.x, blockIdx.x);
p[cuda_global_linear_id<dims>()] = bid;
}
template <typename T>
bool cuda_all_updated(std::array<T,6>& ex, T* d_x,
std::vector<T>& h_x, const unsigned nbytes)
{
cudaMemset(d_x, 0, nbytes);
incr<<<dim3{ex[0],ex[1],ex[2]},dim3{ex[3],ex[4],ex[5]}>>>(d_x); // 0 > 2
incrT<3><<<dim3{ex[0],ex[1],ex[2]},dim3{ex[3],ex[4],ex[5]}>>>(d_x); // 2 > 4
incrT<2><<<dim3{ex[0],ex[1]*ex[2]},dim3{ex[3],ex[4]*ex[5]}>>>(d_x); // 4 > 6
incrT<1><<<dim3{ex[0]*ex[1]*ex[2]},dim3{ex[3]*ex[4]*ex[5]}>>>(d_x); // 6 > 8
cudaMemcpy(h_x.data(), d_x, nbytes, cudaMemcpyDeviceToHost);
return h_x == std::vector<T>(h_x.size(), 8); // 8
}
// checks that the bid_store CUDA kernel writes distinct global block ids
// to contiguous sections of the device memory at d_x
template <typename T>
bool cuda_coalesced(std::array<T,6>& ex, T* d_x,
std::vector<T>& h_x, const unsigned nbytes)
{
unsigned nblocks = ex[0]*ex[1]*ex[2];
unsigned block_size = ex[3]*ex[4]*ex[5];
auto check_block_ids = [&]()
{
bool b = true;
cudaMemcpy(h_x.data(), d_x, nbytes, cudaMemcpyDeviceToHost);
std::vector<T> block_ids(nblocks);
for (unsigned block = 0; block < nblocks; ++block) {
block_ids[block] = h_x[block * block_size];
for (unsigned tid = 0; tid < block_size; ++tid) {
b = b && block_ids[block] == h_x[block * block_size + tid];
}
}
// block_ids will already be sorted as cuda_global_linear_id was used.
std::sort(block_ids.begin(), block_ids.end());
std::vector<T> correct_sorted_block_ids(nblocks);
std::iota(correct_sorted_block_ids.begin(),
correct_sorted_block_ids.end(), 0);
b = b && block_ids == correct_sorted_block_ids;
return b;
};
bool ret = true;
cudaMemset(d_x, 0, nbytes);
bid_store<3><<<dim3{ex[0],ex[1],ex[2]},dim3{ex[3],ex[4],ex[5]}>>>(d_x);
ret = ret && check_block_ids();
bid_store<2><<<dim3{ex[0],ex[1]*ex[2]},dim3{ex[3],ex[4]*ex[5]}>>>(d_x);
ret = ret && check_block_ids();
bid_store<1><<<dim3{ex[0]*ex[1]*ex[2]},dim3{ex[3]*ex[4]*ex[5]}>>>(d_x);
ret = ret && check_block_ids();
return ret;
}
template <typename T>
bool cuda_tests()
{
using std::array; using std::accumulate; using std::begin; using std::end;
using std::multiplies; using std::random_device; using std::mt19937;
using std::shuffle; using std::vector;
array<T,6> ex{2,3,4,5,6,7}; // arbitrary extents of a 6D array
const T sz = accumulate(begin(ex), end(ex), 1, multiplies<>{});
const T nbytes = sz * sizeof(T);
mt19937 g(random_device{}());
shuffle(ex.begin(), ex.end(), g);
vector<T> h_x(sz);
T *d_x;
cudaMalloc(&d_x, nbytes);
const bool b1 = cuda_all_updated(ex, d_x, h_x, nbytes);
const bool b2 = cuda_coalesced(ex, d_x, h_x, nbytes);
cudaFree(d_x);
return b1 && b2;
}
#endif // __NVCC__
template <typename T>
bool cpu_test_3d()
{
T arr3[2][4][6]{};
arr3[1][3][5] = 1;
T *p = reinterpret_cast<T *>(arr3);
T offset = index_left_fold(2,1, 4,3, 6,5);
static_assert(47==index_left_fold(2,1, 4,3, 6,5));
static_assert(ei{48,47}==size_index_left_fold(ei{2,1}, ei{4,3}, ei{6,5}));
p[offset]++;
#if __has_include (<mdspan>)
auto ms3 = std::experimental::mdspan<T,2,4,6>(p);
ms3(1,3,5)++;
return arr3[1][3][5]==3;
#else
return arr3[1][3][5]==2;
#endif
}
template <typename T>
bool cpu_test_4d()
{
T arr4[2][4][6][8]{};
arr4[1][3][5][7] = 1;
T *p = reinterpret_cast<T *>(arr4);
T offset = index_left_fold(2,1, 4,3, 6,5, 8,7);
static_assert(383==index_left_fold(2,1, 4,3, 6,5, 8,7));
static_assert(ei{384,383}==
size_index_left_fold(ei{2,1}, ei{4,3}, ei{6,5}, ei{8,7}));
p[offset]++;
#if __has_include (<mdspan>)
auto ms4 = std::experimental::mdspan<T,2,4,6,8>(p);
ms4(1,3,5,7)++;
return arr4[1][3][5][7]==3;
#else
return arr4[1][3][5][7]==2;
#endif
}
int main(int argc, char *argv[])
{
#ifdef __NVCC__
assert(cuda_tests<unsigned>());
#endif // __NVCC__
assert(cpu_test_3d<unsigned>() && cpu_test_4d<unsigned>());
static_assert(41==index_left_fold(3,0, 2,0, 1,0, 43,41),"");
static_assert(42==index_left_fold(3,0, 2,0, 1,0, 43,42),"");
return 0;
}