10.07.2015 Views

Run Matlab Code in Batch Mode on GPU of HPC - HPC at UCI / OIT

Run Matlab Code in Batch Mode on GPU of HPC - HPC at UCI / OIT

Run Matlab Code in Batch Mode on GPU of HPC - HPC at UCI / OIT

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<str<strong>on</strong>g>Run</str<strong>on</strong>g> <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> <str<strong>on</strong>g>Code</str<strong>on</strong>g> <str<strong>on</strong>g>in</str<strong>on</strong>g> <str<strong>on</strong>g>B<strong>at</strong>ch</str<strong>on</strong>g> <str<strong>on</strong>g>Mode</str<strong>on</strong>g> <strong>on</strong> <strong>GPU</strong> <strong>of</strong> <strong>HPC</strong>Shiwei LanAugust 30, 2012This short note illustr<strong>at</strong>es technical issues runn<str<strong>on</strong>g>in</str<strong>on</strong>g>g <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> code <str<strong>on</strong>g>in</str<strong>on</strong>g> b<strong>at</strong>ch mode <strong>on</strong><strong>GPU</strong> <strong>of</strong> <strong>HPC</strong>. It <strong>on</strong>ly summaries pers<strong>on</strong>al experience <strong>on</strong> Mac OS X and the best helperis always google.1 <str<strong>on</strong>g>Run</str<strong>on</strong>g> <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> <str<strong>on</strong>g>in</str<strong>on</strong>g> Interactive <str<strong>on</strong>g>Mode</str<strong>on</strong>g>It is sometimes necessary to run <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> <str<strong>on</strong>g>in</str<strong>on</strong>g> <str<strong>on</strong>g>in</str<strong>on</strong>g>teractive mode for test<str<strong>on</strong>g>in</str<strong>on</strong>g>g and debugg<str<strong>on</strong>g>in</str<strong>on</strong>g>gpurposes. This is especially true when your <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> code handles large d<strong>at</strong>a set andrequires higher comput<str<strong>on</strong>g>in</str<strong>on</strong>g>g power like <strong>GPU</strong>. To do so, you have to log <strong>on</strong> <strong>GPU</strong> node <str<strong>on</strong>g>in</str<strong>on</strong>g><str<strong>on</strong>g>in</str<strong>on</strong>g>teractive way via x11 (x-quartz for Mounta<str<strong>on</strong>g>in</str<strong>on</strong>g> Li<strong>on</strong>, but you have to <str<strong>on</strong>g>in</str<strong>on</strong>g>stall it):ssh -Y youraccount@hpc.oit.uci.eduAfter <str<strong>on</strong>g>in</str<strong>on</strong>g>putt<str<strong>on</strong>g>in</str<strong>on</strong>g>g password you are <strong>at</strong> log-<str<strong>on</strong>g>in</str<strong>on</strong>g> node, then go to <strong>GPU</strong> node byqrsh -q gpuThen you have to load <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g>, available versi<strong>on</strong> 2011b:module load MATLAB/r2011bAnd then enter <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> <str<strong>on</strong>g>in</str<strong>on</strong>g> <str<strong>on</strong>g>in</str<strong>on</strong>g>teractive way:m<strong>at</strong>labAfter the above command, a <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> w<str<strong>on</strong>g>in</str<strong>on</strong>g>dow will pop-up and you can debug any<str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> code as you do locally.1


