# Sharing CUDA memory by numba

**URL:** <https://numba.discourse.group/t/sharing-cuda-memory-by-numba/1001>\
**Category:** Support: How do I do ...?\
**Created:** [November 3, 2021, 2:18pm UTC](https://numba.discourse.group/t/sharing-cuda-memory-by-numba/1001 "2021-11-03T14:18:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sakuskov](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@sakuskov](https://numba.discourse.group/u/sakuskov)\
**Post date:** [November 3, 2021, 2:18pm UTC](https://numba.discourse.group/t/sharing-cuda-memory-by-numba/1001/1 "2021-11-03T14:18:00Z")

</div>

I try to share pytorch tensor by numba [like this](https://numba.pydata.org/numba-doc/latest/cuda/ipc.html#sharing-between-process) and it gives me strange results.  
when I read the headline with numba, I get an incorrect result from the network from time to time. I added a delay after converting the tensor and it started working correctly. And I not understand why it happens

Code sample:

```auto
backbone_features = torch.cat(backbone_features_list, dim=0)
desc = backbone_features. __cuda_array_interface__
shape = desc["shape"]
strides = desc.get("strides")
dtype = np.dtype(desc["typestr"])
shape, strides, dtype = _prepare_shape_strides_dtype(shape, strides, dtype, order="C")
size = cuda.driver.memory_size_from_info(shape, strides, dtype.itemsize)
devptr = cuda.driver.get_devptr_for_active_ctx(
            backbone_features. __cuda_array_interface__ ["data"][0])
data = cuda.driver.MemoryPointer(
            current_context(), devptr, size=size, owner=backbone_features)
ipch = devices.get_context().get_ipc_handle(data)
desc = dict(shape=shape, strides=strides, dtype=dtype)
handle = pickle.dumps([ipch, desc])

```

Can it happen when new data is loaded before current data was read?

I also try base example with numba.cuda.api:

```auto
        arr = cuda.to_device(tensor)
        handle = arr.get_ipc_handle()
        handle = pickle.dumps(handle)

```

and

```auto
with handle as ipc_array:
    z = cuda.open_ipc_array(ipc_array, 1, dtype='float16', strides=None, offset=0)
    hary = z.args[0].copy_to_host(stream=cuda.stream())

```

handle sending via localhost
