Increase GPU Throughput During training

P
praveen · Jul 2, 2021 · 1.9K views
Question
I have a single Tesla GP100 GPU with 16GB of RAM. When I'm training my neural network, I have two issues Using a imageDatastore spends a HUGE amount of time doing an fread (I'm using a custom ReadFcn because my data is asymmetric and that seemed easiest). I am able to overcome this by reading all the data into memory prior to training but that will not scale. During training I am only using 2.2GB of the 16GB available on the GPU. When I use the exact same network and data with TensorFlow, I use all 16GB. This is the case even if I preload all the data above into memory. I'm guessing that is because TensorFlow is "queuing up" batches and MATLAB is not. Is there a way to increase this? Here is my minimum example code:     function net = run_training_public(dims, nbatch, lr, nepoch) % Load Data ds = imageDatastore('./data/set3', 'IncludeSubfolders',true,... 'ReadFcn',@(x)reader_public(x,dims),... 'LabelSource','foldernames',... 'FileExtensions','.dat'); % load neural network structure network = cnn1; % Setup options for training and execute training options = trainingOptions('adam','MaxEpochs',nepoch,'MiniBatchSize',... nbatch,'Shuffle','every-epoch',... 'InitialLearnRate',lr,... 'ExecutionEnvironment','gpu','Verbose',true); net = trainNetwork(ds,network,options); end function data = reader_public(fileName, dims) f=fopen(fileName,'r'); data = fread(f,[dims(2) dims(1)],'*int16').'; fclose(f); end
Expert Answer
Profile picture of Prashant Kumar
Prashant Kumar PhD Expert
Answered Sep 9, 2026






How to Increase GPU Throughput During Training in MATLAB


Low GPU utilization occurs when the GPU idles while waiting for CPU data preparation, small batch sizes, or excessive CPU-GPU memory transfers. Apply the techniques below to maximize samples processed per second.

1. Increase Mini-Batch Size


Small batches cause kernel launch overhead and underutilize parallel CUDA cores. Double the mini-batch size until GPU VRAM reaches 80% to 90% capacity.

opts = trainingOptions('adam', ...
    'MiniBatchSize', 256, ... % Increase from 32/64 to 128/256/512
    'ExecutionEnvironment', 'gpu');

2. Enable Background Data Prefetching (DispatchInBackground)


Load and augment the next batch in CPU worker threads while the GPU trains on the current batch.

opts = trainingOptions('adam', ...
    'ExecutionEnvironment', 'gpu', ...
    'DispatchInBackground', true, ... % Asynchronous CPU prefetching
    'MiniBatchSize', 256);

3. Use Half-Precision (FP16) on Tensor Core GPUs


Modern NVIDIA GPUs (RTX series, V100, A100, H100) run significantly faster with 16-bit floating point precision and use half the VRAM.

% For trainNetwork / trainnet (R2023b+)
opts = trainingOptions('adam', ...
    'ExecutionEnvironment', 'gpu', ...
    'Precision', 'half'); % Enables FP16 acceleration

4. Disable Real-Time Training Plots and Heavy Callbacks


Drawing plots on every iteration pauses the execution pipeline and forces data transfers back to the CPU.

opts = trainingOptions('adam', ...
    'Plots', 'none', ...       % Turn off GUI rendering
    'Verbose', true, ...
    'VerboseFrequency', 50);   % Print metrics only every 50 iterations

5. Custom Training Loop: Use minibatchqueue with GPU Placement


If you write custom training loops with dlarray and dlgradient, use minibatchqueue with automatic background conversion to gpuArray.

% Configure high-throughput queue
mbq = minibatchqueue(ds, ...
    'MiniBatchSize', 256, ...
    'MiniBatchFcn', @preprocessBatch, ...
    'MiniBatchFormat', {'SSCB', 'BC'}, ...
    'DispatchInBackground', true, ...
    'OutputEnvironment', 'gpu'); % Streams directly to GPU memory

% Fast execution loop
while hasdata(mbq)
    [dlX, dlY] = next(mbq);
    [loss, gradients] = dlfeval(@modelGradients, net, dlX, dlY);
    [net, trailingAvg, trailingAvgSq] = adamupdate(net, gradients, ...
        trailingAvg, trailingAvgSq, iteration, learnRate);
end

6. Scale to Multiple GPUs


If your machine has multiple GPUs, distribute training across all devices with zero code changes.

opts = trainingOptions('adam', ...
    'ExecutionEnvironment', 'multi-gpu', ...
    'MiniBatchSize', 512); % Scales batch across all cards

Optimization Checklist













BottleneckSymptomSolution
CPU Data StarvationGPU usage spikes and drops to 0%Set DispatchInBackground = true
Kernel OverheadGPU memory full, but compute usage lowIncrease MiniBatchSize
GUI RenderingStuttering iterationsSet Plots = 'none'
Memory Transfer LagFrequent gather() calls in loopKeep tensors on GPU using dlarray(..., 'gpuArray')
Compute Bound100% GPU compute utilizationSwitch to 'Precision', 'half' or multi-gpu


Quick Test: Run gpuDevice in MATLAB to confirm your active CUDA device, compute capability, and available VRAM.



100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!