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

[DRAFT][CI Run Only] Fabio/hip global variable #1263

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions include/ur_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ typedef enum ur_function_t {
UR_FUNCTION_COMMAND_BUFFER_APPEND_USM_ADVISE_EXP = 213, ///< Enumerator for ::urCommandBufferAppendUSMAdviseExp
UR_FUNCTION_ENQUEUE_COOPERATIVE_KERNEL_LAUNCH_EXP = 214, ///< Enumerator for ::urEnqueueCooperativeKernelLaunchExp
UR_FUNCTION_KERNEL_SUGGEST_MAX_COOPERATIVE_GROUP_COUNT_EXP = 215, ///< Enumerator for ::urKernelSuggestMaxCooperativeGroupCountExp
UR_FUNCTION_PROGRAM_GET_GLOBAL_VARIABLE_POINTER = 216, ///< Enumerator for ::urProgramGetGlobalVariablePointer
/// @cond
UR_FUNCTION_FORCE_UINT32 = 0x7fffffff
/// @endcond
Expand Down Expand Up @@ -4262,6 +4263,42 @@ urProgramGetFunctionPointer(
void **ppFunctionPointer ///< [out] Returns the pointer to the function if it is found in the program.
);

///////////////////////////////////////////////////////////////////////////////
/// @brief Retrieves a pointer to a device global variable.
///
/// @details
/// - Retrieves a pointer to a device global variable.
/// - The application may call this function from simultaneous threads for
/// the same device.
/// - The implementation of this function should be thread-safe.
///
/// @remarks
/// _Analogues_
/// - **clGetDeviceGlobalVariablePointerINTEL**
///
/// @returns
/// - ::UR_RESULT_SUCCESS
/// - ::UR_RESULT_ERROR_UNINITIALIZED
/// - ::UR_RESULT_ERROR_DEVICE_LOST
/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
/// + `NULL == hDevice`
/// + `NULL == hProgram`
/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER
/// + `NULL == pGlobalVariableName`
/// + `NULL == ppGlobalVariablePointerRet`
/// - ::UR_RESULT_ERROR_INVALID_VALUE
/// + `name` is not a valid variable in the program.
UR_APIEXPORT ur_result_t UR_APICALL
urProgramGetGlobalVariablePointer(
ur_device_handle_t hDevice, ///< [in] handle of the device to retrieve the pointer for.
ur_program_handle_t hProgram, ///< [in] handle of the program where the global variable is.
const char *pGlobalVariableName, ///< [in] mangled name of the global variable to retrieve the pointer for.
size_t *pGlobalVariableSizeRet, ///< [out][optional] Returns the size of the global variable if it is found
///< in the program.
void **ppGlobalVariablePointerRet ///< [out] Returns the pointer to the global variable if it is found in the program.
);

///////////////////////////////////////////////////////////////////////////////
/// @brief Get Program object information
typedef enum ur_program_info_t {
Expand Down Expand Up @@ -9144,6 +9181,18 @@ typedef struct ur_program_get_function_pointer_params_t {
void ***pppFunctionPointer;
} ur_program_get_function_pointer_params_t;

///////////////////////////////////////////////////////////////////////////////
/// @brief Function parameters for urProgramGetGlobalVariablePointer
/// @details Each entry is a pointer to the parameter passed to the function;
/// allowing the callback the ability to modify the parameter's value
typedef struct ur_program_get_global_variable_pointer_params_t {
ur_device_handle_t *phDevice;
ur_program_handle_t *phProgram;
const char **ppGlobalVariableName;
size_t **ppGlobalVariableSizeRet;
void ***pppGlobalVariablePointerRet;
} ur_program_get_global_variable_pointer_params_t;

///////////////////////////////////////////////////////////////////////////////
/// @brief Function parameters for urProgramGetInfo
/// @details Each entry is a pointer to the parameter passed to the function;
Expand Down
10 changes: 10 additions & 0 deletions include/ur_ddi.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,15 @@ typedef ur_result_t(UR_APICALL *ur_pfnProgramGetFunctionPointer_t)(
const char *,
void **);

///////////////////////////////////////////////////////////////////////////////
/// @brief Function-pointer for urProgramGetGlobalVariablePointer
typedef ur_result_t(UR_APICALL *ur_pfnProgramGetGlobalVariablePointer_t)(
ur_device_handle_t,
ur_program_handle_t,
const char *,
size_t *,
void **);

///////////////////////////////////////////////////////////////////////////////
/// @brief Function-pointer for urProgramGetInfo
typedef ur_result_t(UR_APICALL *ur_pfnProgramGetInfo_t)(
Expand Down Expand Up @@ -380,6 +389,7 @@ typedef struct ur_program_dditable_t {
ur_pfnProgramRetain_t pfnRetain;
ur_pfnProgramRelease_t pfnRelease;
ur_pfnProgramGetFunctionPointer_t pfnGetFunctionPointer;
ur_pfnProgramGetGlobalVariablePointer_t pfnGetGlobalVariablePointer;
ur_pfnProgramGetInfo_t pfnGetInfo;
ur_pfnProgramGetBuildInfo_t pfnGetBuildInfo;
ur_pfnProgramSetSpecializationConstants_t pfnSetSpecializationConstants;
Expand Down
44 changes: 44 additions & 0 deletions include/ur_print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,9 @@ inline std::ostream &operator<<(std::ostream &os, ur_function_t value) {
case UR_FUNCTION_KERNEL_SUGGEST_MAX_COOPERATIVE_GROUP_COUNT_EXP:
os << "UR_FUNCTION_KERNEL_SUGGEST_MAX_COOPERATIVE_GROUP_COUNT_EXP";
break;
case UR_FUNCTION_PROGRAM_GET_GLOBAL_VARIABLE_POINTER:
os << "UR_FUNCTION_PROGRAM_GET_GLOBAL_VARIABLE_POINTER";
break;
default:
os << "unknown enumerator";
break;
Expand Down Expand Up @@ -10257,6 +10260,44 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
return os;
}

///////////////////////////////////////////////////////////////////////////////
/// @brief Print operator for the ur_program_get_global_variable_pointer_params_t type
/// @returns
/// std::ostream &
inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct ur_program_get_global_variable_pointer_params_t *params) {

os << ".hDevice = ";

ur::details::printPtr(os,
*(params->phDevice));

os << ", ";
os << ".hProgram = ";

ur::details::printPtr(os,
*(params->phProgram));

os << ", ";
os << ".pGlobalVariableName = ";

ur::details::printPtr(os,
*(params->ppGlobalVariableName));

os << ", ";
os << ".pGlobalVariableSizeRet = ";

ur::details::printPtr(os,
*(params->ppGlobalVariableSizeRet));

os << ", ";
os << ".ppGlobalVariablePointerRet = ";

ur::details::printPtr(os,
*(params->pppGlobalVariablePointerRet));

return os;
}

///////////////////////////////////////////////////////////////////////////////
/// @brief Print operator for the ur_program_get_info_params_t type
/// @returns
Expand Down Expand Up @@ -15992,6 +16033,9 @@ inline ur_result_t UR_APICALL printFunctionParams(std::ostream &os, ur_function_
case UR_FUNCTION_PROGRAM_GET_FUNCTION_POINTER: {
os << (const struct ur_program_get_function_pointer_params_t *)params;
} break;
case UR_FUNCTION_PROGRAM_GET_GLOBAL_VARIABLE_POINTER: {
os << (const struct ur_program_get_global_variable_pointer_params_t *)params;
} break;
case UR_FUNCTION_PROGRAM_GET_INFO: {
os << (const struct ur_program_get_info_params_t *)params;
} break;
Expand Down
37 changes: 37 additions & 0 deletions scripts/core/program.yml
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,43 @@ params:
desc: |
[out] Returns the pointer to the function if it is found in the program.
--- #--------------------------------------------------------------------------
type: function
desc: "Retrieves a pointer to a device global variable."
class: $xProgram
name: GetGlobalVariablePointer
decl: static
ordinal: "7"
analogue:
- "**clGetDeviceGlobalVariablePointerINTEL**"
details:
- "Retrieves a pointer to a device global variable."
- "The application may call this function from simultaneous threads for the same device."
- "The implementation of this function should be thread-safe."
params:
- type: "$x_device_handle_t"
name: hDevice
desc: |
[in] handle of the device to retrieve the pointer for.
- type: "$x_program_handle_t"
name: hProgram
desc: |
[in] handle of the program where the global variable is.
- type: "const char*"
name: pGlobalVariableName
desc: |
[in] mangled name of the global variable to retrieve the pointer for.
- type: "size_t*"
name: pGlobalVariableSizeRet
desc: |
[out][optional] Returns the size of the global variable if it is found in the program.
- type: "void**"
name: ppGlobalVariablePointerRet
desc: |
[out] Returns the pointer to the global variable if it is found in the program.
returns:
- $X_RESULT_ERROR_INVALID_VALUE:
- "`name` is not a valid variable in the program."
--- #--------------------------------------------------------------------------
type: enum
desc: "Get Program object information"
class: $xProgram
Expand Down
3 changes: 3 additions & 0 deletions scripts/core/registry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,9 @@ etors:
- name: KERNEL_SUGGEST_MAX_COOPERATIVE_GROUP_COUNT_EXP
desc: Enumerator for $xKernelSuggestMaxCooperativeGroupCountExp
value: '215'
- name: PROGRAM_GET_GLOBAL_VARIABLE_POINTER
desc: Enumerator for $xProgramGetGlobalVariablePointer
value: '216'
---
type: enum
desc: Defines structure types
Expand Down
32 changes: 6 additions & 26 deletions source/adapters/cuda/enqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1650,20 +1650,11 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableWrite(
bool blockingWrite, size_t count, size_t offset, const void *pSrc,
uint32_t numEventsInWaitList, const ur_event_handle_t *phEventWaitList,
ur_event_handle_t *phEvent) {
// Since CUDA requires a the global variable to be referenced by name, we use
// metadata to find the correct name to access it by.
auto DeviceGlobalNameIt = hProgram->GlobalIDMD.find(name);
if (DeviceGlobalNameIt == hProgram->GlobalIDMD.end())
return UR_RESULT_ERROR_INVALID_VALUE;
std::string DeviceGlobalName = DeviceGlobalNameIt->second;

ur_result_t Result = UR_RESULT_SUCCESS;
try {
CUdeviceptr DeviceGlobal = 0;
size_t DeviceGlobalSize = 0;
UR_CHECK_ERROR(cuModuleGetGlobal(&DeviceGlobal, &DeviceGlobalSize,
hProgram->get(),
DeviceGlobalName.c_str()));
UR_CHECK_ERROR(hProgram->getGlobalVariablePointer(name, &DeviceGlobal,
&DeviceGlobalSize));

if (offset + count > DeviceGlobalSize)
return UR_RESULT_ERROR_INVALID_VALUE;
Expand All @@ -1672,30 +1663,20 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableWrite(
hQueue, blockingWrite, reinterpret_cast<void *>(DeviceGlobal + offset),
pSrc, count, numEventsInWaitList, phEventWaitList, phEvent);
} catch (ur_result_t Err) {
Result = Err;
return Err;
}
return Result;
}

UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableRead(
ur_queue_handle_t hQueue, ur_program_handle_t hProgram, const char *name,
bool blockingRead, size_t count, size_t offset, void *pDst,
uint32_t numEventsInWaitList, const ur_event_handle_t *phEventWaitList,
ur_event_handle_t *phEvent) {
// Since CUDA requires a the global variable to be referenced by name, we use
// metadata to find the correct name to access it by.
auto DeviceGlobalNameIt = hProgram->GlobalIDMD.find(name);
if (DeviceGlobalNameIt == hProgram->GlobalIDMD.end())
return UR_RESULT_ERROR_INVALID_VALUE;
std::string DeviceGlobalName = DeviceGlobalNameIt->second;

ur_result_t Result = UR_RESULT_SUCCESS;
try {
CUdeviceptr DeviceGlobal = 0;
size_t DeviceGlobalSize = 0;
UR_CHECK_ERROR(cuModuleGetGlobal(&DeviceGlobal, &DeviceGlobalSize,
hProgram->get(),
DeviceGlobalName.c_str()));
UR_CHECK_ERROR(hProgram->getGlobalVariablePointer(name, &DeviceGlobal,
&DeviceGlobalSize));

if (offset + count > DeviceGlobalSize)
return UR_RESULT_ERROR_INVALID_VALUE;
Expand All @@ -1705,9 +1686,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableRead(
reinterpret_cast<const void *>(DeviceGlobal + offset), count,
numEventsInWaitList, phEventWaitList, phEvent);
} catch (ur_result_t Err) {
Result = Err;
return Err;
}
return Result;
}

/// Host Pipes
Expand Down
29 changes: 29 additions & 0 deletions source/adapters/cuda/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,25 @@ ur_result_t ur_program_handle_t_::buildProgram(const char *BuildOptions) {
return UR_RESULT_SUCCESS;
}

ur_result_t ur_program_handle_t_::getGlobalVariablePointer(
const char *name, CUdeviceptr *DeviceGlobal, size_t *DeviceGlobalSize) {
/* Since CUDA requires a global variable to be referenced by name, we use
* metadata to find the correct name to access it by. */
auto DeviceGlobalNameIt = this->GlobalIDMD.find(name);
if (DeviceGlobalNameIt == this->GlobalIDMD.end())
return UR_RESULT_ERROR_INVALID_VALUE;
std::string DeviceGlobalName = DeviceGlobalNameIt->second;

try {
UR_CHECK_ERROR(cuModuleGetGlobal(DeviceGlobal, DeviceGlobalSize,
this->get(), DeviceGlobalName.c_str()));
} catch (ur_result_t Err) {
return Err;
}

return UR_RESULT_SUCCESS;
}

/// Finds kernel names by searching for entry points in the PTX source, as the
/// CUDA driver API doesn't expose an operation for this.
/// Note: This is currently only being used by the SYCL program class for the
Expand Down Expand Up @@ -489,3 +508,13 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramGetFunctionPointer(

return Result;
}

UR_APIEXPORT ur_result_t UR_APICALL urProgramGetGlobalVariablePointer(
ur_device_handle_t, ur_program_handle_t hProgram,
const char *pGlobalVariableName, size_t *pGlobalVariableSizeRet,
void **ppGlobalVariablePointerRet) {
return hProgram->getGlobalVariablePointer(
pGlobalVariableName,
reinterpret_cast<CUdeviceptr *>(ppGlobalVariablePointerRet),
pGlobalVariableSizeRet);
}
4 changes: 4 additions & 0 deletions source/adapters/cuda/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ struct ur_program_handle_t_ {
uint32_t decrementReferenceCount() noexcept { return --RefCount; }

uint32_t getReferenceCount() const noexcept { return RefCount; }

ur_result_t getGlobalVariablePointer(const char *name,
CUdeviceptr *DeviceGlobal,
size_t *DeviceGlobalSize);
};
1 change: 1 addition & 0 deletions source/adapters/cuda/ur_interface_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetProgramProcAddrTable(
pDdiTable->pfnCreateWithNativeHandle = urProgramCreateWithNativeHandle;
pDdiTable->pfnGetBuildInfo = urProgramGetBuildInfo;
pDdiTable->pfnGetFunctionPointer = urProgramGetFunctionPointer;
pDdiTable->pfnGetGlobalVariablePointer = urProgramGetGlobalVariablePointer;
pDdiTable->pfnGetInfo = urProgramGetInfo;
pDdiTable->pfnGetNativeHandle = urProgramGetNativeHandle;
pDdiTable->pfnLink = urProgramLink;
Expand Down
13 changes: 3 additions & 10 deletions source/adapters/hip/enqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1689,19 +1689,12 @@ ur_result_t deviceGlobalCopyHelper(
bool blocking, size_t count, size_t offset, void *ptr,
uint32_t numEventsInWaitList, const ur_event_handle_t *phEventWaitList,
ur_event_handle_t *phEvent, GlobalVariableCopy CopyType) {
// Since HIP requires a the global variable to be referenced by name, we use
// metadata to find the correct name to access it by.
auto DeviceGlobalNameIt = hProgram->GlobalIDMD.find(name);
if (DeviceGlobalNameIt == hProgram->GlobalIDMD.end())
return UR_RESULT_ERROR_INVALID_VALUE;
std::string DeviceGlobalName = DeviceGlobalNameIt->second;

try {
hipDeviceptr_t DeviceGlobal = 0;
hipDeviceptr_t DeviceGlobal = nullptr;
size_t DeviceGlobalSize = 0;
UR_CHECK_ERROR(hipModuleGetGlobal(&DeviceGlobal, &DeviceGlobalSize,
hProgram->get(),
DeviceGlobalName.c_str()));
UR_CHECK_ERROR(hProgram->getGlobalVariablePointer(name, &DeviceGlobal,
&DeviceGlobalSize));

if (offset + count > DeviceGlobalSize)
return UR_RESULT_ERROR_INVALID_VALUE;
Expand Down
27 changes: 27 additions & 0 deletions source/adapters/hip/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,25 @@ ur_result_t ur_program_handle_t_::buildProgram(const char *BuildOptions) {
return UR_RESULT_SUCCESS;
}

ur_result_t ur_program_handle_t_::getGlobalVariablePointer(
const char *name, hipDeviceptr_t *DeviceGlobal, size_t *DeviceGlobalSize) {
// Since HIP requires a the global variable to be referenced by name, we use
// metadata to find the correct name to access it by.
auto DeviceGlobalNameIt = this->GlobalIDMD.find(name);
if (DeviceGlobalNameIt == this->GlobalIDMD.end())
return UR_RESULT_ERROR_INVALID_VALUE;
std::string DeviceGlobalName = DeviceGlobalNameIt->second;

try {
UR_CHECK_ERROR(hipModuleGetGlobal(DeviceGlobal, DeviceGlobalSize,
this->get(), DeviceGlobalName.c_str()));
} catch (ur_result_t Err) {
return Err;
}

return UR_RESULT_SUCCESS;
}

/// Finds kernel names by searching for entry points in the PTX source, as the
/// HIP driver API doesn't expose an operation for this.
/// Note: This is currently only being used by the SYCL program class for the
Expand Down Expand Up @@ -495,3 +514,11 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramGetFunctionPointer(

return Result;
}

UR_APIEXPORT ur_result_t UR_APICALL urProgramGetGlobalVariablePointer(
ur_device_handle_t, ur_program_handle_t hProgram,
const char *pGlobalVariableName, size_t *pGlobalVariableSizeRet,
void **ppGlobalVariablePointerRet) {
return hProgram->getGlobalVariablePointer(
pGlobalVariableName, ppGlobalVariablePointerRet, pGlobalVariableSizeRet);
}
Loading
Loading