Skip to content

Commit 2731dd1

Browse files
committed
[CUDA][HIP] add test for uses of std::array on device code
1 parent 4abe1ec commit 2731dd1

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
// call the function in a constexpr and a non-constexpr context
18+
#define TEST(expr) \
19+
do { \
20+
size_t M = expr; \
21+
(void)(M); \
22+
constexpr size_t N = expr; \
23+
(void)(N); \
24+
} while (0)
25+
#define MAYBE_CONSTEXPR constexpr
26+
#else
27+
#define TEST(expr) \
28+
do { \
29+
size_t M = expr; \
30+
(void)(M); \
31+
} while (0)
32+
#define MAYBE_CONSTEXPR
33+
#endif
34+
35+
MAYBE_CONSTEXPR __host__ __device__ size_t test_array() {
36+
// Before C++17 only "operator[] const" is constexpr (thus available on
37+
// device).
38+
#if __cplusplus < 201703L && STDLIB_VERSION < 2017
39+
const
40+
#endif
41+
std::array<int, 4>
42+
A = {0, 1, 2, 3};
43+
44+
size_t N = A.size();
45+
assert(N == 4);
46+
47+
#if __cplusplus >= 201402L && STDLIB_VERSION >= 2014
48+
int fst = A[0];
49+
assert(fst == 0);
50+
#endif
51+
52+
#if __cplusplus >= 201703L && STDLIB_VERSION >= 2017
53+
A[0] = 4;
54+
int snd = A[0];
55+
assert(snd == 4);
56+
#endif
57+
return N;
58+
}
59+
60+
__host__ __device__ void do_all_tests() { TEST(test_array()); }
61+
62+
__global__ void kernel() { do_all_tests(); }
63+
64+
int main() {
65+
kernel<<<32, 32>>>();
66+
cudaError_t err = cudaDeviceSynchronize();
67+
if (err != cudaSuccess) {
68+
printf("CUDA error %d\n", (int)err);
69+
return 1;
70+
}
71+
72+
do_all_tests();
73+
74+
printf("Success!\n");
75+
return 0;
76+
}
77+
78+
#else
79+
80+
int main() {
81+
printf("Success!\n");
82+
return 0;
83+
}
84+
85+
#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

External/HIP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ macro(create_local_hip_tests VariantSuffix)
9797
endforeach()
9898

9999
list(APPEND CUDA_LOCAL_TESTS algorithm)
100+
list(APPEND CUDA_LOCAL_TESTS array)
100101
list(APPEND CUDA_LOCAL_TESTS cmath)
101102
list(APPEND CUDA_LOCAL_TESTS complex)
102103
list(APPEND CUDA_LOCAL_TESTS math_h)

0 commit comments

Comments
 (0)