Files
TheRock/rocm-libraries/projects/miopen/test/gtest/handle_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

240 lines
7.5 KiB
C++

/*******************************************************************************
*
* MIT License
*
* Copyright (c) 2026 Advanced Micro Devices, Inc.
*
* 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.
*
*******************************************************************************/
/// SWDEV-257056?focusedCommentId=6654244&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-6654244
/// \todo Create dedicated ticket and rename macro.
#define WORKAROUND_SWDEV_257056_PCH_MISSING_MACROS 1
// https://gerrit-git.amd.com/c/compute/ec/clr/+/972441
// Issue #2600 is not fixed in 6.0 and still persists in 6.1 release candidates.
// We are expecting it to be fixed in 6.1 RC after week 4 in 2024.
#define WORKAROUND_ISSUE_2600 \
((HIP_PACKAGE_VERSION_FLAT >= 6000000000ULL && HIP_PACKAGE_VERSION_FLAT <= 6000999999ULL) || \
(HIP_PACKAGE_VERSION_FLAT >= 6001000000ULL && HIP_PACKAGE_VERSION_FLAT <= 6001024049ULL))
// Temporarily disable warnings as errors for kernel builds to see real breaks with compiler changes
#define MIOPEN_WORKAROUND_COMPILER_CHANGE 1
#include <miopen/config.h>
#include <miopen/handle.hpp>
#include <miopen/execution_context.hpp>
#if WORKAROUND_SWDEV_257056_PCH_MISSING_MACROS
#include <miopen/hip_build_utils.hpp>
#endif
#include <vector>
#include <thread>
#include "get_handle.hpp"
#include "gtest_common.hpp"
#include "test_parameter_name_generator.hpp"
namespace {
using TestCase = NamedParameter<bool>;
static std::string Write2s()
{
return "#ifndef MIOPEN_HIP_RUNTIME_COMPILE\n"
"#include <hip/hip_runtime.h>\n"
#if WORKAROUND_SWDEV_257056_PCH_MISSING_MACROS
"#else\n"
"#ifdef hipThreadIdx_x\n"
"#undef hipThreadIdx_x\n"
"#endif\n"
"#define hipThreadIdx_x threadIdx.x\n"
"\n"
"#ifdef hipBlockDim_x\n"
"#undef hipBlockDim_x\n"
"#endif\n"
"#define hipBlockDim_x blockDim.x\n"
"\n"
"#ifdef hipBlockIdx_x\n"
"#undef hipBlockIdx_x\n"
"#endif\n"
"#define hipBlockIdx_x blockIdx.x\n"
#endif
"#endif\n"
"extern \"C\" {\n"
"__global__ void write(int* data) {\n"
" int num = hipThreadIdx_x + hipBlockDim_x * hipBlockIdx_x;\n"
" data[num] *= 2;\n"
"}\n"
"}\n";
}
static void run2s(const miopen::Handle& h, std::size_t n)
{
std::vector<int> data_in(n, 1);
auto data_dev = h.Write(data_in);
h.AddKernel("NoAlgo", "", "test_hip.cpp", "write", {n, 1, 1}, {n, 1, 1}, "", 0, Write2s())(
data_dev.get());
std::fill(data_in.begin(), data_in.end(), 2);
auto data_out = h.Read<int>(data_dev, n);
EXPECT_EQ(data_out, data_in);
}
static void test_multithreads(const bool with_stream = false)
{
auto&& h1 = get_handle();
auto&& h2 = get_handle_with_stream(h1);
std::thread([&] { run2s(with_stream ? h2 : h1, 16); }).join();
std::thread([&] { run2s(with_stream ? h2 : h1, 32); }).join();
std::thread([&] { std::thread([&] { run2s(with_stream ? h2 : h1, 64); }).join(); }).join();
run2s(with_stream ? h2 : h1, 4);
}
static std::string WriteError()
{
return "#ifndef MIOPEN_HIP_RUNTIME_COMPILE\n"
"#include <hip/hip_runtime.h>\n"
"#endif\n"
"extern \"C\" {\n"
"__global__ void write(int* data) {\n"
" data[num] *= 2;\n"
"}\n"
"}\n";
}
static void test_errors()
{
auto&& h = get_handle();
EXPECT_ANY_THROW(h.AddKernel(
"NoAlgo", "", "error_hip.cpp", "write", {1, 1, 1}, {1, 1, 1}, "", 0, WriteError()));
try
{
h.AddKernel(
"NoAlgo", "", "error_hip.cpp", "write", {1, 1, 1}, {1, 1, 1}, "", 0, WriteError());
}
catch(miopen::Exception& e)
{
EXPECT_FALSE(std::string(e.what()).empty());
}
}
#if MIOPEN_BUILD_DEV && !WORKAROUND_ISSUE_2600 && !MIOPEN_WORKAROUND_COMPILER_CHANGE
static std::string WriteNop()
{
return "#ifndef MIOPEN_HIP_RUNTIME_COMPILE\n"
"#include <hip/hip_runtime.h>\n"
"#endif\n"
"extern \"C\" {\n"
"__global__ void write(int* data) {\n"
"}\n"
"}\n";
}
#endif
static void test_warnings()
{
#if MIOPEN_BUILD_DEV && !WORKAROUND_ISSUE_2600 && !MIOPEN_WORKAROUND_COMPILER_CHANGE
auto&& h = get_handle();
EXPECT_ANY_THROW(
h.AddKernel("NoAlgo", "", "nop_hip.cpp", "write", {1, 1, 1}, {1, 1, 1}, "", 0, WriteNop()));
#endif
}
static void test_arch_name()
{
auto&& h = get_handle();
const auto known_arch = {"gfx908", "gfx90a", "gfx906", "gfx900", "gfx942",
"gfx950", "gfx803", "gfx1030", "gfx1031", "gfx1100",
"gfx1101", "gfx1102", "gfx1103", "gfx1150", "gfx1151",
"gfx1152", "gfx1153", "gfx1200", "gfx1201", "gfx1250"};
const auto this_arch = h.GetDeviceName();
EXPECT_TRUE(std::any_of(
known_arch.begin(), known_arch.end(), [&](std::string arch) { return arch == this_arch; }));
}
static inline auto GenCases() { return MakeNamedParameterValues<bool>("with_stream", false, true); }
static inline auto GetCases()
{
static const auto cases = GenCases();
return cases;
}
} // namespace
struct HandleTest : testing::TestWithParam<TestCase>
{
void SetUp() override
{
prng::reset_seed();
with_stream = GetParam();
}
void RunHIP()
{
auto&& h = get_handle();
if(h.GetDeviceName() != "gfx803" && miopen::IsHipKernelsEnabled())
{
test_multithreads(with_stream);
test_errors();
test_warnings();
}
test_arch_name();
}
private:
bool with_stream{};
};
struct TestNameGenerator
{
std::string operator()(const auto& info)
{
const auto& with_stream = info.param;
std::stringstream ss;
std::string str;
ss << "with_stream_" << std::boolalpha << with_stream() << "_test_id_" << info.index;
str = ss.str();
// Name format only supports letters, numbers and underscores.
std::transform(str.begin(), str.end(), str.begin(), [](char c) -> char {
return (c == '.') ? 'p' : (std::isalnum(c) ? c : '_');
});
return str;
}
};
using CPU_Handle_NONE = HandleTest;
TEST_P(CPU_Handle_NONE, TestHIP) { this->RunHIP(); }
INSTANTIATE_TEST_SUITE_P(Smoke, CPU_Handle_NONE, GetCases(), TestNameGenerator{});