Skip to content

[CUDA][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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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)
list(APPEND CUDA_LOCAL_TESTS cmath)
list(APPEND CUDA_LOCAL_TESTS complex)
list(APPEND CUDA_LOCAL_TESTS math_h)
Expand Down
71 changes: 71 additions & 0 deletions External/CUDA/array.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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
constexpr
#endif
__host__ __device__ size_t
test_array() {
std::array<int, 4> A = {0, 1, 2, 3};
size_t N = A.size();
assert(N == 4);

#if __cplusplus >= 201402L && STDLIB_VERSION >= 2014
const 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_test() {
// call the function in a constexpr and a non-constexpr context
size_t M = test_array();
(void)(M);
#if __cplusplus >= 201402L && STDLIB_VERSION >= 2014
constexpr size_t N = test_array();
(void)(N);
#endif
}

__global__ void kernel() { do_test(); }

int main() {
kernel<<<32, 32>>>();
Copy link
Contributor

Choose a reason for hiding this comment

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

can std::array be parameter type of kernel or device functions?

can it be used as non-constexpr stack variable in kernel or device functions?

if so, can we have testcases for those?

cudaError_t err = cudaDeviceSynchronize();
if (err != cudaSuccess) {
printf("CUDA error %d\n", (int)err);
return 1;
}

do_test();

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

#else

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

#endif
2 changes: 2 additions & 0 deletions External/CUDA/array.reference_output
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Success!
exit 0
1 change: 1 addition & 0 deletions External/HIP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ macro(create_local_hip_tests VariantSuffix)
list(APPEND HIP_LOCAL_TESTS saxpy)
list(APPEND HIP_LOCAL_TESTS memmove)
list(APPEND HIP_LOCAL_TESTS split-kernel-args)
list(APPEND HIP_LOCAL_TESTS array)

# TODO: Re-enable InOneWeekend after it is fixed
#list(APPEND HIP_LOCAL_TESTS InOneWeekend)
Expand Down
2 changes: 2 additions & 0 deletions External/HIP/array.hip
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "cuda2hip.h"
#include "../CUDA/array.cu"
2 changes: 2 additions & 0 deletions External/HIP/array.reference_output
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Success!
exit 0
10 changes: 10 additions & 0 deletions External/HIP/cuda2hip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef CUDA_2_HIP
#define CUDA_2_HIP

#include "hip/hip_runtime.h"

#define cudaError_t hipError_t
#define cudaSuccess hipSuccess
#define cudaDeviceSynchronize hipDeviceSynchronize
Copy link
Contributor

Choose a reason for hiding this comment

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

I am wondering whether we have a systematic way to use CUDA tests for HIP. Using macro to map CUDA entries to HIP is not only tedious but may impose restrictions on how those CUDA tests are written.

I am wondering whether we could hipify the .cu file instead. Currently /opt/rocm/bin/hipify-perl is available on the HIP buildbot. For each .cu file to be hipified, you can use add_custom_command to hipify it to a .hip file then test it.

Choose a reason for hiding this comment

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

Can we try using hipify-clang instead? hipify-perl is going to be deprecated soon more than likely

@emankov

Copy link
Author

Choose a reason for hiding this comment

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

I've just built the upstream buildbot image from https://github.com/ROCm/aomp/tree/aomp-dev/upstream-buildbots/Ubu22 and there is no hipify in Rocm's installation (it's quite a minimal install).

I'll check if I install them if the tests work. I agree that it would be much better.

Copy link
Author

@jmmartinez jmmartinez May 15, 2025

Choose a reason for hiding this comment

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

I've tried with hipify-perl and it works.

hipify-clang did not, it yield errors for every test.

One common error case was with multiple main definitions (even if they are on different ifdef branches):

#include <stdio.h>
#if __cplusplus < 201103L
int main() { puts("foo\n"); return 0; }
#else
int main() { puts("bar\n"); return 0; }
#endif
/tmp/foo.cu-5f2589.hip:5:5: error: redefinition of 'main'
    5 | int main() { puts("bar\n"); return 0; }
      |     ^
/tmp/foo.cu-5f2589.hip:3:5: note: previous definition is here
    3 | int main() { puts("foo\n"); return 0; }

I'll adapt these tests to run with hipify and remove the hacky header.


#endif