Skip to content

Commit 67c3779

Browse files
author
Hugh Delaney
committed
AMDGPU enable global variable read write
1 parent 9f14fed commit 67c3779

File tree

3 files changed

+82
-6
lines changed

3 files changed

+82
-6
lines changed

source/adapters/hip/enqueue.cpp

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,15 +1545,71 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMMemcpy2D(
15451545
}
15461546

15471547
UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableWrite(
1548-
ur_queue_handle_t, ur_program_handle_t, const char *, bool, size_t, size_t,
1549-
const void *, uint32_t, const ur_event_handle_t *, ur_event_handle_t *) {
1550-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
1548+
ur_queue_handle_t hQueue, ur_program_handle_t hProgram, const char *name,
1549+
bool blockingWrite, size_t count, size_t offset, const void *pSrc,
1550+
uint32_t numEventsInWaitList, const ur_event_handle_t *phEventWaitList,
1551+
ur_event_handle_t *phEvent) {
1552+
// Since HIP requires a the global variable to be referenced by name, we use
1553+
// metadata to find the correct name to access it by.
1554+
auto DeviceGlobalNameIt = hProgram->GlobalIDMD.find(name);
1555+
if (DeviceGlobalNameIt == hProgram->GlobalIDMD.end())
1556+
return UR_RESULT_ERROR_INVALID_VALUE;
1557+
std::string DeviceGlobalName = DeviceGlobalNameIt->second;
1558+
1559+
ur_result_t Result = UR_RESULT_SUCCESS;
1560+
try {
1561+
hipDeviceptr_t DeviceGlobal = 0;
1562+
size_t DeviceGlobalSize = 0;
1563+
UR_CHECK_ERROR(hipModuleGetGlobal(&DeviceGlobal, &DeviceGlobalSize,
1564+
hProgram->get(),
1565+
DeviceGlobalName.c_str()));
1566+
1567+
if (offset + count > DeviceGlobalSize)
1568+
return UR_RESULT_ERROR_INVALID_VALUE;
1569+
1570+
return urEnqueueUSMMemcpy(
1571+
hQueue, blockingWrite,
1572+
reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(DeviceGlobal) +
1573+
offset),
1574+
pSrc, count, numEventsInWaitList, phEventWaitList, phEvent);
1575+
} catch (ur_result_t Err) {
1576+
Result = Err;
1577+
}
1578+
return Result;
15511579
}
15521580

15531581
UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableRead(
1554-
ur_queue_handle_t, ur_program_handle_t, const char *, bool, size_t, size_t,
1555-
void *, uint32_t, const ur_event_handle_t *, ur_event_handle_t *) {
1556-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
1582+
ur_queue_handle_t hQueue, ur_program_handle_t hProgram, const char *name,
1583+
bool blockingRead, size_t count, size_t offset, void *pDst,
1584+
uint32_t numEventsInWaitList, const ur_event_handle_t *phEventWaitList,
1585+
ur_event_handle_t *phEvent) {
1586+
// Since HIP requires a the global variable to be referenced by name, we use
1587+
// metadata to find the correct name to access it by.
1588+
auto DeviceGlobalNameIt = hProgram->GlobalIDMD.find(name);
1589+
if (DeviceGlobalNameIt == hProgram->GlobalIDMD.end())
1590+
return UR_RESULT_ERROR_INVALID_VALUE;
1591+
std::string DeviceGlobalName = DeviceGlobalNameIt->second;
1592+
1593+
ur_result_t Result = UR_RESULT_SUCCESS;
1594+
try {
1595+
hipDeviceptr_t DeviceGlobal = 0;
1596+
size_t DeviceGlobalSize = 0;
1597+
UR_CHECK_ERROR(hipModuleGetGlobal(&DeviceGlobal, &DeviceGlobalSize,
1598+
hProgram->get(),
1599+
DeviceGlobalName.c_str()));
1600+
1601+
if (offset + count > DeviceGlobalSize)
1602+
return UR_RESULT_ERROR_INVALID_VALUE;
1603+
1604+
return urEnqueueUSMMemcpy(
1605+
hQueue, blockingRead, pDst,
1606+
reinterpret_cast<const void *>(
1607+
reinterpret_cast<uint8_t *>(DeviceGlobal) + offset),
1608+
count, numEventsInWaitList, phEventWaitList, phEvent);
1609+
} catch (ur_result_t Err) {
1610+
Result = Err;
1611+
}
1612+
return Result;
15571613
}
15581614

15591615
UR_APIEXPORT ur_result_t UR_APICALL urEnqueueReadHostPipe(

source/adapters/hip/program.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,35 @@ void getCoMgrBuildLog(const amd_comgr_data_set_t BuildDataSet, char *BuildLog,
7878
} // namespace
7979
#endif
8080

81+
std::pair<std::string, std::string>
82+
splitMetadataName(const std::string &metadataName) {
83+
size_t splitPos = metadataName.rfind('@');
84+
if (splitPos == std::string::npos)
85+
return std::make_pair(metadataName, std::string{});
86+
return std::make_pair(metadataName.substr(0, splitPos),
87+
metadataName.substr(splitPos, metadataName.length()));
88+
}
89+
8190
ur_result_t
8291
ur_program_handle_t_::setMetadata(const ur_program_metadata_t *Metadata,
8392
size_t Length) {
8493
for (size_t i = 0; i < Length; ++i) {
8594
const ur_program_metadata_t MetadataElement = Metadata[i];
8695
std::string MetadataElementName{MetadataElement.pName};
8796

97+
auto [Prefix, Tag] = splitMetadataName(MetadataElementName);
98+
8899
if (MetadataElementName ==
89100
__SYCL_UR_PROGRAM_METADATA_TAG_NEED_FINALIZATION) {
90101
assert(MetadataElement.type == UR_PROGRAM_METADATA_TYPE_UINT32);
91102
IsRelocatable = MetadataElement.value.data32;
103+
} else if (Tag == __SYCL_UR_PROGRAM_METADATA_GLOBAL_ID_MAPPING) {
104+
const char *MetadataValPtr =
105+
reinterpret_cast<const char *>(MetadataElement.value.pData) +
106+
sizeof(std::uint64_t);
107+
const char *MetadataValPtrEnd =
108+
MetadataValPtr + MetadataElement.size - sizeof(std::uint64_t);
109+
GlobalIDMD[Prefix] = std::string{MetadataValPtr, MetadataValPtrEnd};
92110
}
93111
}
94112
return UR_RESULT_SUCCESS;

source/adapters/hip/program.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ struct ur_program_handle_t_ {
2929
// Metadata
3030
bool IsRelocatable = false;
3131

32+
std::unordered_map<std::string, std::string> GlobalIDMD;
33+
3234
constexpr static size_t MAX_LOG_SIZE = 8192u;
3335

3436
char ErrorLog[MAX_LOG_SIZE], InfoLog[MAX_LOG_SIZE];

0 commit comments

Comments
 (0)