Skip to content

Commit 44cbac9

Browse files
committed
[CUDA] add test for uses of std::array on device code
1 parent a45d162 commit 44cbac9

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

External/CUDA/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ macro(create_local_cuda_tests VariantSuffix)
6363
list(APPEND CUDA_LOCAL_TESTS assert)
6464
list(APPEND CUDA_LOCAL_TESTS axpy)
6565
list(APPEND CUDA_LOCAL_TESTS algorithm)
66+
list(APPEND CUDA_LOCAL_TESTS array)
6667
list(APPEND CUDA_LOCAL_TESTS cmath)
6768
list(APPEND CUDA_LOCAL_TESTS complex)
6869
list(APPEND CUDA_LOCAL_TESTS math_h)

External/CUDA/array.cu

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Check that we can use std::array on device code
2+
//
3+
// After libstdc++ 15, some internal asserts rely on function that are neither
4+
// constexpr nor device. This can trigger errors when using std::array members
5+
// on device code.
6+
//
7+
// This workaround is implemented in bits/c++config.h
8+
9+
#include <stdio.h>
10+
11+
#if __cplusplus >= 201103L
12+
13+
#include <array>
14+
#include <assert.h>
15+
16+
#if __cplusplus >= 201402L && STDLIB_VERSION >= 2014
17+
constexpr
18+
#endif
19+
__host__ __device__ size_t
20+
test_array() {
21+
std::array<int, 4> A = {0, 1, 2, 3};
22+
size_t N = A.size();
23+
assert(N == 4);
24+
25+
#if __cplusplus >= 201402L && STDLIB_VERSION >= 2014
26+
const int fst = A[0];
27+
assert(fst == 0);
28+
#endif
29+
30+
#if __cplusplus >= 201703L && STDLIB_VERSION >= 2017
31+
A[0] = 4;
32+
int snd = A[0];
33+
assert(snd == 4);
34+
#endif
35+
return N;
36+
}
37+
38+
__host__ __device__ void do_test() {
39+
// call the function in a constexpr and a non-constexpr context
40+
size_t M = test_array();
41+
(void)(M);
42+
#if __cplusplus >= 201402L && STDLIB_VERSION >= 2014
43+
constexpr size_t N = test_array();
44+
(void)(N);
45+
#endif
46+
}
47+
48+
__global__ void kernel() { do_test(); }
49+
50+
int main() {
51+
kernel<<<32, 32>>>();
52+
cudaError_t err = cudaDeviceSynchronize();
53+
if (err != cudaSuccess) {
54+
printf("CUDA error %d\n", (int)err);
55+
return 1;
56+
}
57+
58+
do_test();
59+
60+
printf("Success!\n");
61+
return 0;
62+
}
63+
64+
#else
65+
66+
int main() {
67+
printf("Success!\n");
68+
return 0;
69+
}
70+
71+
#endif

External/CUDA/array.reference_output

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Success!
2+
exit 0

0 commit comments

Comments
 (0)