Skip to content

Commit 0d122f7

Browse files
committed
opal/accelerator: Add get_buffer_id API
Signed-off-by: William Zhang <wilzhang@amazon.com>
1 parent 5e5bacd commit 0d122f7

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

opal/mca/accelerator/accelerator.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979

8080
BEGIN_C_DECLS
8181

82-
8382
#define MCA_ACCELERATOR_NO_DEVICE_ID -1
8483
/**
8584
* Accelerator flags
@@ -103,6 +102,8 @@ typedef enum {
103102
MCA_ACCELERATOR_TRANSFER_DTOD,
104103
} opal_accelerator_transfer_type_t;
105104

105+
typedef uint64_t opal_accelerator_buffer_id_t;
106+
106107
struct opal_accelerator_stream_t {
107108
opal_object_t super;
108109
/* Stream object */
@@ -359,6 +360,20 @@ typedef int (*opal_accelerator_base_module_get_device_fn_t)(
359360
typedef int (*opal_accelerator_base_module_device_can_access_peer_fn_t)(
360361
int *access, int dev1, int dev2);
361362

363+
/**
364+
* Retrieves current device id for a device associated with the local process.
365+
* If MCA_ACCELERATOR_NO_DEVICE_ID is provided, there is no device/process pairing.
366+
*
367+
* @param[IN] dev_id ID of the device or MCA_ACCELERATOR_NO_DEVICE_ID
368+
* @param[IN] addr Buffer pointer to check
369+
* @param[OUT] buf_id ID of the given buffer
370+
*
371+
*
372+
* @return OPAL_SUCCESS or error status on failure
373+
*/
374+
typedef int (*opal_accelerator_base_module_get_buffer_id_fn_t)(
375+
int dev_id, const void *addr, opal_accelerator_buffer_id_t *buf_id);
376+
362377
/*
363378
* the standard public API data structure
364379
*/
@@ -384,6 +399,8 @@ typedef struct {
384399

385400
opal_accelerator_base_module_get_device_fn_t get_device;
386401
opal_accelerator_base_module_device_can_access_peer_fn_t device_can_access_peer;
402+
403+
opal_accelerator_base_module_get_buffer_id_fn_t get_buffer_id;
387404
} opal_accelerator_base_module_t;
388405

389406
/**

opal/mca/accelerator/cuda/accelerator_cuda.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ static int accelerator_cuda_host_unregister(int dev_id, void *ptr);
4747
static int accelerator_cuda_get_device(int *dev_id);
4848
static int accelerator_cuda_device_can_access_peer( int *access, int dev1, int dev2);
4949

50+
static int accelerator_cuda_get_buffer_id(int dev_id, const void *addr, opal_accelerator_buffer_id_t *buf_id);
51+
5052
opal_accelerator_base_module_t opal_accelerator_cuda_module =
5153
{
5254
accelerator_cuda_check_addr,
@@ -68,7 +70,9 @@ opal_accelerator_base_module_t opal_accelerator_cuda_module =
6870
accelerator_cuda_host_unregister,
6971

7072
accelerator_cuda_get_device,
71-
accelerator_cuda_device_can_access_peer
73+
accelerator_cuda_device_can_access_peer,
74+
75+
accelerator_cuda_get_buffer_id
7276
};
7377

7478
static int accelerator_cuda_check_addr(const void *addr, int *dev_id, uint64_t *flags)
@@ -538,3 +542,30 @@ static int accelerator_cuda_device_can_access_peer(int *access, int dev1, int de
538542
}
539543
return 0;
540544
}
545+
546+
/*
547+
* Get the buffer ID from the memory.
548+
* This is needed to ensure the cached registration is not stale. If
549+
* we fail to get buffer ID, print an error and set buffer ID to 0.
550+
* Also set SYNC_MEMOPS on any GPU registration to ensure that
551+
* synchronous copies complete before the buffer is accessed.
552+
*/
553+
static int accelerator_cuda_get_buffer_id(int dev_id, const void *addr, opal_accelerator_buffer_id_t *buf_id)
554+
{
555+
CUresult result;
556+
int enable = 1;
557+
result = opal_accelerator_cuda_func.cuPointerGetAttribute((unsigned long long *)buf_id, CU_POINTER_ATTRIBUTE_BUFFER_ID, (CUdeviceptr) addr);
558+
if (OPAL_UNLIKELY(result != CUDA_SUCCESS)) {
559+
opal_show_help("help-accelerator-cuda.txt", "bufferID failed", true, OPAL_PROC_MY_HOSTNAME,
560+
result);
561+
return result;
562+
}
563+
result = opal_accelerator_cuda_func.cuPointerSetAttribute(&enable, CU_POINTER_ATTRIBUTE_SYNC_MEMOPS,
564+
(CUdeviceptr) addr);
565+
if (OPAL_UNLIKELY(CUDA_SUCCESS != result)) {
566+
opal_show_help("help-accelerator-cuda.txt", "cuPointerSetAttribute failed", true,
567+
OPAL_PROC_MY_HOSTNAME, result, addr);
568+
return result;
569+
}
570+
return OPAL_SUCCESS;
571+
}

opal/mca/accelerator/null/accelerator_null_component.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ static int accelerator_null_host_unregister(int dev_id, void *ptr);
6161
static int accelerator_null_get_device(int *dev_id);
6262
static int accelerator_null_device_can_access_peer(int *access, int dev1, int dev2);
6363

64+
static int accelerator_null_get_buffer_id(int dev_id, const void *addr, opal_accelerator_buffer_id_t *buf_id);
65+
6466
/*
6567
* Instantiate the public struct with all of our public information
6668
* and pointers to our public functions in it
@@ -120,7 +122,9 @@ opal_accelerator_base_module_t opal_accelerator_null_module =
120122
accelerator_null_host_unregister,
121123

122124
accelerator_null_get_device,
123-
accelerator_null_device_can_access_peer
125+
accelerator_null_device_can_access_peer,
126+
127+
accelerator_null_get_buffer_id
124128
};
125129

126130
static int accelerator_null_open(void)
@@ -235,3 +239,8 @@ static int accelerator_null_device_can_access_peer( int *access, int dev1, int d
235239
{
236240
return OPAL_ERR_NOT_IMPLEMENTED;
237241
}
242+
243+
static int accelerator_null_get_buffer_id(int dev_id, const void *addr, opal_accelerator_buffer_id_t *buf_id)
244+
{
245+
return OPAL_ERR_NOT_IMPLEMENTED;
246+
}

opal/mca/accelerator/rocm/accelerator_rocm_module.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static int mca_accelerator_rocm_host_unregister(int dev_id, void *ptr);
3737
static int mca_accelerator_rocm_get_device(int *dev_id);
3838
static int mca_accelerator_rocm_device_can_access_peer( int *access, int dev1, int dev2);
3939

40+
static int mca_accelerator_rocm_get_buffer_id(int dev_id, const void *addr, opal_accelerator_buffer_id_t *buf_id);
4041

4142
opal_accelerator_base_module_t opal_accelerator_rocm_module =
4243
{
@@ -59,7 +60,9 @@ opal_accelerator_base_module_t opal_accelerator_rocm_module =
5960
mca_accelerator_rocm_host_unregister,
6061

6162
mca_accelerator_rocm_get_device,
62-
mca_accelerator_rocm_device_can_access_peer
63+
mca_accelerator_rocm_device_can_access_peer,
64+
65+
mca_accelerator_rocm_get_buffer_id
6366
};
6467

6568

@@ -483,3 +486,9 @@ static int mca_accelerator_rocm_device_can_access_peer(int *access, int dev1, in
483486

484487
return OPAL_SUCCESS;
485488
}
489+
490+
static int accelerator_rocm_get_buffer_id(int dev_id, const void *addr, opal_accelerator_buffer_id_t *buf_id)
491+
{
492+
*buf_id = 0;
493+
return OPAL_SUCCESS;
494+
}

0 commit comments

Comments
 (0)