@@ -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// ================================================================================================
20582073bool 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,
21462162bool 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 ) {
0 commit comments