Note <strong>on</strong> runn<str<strong>on</strong>g>in</str<strong>on</strong>g>g <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> code <strong>on</strong> <strong>GPU</strong>2 <str<strong>on</strong>g>Run</str<strong>on</strong>g> <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> <str<strong>on</strong>g>Code</str<strong>on</strong>g> <str<strong>on</strong>g>in</str<strong>on</strong>g> <str<strong>on</strong>g>B<strong>at</strong>ch</str<strong>on</strong>g> <str<strong>on</strong>g>Mode</str<strong>on</strong>g>It is cool to run <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> code <str<strong>on</strong>g>in</str<strong>on</strong>g> b<strong>at</strong>ch mode so th<strong>at</strong> you d<strong>on</strong>’t have to wait <str<strong>on</strong>g>in</str<strong>on</strong>g> <str<strong>on</strong>g>in</str<strong>on</strong>g>teractivemode without clos<str<strong>on</strong>g>in</str<strong>on</strong>g>g your computer. However, you CANNOT directly wrap yourcommand ’m<strong>at</strong>lab -r yourcode.m’ <str<strong>on</strong>g>in</str<strong>on</strong>g> bash file because <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> w<strong>on</strong>’t let it run. You haveto compile it first.In the follow<str<strong>on</strong>g>in</str<strong>on</strong>g>g I quote an illustr<strong>at</strong>i<strong>on</strong> from BDUC(17.9) to show how to compile<str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> code us<str<strong>on</strong>g>in</str<strong>on</strong>g>g <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> command mcc:3 <str<strong>on</strong>g>Run</str<strong>on</strong>g> <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> <str<strong>on</strong>g>Code</str<strong>on</strong>g> <strong>on</strong> <strong>GPU</strong>Unlike CUDA, <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> provides easy <str<strong>on</strong>g>in</str<strong>on</strong>g>terface to distribute d<strong>at</strong>a to <strong>GPU</strong> by simplyclaim<str<strong>on</strong>g>in</str<strong>on</strong>g>g your d<strong>at</strong>a as ’gpuArray’. Any supported rout<str<strong>on</strong>g>in</str<strong>on</strong>g>e <str<strong>on</strong>g>in</str<strong>on</strong>g>volv<str<strong>on</strong>g>in</str<strong>on</strong>g>g gpuArray d<strong>at</strong>a (notall particip<strong>at</strong><str<strong>on</strong>g>in</str<strong>on</strong>g>g d<strong>at</strong>a are required to be gpuArray) will be autom<strong>at</strong>ically calcul<strong>at</strong>edAugust 30, 2012 2


Note <strong>on</strong> runn<str<strong>on</strong>g>in</str<strong>on</strong>g>g <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> code <strong>on</strong> <strong>GPU</strong><strong>on</strong> <strong>GPU</strong>. By now, <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> 2012a has a list <strong>of</strong> rout<str<strong>on</strong>g>in</str<strong>on</strong>g>es like ’<str<strong>on</strong>g>in</str<strong>on</strong>g>v’, ’chol’, ’fft’ etc. support<str<strong>on</strong>g>in</str<strong>on</strong>g>ggpuArray. For those not yet support<str<strong>on</strong>g>in</str<strong>on</strong>g>g gpuArray, you can always first collectby command ’g<strong>at</strong>her’ the gpuArray as regular type d<strong>at</strong>a and then do the calcul<strong>at</strong>i<strong>on</strong>with regular d<strong>at</strong>a. A start<str<strong>on</strong>g>in</str<strong>on</strong>g>g helper would be the <strong>of</strong>ficial <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> page about <strong>GPU</strong>:http://www.m<strong>at</strong>hworks.com/help/toolbox/distcomp/bsic4fr-1.html, where you can als<strong>of</strong><str<strong>on</strong>g>in</str<strong>on</strong>g>d the list <strong>of</strong> rout<str<strong>on</strong>g>in</str<strong>on</strong>g>es support<str<strong>on</strong>g>in</str<strong>on</strong>g>g gpuArray.Another important <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> command regard<str<strong>on</strong>g>in</str<strong>on</strong>g>g <strong>GPU</strong> is ’gpuDevice’, with whichyou can get <str<strong>on</strong>g>in</str<strong>on</strong>g>form<strong>at</strong>i<strong>on</strong> <strong>of</strong> the <strong>GPU</strong> cards such as ’name’, ’<str<strong>on</strong>g>in</str<strong>on</strong>g>dex’, ’FreeMemory’ etc.Currently there are 4 <strong>GPU</strong> cards <strong>on</strong> the <strong>GPU</strong> node <strong>of</strong> <strong>HPC</strong>, by default the <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> willchoose the first available <strong>GPU</strong> card, however this can be changed by specify<str<strong>on</strong>g>in</str<strong>on</strong>g>g <strong>GPU</strong>card <str<strong>on</strong>g>in</str<strong>on</strong>g>dex i: ’gpuDevice(i)’, especially helpful after you <str<strong>on</strong>g>in</str<strong>on</strong>g>quire the memory left from’FreeMemory’. Refer to the <strong>of</strong>ficial document <strong>on</strong> gpuDevice.Below is a sample <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> code run <strong>on</strong> <strong>GPU</strong>:functi<strong>on</strong>[]=<str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g><str<strong>on</strong>g>Code</str<strong>on</strong>g>()gpuDevice(2) % choose the sec<strong>on</strong>d <strong>GPU</strong> cardD<strong>at</strong>a = load(’XX.m<strong>at</strong>’); % D<strong>at</strong>a records two l<strong>on</strong>g vectors x, y <strong>of</strong> size 1e4x=gpuArray(D<strong>at</strong>a.x); y=D<strong>at</strong>a.y; % x is <strong>on</strong> <strong>GPU</strong> card but y is notz=x.*y; % This will be d<strong>on</strong>e <strong>on</strong> <strong>GPU</strong> card and z is also stored as gpuArrayw=y’*repm<strong>at</strong>(g<strong>at</strong>her(z),1,10); % S<str<strong>on</strong>g>in</str<strong>on</strong>g>ce repe<strong>at</strong> is not yet support<str<strong>on</strong>g>in</str<strong>on</strong>g>g gpuArray <strong>on</strong> m<strong>at</strong>lab 2011b,we have to g<strong>at</strong>her z from <strong>GPU</strong> so it can be applied to repm<strong>at</strong> and calcul<strong>at</strong>ed as regular d<strong>at</strong>a.end4 <str<strong>on</strong>g>Run</str<strong>on</strong>g> <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> <str<strong>on</strong>g>Code</str<strong>on</strong>g> <str<strong>on</strong>g>in</str<strong>on</strong>g> <str<strong>on</strong>g>B<strong>at</strong>ch</str<strong>on</strong>g> <str<strong>on</strong>g>Mode</str<strong>on</strong>g> <strong>on</strong> <strong>GPU</strong><strong>GPU</strong> node is now under Grid Eng<str<strong>on</strong>g>in</str<strong>on</strong>g>e, the same as BDUC, which is good for thosefamiliar with BDUC or GE. Remember ’module load GE’ if you have not set it default<str<strong>on</strong>g>in</str<strong>on</strong>g> your ’.bashrc’. Now we are ready to run compiled <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> code <str<strong>on</strong>g>in</str<strong>on</strong>g> b<strong>at</strong>ch mode <strong>on</strong><strong>GPU</strong> by submitt<str<strong>on</strong>g>in</str<strong>on</strong>g>g your job to queue ’gpu’.A bash script do<str<strong>on</strong>g>in</str<strong>on</strong>g>g all th<str<strong>on</strong>g>in</str<strong>on</strong>g>g (compil<str<strong>on</strong>g>in</str<strong>on</strong>g>g <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> code, specify<str<strong>on</strong>g>in</str<strong>on</strong>g>g request <strong>of</strong> b<strong>at</strong>chjob) together is provided as follows:August 30, 2012 3


