Skip to content
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

EXSWHTEC-111 - Implement tests for the hipModuleLaunchKernel family of APIs #22

Closed
Closed
1 change: 1 addition & 0 deletions catch/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ add_subdirectory(multiThread)
add_subdirectory(compiler)
add_subdirectory(errorHandling)
add_subdirectory(cooperativeGrps)
add_subdirectory(module)
#if(HIP_PLATFORM STREQUAL "amd")
#add_subdirectory(clock)
#endif()
24 changes: 24 additions & 0 deletions catch/unit/module/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
set(TEST_SRC
hip_module_common.cc
hipModuleLaunchKernel.cc
)

if(HIP_PLATFORM MATCHES "amd")
set(TEST_SRC ${TEST_SRC} hipExtModuleLaunchKernel.cc)
endif()

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/launch_kernel_module.code
COMMAND ${CMAKE_CXX_COMPILER} --genco --std=c++17 ${CMAKE_CURRENT_SOURCE_DIR}/launch_kernel_module.cc -o launch_kernel_module.code
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/launch_kernel_module.cc)
add_custom_target(launch_kernel_module ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/launch_kernel_module.code)

if(HIP_PLATFORM MATCHES "amd")
set(RTCLIB "hiprtc")
else()
set(RTCLIB "nvrtc")
endif()
hip_add_exe_to_target(NAME ModuleTest
TEST_SRC ${TEST_SRC}
TEST_TARGET_NAME build_tests
LINKER_LIBS ${RTCLIB})
add_dependencies(ModuleTest launch_kernel_module)
75 changes: 75 additions & 0 deletions catch/unit/module/hipExtModuleLaunchKernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright (c) 2022 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_module_launch_kernel_common.hh"

#include <hip_test_common.hh>
#include <hip/hip_ext.h>

TEST_CASE("Unit_hipExtModuleLaunchKernel_Positive_Basic") {
ModuleLaunchKernelPositiveBasic<hipExtModuleLaunchKernel>();

SECTION("Timed kernel launch with events") {
hipEvent_t start_event = nullptr, stop_event = nullptr;
HIP_CHECK(hipEventCreate(&start_event));
HIP_CHECK(hipEventCreate(&stop_event));
const auto kernel = GetKernel(mg.module(), "Delay");
int clock_rate = 0;
HIP_CHECK(hipDeviceGetAttribute(&clock_rate, hipDeviceAttributeClockRate, 0));
uint32_t interval = 100;
uint32_t ticks_per_second = clock_rate;
void* kernel_params[2] = {&interval, &ticks_per_second};
HIP_CHECK(hipExtModuleLaunchKernel(kernel, 1, 1, 1, 1, 1, 1, 0, nullptr, kernel_params, nullptr,
start_event, stop_event));
HIP_CHECK(hipDeviceSynchronize());
auto elapsed = 0.0f;
HIP_CHECK(hipEventElapsedTime(&elapsed, start_event, stop_event));
REQUIRE(static_cast<uint32_t>(elapsed) >= interval);
}
}

TEST_CASE("Unit_hipExtModuleLaunchKernel_Positive_Parameters") {
ModuleLaunchKernelPositiveParameters<hipExtModuleLaunchKernel>();

SECTION("Pass only start event") {
hipEvent_t start_event = nullptr;
HIP_CHECK(hipEventCreate(&start_event));
const auto kernel = GetKernel(mg.module(), "NOPKernel");
HIP_CHECK(hipExtModuleLaunchKernel(kernel, 1, 1, 1, 1, 1, 1, 0, nullptr, nullptr, nullptr,
start_event, nullptr));
HIP_CHECK(hipDeviceSynchronize());
HIP_CHECK(hipEventQuery(start_event));
}

SECTION("Pass only stop event") {
hipEvent_t stop_event = nullptr;
HIP_CHECK(hipEventCreate(&stop_event));
const auto kernel = GetKernel(mg.module(), "NOPKernel");
HIP_CHECK(hipExtModuleLaunchKernel(kernel, 1, 1, 1, 1, 1, 1, 0, nullptr, nullptr, nullptr,
nullptr, stop_event));
HIP_CHECK(hipDeviceSynchronize());
HIP_CHECK(hipEventQuery(stop_event));
}
}

TEST_CASE("Unit_hipExtModuleLaunchKernel_Negative_Parameters") {
ModuleLaunchKernelNegativeParameters<hipExtModuleLaunchKernel>();
}
49 changes: 49 additions & 0 deletions catch/unit/module/hipModuleLaunchKernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright (c) 2022 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_module_launch_kernel_common.hh"

#include <hip_test_common.hh>
#include <hip/hip_runtime_api.h>

