mexcuda — Build a native CUDA MEX module with the configured nvcc toolchain.
mexcuda(source, options...) delegates to the shared MEX builder in CUDA mode. It requires at least one .cu translation unit and returns no value. CUDA compilation is currently supported only on native Linux and Windows targets with a compatible nvcc toolchain.
Syntax
mexcuda(arguments)Inputs
arguments- Type
Any- Required
- Variadic
- Default
- —
- Description
- Native CUDA source files and MEX options as scalar text; RunMat also converts scalar numeric and logical arguments to text.
Errors
RunMat:MEX:UnsupportedTarget- When
- The current target cannot load native MEX modules.
- Message
MEX modules are unavailable on this target.
RunMat:MEX:InvalidBuildArguments- When
- The MEX command receives no source file or an invalid build option.
- Message
mex: invalid build arguments.
RunMat:MEX:BuildFailed- When
- The selected compiler cannot produce the requested MEX module.
- Message
mex: C MEX compilation failed.
RunMat:MEX:TooManyOutputs- When
- The caller requests more results than the MEX build or target query returns.
- Message
Too many MEX output arguments.
CUDA build contract
A .cu source is required even when C or C++ sources accompany it. Source paths, output naming, include/library flags, compiler assignments, verbosity, and the -R2017b/-R2018a API pins use the same parser as mex. Mixed Fortran/CUDA gateway sources are rejected by the adapter. The build records a target- and ABI-bound manifest for the resulting module.
Scalar numbers and logical arguments are currently converted to text by RunMat's call boundary, like mex; this coercion is not MATLAB API parity. Unsupported targets are reported as UnsupportedTarget, invalid invocation options as InvalidBuildArguments, and compiler/SDK/output failures as BuildFailed with their build cause.
Execution limits
CUDA compilation uses native process, filesystem, environment, and console effects. It is not itself a provider-resident array computation: the CUDA gateway's runtime behavior and ownership are validated separately when loaded. Building is unsupported in browser/Worker sessions and on native targets lacking the supported CUDA toolchain contract.
The current compiler call blocks the caller. It cannot interrupt an in-flight compiler on invocation cancellation and does not enforce a complete-call host-memory budget.
Examples
Build a CUDA gateway with the R2018a API
mexcuda('-R2018a', '-output', 'cuda_gateway', 'cuda_gateway.cu');
module = strcat('cuda_gateway.', mexext());
assert(isfile(module));Require a source argument
mexcuda();Using mexcuda with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how mexcuda changes the result.
Run a small mexcuda example, explain the result, then change one input and compare the output.
FAQ
Is a CUDA-capable GPU needed just to build?⌄
The build requires a supported native CUDA compiler/toolchain; executing the compiled gateway has separate accelerator and loader requirements.
Can I mix a Fortran gateway with CUDA?⌄
No. The shared MEX builder rejects mixed Fortran and CUDA gateway sources.
Related Interop functions
Native
calllib · clibgen.buildInterface · libfunctions · libisloaded · libpointer · libstruct · loadlibrary · setdatatype · unloadlibrary
Java
isjava · javaaddpath · javaArray · javaclasspath · javaMethod · javaMethodEDT · javaObject · javaObjectEDT · javarmpath · jenv · usejava
Related guides
Source you can read
Every RunMat function's source is public. Read exactly how mexcuda is executed, line by line, in Rust.
- View the source for mexcuda in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.