1 / 64

MATLAB

MATLAB. C enter for I ntegrated R esearch C omputing. http:// www.circ.rochester.edu /wiki/ index.php / MatlabWorkshop. Outline. Part I – Interacting with Matlab Running Matlab interactively Accessing the GUI Using the terminal for command entry Using just the terminal

lapis
Download Presentation

MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MATLAB Center for Integrated Research Computing http://www.circ.rochester.edu/wiki/index.php/MatlabWorkshop

  2. Outline Part I – Interacting with Matlab • Running Matlab interactively • Accessing the GUI • Using the terminal for command entry • Using just the terminal • Running Matlab in batch mode • Using PBS job arrays to do embarrassingly parallel computations Part II – Speeding up Matlab Computations • Symmetric Multi-Processing with Matlab • Accelerating Matlab computations with GPUs • Running Matlab in distributed memory environments • Using the Parallel Computing Toolbox • Using the Matlab Distributed Compute Engine Server • Using pMatlab Part III – Mixing Matlab and Fortran/C code • Compiling MEX code from C/Fortran • Turning Matlab routines into C code

  3. Running Matlab Interactively • To use Matlab's GUI you must connect through suitable environment • Why NX? • Faster than using X11 forwarding (compresses data) • Has clients for all major operating systems • Saves your session when you are disconnected • You don’t have to restart Matlab if your network connection drops. • Instructions for obtaining/installing/connecting through NX can be found at: http://www.circ.rochester.edu/wiki/index.php/NX_Cluster

  4. Running Matlab Interactively • To use GUI you must connect through suitable environment • Why NX? • Faster than using X11 forwarding (compresses data) • Has clients for all major operating systems • Saves your session when you are disconnected • You don’t have to restart Matlab if your network connection drops. • The link to Matlab on the NX desktop menu bar actually launches a script that submits a job to the blue hive cluster. It does not run Matlab locally, but instead uses X11 forwarding between compute nodes and the NX server. http://www.circ.rochester.edu/wiki/index.php/NX_Cluster

  5. Running Matlab Interactively We could also launch a terminal on the NX desktop and submit an interactive job from there.

  6. Running Matlab Interactively We could also launch a terminal on the NX desktop and submit an interactive job from there. qsub -I -X -q interactive -l walltime=1:00:00,nodes=1:ppn=1,vmem=4gb,pvmem=-1module load matlab-R2013a-local matlab -singlecompthread

  7. Running Matlab Interactively We could also launch a terminal on the NX desktop and submit an interactive job from there. Occasionally the Matlab Desktop will respond slowly to commands which can be VERY frustrating. One work around is to use the terminal window as the "desktop" – while still retaining the ability to plot windows / access help etc... qsub -I -X -q interactive -l walltime=1:00:00,nodes=1:ppn=1,vmem=4gb,pvmem=-1module load matlab matlab -singlecompthread matlab -nodesktop -nosplash

  8. Running Matlab Interactively We could also launch a terminal on the NX desktop and submit an interactive job from there. Occasionally the Matlab Desktop will respond slowly to commands which can be VERY frustrating. One work around is to use the terminal window as the Desktop – while still retaining the ability to plot windows / access help etc... And finally you may not need to plot anything on the screen – or use any of the GUI features. In that case you can... qsub -I -X -q interactive -l walltime=1:00:00,nodes=1:ppn=1,vmem=4gb,pvmem=-1module load matlab matlab -singlecompthread matlab -nodesktop -nosplash matlab -nodisplay

  9. Running Matlab Interactively If you are running Matlab without a connected display you can still make plots directly to a file in Matlab You may also find it useful to enter many commands into a script file and then execute the script – so you can do something else while Matlab creates several figures etc... This is also a good way to develop a script for batch jobs. H=hilb(1000); Z=fft2(H); f=figure('Visible','off');imagesc(log(abs(Z)));
