Skip to content

[HIP] add test for uses of std::array on device code #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions External/CUDA/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ macro(create_local_cuda_tests VariantSuffix)
list(APPEND CUDA_LOCAL_TESTS assert)
list(APPEND CUDA_LOCAL_TESTS axpy)
list(APPEND CUDA_LOCAL_TESTS algorithm)
list(APPEND CUDA_LOCAL_TESTS array)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no matching array.cu file committed under CUDA which makes cmake configuration fail.

list(APPEND CUDA_LOCAL_TESTS cmath)
list(APPEND CUDA_LOCAL_TESTS complex)
list(APPEND CUDA_LOCAL_TESTS math_h)
Expand Down
1 change: 1 addition & 0 deletions External/HIP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ macro(create_local_hip_tests VariantSuffix)
#set_source_files_properties(split-kernel-args.hip PROPERTIES
# COMPILE_FLAGS "-mllvm -amdgpu-enable-split-kernel-args")
# Add HIP tests to be added to hip-tests-simple
list(APPEND HIP_LOCAL_TESTS array)
list(APPEND HIP_LOCAL_TESTS empty)
list(APPEND HIP_LOCAL_TESTS with-fopenmp)
list(APPEND HIP_LOCAL_TESTS saxpy)
Expand Down
86 changes: 86 additions & 0 deletions External/HIP/array.hip
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "hip/hip_runtime.h"
// Check that we can use std::array on device code
//
// After libstdc++ 15, some internal asserts rely on function that are neither
// constexpr nor device. This can trigger errors when using std::array members
// on device code.
//
// This workaround is implemented in bits/c++config.h

#include <stdio.h>

#if __cplusplus >= 201103L

#include <array>
#include <assert.h>

#if __cplusplus >= 201402L && STDLIB_VERSION >= 2014
// call the function in a constexpr and a non-constexpr context
#define TEST(expr) \
do { \
size_t M = expr; \
(void)(M); \
constexpr size_t N = expr; \
(void)(N); \
} while (0)
#define MAYBE_CONSTEXPR constexpr
#else
#define TEST(expr) \
do { \
size_t M = expr; \
(void)(M); \
} while (0)
#define MAYBE_CONSTEXPR
#endif

MAYBE_CONSTEXPR __host__ __device__ size_t test_array() {
// Before C++17 only "operator[] const" is constexpr (thus available on
// device).
#if __cplusplus < 201703L && STDLIB_VERSION < 2017
const
#endif
std::array<int, 4>
A = {0, 1, 2, 3};

size_t N = A.size();
assert(N == 4);

#if __cplusplus >= 201402L && STDLIB_VERSION >= 2014
int fst = A[0];
assert(fst == 0);
#endif

#if __cplusplus >= 201703L && STDLIB_VERSION >= 2017
A[0] = 4;
int snd = A[0];
assert(snd == 4);
#endif
return N;
}

__host__ __device__ void do_all_tests() { TEST(test_array()); }

__global__ void kernel() { do_all_tests(); }

int main() {
kernel<<<32, 32>>>();
hipError_t err = hipDeviceSynchronize();
if (err != hipSuccess) {
printf("CUDA error %d\n", (int)err);
return 1;
}

do_all_tests();

printf("Success!\n");
return 0;
}

#else

int main() {
printf("Success!\n");
return 0;
}

#endif