From e5d077f70e27540b626cf1aedaf73c278343a685 Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 10 Feb 2020 15:52:34 -0600 Subject: [PATCH] Create context for cuda --- .../module/hipModuleLoadDataMultThreaded.cpp | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/tests/src/runtimeApi/module/hipModuleLoadDataMultThreaded.cpp b/tests/src/runtimeApi/module/hipModuleLoadDataMultThreaded.cpp index ff2d5b1ef3..92a1cb77c7 100644 --- a/tests/src/runtimeApi/module/hipModuleLoadDataMultThreaded.cpp +++ b/tests/src/runtimeApi/module/hipModuleLoadDataMultThreaded.cpp @@ -35,28 +35,31 @@ THE SOFTWARE. #define LEN 64 #define SIZE LEN << 2 -#define THREADS 4 +#define THREADS 8 #define FILENAME "vcpy_kernel.code" #define kernel_name "hello_world" using ModuleFunction = std::pair; -ModuleFunction load() { - hipModule_t Module; - hipFunction_t Function; +std::vector load_file() +{ std::ifstream file(FILENAME, std::ios::binary | std::ios::ate); std::streamsize fsize = file.tellg(); file.seekg(0, std::ios::beg); std::vector buffer(fsize); - if (file.read(buffer.data(), fsize)) { - HIPCHECK(hipModuleLoadData(&Module, &buffer[0])); - HIPCHECK(hipModuleGetFunction(&Function, Module, kernel_name)); - } - else { + if (!file.read(buffer.data(), fsize)) { failed("could not open code object '%s'\n", FILENAME); } + return buffer; +} + +ModuleFunction load(const std::vector& buffer) { + hipModule_t Module; + hipFunction_t Function; + HIPCHECK(hipModuleLoadData(&Module, &buffer[0])); + HIPCHECK(hipModuleGetFunction(&Function, Module, kernel_name)); return {Module, Function}; } @@ -121,25 +124,37 @@ struct joinable_thread : std::thread } }; +hipCtx_t create_context() { + hipDevice_t device; + HIPCHECK(hipDeviceGet(&device, 0)); + + hipCtx_t ctx; + HIPCHECK(hipCtxCreate(&ctx, 0, device)); + return ctx; +} + void run_multi_threads(uint32_t n) { std::vector mf(n); { + auto buffer = load_file(); std::vector threads; for (uint32_t i = 0; i < n; i++) { - threads.emplace_back(std::thread{[=, &mf] { - hipSetDevice(0); - mf[i] = load(); + threads.emplace_back(std::thread{[&, i, buffer] { + mf[i] = load(buffer); }}); } } for(auto&& x:mf) run(x); + } int main() { HIPCHECK(hipInit(0)); + auto ctx = create_context(); run_multi_threads(THREADS * std::thread::hardware_concurrency()); + hipCtxDestroy(ctx); passed(); }