SWDEV-277697 - Adding Infra and dependent libs: Catch2 and json parser, for new HIP Testing framework

Change-Id: Iedfa041ec9acc13eeb631ff67e1677e2fe29463d
This commit is contained in:
cjatin
2021-04-08 14:09:19 +05:30
committed by Maneesh Gupta
parent 871c167fd0
commit 8084df7b49
34 changed files with 20953 additions and 8 deletions
+14
View File
@@ -0,0 +1,14 @@
# Common Tests - Test independent of all platforms
set(TEST_SRC
saxpy.cc
)
# Set source File properties
set_source_files_properties(saxpy.cc PROPERTIES COMPILE_FLAGS " -std=c++14 ")
set_source_files_properties(test.cc PROPERTIES COMPILE_FLAGS " -std=c++17 ")
# Create shared lib of all tests
add_library(RTC SHARED EXCLUDE_FROM_ALL ${TEST_SRC})
# Add dependency on build_tests to build it on this custom target
add_dependencies(build_tests RTC)
+113
View File
@@ -0,0 +1,113 @@
#include <hip_test_common.hh>
#include <hip/hiprtc.h>
#include <hip/hip_runtime.h>
#include <cassert>
#include <cstddef>
#include <memory>
#include <iostream>
#include <iterator>
#include <vector>
static constexpr auto NUM_THREADS{128};
static constexpr auto NUM_BLOCKS{32};
static constexpr auto saxpy{
R"(
#include <hip/hip_runtime.h>
extern "C"
__global__
void saxpy(float a, float* x, float* y, float* out, size_t n)
{
size_t tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid < n) {
out[tid] = a * x[tid] + y[tid] ;
}
}
)"};
TEST_CASE("saxpy", "[hiprtc][saxpy]") {
using namespace std;
hiprtcProgram prog;
hiprtcCreateProgram(&prog, // prog
saxpy, // buffer
"saxpy.cu", // name
0, nullptr, nullptr);
hipDeviceProp_t props;
int device = 0;
hipGetDeviceProperties(&props, device);
std::string sarg = std::string("--gpu-architecture=") + props.gcnArchName;
const char* options[] = {sarg.c_str()};
hiprtcResult compileResult{hiprtcCompileProgram(prog, 1, options)};
size_t logSize;
hiprtcGetProgramLogSize(prog, &logSize);
if (logSize) {
string log(logSize, '\0');
hiprtcGetProgramLog(prog, &log[0]);
std::cout << log << '\n';
}
REQUIRE(compileResult == HIPRTC_SUCCESS);
size_t codeSize;
hiprtcGetCodeSize(prog, &codeSize);
vector<char> code(codeSize);
hiprtcGetCode(prog, code.data());
hiprtcDestroyProgram(&prog);
hipModule_t module;
hipFunction_t kernel;
hipModuleLoadData(&module, code.data());
hipModuleGetFunction(&kernel, module, "saxpy");
size_t n = NUM_THREADS * NUM_BLOCKS;
size_t bufferSize = n * sizeof(float);
float a = 5.1f;
unique_ptr<float[]> hX{new float[n]};
unique_ptr<float[]> hY{new float[n]};
unique_ptr<float[]> hOut{new float[n]};
for (size_t i = 0; i < n; ++i) {
hX[i] = static_cast<float>(i);
hY[i] = static_cast<float>(i * 2);
}
hipDeviceptr_t dX, dY, dOut;
hipMalloc(&dX, bufferSize);
hipMalloc(&dY, bufferSize);
hipMalloc(&dOut, bufferSize);
hipMemcpyHtoD(dX, hX.get(), bufferSize);
hipMemcpyHtoD(dY, hY.get(), bufferSize);
struct {
float a_;
hipDeviceptr_t b_;
hipDeviceptr_t c_;
hipDeviceptr_t d_;
size_t e_;
} args{a, dX, dY, dOut, n};
auto size = sizeof(args);
void* config[] = {HIP_LAUNCH_PARAM_BUFFER_POINTER, &args,
HIP_LAUNCH_PARAM_BUFFER_SIZE, &size,
HIP_LAUNCH_PARAM_END};
hipModuleLaunchKernel(kernel, NUM_BLOCKS, 1, 1, NUM_THREADS, 1, 1,
0, nullptr, nullptr, config);
hipMemcpyDtoH(hOut.get(), dOut, bufferSize);
for (size_t i = 0; i < n; ++i) {
REQUIRE(fabs(a * hX[i] + hY[i] - hOut[i]) > fabs(hOut[i]) * 1e-6);
}
hipFree(dX);
hipFree(dY);
hipFree(dOut);
hipModuleUnload(module);
}
+6
View File
@@ -0,0 +1,6 @@
#include <hip_test_common.hh>
TEST_CASE("cpp17 test") {
constexpr auto l = []() { return 2 * 10 * 30; };
REQUIRE(l() == 600);
}