Files
TheRock/rocm-systems/projects/hip-tests/catch/unit/rtc/shfl.cc
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

145 lines
3.5 KiB
C++

/*
* Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
*
* SPDX-License-Identifier: MIT
*/
#include <hip_test_common.hh>
#include <hip/hiprtc.h>
#include <hip/hip_runtime.h>
#include <hip/hip_fp16.h>
#include <cassert>
#include <cstddef>
#include <memory>
#include <iostream>
#include <iterator>
#include <vector>
#include "shfl.hh"
static constexpr auto shfl{
R"(
template <typename T>
__global__ void shflUpSum(T* a, int size) {
T val = a[threadIdx.x];
for (unsigned int i = size / 2; i > 0; i /= 2) {
val += __shfl_up(val, i, size);
}
a[threadIdx.x] = val;
}
template <typename T>
__global__ void shflDownSum(T* a, int size) {
T val = a[threadIdx.x];
for (int i = size / 2; i > 0; i /= 2) {
val += __shfl_down(val, i, size);
}
a[threadIdx.x] = val;
}
template <typename T>
__global__ void shflXorSum(T* a, int size) {
T val = a[threadIdx.x];
for (int i = size/2; i > 0; i /= 2)
val += __shfl_xor(val, i, size);
a[threadIdx.x] = val;
}
)"};
template <typename T> void runTestShfl(int option) {
using namespace std;
hiprtcProgram prog;
hiprtcCreateProgram(&prog, // prog
shfl, // buffer
"shfl.cu", // name
0, nullptr, nullptr);
string str;
switch (option) {
case 1:
str = "shflUpSum<__half>";
break;
case 2:
str = "shflDownSum<__half>";
break;
case 3:
str = "shflXorSum<__half>";
break;
default:
INFO("Options 1,2,3 are supported, but the passed option is: " << option);
REQUIRE(false);
}
hiprtcAddNameExpression(prog, str.c_str());
hiprtcResult compileResult{hiprtcCompileProgram(prog, 0, 0)};
size_t logSize;
HIPRTC_CHECK(hiprtcGetProgramLogSize(prog, &logSize));
if (logSize) {
string log(logSize, '\0');
HIPRTC_CHECK(hiprtcGetProgramLog(prog, &log[0]));
std::cout << log << '\n';
}
REQUIRE(compileResult == HIPRTC_SUCCESS);
size_t codeSize;
HIPRTC_CHECK(hiprtcGetCodeSize(prog, &codeSize));
vector<char> code(codeSize);
HIPRTC_CHECK(hiprtcGetCode(prog, code.data()));
// Do hip malloc first so that we donot need to do a cuInit manually before calling hipModule APIs
size_t bufferSize = n * sizeof(T);
T a[n];
T cpuSum = sum(a);
T* d_a;
HIP_CHECK(hipMalloc(&d_a, bufferSize));
hipModule_t module;
hipFunction_t kernel;
HIP_CHECK(hipModuleLoadData(&module, code.data()));
const char* name;
hiprtcGetLoweredName(prog, str.c_str(), &name);
HIP_CHECK(hipModuleGetFunction(&kernel, module, name));
HIP_CHECK(hipMemcpy(d_a, &a, bufferSize, hipMemcpyDefault));
struct {
T* a_;
int b_;
} args{d_a, n};
auto size = sizeof(args);
void* config[] = {HIP_LAUNCH_PARAM_BUFFER_POINTER, &args, HIP_LAUNCH_PARAM_BUFFER_SIZE, &size,
HIP_LAUNCH_PARAM_END};
HIP_CHECK(hipModuleLaunchKernel(kernel, 1, 1, 1, n, 1, 1, 0, nullptr, nullptr, config));
HIP_CHECK(hipMemcpy(&a, d_a, bufferSize, hipMemcpyDefault));
bool result;
switch (option) {
case 1: // shflUpSum
result = compare(a[n - 1], cpuSum);
break;
case 2: // shflDownSum
case 3: // shflXorSum
result = compare(a[0], cpuSum);
break;
}
if (result) {
HIP_CHECK(hipFree(d_a));
REQUIRE(false);
}
HIP_CHECK(hipFree(d_a));
HIP_CHECK(hipModuleUnload(module));
HIPRTC_CHECK(hiprtcDestroyProgram(&prog));
}
HIP_TEST_CASE(Unit_hiprtc_half_shuffle) {
runTestShfl<__half>(1);
runTestShfl<__half>(2);
runTestShfl<__half>(3);
}