print('-dpdf','-r300','fig1.pdf')

  10. Running Matlab Interactively If you are running a machine that has an X-server – you can bypass NX and just use X11 Forwarding. Though if your connection drops – your Matlab session (and your interactive job) will terminate Also if you do use NX and you finish using Matlab – please terminate your session instead of just disconnecting. This will cleanup any jobs you have running and free up resources for other users. ssh -X jcarrol5@bluehive.circ.rochester.edu qsub -I -X -q interactive –l walltime=1:00:00,nodes=1:ppn=1,vmem=4gb,pvmem=-1

  11. Outline Part I – Interacting with Matlab • Running Matlab interactively • Accessing the GUI • Using the terminal for command entry • Using just the terminal • Running Matlab in batch mode • Using PBS job arrays to do embarrassingly parallel computations Part II – Speeding up Matlab Computations • Symmetric Multi-Processing with Matlab • Accelerating Matlab computations with GPUs • Running Matlab in distributed memory environments • Using the Parallel Computing Toolbox • Using the Matlab Distributed Compute Engine Server • Using pMatlab Part III – Mixing Matlab and Fortran/C code • Compiling MEX code from C/Fortran • Turning Matlab routines into C code

  12. Running Matlab in Batch Mode To submit a job in batch mode we need to create a batch script And a Matlab script containing the commands to run And we should place both files in a folder on /scratch where we will submit the job from. #PBS -N Matlab #PBS -q standard #PBS -l walltime=1:00:00,nodes=1:ppn=1,vmem=4gb,pvmem=-1 . /usr/local/modules/init/bash module load matlab matlab -nodisplay -r "sample_script" sample_script.pbs H=hilb(1000); Z=fft2(H); imagesc(log(abs(Z))); print('-dpdf','-r300','fig1-batch.pdf'); sample_script.m qsubsample_script.pbs

  13. Outline Part I – Interacting with Matlab • Running Matlab interactively • Accessing the GUI • Using the terminal for command entry • Using just the terminal • Running Matlab in batch mode • Using PBS job arrays to do embarrassingly parallel computations Part II – Speeding up Matlab Computations • Symmetric Multi-Processing with Matlab • Accelerating Matlab computations with GPUs • Running Matlab in distributed memory environments • Using the Parallel Computing Toolbox • Using the Matlab Distributed Compute Engine Server • Using pMatlab Part III – Mixing Matlab and Fortran/C code • Compiling MEX code from C/Fortran • Turning Matlab routines into C code

  14. Using Job Arrays in Batch Mode To use a job array we need to use the “-t” PBS option And turn our Matlab script into a function that takes arguments. (sample_function.m) #PBS -t 0-3 #PBS -N Matlab #PBS -q standard #PBS -l walltime=1:00:00,nodes=1:ppn=1,vmem=4gb,pvmem=-1 . /usr/local/modules/init/bash module load matlab matlab -nodisplay -r "sample_function($PBS_ARRAYID)" sample_script.pbs sample_function.m function sample_function(n) H=hilb(n); Z=fft2(H); imagesc(log(abs(Z))); print('-dpdf','-r300',sprintf('%s%03d%s','fig1-batch_',n,'.pdf'));

  15. Outline Part I – Interacting with Matlab • Running Matlab interactively • Accessing the GUI • Using the terminal for command entry • Using just the terminal • Running Matlab in batch mode • Using PBS job arrays to do embarrassingly parallel computations Part II – Speeding up Matlab Computations • Symmetric Multi-Processing with Matlab • Accelerating Matlab computations with GPUs • Running Matlab in distributed memory environments • Using the Parallel Computing Toolbox • Using the Matlab Distributed Compute Engine Server • Using pMatlab Part III – Mixing Matlab and Fortran/C code • Compiling MEX code from C/Fortran • Turning Matlab routines into C code

  16. Symmetric Multi-Processing By default Matlab uses all cores on a given node for operations that can be threaded – regardless of the submission script. Arrays and matrices
• Basic information: ISFINITE, ISINF, ISNAN, MAX, MIN
• Operators: +, -, .*, ./, .\, .^, *, ^, \ (MLDIVIDE), / (MRDIVIDE)
• Array operations: PROD, SUM
• Array manipulation: BSXFUN, SORT

Linear algebra
• Matrix Analysis: DET, RCOND
• Linear Equations: CHOL, INV, LDL, LINSOLVE, LU, QR
• Eigenvalues and singular values: EIG, HESS, SCHUR, SVD, QZ

Elementary math
• Trigonometric: ATAN2, COS, CSC, HYPOT, SEC, SIN, TAN, including variants for radians, degrees, hyperbolics, and inverses.
• Exponential: EXP, POW2, SQRT
• Complex: ABS
• Rounding and remainder: CEIL, FIX, FLOOR, MOD, REM, ROUND
• LOG, LOG2, LOG10, LOG1P, EXPM1, SIGN, BITAND, BITOR, BITXOR

Special Functions
• ERF, ERFC, ERFCINV, ERFCX, ERFINV, GAMMA, GAMMALN

