Pass GPU Data Between MATLAB and Python
R2026bThis page explains how to use the functions for transferring GPU data between MATLAB® and Python®, and how to choose between them.
After you pass GPU data to Python, you can call Python GPU computing functions directly from MATLAB. For more information about calling Python from MATLAB, see Directly Call Python Functionality from MATLAB. For information about transferring non-GPU data to Python, see Pass Data Between MATLAB and Python from MATLAB.
To avoid slowing down your code, move data between GPU memory and host memory only when necessary. All of the functions listed below can pass GPU data between MATLAB and Python without first moving the data to host memory.
Pass GPU Data from MATLAB to Python
If you have GPU data in MATLAB, represented by a gpuArray, you can use these
functions to send the data to Python.
| Your Task in Python | Target Python Libraries | Data Transfer Function |
|---|---|---|
Generic GPU computing | CuPy | pycupyarray |
| Deep learning | PyTorch® | arrayToTorchTensor (Deep Learning Toolbox)
|
| Deep learning | Any library that implements the DLPack specification, including TensorFlow | pydlpack |
Pass GPU Data from Python to MATLAB
If you have GPU data in Python, you can use these functions to send the data to MATLAB.
| Python Object | Origin Python Library | Data Transfer Function |
|---|---|---|
| CuPy | gpuArray |
| DLPack |
| |
| PyTorch | torchTensorToArray (Deep Learning Toolbox) |
By using these functions to send and receive GPU data, you can perform GPU computing across MATLAB and Python.
Process Data on GPU in Python
First, convert a gpuArray to a
cupy.ndarray.
X = rand(1000,"gpuArray");
pyX = pycupyarray(X);Operate on the CuPy array, on the GPU, using Python functions. Replace the call to myFunctions.proccessData
with your desired Python functions.
pyZ = py.myFunctions.processData(pyY)
Transfer the resulting Python data back to MATLAB.
Z = gpuArray(pyZ);
Use Multiple GPUs
If you have multiple GPUs, consult this table to determine which GPU your data is sent to.
| Direction | GPU Data Destination |
|---|---|
| MATLAB to Python | The GPU device currently selected by MATLAB. To see the currently selected GPU
device, call |
| Python to MATLAB | The GPU device currently selected by MATLAB or, if no device is selected, the default GPU device. To see this device, call Note When you transfer GPU data between MATLAB and Python, using the same GPU in both environments usually gives the best performance. |