Skip to content
Merged
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
48 changes: 39 additions & 9 deletions examples/cuda_shared_memory/cuda_shared_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,51 @@ int main(void) {
// Create a context on the device
cuCtxCreate(&cuContext, 0, cuDevice);

// Setup parameters for the CUDA memory provider. It will be used for
// Setup parameters for the CUDA Memory Provider. It will be used for
// allocating memory from CUDA devices.
cuda_memory_provider_params_t cu_memory_provider_params;
cu_memory_provider_params.cuda_context_handle = cuContext;
cu_memory_provider_params.cuda_device_handle = cuDevice;
umf_cuda_memory_provider_params_handle_t cu_memory_provider_params = NULL;
res = umfCUDAMemoryProviderParamsCreate(&cu_memory_provider_params);
if (res != UMF_RESULT_SUCCESS) {
fprintf(stderr, "Failed to create memory provider params!\n");
ret = -1;
goto cuda_destroy;
}

res = umfCUDAMemoryProviderParamsSetContext(cu_memory_provider_params,
cuContext);
if (res != UMF_RESULT_SUCCESS) {
fprintf(stderr, "Failed to set context in memory provider params!\n");
ret = -1;
goto provider_params_destroy;
}

res = umfCUDAMemoryProviderParamsSetDevice(cu_memory_provider_params,
cuDevice);
if (res != UMF_RESULT_SUCCESS) {
fprintf(stderr, "Failed to set device in memory provider params!\n");
ret = -1;
goto provider_params_destroy;
}
// Set the memory type to shared to allow the memory to be accessed on both
// CPU and GPU.
cu_memory_provider_params.memory_type = UMF_MEMORY_TYPE_SHARED;
res = umfCUDAMemoryProviderParamsSetMemoryType(cu_memory_provider_params,
UMF_MEMORY_TYPE_SHARED);
if (res != UMF_RESULT_SUCCESS) {
fprintf(stderr,
"Failed to set memory type in memory provider params!\n");
ret = -1;
goto provider_params_destroy;
}

// Create CUDA memory provider
umf_memory_provider_handle_t cu_memory_provider;
res = umfMemoryProviderCreate(umfCUDAMemoryProviderOps(),
&cu_memory_provider_params,
&cu_memory_provider);
res =
umfMemoryProviderCreate(umfCUDAMemoryProviderOps(),
cu_memory_provider_params, &cu_memory_provider);
if (res != UMF_RESULT_SUCCESS) {
fprintf(stderr, "Failed to create a memory provider!\n");
ret = -1;
goto cuda_destroy;
goto provider_params_destroy;
}

printf("CUDA memory provider created at %p\n", (void *)cu_memory_provider);
Expand Down Expand Up @@ -147,6 +174,9 @@ int main(void) {
memory_provider_destroy:
umfMemoryProviderDestroy(cu_memory_provider);

provider_params_destroy:
umfCUDAMemoryProviderParamsDestroy(cu_memory_provider_params);

cuda_destroy:
ret = cuCtxDestroy(cuContext);
return ret;
Expand Down
44 changes: 38 additions & 6 deletions include/umf/providers/provider_cuda.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,44 @@
extern "C" {
#endif

/// @brief CUDA Memory Provider settings struct
typedef struct cuda_memory_provider_params_t {
void *cuda_context_handle; ///< Handle to the CUDA context
int cuda_device_handle; ///< Handle to the CUDA device
umf_usm_memory_type_t memory_type; ///< Allocation memory type
} cuda_memory_provider_params_t;
struct umf_cuda_memory_provider_params_t;

typedef struct umf_cuda_memory_provider_params_t
*umf_cuda_memory_provider_params_handle_t;

/// @brief Create a struct to store parameters of the CUDA Memory Provider.
/// @param hParams [out] handle to the newly created parameters struct.
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
umf_result_t umfCUDAMemoryProviderParamsCreate(
umf_cuda_memory_provider_params_handle_t *hParams);

/// @brief Destroy parameters struct.
/// @param hParams handle to the parameters of the CUDA Memory Provider.
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
umf_result_t umfCUDAMemoryProviderParamsDestroy(
umf_cuda_memory_provider_params_handle_t hParams);

/// @brief Set the CUDA context handle in the parameters struct.
/// @param hParams handle to the parameters of the CUDA Memory Provider.
/// @param hContext handle to the CUDA context.
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
umf_result_t umfCUDAMemoryProviderParamsSetContext(
umf_cuda_memory_provider_params_handle_t hParams, void *hContext);

/// @brief Set the CUDA device handle in the parameters struct.
/// @param hParams handle to the parameters of the CUDA Memory Provider.
/// @param hDevice handle to the CUDA device.
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
umf_result_t umfCUDAMemoryProviderParamsSetDevice(
umf_cuda_memory_provider_params_handle_t hParams, int hDevice);

/// @brief Set the memory type in the parameters struct.
/// @param hParams handle to the parameters of the CUDA Memory Provider.
/// @param memoryType memory type.
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
umf_result_t umfCUDAMemoryProviderParamsSetMemoryType(
umf_cuda_memory_provider_params_handle_t hParams,
umf_usm_memory_type_t memoryType);

umf_memory_provider_ops_t *umfCUDAMemoryProviderOps(void);

Expand Down
5 changes: 5 additions & 0 deletions src/libumf.def
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ EXPORTS
umfCoarseMemoryProviderGetStats
umfCoarseMemoryProviderOps
umfCUDAMemoryProviderOps
umfCUDAMemoryProviderParamsCreate
umfCUDAMemoryProviderParamsDestroy
umfCUDAMemoryProviderParamsSetContext
umfCUDAMemoryProviderParamsSetDevice
umfCUDAMemoryProviderParamsSetMemoryType
umfDevDaxMemoryProviderOps
umfFree
umfFileMemoryProviderOps
Expand Down
5 changes: 5 additions & 0 deletions src/libumf.map
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ UMF_1.0 {
umfCoarseMemoryProviderGetStats;
umfCoarseMemoryProviderOps;
umfCUDAMemoryProviderOps;
umfCUDAMemoryProviderParamsCreate;
umfCUDAMemoryProviderParamsDestroy;
umfCUDAMemoryProviderParamsSetContext;
umfCUDAMemoryProviderParamsSetDevice;
umfCUDAMemoryProviderParamsSetMemoryType;
umfDevDaxMemoryProviderOps;
umfFree;
umfFileMemoryProviderOps;
Expand Down
112 changes: 110 additions & 2 deletions src/provider/provider_cuda.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,40 @@

#if defined(UMF_NO_CUDA_PROVIDER)

umf_result_t umfCUDAMemoryProviderParamsCreate(
umf_cuda_memory_provider_params_handle_t *hParams) {
(void)hParams;
return UMF_RESULT_ERROR_NOT_SUPPORTED;
}

umf_result_t umfCUDAMemoryProviderParamsDestroy(
umf_cuda_memory_provider_params_handle_t hParams) {
(void)hParams;
return UMF_RESULT_ERROR_NOT_SUPPORTED;
}

umf_result_t umfCUDAMemoryProviderParamsSetContext(
umf_cuda_memory_provider_params_handle_t hParams, void *hContext) {
(void)hParams;
(void)hContext;
return UMF_RESULT_ERROR_NOT_SUPPORTED;
}

umf_result_t umfCUDAMemoryProviderParamsSetDevice(
umf_cuda_memory_provider_params_handle_t hParams, int hDevice) {
(void)hParams;
(void)hDevice;
return UMF_RESULT_ERROR_NOT_SUPPORTED;
}

umf_result_t umfCUDAMemoryProviderParamsSetMemoryType(
umf_cuda_memory_provider_params_handle_t hParams,
umf_usm_memory_type_t memoryType) {
(void)hParams;
(void)memoryType;
return UMF_RESULT_ERROR_NOT_SUPPORTED;
}

umf_memory_provider_ops_t *umfCUDAMemoryProviderOps(void) {
// not supported
return NULL;
Expand Down Expand Up @@ -48,6 +82,13 @@ typedef struct cu_memory_provider_t {
size_t min_alignment;
} cu_memory_provider_t;

// CUDA Memory Provider settings struct
typedef struct umf_cuda_memory_provider_params_t {
void *cuda_context_handle; ///< Handle to the CUDA context
int cuda_device_handle; ///< Handle to the CUDA device
umf_usm_memory_type_t memory_type; ///< Allocation memory type
} umf_cuda_memory_provider_params_t;

typedef struct cu_ops_t {
CUresult (*cuMemGetAllocationGranularity)(
size_t *granularity, const CUmemAllocationProp *prop,
Expand Down Expand Up @@ -158,14 +199,81 @@ static void init_cu_global_state(void) {
}
}

umf_result_t umfCUDAMemoryProviderParamsCreate(
umf_cuda_memory_provider_params_handle_t *hParams) {
if (!hParams) {
LOG_ERR("CUDA Memory Provider params handle is NULL");
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

umf_cuda_memory_provider_params_handle_t params_data =
umf_ba_global_alloc(sizeof(umf_cuda_memory_provider_params_t));
if (!params_data) {
LOG_ERR("Cannot allocate memory for CUDA Memory Provider params");
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}

params_data->cuda_context_handle = NULL;
params_data->cuda_device_handle = -1;
params_data->memory_type = UMF_MEMORY_TYPE_UNKNOWN;

*hParams = params_data;

return UMF_RESULT_SUCCESS;
}

umf_result_t umfCUDAMemoryProviderParamsDestroy(
umf_cuda_memory_provider_params_handle_t hParams) {
umf_ba_global_free(hParams);

return UMF_RESULT_SUCCESS;
}

umf_result_t umfCUDAMemoryProviderParamsSetContext(
umf_cuda_memory_provider_params_handle_t hParams, void *hContext) {
if (!hParams) {
LOG_ERR("CUDA Memory Provider params handle is NULL");
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

hParams->cuda_context_handle = hContext;

return UMF_RESULT_SUCCESS;
}

umf_result_t umfCUDAMemoryProviderParamsSetDevice(
umf_cuda_memory_provider_params_handle_t hParams, int hDevice) {
if (!hParams) {
LOG_ERR("CUDA Memory Provider params handle is NULL");
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

hParams->cuda_device_handle = hDevice;

return UMF_RESULT_SUCCESS;
}

umf_result_t umfCUDAMemoryProviderParamsSetMemoryType(
umf_cuda_memory_provider_params_handle_t hParams,
umf_usm_memory_type_t memoryType) {
if (!hParams) {
LOG_ERR("CUDA Memory Provider params handle is NULL");
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

hParams->memory_type = memoryType;

return UMF_RESULT_SUCCESS;
}

static umf_result_t cu_memory_provider_initialize(void *params,
void **provider) {
if (params == NULL) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

cuda_memory_provider_params_t *cu_params =
(cuda_memory_provider_params_t *)params;
umf_cuda_memory_provider_params_handle_t cu_params =
(umf_cuda_memory_provider_params_handle_t)params;

if (cu_params->memory_type == UMF_MEMORY_TYPE_UNKNOWN ||
cu_params->memory_type > UMF_MEMORY_TYPE_SHARED) {
Expand Down
Loading
Loading