Data Analysis
• CONV2, FILTER, FFT and IFFT of multiple columns or long vectors, FFTN, IFFTN

  17. Symmetric Multi-Processing To be sure you only use the resources you request, you should either request an entire node and all of the CPU’s... Or request a single cpu and specify that Matlab should only use a single thread qsub -I -X -q interactive -l walltime=1:00:00,nodes=1:ppn=8,vmem=16gb,pvmem=-1 . /usr/local/modules/init/bash module load matlab matlab qsub -I -X -q interactive -l walltime=1:00:00,nodes=1:ppn=1,vmem=4gb,pvmem=-1 . /usr/local/modules/init/bash module load matlab matlab -singleCompThread

  18. Outline Part I – Interacting with Matlab • Running Matlab interactively • Accessing the GUI • Using the terminal for command entry • Using just the terminal • Running Matlab in batch mode • Using PBS job arrays to do embarrassingly parallel computations Part II – Speeding up Matlab Computations • Symmetric Multi-Processing with Matlab • Accelerating Matlab computations with GPUs • Running Matlab in distributed memory environments • Using the Parallel Computing Toolbox • Using the Matlab Distributed Compute Engine Server • Using pMatlab Part III – Mixing Matlab and Fortran/C code • Compiling MEX code from C/Fortran • Turning Matlab routines into C code

  19. Using GPUs with Matlab Matlab can use GPUs to do calculations, provided a GPU is available on the node Matlab is running on. We can query the connected GPUs from within Matlab using qsub-I -X -q blugpu-l walltime=1:00:00,nodes=1:ppn=1:gpus=1,vmem=16gb,pvmem=-1 . /usr/local/modules/init/bash module load matlab module load cuda matlab gpuDeviceCount() gpuDevice()

  20. Using GPUs with Matlab Matlab can use GPUs to do calculations, provided a GPU is available on the node Matlab is running on. We can query the connected GPUs from within Matlab using And obtain a list of GPU supported functions using qsub -I -X -q blugpu-l walltime=1:00:00,nodes=1:ppn=1:gpus=1,vmem=16gb,pvmem=-1 . /usr/local/modules/init/bash module load matlab module load cuda matlab gpuDeviceCount() gpuDevice() methods('gpuArray')

  21. Using GPUs with Matlab So there is a 2D FFT – but no Hilbert function... We could do the log and abs functions on the GPU as well. H=hilb(1000); H_=gpuArray(H); Z_=fft2(H_); Z=gather(Z_); imagesc(log(abs(Z))); Distribute data to GPU FFT performed on GPU Gather data from GPU onto CPU H=hilb(1000); H_=gpuArray(H); Z_=fft2(H_); imagesc(gather(log(abs(Z_)));

  22. Using GPUs with Matlab For our example, doing the FFT on the GPU is 7x faster. (4x if you include moving the data to the GPU and back) >> H=hilb(5000); >> tic; A=gather(gpuArray(H)); toc Elapsed time is 0.161166 seconds. >> tic; A=gather(fft2(gpuArray(H))); toc Elapsed time is 0.348159 seconds. >> tic; A=fft2(H); toc Elapsed time is 1.210464 seconds.

  23. Using GPUs with Matlab Matlab has no built in hilb() function that can run on the GPU – but we can write our own function(kernel) in cuda – and save it to hilbert.cu And compile it with nvcc to generate a Parallel Thread eXecution file __global__ void HilbertKernel( double * const out, size_tconstnumRows, size_tconstnumCols) { constintrowIdx = blockIdx.x * blockDim.x + threadIdx.x; constintcolIdx = blockIdx.y * blockDim.y + threadIdx.y; if ( rowIdx >= numRows ) return; if ( colIdx >= numCols ) return; size_tlinearIdx = rowIdx + colIdx*numRows; out[linearIdx] = 1.0 / (double)(1+rowIdx+colIdx) ; } nvcc -ptxhilbert.cu

  24. Using GPUs with Matlab We have to initialize the kernel and specify the grid size before executing the kernel. The default for matlab kernel’s is 1 thread per block, but we could create fewer blocks that were each 10 x 10 threads. In any event, our speedup is a factor of 50 compared to 1 CPU. H_=gpuArray.zeros(1000); hilbert_kernel=parallel.gpu.CUDAKernel('hilbert.ptx','hilbert.cu'); hilbert_kernel.GridSize=size(H_); H_=feval(hilbert_kernel, H_, 1000,1000); Z_=fft2(H_); imagesc(gather(log(abs(Z_)))); hilbert_kernel.ThreadBlockSize=[10,10,1]; hilbert_kernel.GridSize=[100,100];

  25. Outline Part I – Interacting with Matlab • Running Matlab interactively • Accessing the GUI • Using the terminal for command entry • Using just the terminal • Running Matlab in batch mode • Using PBS job arrays to do embarrassingly parallel computations Part II – Speeding up Matlab Computations • Symmetric Multi-Processing with Matlab • Accelerating Matlab computations with GPUs • Running Matlab in distributed memory environments • Using the Parallel Computing Toolbox • Using the Matlab Distributed Compute Engine Server • Using pMatlab Part III – Mixing Matlab and Fortran/C code • Compiling MEX code from C/Fortran • Turning Matlab routines into C code

  26. Parallel Computing Toolbox As an alternative you can also use the Parallel Computing Toolbox. This supports parallelism via MPI You can enable a pool of matlab workers using matlabpool You should create a pool that is the same size as the number of processors you requested in your job submission. Matlab also sells licenses for using a Distributed Computing Server which allows for matlabpools that use more than just the local node. qsub -I -X -q interactive -l walltime=1:00:00,nodes=1:ppn=8,vmem=16gb,pvmem=-1 . /usr/local/modules/init/bash module load matlab matlab -singleCompThread matlabpool(8)

  27. Parallel Computing Toolbox • You can achieve parallelism in several ways: • parfor loops – execute for loops in parallel • smpd – execute instructions in parallel (using ‘labindex’ or ‘drange’) • pmode – interactive version of smpd • distributed arrays – very similar to gpuArrays.

  28. Parallel Computing Toolbox • You can achieve parallelism in several ways: • parfor loops – execute for loops in parallel • smpd – execute instructions in parallel (using ‘labindex’ or ‘drange’) • pmode – interactive version of smpd • distributed arrays – very similar to gpuArrays. matlabpool(4) parforn=1:100 H=hilb(n); Z=fft2(H); f=figure('Visible','off');imagesc(log(abs(Z))); print('-dpdf','-r300',sprintf('%s%03d%s','fig1-batch_',n,'.pdf')); end matlabpool close

  29. Parallel Computing Toolbox • You can achieve parallelism in several ways: • parfor loops – execute for loops in parallel • smpd – execute instructions in parallel (using ‘labindex’ or ‘drange’) • pmode – interactive version of smpd • distributed arrays – very similar to gpuArrays. matlabpool(4) spmd for n=drange(1:100) H=hilb(n); Z=fft2(H); f=figure('Visible','off');imagesc(log(abs(Z))); end end matlabpool close matlabpool(4) spmd for n=labindex:numlabs:100 H=hilb(n); Z=fft2(H); f=figure('Visible','off');imagesc(log(abs(Z))); end end matlabpool close

  30. Parallel Computing Toolbox • You can achieve parallelism in several ways: • parfor loops – execute for loops in parallel • smpd – execute instructions in parallel (using ‘labindex’ or ‘drange’) • pmode – interactive version of smpd • distributed arrays – very similar to gpuArrays. pmode start 4 pmode lab2client H 3 H3 H3 pmode close n=labindex; H=hilb(n); Z=fft2(H); f=figure('Visible','off');
imagesc(log(abs(Z))); print('-dpdf','-r300',sprintf('%s%03d%s','fig1-batch_',n,'.pdf'));

  31. Parallel Computing Toolbox • You can achieve parallelism in several ways: • parfor loops – execute for loops in parallel • smpd – execute instructions in parallel (using ‘labindex’ or ‘drange’) • pmode – interactive version of smpd • distributed arrays – very similar to gpuArrays Example using distributed arrays matlabpool(8) H=hilb(1000); H_=distributed(H); Z_=fft(fft(H_,[],1),[],2); Z=gather(Z_); imagesc(log(abs(Z))); matlabpool close Example using gpuArray H=hilb(1000); H_=gpuArray(H); Z_=fft2(H_); Z=gather(Z_); imagesc(log(abs(Z)));

  32. Parallel Computing Toolbox What about building hilbert matrix in parallel? matlabpool(4) spmd codist=codistributor1d(1,[250,250,250,250],[1000,1000]); [i_lo, i_hi]=codist.globalIndices(1); H_local=zeros(250,1000); for i=i_lo:i_hi for j=1:1000 H_local(i-i_lo+1,j)=1/(i+j-1); end end H_ = codistributed.build(H_local, codist); end Z_=fft(fft(H_,[],1),[],2); Z=gather(Z_); imagesc(log(abs(Z))); matlabpool close Define partition Get local indices in x-direction Initialize local array with Hilbert values. Allocate space for local part Assemble codistributed array Now it's distributed like before!

  33. Outline Part I – Interacting with Matlab • Running Matlab interactively • Accessing the GUI • Using the terminal for command entry • Using just the terminal • Running Matlab in batch mode • Using PBS job arrays to do embarrassingly parallel computations Part II – Speeding up Matlab Computations • Symmetric Multi-Processing with Matlab • Accelerating Matlab computations with GPUs • Running Matlab in distributed memory environments • Using the Parallel Computing Toolbox • Using the Matlab Distributed Compute Engine Server • Using pMatlab Part III – Mixing Matlab and Fortran/C code • Compiling MEX code from C/Fortran • Turning Matlab routines into C code

  34. Using the Matlab Distributed Compute Engine • To get started, first cd into an empty directory and run • This will generate 4 files: • mdce_job.pbs – pbs submission script • mdce_script.m – sample matlab script that uses parallel computing toolbox • mdce_profile.m – matlab function that uses your environment variables to locate the matlab compute cluster for your job • mdce_cleanup is an epilogue script that cleans up the matlab distributed compute server when your job terminates • Then you can submit the sample job with mdce_init qsubmdce_job.pbs

  35. Using the Matlab Distributed Compute Engine Here is the job submission script This script loads the matlab module, starts the cluster with pbs_mdce_start, and runs the matlab script "mdce_script.m" #!/bin/bash #PBS -N Matlab_mdce #PBS -j oe #PBS -l nodes=2:ppn=8,pvmem=2000mb #PBS -l walltime=1:00:00 #PBS -l epilogue=mdce_cleanup #PBS -q standard #PBS -o matlab.log . /usr/local/modules/init/bash module load matlab-R2013a-local cd $PBS_O_WORKDIR pbs_mdce_start matlab -nodisplay -r "mdce_script" mdce_job.pbs Note that other versions of matlab could take hours to start the matlab cluster!!! This epilogue script is important to ensure that the cluster is taken down when your job terminates

  36. Using the Matlab Distributed Compute Engine And here is the sample matlab script The mdce_profile() function returns a profile that can be used to connect to the mdce cluster for your job. You can then use matlabpoolor pmode, or spmd etc... to startup parallel computations across the matlab cluster. profile=mdce_profile() matlabpool('open', profile) parfor n=1:matlabpool('size') H=hilb(n); Z=fft2(H); imagesc(log(abs(Z))); print('-dpdf','-r300',sprintf('%s%03d%s','fig1-batch',n,'.pdf')); end matlabpool('close') mdce_script.m

  37. Using the Matlab Distributed Compute Engine For interactive mode, you can use the qMatlab_mdce script. This script will inherit your matlab path from your environment, so be sure to load the matlab-R2013a-local module to speed up the initilization of the cluster. This will create a matlab cluster which in this case consists of 4 nodes each with 8 workers and 16 GB of memory per. To use the matab cluster, load the profile using the mdce_profile() function and then open the pool of workers with matlabpool– or pmode etc... mkdir/scratch/jcarrol5/matlab_mdce cd /scratch/jcarrol5/matlab_mdce module load matlab-R2013a-local qMatlab_mdce 4 8 16 profile=mdce_profile() matlabpool('open', profile)

  38. Outline Part I – Interacting with Matlab • Running Matlab interactively • Accessing the GUI • Using the terminal for command entry • Using just the terminal • Running Matlab in batch mode • Using PBS job arrays to do embarrassingly parallel computations Part II – Speeding up Matlab Computations • Symmetric Multi-Processing with Matlab • Accelerating Matlab computations with GPUs • Running Matlab in distributed memory environments • Using the Parallel Computing Toolbox • Using the Matlab Distributed Compute Engine Server • Using pMatlab Part III – Mixing Matlab and Fortran/C code • Compiling MEX code from C/Fortran • Turning Matlab routines into C code

  39. Using pMatlab • pMatlab is an alternative method to get distributed matlab functionality without relying on Matlab’s Distributed Computing Server. • It is built on top of MapMPI (an MPI implementation for matlab – written in matlab - that uses file I/O for communication) • It supports various operations on distributed arrays (up to 4D) • Remapping, aggregating, finding non-zero entries, transposing, ghosting • Elementary math functions (trig, exponential, complex, remainder/rounding) • 2D Convolutions, FFTs, Discrete Cosine Transform • FFT's need to be properly mapped (cannot be distributed along transform dimension). • It does not have as much functionality as the parallel computing toolbox – but it does support ghosting and more flexible partitioning!

More Related