Skip to content

Commit

Permalink
fix hipMalloc() issue when running in multithreads. (#8127)
Browse files Browse the repository at this point in the history
* fix hipMalloc() issue when running in multithreads.

Signed-off-by: Chiming Zhang <[email protected]>

* remove unnecessary template parameter.

Signed-off-by: Chiming Zhang <[email protected]>

---------

Signed-off-by: Chiming Zhang <[email protected]>
  • Loading branch information
zhangchiming authored May 2, 2024
1 parent 38bcaf1 commit 2e25aab
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/runtime_src/hip/api/hip_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ device_init();
}

namespace {
std::once_flag device_init_flag;
thread_local std::once_flag device_init_flag;

// Creates devices at library load
// User may not explicitly call init or device create
Expand All @@ -44,6 +44,8 @@ device_init()
// create all devices ahead
// Used when user doesn't explicitly create device
for (uint32_t i = 0; i < dev_count; i++) {
if (device_cache.count(static_cast<device_handle>(i)) > 0)
continue;
auto dev = std::make_shared<xrt::core::hip::device>(i);
device_cache.add(i, std::move(dev));
}
Expand Down
2 changes: 2 additions & 0 deletions src/runtime_src/hip/core/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,14 @@ namespace xrt::core::hip
void
memory_database::insert(uint64_t addr, size_t size, std::shared_ptr<xrt::core::hip::memory> hip_mem)
{
std::lock_guard lock(m_mutex);
m_addr_map.insert({address_range_key(addr, size), hip_mem});
}

void
memory_database::remove(uint64_t addr)
{
std::lock_guard lock(m_mutex);
m_addr_map.erase(address_range_key(addr, 0));
}

Expand Down
1 change: 1 addition & 0 deletions src/runtime_src/hip/core/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ namespace xrt::core::hip
{
private:
addr_map m_addr_map;
std::mutex m_mutex;

protected:
memory_database();
Expand Down

0 comments on commit 2e25aab

Please sign in to comment.