Skip to content

Commit 631eac0

Browse files
committed
[HIP] add test for uses of std::array on device code
1 parent b6566dd commit 631eac0

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-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/HIP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ macro(create_local_hip_tests VariantSuffix)
6565
#set_source_files_properties(split-kernel-args.hip PROPERTIES
6666
# COMPILE_FLAGS "-mllvm -amdgpu-enable-split-kernel-args")
6767
# Add HIP tests to be added to hip-tests-simple
68+
list(APPEND HIP_LOCAL_TESTS array)
6869
list(APPEND HIP_LOCAL_TESTS empty)
6970
list(APPEND HIP_LOCAL_TESTS with-fopenmp)
7071
list(APPEND HIP_LOCAL_TESTS saxpy)

External/HIP/array.hip

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

0 commit comments

Comments
 (0)