static hipError_t hipModuleLaunchKernelWrapper(hipFunction_t f, uint32_t gridX, uint32_t gridY,
uint32_t gridZ, uint32_t blockX, uint32_t blockY,
uint32_t blockZ, size_t sharedMemBytes,
hipStream_t hStream, void** kernelParams,
void** extra, hipEvent_t, hipEvent_t, uint32_t) {
return hipModuleLaunchKernel(f, gridX, gridY, gridZ, blockX, blockY, blockZ, sharedMemBytes,
hStream, kernelParams, extra);
}

TEST_CASE("Unit_hipModuleLaunchKernel_Positive_Basic") {
HIP_CHECK(hipFree(nullptr));
ModuleLaunchKernelPositiveBasic<hipModuleLaunchKernelWrapper>();
}

TEST_CASE("Unit_hipModuleLaunchKernel_Positive_Parameters") {
HIP_CHECK(hipFree(nullptr));
ModuleLaunchKernelPositiveParameters<hipModuleLaunchKernelWrapper>();
}

TEST_CASE("Unit_hipModuleLaunchKernel_Negative_Parameters") {
HIP_CHECK(hipFree(nullptr));
ModuleLaunchKernelNegativeParameters<hipModuleLaunchKernelWrapper>();
}
71 changes: 71 additions & 0 deletions catch/unit/module/hip_module_common.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright (c) 2022 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_module_common.hh"

#include <experimental/filesystem>
#include <fstream>

#include <hip_test_common.hh>
#include <hip/hiprtc.h>

ModuleGuard ModuleGuard::LoadModule(const char* fname) {
hipModule_t module = nullptr;
HIP_CHECK(hipModuleLoad(&module, fname));
return ModuleGuard{module};
}

ModuleGuard ModuleGuard::LoadModuleDataFile(const char* fname) {
const auto loaded_module = LoadModuleIntoBuffer(fname);
hipModule_t module = nullptr;
HIP_CHECK(hipModuleLoadData(&module, loaded_module.data()));
return ModuleGuard{module};
}

ModuleGuard ModuleGuard::LoadModuleDataRTC(const char* code) {
const auto rtc = CreateRTCCharArray(code);
hipModule_t module = nullptr;
HIP_CHECK(hipModuleLoadData(&module, rtc.data()));
return ModuleGuard{module};
}

// Load module into buffer instead of mapping file to avoid platform specific mechanisms
std::vector<char> LoadModuleIntoBuffer(const char* path_string) {
std::experimental::filesystem::path p(path_string);
const auto file_size = std::experimental::filesystem::file_size(p);
std::ifstream f(p, std::ios::binary | std::ios::in);
REQUIRE(f);
std::vector<char> empty_module(file_size);
REQUIRE(f.read(empty_module.data(), file_size));
return empty_module;
}

std::vector<char> CreateRTCCharArray(const char* src) {
hiprtcProgram prog;
HIPRTC_CHECK(hiprtcCreateProgram(&prog, src, "prog", 0, nullptr, nullptr));
HIPRTC_CHECK(hiprtcCompileProgram(prog, 0, nullptr));
size_t code_size = 0;
HIPRTC_CHECK(hiprtcGetCodeSize(prog, &code_size));
std::vector<char> code(code_size, '\0');
HIPRTC_CHECK(hiprtcGetCode(prog, code.data()));
HIPRTC_CHECK(hiprtcDestroyProgram(&prog));
return code;
}
57 changes: 57 additions & 0 deletions catch/unit/module/hip_module_common.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright (c) 2022 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.
*/

#pragma once

#include <vector>

#include <hip_test_common.hh>

class ModuleGuard {
public:
~ModuleGuard() { static_cast<void>(hipModuleUnload(module_)); }

ModuleGuard(const ModuleGuard&) = delete;
ModuleGuard(ModuleGuard&&) = delete;

static ModuleGuard LoadModule(const char* fname);

static ModuleGuard LoadModuleDataFile(const char* fname);

static ModuleGuard LoadModuleDataRTC(const char* code);

hipModule_t module() const { return module_; }

private:
ModuleGuard(const hipModule_t module) : module_{module} {}
hipModule_t module_ = nullptr;
};

// Load module into buffer instead of mapping file to avoid platform specific mechanisms
std::vector<char> LoadModuleIntoBuffer(const char* path_string);

std::vector<char> CreateRTCCharArray(const char* src);

inline hipFunction_t GetKernel(const hipModule_t module, const char* kname) {
hipFunction_t kernel = nullptr;
HIP_CHECK(hipModuleGetFunction(&kernel, module, kname));
return kernel;
}
Loading