Note <strong>on</strong> runn<str<strong>on</strong>g>in</str<strong>on</strong>g>g <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> code <strong>on</strong> <strong>GPU</strong>#!/b<str<strong>on</strong>g>in</str<strong>on</strong>g>/bash#$ -N m<strong>at</strong>labjobname # this name shows <str<strong>on</strong>g>in</str<strong>on</strong>g> qst<strong>at</strong>#$ -q gpu # submit job to queue ’gpu’#$ -l gpu=1 # request 1 <strong>GPU</strong> card#$ -pe gpu-node-cores 6 # run with parallel envir<strong>on</strong>ment ’gpu-node-core’ request<str<strong>on</strong>g>in</str<strong>on</strong>g>g 6 nodecorescd yourwork<str<strong>on</strong>g>in</str<strong>on</strong>g>gdirectory # specify your work<str<strong>on</strong>g>in</str<strong>on</strong>g>g directorymodule load MATLAB/r2011b # load the MATLAB envir<strong>on</strong>mentmcc -m m<strong>at</strong>labcode.m -I directorytoothernecessarydocs; # compile your <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> code./run m<strong>at</strong>labcode.sh /d<strong>at</strong>a/apps/MATLAB/r2011b # run the gener<strong>at</strong>ed bash to let run your<str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> code <str<strong>on</strong>g>in</str<strong>on</strong>g> b<strong>at</strong>ch modeNow save your script as ’m<strong>at</strong>lab-b<strong>at</strong>ch.sh’ <str<strong>on</strong>g>in</str<strong>on</strong>g> the same folder <strong>of</strong> your <str<strong>on</strong>g>M<strong>at</strong>lab</str<strong>on</strong>g> code,and submit it <str<strong>on</strong>g>in</str<strong>on</strong>g> your x-11 or other term<str<strong>on</strong>g>in</str<strong>on</strong>g>al:qsub m<strong>at</strong>lab-b<strong>at</strong>ch.shAnd you can always check the st<strong>at</strong>us <strong>of</strong> your job byqst<strong>at</strong> -u useridError and out put files with same name ’m<strong>at</strong>lab-b<strong>at</strong>ch.e (or o)’ will also be gener<strong>at</strong>ed<str<strong>on</strong>g>in</str<strong>on</strong>g> the same folder for your reference.Hooray! Enjoy!August 30, 2012 4

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!