View all functions

CategoryMath: Reduction
GPUYes

What does the cumprod function do in MATLAB / RunMat?

cumprod(X) multiplies elements cumulatively along a chosen dimension. The result has the same size as X, and each element stores the running product.

How does the cumprod function behave in MATLAB / RunMat?

  • By default, the running product is taken along the first dimension whose length is greater than 1.
  • cumprod(X, dim) lets you choose the dimension explicitly; if dim > ndims(X), the input is returned unchanged.
  • Passing [] for the dimension argument keeps the default dimension (MATLAB treats it as a placeholder).
  • cumprod(..., "reverse") accumulates from the end toward the beginning; "forward" (default) works from start to finish.
  • cumprod(..., "omitnan") treats NaN values as missing. Empty prefixes yield 1, the multiplicative identity.
  • Synonyms such as "omitmissing" / "includemissing" are accepted for MATLAB compatibility.
  • The function supports real or complex scalars and dense tensors. Logical inputs are promoted to double precision.

cumprod Function GPU Execution Behavior

When data already lives on the GPU, RunMat asks the active acceleration provider for a device-side prefix-product implementation. The runtime calls the cumprod_scan hook with the chosen dimension, direction, and NaN mode. Providers that lack this hook—or that report an error for the requested options—trigger a gather to host memory, perform the cumulative product on the CPU, and return the dense tensor result. Residency metadata is cleared so downstream operations can re-promote the tensor when profitable.

Examples of using the cumprod function in MATLAB / RunMat

Running products down each column (default dimension)

A = [1 2 3; 4 5 6];
columnProducts = cumprod(A);

Expected output:

columnProducts =
     1     2     3
     4    10    18

Tracking cumulative products across rows

A = [1 2 3; 4 5 6];
rowProducts = cumprod(A, 2);

Expected output:

rowProducts =
     1     2     6
     4    20   120

Reversing the accumulation direction

v = [2 3 4 5];
reverseProducts = cumprod(v, "reverse");

Expected output:

reverseProducts =
   120    60    20     5

Ignoring NaN values while multiplying

v = [2 NaN 4 NaN 3];
running = cumprod(v, "omitnan");

Expected output:

running =
     2     2     8     8    24

Computing a cumulative product inside a GPU workflow

G = gpuArray(1 + 0.1*rand(1, 5));
totals = cumprod(G);
hostResult = gather(totals);

Expected behavior:

  • totals stays on the GPU when the provider implements prefix products.
  • Otherwise RunMat gathers G to the host, performs the accumulation, and returns the CPU result.

GPU residency in RunMat (Do I need gpuArray?)

Manual gpuArray calls are optional. RunMat promotes tensors automatically when the planner predicts a benefit, keeping fused expressions resident on the device. Explicit gpuArray is still supported for MATLAB compatibility or when you want to guarantee GPU residency before entering a critical loop.

FAQ

Does cumprod change the size of the input?

No. The output always equals the input shape.

What happens if I request a dimension larger than ndims(X)?

The input is returned unchanged, matching MATLAB behaviour.

How does "omitnan" treat leading NaN values?

They are ignored, so the cumulative product uses the multiplicative identity 1 until a finite value appears.

Can I combine "reverse" and "omitnan"?

Yes. The options can be specified in any order and RunMat mirrors MATLAB’s results.

Does the GPU path respect "omitnan"?

Only when the active provider offers a native prefix-product kernel with missing-value support. Otherwise the runtime gathers to the host to preserve MATLAB semantics.

See Also

prod, cumsum, sum, gpuArray, gather

Source & Feedback