Files
TheRock/rocm-systems/projects/hip-tests/catch/unit/deviceLib/memcpy_async.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

172 lines
5.9 KiB
C++

/*
Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <hip_test_common.hh>
#include <hip/hip_cooperative_groups.h>
#include <hip/cooperative_groups/memcpy_async.h>
// trivial vector add using shared memory
// a[i] * x + b[i]
__global__ void vector_add_mem(float* out, float* a, float* b, float x, size_t size) {
extern __shared__ float b_smem[];
auto tg = cooperative_groups::this_thread_block();
// Async copy memory
cooperative_groups::memcpy_async(tg, b_smem, b, size * sizeof(float));
size_t i = threadIdx.x;
// While copy is being done, we a[i] * x
float tmp = a[i] * x;
tg.sync(); // make sure copy is finished
// do `+ b[i]` from shared mem
tmp += b_smem[i];
// Write back to shared mem, we can simplify this (do this in one go) but this is a test
b_smem[i] = tmp;
// Copy back data to global memory
cooperative_groups::memcpy_async(tg, out, b_smem, size * sizeof(float));
// Wait till its done
tg.sync();
}
__global__ void vector_add_mem_layout(float* out, float* a, float* b, float x, size_t size) {
extern __shared__ float b_smem[];
auto tg = cooperative_groups::this_thread_block();
// Async copy memory
cooperative_groups::memcpy_async(tg, b_smem, size, b, size);
size_t i = threadIdx.x;
// While copy is being done, we a[i] * x
float tmp = a[i] * x;
tg.sync(); // make sure copy is finished
// do `+ b[i]` from shared mem
tmp += b_smem[i];
// Write back to shared mem, we can simplify this (do this in one go) but this is a test
b_smem[i] = tmp;
// Copy back data to global memory
cooperative_groups::memcpy_async(tg, out, size, b_smem, size);
// Wait till its done
tg.sync();
}
TEST_CASE("Unit_device_memcpy_async") {
std::vector<std::string> supported_arch{"gfx1250"};
hipDeviceProp_t prop;
HIP_CHECK(hipGetDeviceProperties(&prop, 0));
const std::string arch_name{prop.gcnArchName};
std::cout << "cluster lauch: " << prop.clusterLaunch << std::endl;
const bool should_run = std::any_of(
supported_arch.begin(), supported_arch.end(),
[&arch_name](const std::string_view& in) { return in.find(arch_name) != std::string::npos; });
if (should_run) {
SECTION("bytes copy") {
for (const size_t size : {32, 64, 128, 129 /* weird non aligned size */}) {
const size_t alloc_size = size * sizeof(float);
const float x = 2.0f;
float *d_out, *d_a, *d_b;
HIP_CHECK(hipMalloc(&d_out, alloc_size));
HIP_CHECK(hipMalloc(&d_a, alloc_size));
HIP_CHECK(hipMalloc(&d_b, alloc_size));
std::vector<float> a(size, 0.0f), b(size, 0.0f), cpu_out(size, 0.0f), gpu_out(size, 0.0f);
for (size_t i = 0; i < size; i++) {
a[i] = i + 1;
b[i] = (i + 1) * 2;
cpu_out[i] = (a[i] * x) + b[i];
}
HIP_CHECK(hipMemcpy(d_a, a.data(), alloc_size, hipMemcpyHostToDevice));
HIP_CHECK(hipMemcpy(d_b, b.data(), alloc_size, hipMemcpyHostToDevice));
HIP_CHECK(hipMemset(d_out, 0, alloc_size));
vector_add_mem<<<1, size, alloc_size, nullptr>>>(d_out, d_a, d_b, x, size);
HIP_CHECK(hipMemcpy(gpu_out.data(), d_out, alloc_size, hipMemcpyDeviceToHost));
HIP_CHECK(hipFree(d_out));
HIP_CHECK(hipFree(d_a));
HIP_CHECK(hipFree(d_b));
for (size_t i = 0; i < size; i++) {
INFO("size: " << size << " index: " << i << " calc: " << a[i] << " * " << x << " + "
<< b[i] << " = " << cpu_out[i] << ", " << gpu_out[i]);
REQUIRE(cpu_out[i] == gpu_out[i]);
}
}
}
SECTION("layout copy") {
for (const size_t size : {31, 65, 128, 256}) {
const size_t alloc_size = size * sizeof(float);
const float x = 2.0f;
float *d_out, *d_a, *d_b;
HIP_CHECK(hipMalloc(&d_out, alloc_size));
HIP_CHECK(hipMalloc(&d_a, alloc_size));
HIP_CHECK(hipMalloc(&d_b, alloc_size));
std::vector<float> a(size, 0.0f), b(size, 0.0f), cpu_out(size, 0.0f), gpu_out(size, 0.0f);
for (size_t i = 0; i < size; i++) {
a[i] = i + 1;
b[i] = (i + 1) * 2;
cpu_out[i] = (a[i] * x) + b[i];
}
HIP_CHECK(hipMemcpy(d_a, a.data(), alloc_size, hipMemcpyHostToDevice));
HIP_CHECK(hipMemcpy(d_b, b.data(), alloc_size, hipMemcpyHostToDevice));
HIP_CHECK(hipMemset(d_out, 0, alloc_size));
vector_add_mem_layout<<<1, size, alloc_size, nullptr>>>(d_out, d_a, d_b, x, size);
HIP_CHECK(hipMemcpy(gpu_out.data(), d_out, alloc_size, hipMemcpyDeviceToHost));
HIP_CHECK(hipFree(d_out));
HIP_CHECK(hipFree(d_a));
HIP_CHECK(hipFree(d_b));
for (size_t i = 0; i < size; i++) {
INFO("size: " << size << " index: " << i << " calc: " << a[i] << " * " << x << " + "
<< b[i] << " = " << cpu_out[i] << ", " << gpu_out[i]);
REQUIRE(cpu_out[i] == gpu_out[i]);
}
}
}
}
}