2023-08-14 21:17:55 +05:30
|
|
|
|
/*
|
|
|
|
|
|
Copyright (c) 2023 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.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-08-15 16:09:19 -04:00
|
|
|
|
* @addtogroup hipMemcpy hipMemcpy
|
|
|
|
|
|
* @{
|
|
|
|
|
|
* @ingroup perfMemoryTest
|
|
|
|
|
|
* `hipMemcpy(void* dst, const void* src, size_t count, hipMemcpyKind kind)` -
|
|
|
|
|
|
* Copies data between host and device.
|
|
|
|
|
|
*/
|
2023-08-14 21:17:55 +05:30
|
|
|
|
|
|
|
|
|
|
#include <hip_test_common.hh>
|
2025-08-15 16:09:19 -04:00
|
|
|
|
// #define ENABLE_DEBUG 1
|
2023-10-26 19:48:58 +00:00
|
|
|
|
#define NUM_SIZE 14
|
|
|
|
|
|
#define NUM_ITER 1000
|
|
|
|
|
|
// max BW number for DevicetoDeviceNoCU
|
|
|
|
|
|
#define NOCU_MAX_BW 128
|
2023-08-14 21:17:55 +05:30
|
|
|
|
|
|
|
|
|
|
class hipPerfMemcpy {
|
|
|
|
|
|
private:
|
|
|
|
|
|
size_t totalSizes_[NUM_SIZE];
|
2025-08-15 16:09:19 -04:00
|
|
|
|
void setHostBuffer(int* A, int val, size_t size);
|
|
|
|
|
|
|
2023-08-14 21:17:55 +05:30
|
|
|
|
public:
|
|
|
|
|
|
hipPerfMemcpy();
|
|
|
|
|
|
~hipPerfMemcpy() {}
|
2023-10-26 19:48:58 +00:00
|
|
|
|
void TestResult(unsigned int numTests, std::chrono::duration<double, std::micro> diff,
|
|
|
|
|
|
hipMemcpyKind type);
|
|
|
|
|
|
bool run_h2d(unsigned int numTests);
|
|
|
|
|
|
bool run_d2h(unsigned int numTests);
|
|
|
|
|
|
bool run_d2d(unsigned int numTests);
|
|
|
|
|
|
bool run_d2d_nocu(unsigned int numTests);
|
2023-08-14 21:17:55 +05:30
|
|
|
|
};
|
|
|
|
|
|
|
2023-10-26 19:48:58 +00:00
|
|
|
|
hipPerfMemcpy::hipPerfMemcpy() {
|
2023-08-14 21:17:55 +05:30
|
|
|
|
for (int i = 0; i < NUM_SIZE; i++) {
|
2023-10-26 19:48:58 +00:00
|
|
|
|
totalSizes_[i] = 1 << (i + 9);
|
2023-08-14 21:17:55 +05:30
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-15 16:09:19 -04:00
|
|
|
|
void hipPerfMemcpy::setHostBuffer(int* A, int val, size_t size) {
|
2023-08-14 21:17:55 +05:30
|
|
|
|
size_t len = size / sizeof(int);
|
|
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
|
|
A[i] = val;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 19:48:58 +00:00
|
|
|
|
void hipPerfMemcpy::TestResult(unsigned int numTests,
|
2025-08-15 16:09:19 -04:00
|
|
|
|
std::chrono::duration<double, std::micro> diff, hipMemcpyKind type) {
|
2023-10-26 19:48:58 +00:00
|
|
|
|
// BW in GB/s
|
2025-08-15 16:09:19 -04:00
|
|
|
|
double perf =
|
|
|
|
|
|
(static_cast<double>(totalSizes_[numTests] * NUM_ITER) * static_cast<double>(1e-03)) /
|
|
|
|
|
|
diff.count();
|
|
|
|
|
|
|
|
|
|
|
|
const char* typestr = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
if (type == hipMemcpyHostToDevice) {
|
|
|
|
|
|
typestr = "Host to Device";
|
|
|
|
|
|
} else if (type == hipMemcpyDeviceToHost) {
|
|
|
|
|
|
typestr = "Device to Host";
|
|
|
|
|
|
} else if (type == hipMemcpyDeviceToDevice) {
|
|
|
|
|
|
typestr = "Device to Device";
|
|
|
|
|
|
perf *= 2.0;
|
|
|
|
|
|
} else if (type == hipMemcpyDeviceToDeviceNoCU) {
|
|
|
|
|
|
typestr = "Device to Device No CU";
|
|
|
|
|
|
perf *= 2.0;
|
2023-10-26 19:48:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-23 21:56:15 -04:00
|
|
|
|
CONSOLE_PRINT("hipPerfMemcpy[%d] %s copy BW %.2f GB/s for memory size of %zu Bytes.\n", numTests,
|
2025-08-15 16:09:19 -04:00
|
|
|
|
typestr, perf, totalSizes_[numTests]);
|
2023-10-26 19:48:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool hipPerfMemcpy::run_h2d(unsigned int numTests) {
|
2023-08-14 21:17:55 +05:30
|
|
|
|
int *A, *Ad;
|
|
|
|
|
|
A = new int[totalSizes_[numTests]];
|
2023-10-26 19:48:58 +00:00
|
|
|
|
HIP_CHECK(hipHostRegister(A, totalSizes_[numTests], hipHostRegisterDefault));
|
2023-08-14 21:17:55 +05:30
|
|
|
|
setHostBuffer(A, 1, totalSizes_[numTests]);
|
|
|
|
|
|
HIP_CHECK(hipMalloc(&Ad, totalSizes_[numTests]));
|
|
|
|
|
|
|
|
|
|
|
|
// measure performance based on host time
|
|
|
|
|
|
auto all_start = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < NUM_ITER; j++) {
|
2023-10-26 19:48:58 +00:00
|
|
|
|
HIP_CHECK(hipMemcpyAsync(Ad, A, totalSizes_[numTests], hipMemcpyHostToDevice, nullptr));
|
2023-08-14 21:17:55 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HIP_CHECK(hipDeviceSynchronize());
|
|
|
|
|
|
|
|
|
|
|
|
auto all_end = std::chrono::steady_clock::now();
|
|
|
|
|
|
std::chrono::duration<double, std::micro> diff = all_end - all_start;
|
|
|
|
|
|
|
2023-10-26 19:48:58 +00:00
|
|
|
|
TestResult(numTests, diff, hipMemcpyHostToDevice);
|
2023-08-14 21:17:55 +05:30
|
|
|
|
|
2023-10-26 19:48:58 +00:00
|
|
|
|
HIP_CHECK(hipHostUnregister(A));
|
2025-08-15 16:09:19 -04:00
|
|
|
|
delete[] A;
|
2023-08-14 21:17:55 +05:30
|
|
|
|
HIP_CHECK(hipFree(Ad));
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 19:48:58 +00:00
|
|
|
|
bool hipPerfMemcpy::run_d2h(unsigned int numTests) {
|
|
|
|
|
|
int *A, *Ad;
|
|
|
|
|
|
A = new int[totalSizes_[numTests]];
|
|
|
|
|
|
HIP_CHECK(hipHostRegister(A, totalSizes_[numTests], hipHostRegisterDefault));
|
|
|
|
|
|
HIP_CHECK(hipMalloc(&Ad, totalSizes_[numTests]));
|
|
|
|
|
|
HIP_CHECK(hipMemset(Ad, 0x1, totalSizes_[numTests]));
|
|
|
|
|
|
|
|
|
|
|
|
// measure performance based on host time
|
|
|
|
|
|
auto all_start = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < NUM_ITER; j++) {
|
|
|
|
|
|
HIP_CHECK(hipMemcpyAsync(A, Ad, totalSizes_[numTests], hipMemcpyDeviceToHost, nullptr));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HIP_CHECK(hipDeviceSynchronize());
|
|
|
|
|
|
|
|
|
|
|
|
auto all_end = std::chrono::steady_clock::now();
|
|
|
|
|
|
std::chrono::duration<double, std::micro> diff = all_end - all_start;
|
|
|
|
|
|
|
|
|
|
|
|
TestResult(numTests, diff, hipMemcpyDeviceToHost);
|
|
|
|
|
|
|
|
|
|
|
|
HIP_CHECK(hipHostUnregister(A));
|
2025-08-15 16:09:19 -04:00
|
|
|
|
delete[] A;
|
2023-10-26 19:48:58 +00:00
|
|
|
|
HIP_CHECK(hipFree(Ad));
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool hipPerfMemcpy::run_d2d(unsigned int numTests) {
|
|
|
|
|
|
int *Ad1, *Ad2;
|
|
|
|
|
|
HIP_CHECK(hipMalloc(&Ad1, totalSizes_[numTests]));
|
|
|
|
|
|
HIP_CHECK(hipMalloc(&Ad2, totalSizes_[numTests]));
|
|
|
|
|
|
HIP_CHECK(hipMemset(Ad2, 0x1, totalSizes_[numTests]));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// measure performance based on host time
|
|
|
|
|
|
auto all_start = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < NUM_ITER; j++) {
|
|
|
|
|
|
HIP_CHECK(hipMemcpyAsync(Ad1, Ad2, totalSizes_[numTests], hipMemcpyDeviceToDevice, nullptr));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HIP_CHECK(hipDeviceSynchronize());
|
|
|
|
|
|
|
|
|
|
|
|
auto all_end = std::chrono::steady_clock::now();
|
|
|
|
|
|
std::chrono::duration<double, std::micro> diff = all_end - all_start;
|
|
|
|
|
|
|
|
|
|
|
|
TestResult(numTests, diff, hipMemcpyDeviceToDevice);
|
|
|
|
|
|
|
|
|
|
|
|
HIP_CHECK(hipFree(Ad1));
|
|
|
|
|
|
HIP_CHECK(hipFree(Ad2));
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool hipPerfMemcpy::run_d2d_nocu(unsigned int numTests) {
|
|
|
|
|
|
int *Ad1, *Ad2;
|
|
|
|
|
|
HIP_CHECK(hipMalloc(&Ad1, totalSizes_[numTests]));
|
|
|
|
|
|
HIP_CHECK(hipMalloc(&Ad2, totalSizes_[numTests]));
|
|
|
|
|
|
HIP_CHECK(hipMemset(Ad2, 0x1, totalSizes_[numTests]));
|
|
|
|
|
|
|
|
|
|
|
|
// measure performance based on host time
|
|
|
|
|
|
auto all_start = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < NUM_ITER; j++) {
|
2025-08-15 16:09:19 -04:00
|
|
|
|
HIP_CHECK(
|
|
|
|
|
|
hipMemcpyAsync(Ad1, Ad2, totalSizes_[numTests], hipMemcpyDeviceToDeviceNoCU, nullptr));
|
2023-10-26 19:48:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HIP_CHECK(hipDeviceSynchronize());
|
|
|
|
|
|
|
|
|
|
|
|
auto all_end = std::chrono::steady_clock::now();
|
|
|
|
|
|
std::chrono::duration<double, std::micro> diff = all_end - all_start;
|
|
|
|
|
|
|
|
|
|
|
|
TestResult(numTests, diff, hipMemcpyDeviceToDeviceNoCU);
|
|
|
|
|
|
|
|
|
|
|
|
HIP_CHECK(hipFree(Ad1));
|
|
|
|
|
|
HIP_CHECK(hipFree(Ad2));
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-14 21:17:55 +05:30
|
|
|
|
/**
|
2025-08-15 16:09:19 -04:00
|
|
|
|
* Test Description
|
|
|
|
|
|
* ------------------------
|
|
|
|
|
|
* - Verify hipPerfMemcpy status.
|
|
|
|
|
|
* Test source
|
|
|
|
|
|
* ------------------------
|
|
|
|
|
|
* - perftests/memory/hipPerfMemcpy.cc
|
|
|
|
|
|
* Test requirements
|
|
|
|
|
|
* ------------------------
|
|
|
|
|
|
* - HIP_VERSION >= 5.6
|
|
|
|
|
|
*/
|
2023-08-14 21:17:55 +05:30
|
|
|
|
|
|
|
|
|
|
TEST_CASE("Perf_hipPerfMemcpy_test") {
|
|
|
|
|
|
int numDevices = 0;
|
|
|
|
|
|
HIP_CHECK(hipGetDeviceCount(&numDevices));
|
|
|
|
|
|
|
|
|
|
|
|
if (numDevices <= 0) {
|
|
|
|
|
|
SUCCEED("Skipped testcase hipPerfMemcpy as there is no device to test.");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
int deviceId = 0;
|
|
|
|
|
|
HIP_CHECK(hipSetDevice(deviceId));
|
2023-10-26 19:48:58 +00:00
|
|
|
|
hipDeviceProp_t props;
|
2023-08-14 21:17:55 +05:30
|
|
|
|
HIP_CHECK(hipGetDeviceProperties(&props, deviceId));
|
|
|
|
|
|
|
2025-08-15 16:09:19 -04:00
|
|
|
|
CONSOLE_PRINT("info: running on bus 0x%x %s with %d CUs and device id: %d\n", props.pciBusID,
|
|
|
|
|
|
props.name, props.multiProcessorCount, deviceId);
|
2023-08-14 21:17:55 +05:30
|
|
|
|
|
|
|
|
|
|
hipPerfMemcpy hipPerfMemcpy;
|
2025-08-15 16:09:19 -04:00
|
|
|
|
SECTION("Perf test Host Memory to Device Memory") {
|
2023-10-26 19:48:58 +00:00
|
|
|
|
for (auto testCase = 0; testCase < NUM_SIZE; testCase++) {
|
2025-08-15 16:09:19 -04:00
|
|
|
|
REQUIRE(true == hipPerfMemcpy.run_h2d(testCase));
|
2023-10-26 19:48:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-15 16:09:19 -04:00
|
|
|
|
SECTION("Perf test Device Memory to Host Memory") {
|
2023-10-26 19:48:58 +00:00
|
|
|
|
for (auto testCase = 0; testCase < NUM_SIZE; testCase++) {
|
2025-08-15 16:09:19 -04:00
|
|
|
|
REQUIRE(true == hipPerfMemcpy.run_d2h(testCase));
|
2023-10-26 19:48:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-15 16:09:19 -04:00
|
|
|
|
SECTION("Perf test Device Memory to Device Memory") {
|
2023-10-26 19:48:58 +00:00
|
|
|
|
for (auto testCase = 0; testCase < NUM_SIZE; testCase++) {
|
2025-08-15 16:09:19 -04:00
|
|
|
|
REQUIRE(true == hipPerfMemcpy.run_d2d(testCase));
|
2023-10-26 19:48:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-15 16:09:19 -04:00
|
|
|
|
SECTION("Perf test Device Memory to Device Memory No CU") {
|
2023-10-26 19:48:58 +00:00
|
|
|
|
for (auto testCase = 0; testCase < NUM_SIZE; testCase++) {
|
2025-08-15 16:09:19 -04:00
|
|
|
|
REQUIRE(true == hipPerfMemcpy.run_d2d_nocu(testCase));
|
2023-10-26 19:48:58 +00:00
|
|
|
|
}
|
2023-08-14 21:17:55 +05:30
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-03-22 11:17:00 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
2025-08-15 16:09:19 -04:00
|
|
|
|
* End doxygen group perfMemoryTest.
|
|
|
|
|
|
* @}
|
|
|
|
|
|
*/
|