gpuArray — Move data to the GPU as gpuArray values in MATLAB and RunMat.
gpuArray(X) moves data to the active GPU and returns a gpuArray handle for accelerated execution. RunMat mode additionally supports explicit dtype, size, and 'like' construction extensions.
Syntax
G = gpuArray(X)
G = gpuArray(X, dim, ...)
G = gpuArray(X, dtype)
G = gpuArray(X, "like", prototype)
G = gpuArray(X, dim, ..., option, ...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
X | Any | Yes | — | Input value to upload or recast on GPU. |
dim | SizeArg | Variadic | — | Reshape dimensions (scalar dims or a single size vector tensor). |
dtype | StringScalar | Yes | "double" | Class tag such as `single`, `int32`, `uint8`, `logical`, or `double`. |
like | StringScalar | Yes | — | Literal keyword `"like"`. |
prototype | LikePrototype | Yes | — | Prototype value whose class drives output conversion. |
option | Any | Variadic | — | Class tags and/or `"like", prototype` qualifiers. |
Returns
| Name | Type | Description |
|---|---|---|
G | Any | GPU-resident handle containing uploaded/converted data. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:gpuArray:NoProvider | No acceleration provider is registered for host/device transfers. | gpuArray: no acceleration provider registered |
RunMat:gpuArray:OptionArgument | Option tail contains non-text values where class tags/keywords are expected. | gpuArray: invalid option argument |
RunMat:gpuArray:LikeMissingPrototype | Keyword `like` is supplied without a following prototype value. | gpuArray: expected a prototype value after 'like' |
RunMat:gpuArray:LikeDuplicate | Keyword `like` appears more than once. | gpuArray: duplicate 'like' qualifier |
RunMat:gpuArray:CodistributedUnsupported | Distributed/codistributed qualifiers are requested. | gpuArray: codistributed arrays are not supported yet |
RunMat:gpuArray:ConflictingTypeQualifiers | Multiple incompatible class qualifiers are supplied. | gpuArray: conflicting type qualifiers supplied |
RunMat:gpuArray:UnknownOption | Text option is not a recognized class/keyword token. | gpuArray: unrecognised option |
RunMat:gpuArray:InvalidSizeArgument | Size arguments are malformed (not finite integers, negative, or invalid combinations). | gpuArray: invalid size argument |
RunMat:gpuArray:InvalidLikePrototype | `like` prototype is unsupported for type inference. | gpuArray: invalid 'like' prototype |
RunMat:gpuArray:UnsupportedInputType | Input value type cannot be uploaded/coerced to supported gpuArray storage. | gpuArray: unsupported input type |
RunMat:gpuArray:TypedIntegerUnsupported | A native integer value or integer GPU class is requested without matching provider storage. | gpuArray: native integer storage is not supported by the active acceleration provider |
RunMat:gpuArray:ConversionFailed | Requested dtype conversion cannot be performed (for example NaN->logical). | gpuArray: conversion failed |
RunMat:gpuArray:ReshapeMismatch | Requested shape does not preserve the element count. | gpuArray: cannot reshape gpuArray into requested size |
RunMat:gpuArray:ProviderIO | Provider upload/download interaction fails. | gpuArray: provider I/O failed |
RunMat:gpuArray:InternalError | Internal tensor/container conversion fails. | gpuArray: internal error |
How gpuArray works
- Accepts numeric tensors, complex tensors, logical arrays, booleans, character vectors, and existing gpuArray handles. Double, single, and all eight integer classes retain native real or supported complex storage during transfer.
- In
runmatcompatibility mode, optional leading size arguments reshape the uploaded value while preserving element count. - In
runmatcompatibility mode, class strings convert real data before upload and complex inputs can select double or single precision. - In
runmatcompatibility mode,'like', prototypeinfers dtype and logical state from the prototype. "gpuArray"strings are accepted as no-ops so call-sites that forward arguments from constructors such aszeros(..., 'gpuArray')remain compatible.- Inputs that are already gpuArray handles pass through by default. When a class change is requested, RunMat gathers the data, performs the conversion, and uploads a fresh buffer without invalidating the original handle.
- When no acceleration provider is registered, the builtin raises
gpuArray: no acceleration provider registered.
Does RunMat run gpuArray on the GPU?
gpuArray itself runs on the CPU. Host values enter one provider transfer contract carrying their native element type, shape, and real or interleaved-complex layout. gather uses the matching native download contract to reconstruct the same host class without widening single or integer values. A requested dtype conversion creates a new buffer and leaves the source gpuArray valid; providers that cannot represent the requested native transfer return an informative gpuArray: error.
GPU memory and residency
RunMat’s auto-offload planner transparently moves and keeps tensors on the GPU when it predicts a benefit. MATLAB-compatible scripts use gpuArray(X) for explicit upload; dtype, size, and 'like' construction forms are RunMat-only extensions.
Examples
Moving a matrix to the GPU for elementwise work
A = [1 2 3; 4 5 6];
G = gpuArray(A);
out = gather(sin(G))Expected output:
out =
2×3
0.8415 0.9093 0.1411
-0.7568 -0.9589 -0.2794Uploading a scalar with dtype conversion
pi_single = gpuArray(pi, 'single');
isa(pi_single, 'gpuArray');
class(gather(pi_single))Expected output:
ans =
logical
1
ans =
singleConverting host data to a logical gpuArray
mask = gpuArray([0 2 -5 0], 'logical');
gather(mask)Expected output:
ans =
1×4 logical array
0 1 1 0Matching an existing prototype with 'like'
template = gpuArray(true(2, 2));
values = gpuArray([10 20 30 40], [2 2], 'like', template);
isequal(gather(values), logical([10 20; 30 40]))Expected output:
ans =
logical
1Reshaping during upload
flat = 1:6;
G = gpuArray(flat, 2, 3);
size(G)Expected output:
ans =
2 3Calling gpuArray on an existing gpuArray handle
G = gpuArray([1 2 3]);
H = gpuArray(G, 'double');
isequal(G, H)Expected output:
ans =
logical
1Using gpuArray with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how gpuArray changes the result.
Run a small gpuArray example, explain the result, then change one input and compare the output.
Related Acceleration functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how gpuArray is executed, line by line, in Rust.
- View the source for gpuArray in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.