From a224eef6247a33740484f0106de702b2fcc50d40 Mon Sep 17 00:00:00 2001 From: kjayapra-amd Date: Thu, 8 Dec 2022 11:44:33 -0800 Subject: [PATCH] SWDEV-359061 - Tests for hiprtcLink File and Data. Change-Id: I2e75f3d4e50e410db31c8906cad2453da158816d --- catch/unit/rtc/CMakeLists.txt | 3 + catch/unit/rtc/hiprtcHelper.cc | 318 +++++++++++++++++++++++++++++++ catch/unit/rtc/hiprtcHelper.hpp | 49 +++++ catch/unit/rtc/hiprtcLinkData.cc | 106 +++++++++++ catch/unit/rtc/hiprtcLinkFile.cc | 111 +++++++++++ 5 files changed, 587 insertions(+) create mode 100644 catch/unit/rtc/hiprtcHelper.cc create mode 100644 catch/unit/rtc/hiprtcHelper.hpp create mode 100644 catch/unit/rtc/hiprtcLinkData.cc create mode 100644 catch/unit/rtc/hiprtcLinkFile.cc diff --git a/catch/unit/rtc/CMakeLists.txt b/catch/unit/rtc/CMakeLists.txt index 534289455..179471360 100644 --- a/catch/unit/rtc/CMakeLists.txt +++ b/catch/unit/rtc/CMakeLists.txt @@ -3,6 +3,9 @@ set(TEST_SRC saxpy.cc warpsize.cc hipRtcFunctional.cc + hiprtcHelper.cc + hiprtcLinkFile.cc + hiprtcLinkData.cc ) # AMD only tests diff --git a/catch/unit/rtc/hiprtcHelper.cc b/catch/unit/rtc/hiprtcHelper.cc new file mode 100644 index 000000000..be2d0ab51 --- /dev/null +++ b/catch/unit/rtc/hiprtcHelper.cc @@ -0,0 +1,318 @@ +#include "hiprtcHelper.hpp" + +#include + +#include +#include + +#if defined(_WIN32) +#include +#else +#include +#include +#include +#include +#endif + +#include +#include + +#define kernel_name "vcpy_kernel" +#define kernel_name0 "testKernel" + +bool CommitBCToFile(char* executable, size_t exe_size, const std::string& bit_code_file) { + std::fstream bc_file; + bc_file.open(bit_code_file, std::ios::out | std::ios::binary); + if (!bc_file) { + std::cout << "File not created" << std::endl; + } + + // std::cout<<"EXE SIZE: "< idx: "<(&Ad), SIZE)); + HIPCHECK(hipMalloc(reinterpret_cast(&Bd), SIZE)); + HIPCHECK(hipMemcpyHtoD(Ad, A, SIZE)); + HIPCHECK(hipMemcpyHtoD(Bd, B, SIZE)); + + hipStream_t hip_stream; + HIPCHECK(hipStreamCreate(&hip_stream)); + + struct { + void* _Ad; + void* _Bd; + } args; + args._Ad = reinterpret_cast(Ad); + args._Bd = reinterpret_cast(Bd); + size_t args_size = sizeof(args); + + void* config[] = {HIP_LAUNCH_PARAM_BUFFER_POINTER, &args, HIP_LAUNCH_PARAM_BUFFER_SIZE, + &args_size, HIP_LAUNCH_PARAM_END}; + + hipModule_t hip_module = nullptr; + hipFunction_t hip_function = nullptr; + HIPCHECK(hipModuleLoadData(&hip_module, cuOut)); + HIPCHECK(hipModuleGetFunction(&hip_function, hip_module, kernel_name)); + HIPCHECK(hipModuleLaunchKernel(hip_function, 1, 1, 1, LEN, 1, 1, 0, hip_stream, NULL, + reinterpret_cast(&config))); + HIPCHECK(hipStreamSynchronize(hip_stream)); + + HIPCHECK(hipMemcpyDtoH(B, Bd, SIZE)); + + for (size_t idx = 0; idx < LEN; ++idx) { + if (A[idx] != B[idx]) { + test_passed = false; + std::cout << "FAIL --> idx: " << idx << " A: " << A[idx] << " B: " << B[idx] << std::endl; + break; + } else { + // std::cout<<"PASS --> idx: "<(x); + size_t args_size = sizeof(args); + + void* config[] = {HIP_LAUNCH_PARAM_BUFFER_POINTER, &args, HIP_LAUNCH_PARAM_BUFFER_SIZE, + &args_size, HIP_LAUNCH_PARAM_END}; + + hipModule_t hip_module = nullptr; + hipFunction_t hip_function = nullptr; + HIPCHECK(hipModuleLoadData(&hip_module, cuOut)); + HIPCHECK(hipModuleGetFunction(&hip_function, hip_module, kernel_name0)); + HIPCHECK(hipModuleLaunchKernel(hip_function, 1, 1, 1, 1, 1, 1, 0, NULL, NULL, + reinterpret_cast(&config))); + HIPCHECK(hipDeviceSynchronize()); + + HIPCHECK(hipModuleUnload(hip_module)); + HIPCHECK(hipFree(x)); + return test_passed; +} + +bool OpenFileHandle(const char* fname, FileDesc* fd_ptr, size_t* sz_ptr) { + if ((fd_ptr == nullptr) || (sz_ptr == nullptr)) { + std::cout << "Invalid arguments, fname: " << fname << " fd_ptr: " << fd_ptr + << "sz_ptr: " << sz_ptr << std::endl; + return false; + } + +#if defined(_WIN32) + *fd_ptr = INVALID_HANDLE_VALUE; + *fd_ptr = + CreateFileA(fname, GENERIC_READ, 0x1, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); + if (*fd_ptr == INVALID_HANDLE_VALUE) { + return false; + } + + *sz_ptr = GetFileSize(*fd_ptr, NULL); + +#else + // open system function call, return false on fail + struct stat stat_buf; + *fd_ptr = open(fname, O_RDONLY); + if (*fd_ptr < 0) { + return false; + } + + // Retrieve stat info and size + if (fstat(*fd_ptr, &stat_buf) != 0) { + close(*fd_ptr); + return false; + } + + *sz_ptr = stat_buf.st_size; +#endif + + return true; +} + +bool CloseFileHandle(FileDesc fdesc) { +#if defined(_WIN32) + // return false on failure + if (CloseHandle(fdesc) < 0) { + return false; + } +#else + // Return false if close system call fails + if (close(fdesc) < 0) { + return false; + } +#endif + return true; +} + +bool MemoryMapFileDesc(FileDesc fdesc, size_t fsize, size_t foffset, const void** mmap_pptr) { +#if defined(_WIN32) + if (fdesc == INVALID_HANDLE_VALUE) { + return false; + } + + HANDLE map_handle = CreateFileMappingA(fdesc, NULL, PAGE_READONLY, 0, 0, NULL); + if (map_handle == INVALID_HANDLE_VALUE) { + CloseHandle(map_handle); + return false; + } + + *mmap_pptr = MapViewOfFile(map_handle, FILE_MAP_READ, 0, 0, 0); +#else + if (fdesc <= 0) { + return false; + } + + // If the offset is not aligned then align it + // and recalculate the new size + if (foffset > 0) { + size_t old_foffset = foffset; + foffset = alignUp(foffset, 4096); + fsize += (foffset - old_foffset); + } + + *mmap_pptr = mmap(NULL, fsize, PROT_READ, MAP_SHARED, fdesc, foffset); +#endif + return true; +} + +bool MemoryUnmapFile(const void* mmap_ptr, size_t mmap_size) { +#if defined(_WIN32) + if (!UnmapViewOfFile(mmap_ptr)) { + std::cout << "Unmap file failed: " << mmap_ptr << std::endl; + return false; + } +#else + if (munmap(const_cast(mmap_ptr), mmap_size) != 0) { + std::cout << "Unmap file failed: " << mmap_ptr << std::endl; + return false; + } +#endif + return true; +} + +bool GetMapPtr(const char* fname, FileDesc* fd_ptr, size_t* sz_ptr, const void** mmap_pptr) { + if (!OpenFileHandle(fname, fd_ptr, sz_ptr)) { + std::cout << "Opening File handle Failed: " << fname << std::endl; + return false; + } + + if (!MemoryMapFileDesc(*fd_ptr, *sz_ptr, 0, mmap_pptr)) { + std::cout << "Memmap failed: fd_ptr: " << *fd_ptr << "fd_size" << *sz_ptr << std::endl; + return false; + } + + if (!CloseFileHandle(*fd_ptr)) { + std::cout << "Closing the file handle Failed: " << *fd_ptr << std::endl; + return false; + } + + return true; +} + +bool DelMapPtr(const void* mmap_ptr, size_t mmap_size) { + return MemoryUnmapFile(mmap_ptr, mmap_size); +} diff --git a/catch/unit/rtc/hiprtcHelper.hpp b/catch/unit/rtc/hiprtcHelper.hpp new file mode 100644 index 000000000..c9c2ad24b --- /dev/null +++ b/catch/unit/rtc/hiprtcHelper.hpp @@ -0,0 +1,49 @@ +#ifndef HIPRTC_HELPER_HPP +#define HIPRTC_HELPER_HPP + +#include + +// Header for File Desc +#if defined(_WIN32) + typedef void* FileDesc; +#else + typedef int FileDesc; +#endif + +// Templated Helper function +template inline T alignDown(T value, size_t alignment) { + return (T)(value & ~(alignment - 1)); +} + +template inline T* alignDown(T* value, size_t alignment) { + return (T*)alignDown((intptr_t)value, alignment); +} + +template inline T alignUp(T value, size_t alignment) { + return alignDown((T)(value + alignment - 1), alignment); +} + +template inline T* alignUp(T* value, size_t alignment) { + return (T*)alignDown((intptr_t)(value + alignment - 1), alignment); +} + +// Exported Functions +bool CommitBCToFile(char* executable, size_t exe_size, const std::string& bit_code_file); + +bool TestModuleLoadData(void* cuOut); +bool TestModuleLoad2Data(void* cuOut); +bool TestCompileRDC(char** bit_code_pptr, size_t* bit_code_size_ptr, + const char* routine_ptr, std::string routine_name); +bool TestCompileProgram(const char* source_code, hiprtcProgram* prog_ptr, std::string prog_name, + std::string func_name); + +bool OpenFileHandle(const char* fname, FileDesc* fd_ptr, size_t* sz_ptr); +bool CloseFileHandle(FileDesc fdesc); + +bool MemoryMapFileDesc(FileDesc fdesc, size_t fsize, size_t foffset, const void** mmap_pptr); +bool MemoryUnmapFile(const void* mmap_ptr, size_t mmap_size); + +bool GetMapPtr(const char* fname, FileDesc* fd_ptr, size_t* sz_ptr, const void** mmap_pptr); +bool DelMapPtr(const void* mmap_ptr, size_t mmap_size); + +#endif /* HIPRTC_HELPER_HPP */ diff --git a/catch/unit/rtc/hiprtcLinkData.cc b/catch/unit/rtc/hiprtcLinkData.cc new file mode 100644 index 000000000..ad10ac23a --- /dev/null +++ b/catch/unit/rtc/hiprtcLinkData.cc @@ -0,0 +1,106 @@ +/* + * 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 + +#include "hiprtcHelper.hpp" +#include +#include + +static constexpr auto vcpy{ + R"( + extern "C" __global__ void vcpy_kernel(float* a, float* b) { + int tx = __ockl_get_local_id(0); + b[tx] = a[tx]; + } +)"}; + +bool TestAddLinkData(char* bit_code_ptr, size_t bit_code_size, + hiprtcJITInputType input_type, void** cuOut, size_t* out_size) { + + hiprtcLinkState hiprtc_link_state; + hiprtcJIT_option options[6]; + void* option_vals[6]; + float wall_time; + size_t log_size = 8192; + char error_log[8192]; + char info_log[8192]; + + options[0] = HIPRTC_JIT_WALL_TIME; + option_vals[0] = (void*)(&wall_time); + + options[1] = HIPRTC_JIT_INFO_LOG_BUFFER; + option_vals[1] = (void*)info_log; + + options[2] = HIPRTC_JIT_INFO_LOG_BUFFER_SIZE_BYTES; + option_vals[2] = (void*)(log_size); + + options[3] = HIPRTC_JIT_ERROR_LOG_BUFFER; + option_vals[3] = (void*)error_log; + + options[4] = HIPRTC_JIT_ERROR_LOG_BUFFER_SIZE_BYTES; + option_vals[4] = (void*)(log_size); + + options[5] = HIPRTC_JIT_LOG_VERBOSE; + option_vals[5] = (void*)1; + + if (HIPRTC_SUCCESS != hiprtcLinkCreate(6, options, option_vals, &hiprtc_link_state)) { + std::cout<<"hipLinkCreate failed "< + +#include +#include +#include + +#include "hiprtcHelper.hpp" +#include +#include + +static constexpr auto vcpy{ + R"( + extern "C" __global__ void vcpy_kernel(float* a, float* b) { + int tx = __ockl_get_local_id(0); + b[tx] = a[tx]; + } +)"}; + +bool TestAddLinkFile(std::string bc_file_path, hiprtcJITInputType input_type, void** cuOut, + size_t* cuSize) { + hiprtcLinkState hiprtc_link_state; + hiprtcJIT_option options[6]; + void* option_vals[6]; + float wall_time; + size_t log_size = 8192; + char error_log[8192]; + char info_log[8192]; + + options[0] = HIPRTC_JIT_WALL_TIME; + option_vals[0] = (void*)(&wall_time); + + options[1] = HIPRTC_JIT_INFO_LOG_BUFFER; + option_vals[1] = (void*)info_log; + + options[2] = HIPRTC_JIT_INFO_LOG_BUFFER_SIZE_BYTES; + option_vals[2] = (void*)(log_size); + + options[3] = HIPRTC_JIT_ERROR_LOG_BUFFER; + option_vals[3] = (void*)error_log; + + options[4] = HIPRTC_JIT_ERROR_LOG_BUFFER_SIZE_BYTES; + option_vals[4] = (void*)(log_size); + + options[5] = HIPRTC_JIT_LOG_VERBOSE; + option_vals[5] = (void*)1; + + if (HIPRTC_SUCCESS != hiprtcLinkCreate(6, options, option_vals, &hiprtc_link_state)) { + std::cout<<"hipLinkCreate failed "<