Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

Commit 94be985

Browse files
committed
Add support for CPU device in advise
Change-Id: I7250e0183580c14cd3d6050ef85f9ce26e36f4a8
1 parent 9ea24e3 commit 94be985

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

device/device.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@ class Device : public RuntimeObject {
14041404
* @return True if the device successfully applied the SVM attributes in HMM for device memory
14051405
*/
14061406
virtual bool SetSvmAttributes(const void* dev_ptr, size_t count,
1407-
amd::MemoryAdvice advice, bool first_alloc = false) const {
1407+
amd::MemoryAdvice advice, bool use_cpu = false) const {
14081408
ShouldNotCallThis();
14091409
return false;
14101410
}

device/rocm/rocdevice.cpp

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,8 +2003,8 @@ void* Device::svmAlloc(amd::Context& context, size_t size, size_t alignment, cl_
20032003
}
20042004

20052005
// ================================================================================================
2006-
bool Device::SetSvmAttributes(const void* dev_ptr, size_t count,
2007-
amd::MemoryAdvice advice, bool first_alloc) const {
2006+
bool Device::SetSvmAttributesInt(const void* dev_ptr, size_t count,
2007+
amd::MemoryAdvice advice, bool first_alloc, bool use_cpu) const {
20082008
if ((settings().hmmFlags_ & Settings::Hmm::EnableSvmTracking) && !first_alloc) {
20092009
amd::Memory* svm_mem = amd::MemObjMap::FindMemObj(dev_ptr);
20102010
if (nullptr == svm_mem) {
@@ -2026,18 +2026,26 @@ bool Device::SetSvmAttributes(const void* dev_ptr, size_t count,
20262026
attr.push_back({HSA_AMD_SVM_ATTRIB_READ_ONLY, false});
20272027
break;
20282028
case amd::MemoryAdvice::SetPreferredLocation:
2029-
attr.push_back({HSA_AMD_SVM_ATTRIB_PREFERRED_LOCATION, getBackendDevice().handle});
2029+
if (use_cpu) {
2030+
attr.push_back({HSA_AMD_SVM_ATTRIB_PREFERRED_LOCATION, getCpuAgent().handle});
2031+
} else {
2032+
attr.push_back({HSA_AMD_SVM_ATTRIB_PREFERRED_LOCATION, getBackendDevice().handle});
2033+
}
20302034
break;
20312035
case amd::MemoryAdvice::UnsetPreferredLocation:
2032-
// Note: The current behavior doesn't match hip spec precisely
2033-
attr.push_back({HSA_AMD_SVM_ATTRIB_PREFERRED_LOCATION, getCpuAgent().handle});
2036+
// @note: 0 may cause a failure on old runtimes
2037+
attr.push_back({HSA_AMD_SVM_ATTRIB_PREFERRED_LOCATION, 0});
20342038
break;
20352039
case amd::MemoryAdvice::SetAccessedBy:
2036-
attr.push_back({HSA_AMD_SVM_ATTRIB_AGENT_ACCESSIBLE, getBackendDevice().handle});
2040+
if (use_cpu) {
2041+
attr.push_back({HSA_AMD_SVM_ATTRIB_AGENT_ACCESSIBLE_IN_PLACE, getCpuAgent().handle});
2042+
} else {
2043+
attr.push_back({HSA_AMD_SVM_ATTRIB_AGENT_ACCESSIBLE_IN_PLACE, getBackendDevice().handle});
2044+
}
20372045
break;
20382046
case amd::MemoryAdvice::UnsetAccessedBy:
2039-
// @note: The current behavior doesn't match hip spec precisely
2040-
attr.push_back({HSA_AMD_SVM_ATTRIB_AGENT_ACCESSIBLE, getCpuAgent().handle});
2047+
// @note: 0 may cause a failure on old runtimes
2048+
attr.push_back({HSA_AMD_SVM_ATTRIB_AGENT_ACCESSIBLE_IN_PLACE, 0});
20412049
break;
20422050
default:
20432051
return false;
@@ -2047,13 +2055,20 @@ bool Device::SetSvmAttributes(const void* dev_ptr, size_t count,
20472055
hsa_status_t status = hsa_amd_svm_attributes_set(const_cast<void*>(dev_ptr), count,
20482056
attr.data(), attr.size());
20492057
if (status != HSA_STATUS_SUCCESS) {
2050-
LogError("hsa_amd_svm_attributes_set() failed");
2058+
LogPrintfError("hsa_amd_svm_attributes_set() failed. Advice: %d", advice);
20512059
return false;
20522060
}
20532061
#endif // AMD_HMM_SUPPORT
20542062
return true;
20552063
}
20562064

2065+
// ================================================================================================
2066+
bool Device::SetSvmAttributes(const void* dev_ptr, size_t count,
2067+
amd::MemoryAdvice advice, bool use_cpu) const {
2068+
constexpr bool kFirstAlloc = false;
2069+
return SetSvmAttributesInt(dev_ptr, count, advice, kFirstAlloc, use_cpu);
2070+
}
2071+
20572072
// ================================================================================================
20582073
bool Device::GetSvmAttributes(void** data, size_t* data_sizes, int* attributes,
20592074
size_t num_attributes, const void* dev_ptr, size_t count) const {
@@ -2102,7 +2117,8 @@ bool Device::GetSvmAttributes(void** data, size_t* data_sizes, int* attributes,
21022117
return false;
21032118
}
21042119
// Cast ROCr value into the hip format
2105-
*reinterpret_cast<uint32_t*>(data[idx]) = static_cast<uint32_t>(it.value);
2120+
*reinterpret_cast<uint32_t*>(data[idx]) =
2121+
(static_cast<uint32_t>(it.value) > 0) ? true : false;
21062122
break;
21072123
// The logic should be identical for the both queries
21082124
case HSA_AMD_SVM_ATTRIB_PREFERRED_LOCATION:
@@ -2146,14 +2162,14 @@ bool Device::GetSvmAttributes(void** data, size_t* data_sizes, int* attributes,
21462162
bool Device::SvmAllocInit(void* memory, size_t size) const {
21472163
amd::MemoryAdvice advice = amd::MemoryAdvice::SetAccessedBy;
21482164
constexpr bool kFirstAlloc = true;
2149-
SetSvmAttributes(memory, size, advice, kFirstAlloc);
2165+
SetSvmAttributesInt(memory, size, advice, kFirstAlloc);
21502166

21512167
if (settings().hmmFlags_ & Settings::Hmm::EnableSystemMemory) {
21522168
advice = amd::MemoryAdvice::UnsetPreferredLocation;
2153-
SetSvmAttributes(memory, size, advice);
2169+
SetSvmAttributesInt(memory, size, advice);
21542170
} else {
21552171
advice = amd::MemoryAdvice::SetPreferredLocation;
2156-
SetSvmAttributes(memory, size, advice);
2172+
SetSvmAttributesInt(memory, size, advice);
21572173
}
21582174

21592175
if ((settings().hmmFlags_ & Settings::Hmm::EnableMallocPrefetch) == 0) {

device/rocm/rocdevice.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ class Device : public NullDevice {
379379
virtual void svmFree(void* ptr) const;
380380

381381
virtual bool SetSvmAttributes(const void* dev_ptr, size_t count,
382-
amd::MemoryAdvice advice, bool first_alloc = false) const;
382+
amd::MemoryAdvice advice, bool use_cpu = false) const;
383383
virtual bool GetSvmAttributes(void** data, size_t* data_sizes, int* attributes,
384384
size_t num_attributes, const void* dev_ptr, size_t count) const;
385385

@@ -481,6 +481,8 @@ class Device : public NullDevice {
481481
bool SvmAllocInit(void* memory, size_t size) const;
482482

483483
private:
484+
bool SetSvmAttributesInt(const void* dev_ptr, size_t count, amd::MemoryAdvice advice,
485+
bool first_alloc = false, bool use_cpu = false) const;
484486
static constexpr hsa_signal_value_t InitSignalValue = 1;
485487

486488
static hsa_ven_amd_loader_1_00_pfn_t amd_loader_ext_table;

0 commit comments

Comments
 (0)