From 1824fb76985d1a4b172914501617fe34bc12df2d Mon Sep 17 00:00:00 2001 From: Alex Voicu Date: Mon, 20 Nov 2017 22:41:46 +0000 Subject: [PATCH] Clean-up some remaining noise in program_state.cpp. --- src/program_state.cpp | 45 ++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/src/program_state.cpp b/src/program_state.cpp index a4f7fdbdbe..d5e2f80a05 100644 --- a/src/program_state.cpp +++ b/src/program_state.cpp @@ -17,9 +17,11 @@ #include #include #include +#include #include #include #include +#include #include #include @@ -132,21 +134,39 @@ namespace const auto tmp = find_symbol_address( symbol_section_accessor{self_reader, process_symtab}, x); - assert(tmp.first); + if (!tmp.first) { + throw runtime_error{ + "The global variable: " + x + ", could not be found."}; + } - void* p = nullptr; - hsa_amd_memory_lock( - reinterpret_cast(tmp.first), tmp.second, &agent, 1, &p); + static unordered_map< + Elf64_Addr, + unique_ptr> globals; + + if (globals.count(tmp.first) == 0) { + void* p = nullptr; + hsa_amd_memory_lock( + reinterpret_cast(tmp.first), + tmp.second, + &agent, + 1, + &p); + + static mutex mtx; + + lock_guard lck{mtx}; + globals.emplace( + piecewise_construct, + make_tuple(tmp.first), + make_tuple(p, hsa_amd_memory_unlock)); + } + + const auto it = globals.find(tmp.first); + + assert(it != globals.cend()); hsa_executable_agent_global_variable_define( - executable, agent, x.c_str(), p); - - static vector< - unique_ptr> globals; - static mutex mtx; - - lock_guard lck{mtx}; - globals.emplace_back(p, hsa_amd_memory_unlock); + executable, agent, x.c_str(), it->second.get()); } } @@ -265,7 +285,6 @@ namespace } }); - cout << r.size() << endl; return r; }