Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mutex locks in register_target.cpp and created a multithreading… #2224

Closed
wants to merge 13 commits into from
20 changes: 19 additions & 1 deletion src/register_target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <mutex>
#include <string>
#include <unordered_map>
#include <migraphx/register_target.hpp>
Expand All @@ -33,6 +34,9 @@ inline namespace MIGRAPHX_INLINE_NS {
void store_target_lib(const dynamic_loader& lib)
{
static std::vector<dynamic_loader> target_loader;
std::mutex mutex;
std::unique_lock<std::mutex> lock(mutex);

target_loader.emplace_back(lib);
}

Expand All @@ -46,14 +50,28 @@ void register_target_init() { (void)target_map(); }

void unregister_target(const std::string& name)
{
std::mutex mutex;
std::unique_lock<std::mutex> lock(mutex);
assert(target_map().count(name));
target_map().erase(name);
}

void register_target(const target& t) { target_map()[t.name()] = t; }
void register_target(const target& t)
{
std::mutex mutex;
std::unique_lock<std::mutex> lock(mutex);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add unlock before each return

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need to--see above

target_map()[t.name()] = t;
}

target make_target(const std::string& name)
{
// debug
for(auto pp : target_map())
std::cout << pp.first << "\n";

std::mutex mutex;
std::unique_lock<std::mutex> lock(mutex);
if(not contains(target_map(), name))
{
std::string target_name = "libmigraphx_" + name + ".so";
Expand Down
20 changes: 20 additions & 0 deletions test/targets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
#include <migraphx/register_target.hpp>
#include <migraphx/target.hpp>
#include <migraphx/par_for.hpp>
#include <mutex>
#include <thread>
#include "test.hpp"

TEST_CASE(make_target)
Expand Down Expand Up @@ -50,4 +53,21 @@ TEST_CASE(targets)
EXPECT(ts.size() >= 1);
}

TEST_CASE(concurrent_targets)
bpickrel marked this conversation as resolved.
Show resolved Hide resolved
{
std::vector<std::thread> threads;

for(auto i = 0u; i < 10000; i++)
{
auto thread_body = []() {
auto ref_target = migraphx::make_target("gpu");
migraphx::register_target(ref_target);
bpickrel marked this conversation as resolved.
Show resolved Hide resolved
};

threads.emplace_back(thread_body);
}
for(auto& tt : threads)
tt.join();
bpickrel marked this conversation as resolved.
Show resolved Hide resolved
}

int main(int argc, const char* argv[]) { test::run(argc, argv); }