Skip to content

Commit 3d0fe19

Browse files
Merge pull request #921 from vinser52/svinogra_cuda_params
Update Cuda provider config API
2 parents 0dd3648 + d3a1398 commit 3d0fe19

File tree

12 files changed

+538
-84
lines changed

12 files changed

+538
-84
lines changed

examples/cuda_shared_memory/cuda_shared_memory.c

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,51 @@ int main(void) {
4343
// Create a context on the device
4444
cuCtxCreate(&cuContext, 0, cuDevice);
4545

46-
// Setup parameters for the CUDA memory provider. It will be used for
46+
// Setup parameters for the CUDA Memory Provider. It will be used for
4747
// allocating memory from CUDA devices.
48-
cuda_memory_provider_params_t cu_memory_provider_params;
49-
cu_memory_provider_params.cuda_context_handle = cuContext;
50-
cu_memory_provider_params.cuda_device_handle = cuDevice;
48+
umf_cuda_memory_provider_params_handle_t cu_memory_provider_params = NULL;
49+
res = umfCUDAMemoryProviderParamsCreate(&cu_memory_provider_params);
50+
if (res != UMF_RESULT_SUCCESS) {
51+
fprintf(stderr, "Failed to create memory provider params!\n");
52+
ret = -1;
53+
goto cuda_destroy;
54+
}
55+
56+
res = umfCUDAMemoryProviderParamsSetContext(cu_memory_provider_params,
57+
cuContext);
58+
if (res != UMF_RESULT_SUCCESS) {
59+
fprintf(stderr, "Failed to set context in memory provider params!\n");
60+
ret = -1;
61+
goto provider_params_destroy;
62+
}
63+
64+
res = umfCUDAMemoryProviderParamsSetDevice(cu_memory_provider_params,
65+
cuDevice);
66+
if (res != UMF_RESULT_SUCCESS) {
67+
fprintf(stderr, "Failed to set device in memory provider params!\n");
68+
ret = -1;
69+
goto provider_params_destroy;
70+
}
5171
// Set the memory type to shared to allow the memory to be accessed on both
5272
// CPU and GPU.
53-
cu_memory_provider_params.memory_type = UMF_MEMORY_TYPE_SHARED;
73+
res = umfCUDAMemoryProviderParamsSetMemoryType(cu_memory_provider_params,
74+
UMF_MEMORY_TYPE_SHARED);
75+
if (res != UMF_RESULT_SUCCESS) {
76+
fprintf(stderr,
77+
"Failed to set memory type in memory provider params!\n");
78+
ret = -1;
79+
goto provider_params_destroy;
80+
}
5481

5582
// Create CUDA memory provider
5683
umf_memory_provider_handle_t cu_memory_provider;
57-
res = umfMemoryProviderCreate(umfCUDAMemoryProviderOps(),
58-
&cu_memory_provider_params,
59-
&cu_memory_provider);
84+
res =
85+
umfMemoryProviderCreate(umfCUDAMemoryProviderOps(),
86+
cu_memory_provider_params, &cu_memory_provider);
6087
if (res != UMF_RESULT_SUCCESS) {
6188
fprintf(stderr, "Failed to create a memory provider!\n");
6289
ret = -1;
63-
goto cuda_destroy;
90+
goto provider_params_destroy;
6491
}
6592

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

177+
provider_params_destroy:
178+
umfCUDAMemoryProviderParamsDestroy(cu_memory_provider_params);
179+
150180
cuda_destroy:
151181
ret = cuCtxDestroy(cuContext);
152182
return ret;

include/umf/providers/provider_cuda.h

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,44 @@
1414
extern "C" {
1515
#endif
1616

17-
/// @brief CUDA Memory Provider settings struct
18-
typedef struct cuda_memory_provider_params_t {
19-
void *cuda_context_handle; ///< Handle to the CUDA context
20-
int cuda_device_handle; ///< Handle to the CUDA device
21-
umf_usm_memory_type_t memory_type; ///< Allocation memory type
22-
} cuda_memory_provider_params_t;
17+
struct umf_cuda_memory_provider_params_t;
18+
19+
typedef struct umf_cuda_memory_provider_params_t
20+
*umf_cuda_memory_provider_params_handle_t;
21+
22+
/// @brief Create a struct to store parameters of the CUDA Memory Provider.
23+
/// @param hParams [out] handle to the newly created parameters struct.
24+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
25+
umf_result_t umfCUDAMemoryProviderParamsCreate(
26+
umf_cuda_memory_provider_params_handle_t *hParams);
27+
28+
/// @brief Destroy parameters struct.
29+
/// @param hParams handle to the parameters of the CUDA Memory Provider.
30+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
31+
umf_result_t umfCUDAMemoryProviderParamsDestroy(
32+
umf_cuda_memory_provider_params_handle_t hParams);
33+
34+
/// @brief Set the CUDA context handle in the parameters struct.
35+
/// @param hParams handle to the parameters of the CUDA Memory Provider.
36+
/// @param hContext handle to the CUDA context.
37+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
38+
umf_result_t umfCUDAMemoryProviderParamsSetContext(
39+
umf_cuda_memory_provider_params_handle_t hParams, void *hContext);
40+
41+
/// @brief Set the CUDA device handle in the parameters struct.
42+
/// @param hParams handle to the parameters of the CUDA Memory Provider.
43+
/// @param hDevice handle to the CUDA device.
44+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
45+
umf_result_t umfCUDAMemoryProviderParamsSetDevice(
46+
umf_cuda_memory_provider_params_handle_t hParams, int hDevice);
47+
48+
/// @brief Set the memory type in the parameters struct.
49+
/// @param hParams handle to the parameters of the CUDA Memory Provider.
50+
/// @param memoryType memory type.
51+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
52+
umf_result_t umfCUDAMemoryProviderParamsSetMemoryType(
53+
umf_cuda_memory_provider_params_handle_t hParams,
54+
umf_usm_memory_type_t memoryType);
2355

2456
umf_memory_provider_ops_t *umfCUDAMemoryProviderOps(void);
2557

src/libumf.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ EXPORTS
1717
umfCoarseMemoryProviderGetStats
1818
umfCoarseMemoryProviderOps
1919
umfCUDAMemoryProviderOps
20+
umfCUDAMemoryProviderParamsCreate
21+
umfCUDAMemoryProviderParamsDestroy
22+
umfCUDAMemoryProviderParamsSetContext
23+
umfCUDAMemoryProviderParamsSetDevice
24+
umfCUDAMemoryProviderParamsSetMemoryType
2025
umfDevDaxMemoryProviderOps
2126
umfFree
2227
umfFileMemoryProviderOps

src/libumf.map

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ UMF_1.0 {
1111
umfCoarseMemoryProviderGetStats;
1212
umfCoarseMemoryProviderOps;
1313
umfCUDAMemoryProviderOps;
14+
umfCUDAMemoryProviderParamsCreate;
15+
umfCUDAMemoryProviderParamsDestroy;
16+
umfCUDAMemoryProviderParamsSetContext;
17+
umfCUDAMemoryProviderParamsSetDevice;
18+
umfCUDAMemoryProviderParamsSetMemoryType;
1419
umfDevDaxMemoryProviderOps;
1520
umfFree;
1621
umfFileMemoryProviderOps;

src/provider/provider_cuda.c

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,40 @@
1414

1515
#if defined(UMF_NO_CUDA_PROVIDER)
1616

17+
umf_result_t umfCUDAMemoryProviderParamsCreate(
18+
umf_cuda_memory_provider_params_handle_t *hParams) {
19+
(void)hParams;
20+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
21+
}
22+
23+
umf_result_t umfCUDAMemoryProviderParamsDestroy(
24+
umf_cuda_memory_provider_params_handle_t hParams) {
25+
(void)hParams;
26+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
27+
}
28+
29+
umf_result_t umfCUDAMemoryProviderParamsSetContext(
30+
umf_cuda_memory_provider_params_handle_t hParams, void *hContext) {
31+
(void)hParams;
32+
(void)hContext;
33+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
34+
}
35+
36+
umf_result_t umfCUDAMemoryProviderParamsSetDevice(
37+
umf_cuda_memory_provider_params_handle_t hParams, int hDevice) {
38+
(void)hParams;
39+
(void)hDevice;
40+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
41+
}
42+
43+
umf_result_t umfCUDAMemoryProviderParamsSetMemoryType(
44+
umf_cuda_memory_provider_params_handle_t hParams,
45+
umf_usm_memory_type_t memoryType) {
46+
(void)hParams;
47+
(void)memoryType;
48+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
49+
}
50+
1751
umf_memory_provider_ops_t *umfCUDAMemoryProviderOps(void) {
1852
// not supported
1953
return NULL;
@@ -48,6 +82,13 @@ typedef struct cu_memory_provider_t {
4882
size_t min_alignment;
4983
} cu_memory_provider_t;
5084

85+
// CUDA Memory Provider settings struct
86+
typedef struct umf_cuda_memory_provider_params_t {
87+
void *cuda_context_handle; ///< Handle to the CUDA context
88+
int cuda_device_handle; ///< Handle to the CUDA device
89+
umf_usm_memory_type_t memory_type; ///< Allocation memory type
90+
} umf_cuda_memory_provider_params_t;
91+
5192
typedef struct cu_ops_t {
5293
CUresult (*cuMemGetAllocationGranularity)(
5394
size_t *granularity, const CUmemAllocationProp *prop,
@@ -158,14 +199,81 @@ static void init_cu_global_state(void) {
158199
}
159200
}
160201

202+
umf_result_t umfCUDAMemoryProviderParamsCreate(
203+
umf_cuda_memory_provider_params_handle_t *hParams) {
204+
if (!hParams) {
205+
LOG_ERR("CUDA Memory Provider params handle is NULL");
206+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
207+
}
208+
209+
umf_cuda_memory_provider_params_handle_t params_data =
210+
umf_ba_global_alloc(sizeof(umf_cuda_memory_provider_params_t));
211+
if (!params_data) {
212+
LOG_ERR("Cannot allocate memory for CUDA Memory Provider params");
213+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
214+
}
215+
216+
params_data->cuda_context_handle = NULL;
217+
params_data->cuda_device_handle = -1;
218+
params_data->memory_type = UMF_MEMORY_TYPE_UNKNOWN;
219+
220+
*hParams = params_data;
221+
222+
return UMF_RESULT_SUCCESS;
223+
}
224+
225+
umf_result_t umfCUDAMemoryProviderParamsDestroy(
226+
umf_cuda_memory_provider_params_handle_t hParams) {
227+
umf_ba_global_free(hParams);
228+
229+
return UMF_RESULT_SUCCESS;
230+
}
231+
232+
umf_result_t umfCUDAMemoryProviderParamsSetContext(
233+
umf_cuda_memory_provider_params_handle_t hParams, void *hContext) {
234+
if (!hParams) {
235+
LOG_ERR("CUDA Memory Provider params handle is NULL");
236+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
237+
}
238+
239+
hParams->cuda_context_handle = hContext;
240+
241+
return UMF_RESULT_SUCCESS;
242+
}
243+
244+
umf_result_t umfCUDAMemoryProviderParamsSetDevice(
245+
umf_cuda_memory_provider_params_handle_t hParams, int hDevice) {
246+
if (!hParams) {
247+
LOG_ERR("CUDA Memory Provider params handle is NULL");
248+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
249+
}
250+
251+
hParams->cuda_device_handle = hDevice;
252+
253+
return UMF_RESULT_SUCCESS;
254+
}
255+
256+
umf_result_t umfCUDAMemoryProviderParamsSetMemoryType(
257+
umf_cuda_memory_provider_params_handle_t hParams,
258+
umf_usm_memory_type_t memoryType) {
259+
if (!hParams) {
260+
LOG_ERR("CUDA Memory Provider params handle is NULL");
261+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
262+
}
263+
264+
hParams->memory_type = memoryType;
265+
266+
return UMF_RESULT_SUCCESS;
267+
}
268+
161269
static umf_result_t cu_memory_provider_initialize(void *params,
162270
void **provider) {
163271
if (params == NULL) {
164272
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
165273
}
166274

167-
cuda_memory_provider_params_t *cu_params =
168-
(cuda_memory_provider_params_t *)params;
275+
umf_cuda_memory_provider_params_handle_t cu_params =
276+
(umf_cuda_memory_provider_params_handle_t)params;
169277

170278
if (cu_params->memory_type == UMF_MEMORY_TYPE_UNKNOWN ||
171279
cu_params->memory_type > UMF_MEMORY_TYPE_SHARED) {

0 commit comments

Comments
 (0)