run matlab functions on multiple gpus -凯发k8网页登录
this example shows how to run matlab® code on multiple gpus in parallel, first on your local machine, then scaling up to a cluster. as a sample problem, the example uses the logistic map, an equation that models the growth of a population.
a growing number of features in matlab offer automatic parallel support, including multi-gpu support, without requiring any extra coding. for details, see . for example, the trainnetwork
function offers multi-gpu support for training of neural networks and inference. for more information, see (deep learning toolbox).
use a single gpu
to run computations on a single gpu, use gpuarray
objects as inputs to gpu-enabled matlab functions. to learn more about gpu-enabled functions, see run matlab functions on a gpu.
create gpu arrays defining the growth rate, r
, and the population, x
. for more information on creating gpuarray
objects, see .
n = 1000;
r = gpuarray.linspace(0,4,n);
x = rand(1,n,"gpuarray");
use a simple algorithm to iterate the logistic map. because the algorithm uses gpu-enabled operators on gpuarray
input data, the computations run on the gpu.
numiterations = 1000; for n=1:numiterations x = r.*x.*(1-x); end
when the computations are done, plot the growth rate against the population.
plot(r,x,'.');
if you need more performance, gpu arrays supports several options. for a list, see the gpuarray
function page. for example, the algorithm in this example only performs element-wise operations on gpu arrays, and so you can use the function to precompile them for gpu.
use multiple gpus with parfor
you can use parfor
-loops to distribute for
-loop iterations among parallel workers. if your computations use gpu-enabled functions, then the computations run on the gpu of the worker. for example, if you use the monte carlo method to randomly simulate the evolution of populations, simulations are computed with multiple gpus in parallel using a parfor
-loop.
create a parallel pool with as many workers as gpus available using parpool
. to determine the number of gpus available, use the function. by default, matlab assigns a different gpu to each worker for best performance. for more information on selecting gpus in a parallel pool, see use multiple gpus in parallel pool.
numgpus = gpudevicecount("available");
parpool(numgpus);
starting parallel pool (parpool) using the 'processes' profile ... connected to the parallel pool (number of workers: 2).
define the number of simulations, and create an array in the gpu to store the population vector for each simulation.
numsimulations = 100;
x = zeros(numsimulations,n,"gpuarray");
use a parfor
loop to distribute simulations to workers in the pool. the code inside the loop creates a random gpuarray
for the initial population, and iterates the logistic map on it. because the code uses gpu-enabled operators on gpuarray
input data, the computations automatically run on the gpu of the worker.
parfor i = 1:numsimulations x(i,:) = rand(1,n,"gpuarray"); for n=1:numiterations x(i,:) = r.*x(i,:).*(1-x(i,:)); end end
when the computations are done, plot the results of all simulations. each color represents a different simulation.
figure
plot(r,x,'.');
with default preferences, parpool
starts a parallel pool of process workers. running code in parallel on process workers often results in data being copied to each worker which can use a significant amount of gpu memory when working with gpu arrays. in contrast, thread workers can share memory. to reduce memory usage and lower data transfer costs, use a parallel pool of thread workers by calling parpool("threads")
. thread workers support only a subset of functions available for processes workers. for more information, see .
if you need greater control over your calculations, you can use more advanced parallel functionality. for example, you can use a to send data from the workers during computations. for an example, see .
if you want to generate a reproducible set of random numbers, you can control the random number generation on the gpu of a worker. for more information, see .
use multiple gpus asynchronously with parfeval
you can use to run computations asynchronously on parallel pool workers. if your computations use gpu-enabled functions, then the computations run on the gpu of the worker. as an example, you run monte carlo simulations on multiple gpus asynchronously.
to hold the results of computations after the workers complete them, use future objects. preallocate an array of future objects for the result of each simulation.
f(numsimulations) = parallel.fevalfuture;
to run computations with parfeval
, you must place them inside a function. for example, myparallelfcn
contains the code of a single simulation.
type myparallelfcn
function x = myparallelfcn(r) n = 1000; x = gpuarray.rand(1,n); numiterations = 1000; for n=1:numiterations x = r.*x.*(1-x); end end
use a for
loop to loop over simulations, and use parfeval
to run them asynchronously on a worker in the parallel pool. myparallelfcn
uses gpu-enabled functions on gpuarray
input data, so they run on the gpu of the worker. because parfeval
performs the computations asynchronously, it does not block matlab, and you can continue working while computations happen.
for i=1:numsimulations f(i) = parfeval(@myparallelfcn,1,r); end
to collect the results from parfeval
when they are ready, you can use or on the future objects. also, you can use or to invoke functions on the results automatically when they are ready. for example, to plot the result of each simulation immediately after it completes, use aftereach
on the future objects. each color represents a different simulation.
figure hold on aftereach(f,@(x) plot(r,x,'.'),0);
use multiple gpus in a cluster
if you have access to a cluster with multiple gpus, then you can scale up your computations. use the parpool
function to start a parallel pool on the cluster. when you do so, parallel features, such as parfor
loops or parfeval
, run on the cluster workers. if your computations use gpu-enabled functions on gpuarray
input data, then those functions run on the gpu of the cluster worker. note that cluster features are supported only in process-based environments. to learn more about running parallel features on a cluster, see scale up from desktop to cluster.
advanced support for fast multi-node gpu communication
some multi-gpu features in matlab®, including trainnetwork
, are optimized for direct communication via fast interconnects for improved performance.
if you have appropriate hardware connections, then data transfer between multiple gpus uses fast peer-to-peer communication, including nvlink, if available.
if you are using a linux compute cluster with fast interconnects between machines such as infiniband, or fast interconnects between gpus on different machines, such as gpudirect rdma, you might be able to take advantage of fast multi-node support in matlab. enable this support on all the workers in your pool by setting the environment variable parallel_server_fast_multinode_gpu_communication
to 1
. set this environment variable in the cluster profile manager.
this feature is part of the nvidia nccl library for gpu communication. to configure it, you must set additional environment variables to define the network interface protocol, especially nccl_socket_ifname
. for more information, see the and in particular the section on .
see also
gpuarray
| gpudevice
| parpool
| parfor
| | |
related examples
- run matlab functions on a gpu
- scale up from desktop to cluster
- (deep learning toolbox)