Files
TheRock/rocm-systems/emulation/rocjitsu/tests/hip_memcpy_test.cpp
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

91 lines
2.7 KiB
C++

// Copyright (c) 2026 Advanced Micro Devices, Inc.
// SPDX-License-Identifier: MIT
/// @file hip_memcpy_test.cpp
/// @brief Validates hipMemcpy H2D and D2H data correctness on simulated GPU.
///
/// Compiled with hipcc. Requires LD_PRELOAD=librocjitsu.so.
#include <hip/hip_runtime.h>
#include <vector>
#include <gtest/gtest.h>
#include <cstdlib>
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
int rc = RUN_ALL_TESTS();
(void)hipDeviceReset();
return rc;
}
#define HIP_ASSERT(call) \
do { \
hipError_t err = (call); \
ASSERT_EQ(err, hipSuccess) << "HIP error: " << hipGetErrorString(err); \
} while (0)
TEST(HipMemcpyTest, RoundTripFloat) {
constexpr int N = 16;
constexpr size_t bytes = N * sizeof(float);
std::vector<float> src(N), dst(N, 0.0f);
for (int i = 0; i < N; ++i)
src[i] = static_cast<float>(i + 1) * 1.5f;
float *d_buf = nullptr;
HIP_ASSERT(hipMalloc(&d_buf, bytes));
HIP_ASSERT(hipMemcpy(d_buf, src.data(), bytes, hipMemcpyHostToDevice));
HIP_ASSERT(hipMemcpy(dst.data(), d_buf, bytes, hipMemcpyDeviceToHost));
for (int i = 0; i < N; ++i)
EXPECT_FLOAT_EQ(dst[i], src[i]) << "mismatch at index " << i;
(void)hipFree(d_buf);
}
TEST(HipMemcpyTest, RoundTripInt) {
constexpr int N = 256;
constexpr size_t bytes = N * sizeof(int);
std::vector<int> src(N), dst(N, 0);
for (int i = 0; i < N; ++i)
src[i] = i * 42 + 7;
int *d_buf = nullptr;
HIP_ASSERT(hipMalloc(&d_buf, bytes));
HIP_ASSERT(hipMemcpy(d_buf, src.data(), bytes, hipMemcpyHostToDevice));
HIP_ASSERT(hipMemcpy(dst.data(), d_buf, bytes, hipMemcpyDeviceToHost));
for (int i = 0; i < N; ++i)
EXPECT_EQ(dst[i], src[i]) << "mismatch at index " << i;
(void)hipFree(d_buf);
}
TEST(HipMemcpyTest, DeviceToDevice) {
constexpr int N = 64;
constexpr size_t bytes = N * sizeof(float);
std::vector<float> src(N), dst(N, 0.0f);
for (int i = 0; i < N; ++i)
src[i] = static_cast<float>(i) * 3.14f;
float *d_src = nullptr, *d_dst = nullptr;
HIP_ASSERT(hipMalloc(&d_src, bytes));
HIP_ASSERT(hipMalloc(&d_dst, bytes));
HIP_ASSERT(hipMemcpy(d_src, src.data(), bytes, hipMemcpyHostToDevice));
HIP_ASSERT(hipMemcpy(d_dst, d_src, bytes, hipMemcpyDeviceToDevice));
HIP_ASSERT(hipMemcpy(dst.data(), d_dst, bytes, hipMemcpyDeviceToHost));
for (int i = 0; i < N; ++i)
EXPECT_FLOAT_EQ(dst[i], src[i]) << "mismatch at index " << i;
(void)hipFree(d_src);
(void)hipFree(d_dst);
}