attachment:cbu_scheduler_example.m of MatlabLecturesSchedule - Methods
location: attachment:cbu_scheduler_example.m of MatlabLecturesSchedule

Attachment 'cbu_scheduler_example.m'

Download

   1 % Parallel processing in Matlab workshop
   2 % Darren Price, CBU, Cambridge 2017
   3 
   4 clear
   5 close all
   6 clc
   7 
   8 % CBU Cluster Example, running independent jobs
   9 
  10 addpath /hpc-software/matlab/cbu/
  11 
  12 S = cbu_scheduler();
  13 S.NumWorkers = 2;
  14 S.SubmitArguments = '-l mem=1GB -l walltime=1:00:00';
  15 
  16 % You should first determine how much memory and how many hours you need to
  17 % run your code.
  18 
  19 %% Job1 will output 1 argument
  20 J = [];
  21 J.task = @(x) rand(x);
  22 J.n_return_values = 1;
  23 J.input_args = {5};
  24 J.depends_on = 0;
  25 
  26 cbu_qsub(J, S)
  27 
  28 % You will find all your job information in the Job folders (example
  29 % below). You might need to wait some time for the files to be saved
  30 pause(20)
  31 IN = load(sprintf('%s/Job1/Task1.in.mat',S.JobStorageLocation));
  32 OUT = load(sprintf('%s/Job1/Task1.out.mat',S.JobStorageLocation));
  33 
  34 S.JobStorageLocation = '/home/dp01/teaching/cbu_parallel_workshop/';
  35 
  36 
  37 %% Job2 will display the result with no output args
  38 
  39 % clear all job folders using a unix command
  40 !rm -Rfv /home/dp01/teaching/cbu_parallel_workshop/Job*
  41 
  42 J = [];
  43 J.task = @(x) disp(rand(x));
  44 J.n_return_values = 0; % important
  45 J.input_args = {5};
  46 J.depends_on = 0;
  47 
  48 ID = cbu_qsub(J, S);
  49 
  50 % Check the diary. This contains output normally sent to the command
  51 % window. This is useful for debugging if something goes wrong.
  52 
  53 % this is how to construct a unix string based on the job id and storage location
  54 % This is the recommended way to create strings for loading and saving files in
  55 % matlab in general see "help sprintf"
  56 [~,t] = unix(sprintf('more %s/Job%d/Task1.diary.txt', S.JobStorageLocation, ID));
  57 disp(t) % Print output to the screen
  58 
  59 
  60 
  61 %% Job3 will use an external function to save variables to specified
  62 % location. You should create a function (using a file) that takes two
  63 % input arguments (one of which being the save path)
  64 
  65 
  66 % NOTE: Here is the code for fun1.m. You need to save this to the same folder as
  67 % cbu_scheduler_example.m
  68 % 
  69 % function [x, y] = fun1(z, subjectid)
  70 % 
  71 %     x = z*2;
  72 %     y = z*3;
  73 %     
  74 % save(sprintf('/imaging/dp01/output_subject_%s.mat',subjectid),'x','y')
  75 
  76 % fun1.m cannot use plotting functions. Instead save the data to disk and
  77 % plot locally with a graphics enabled instance of matlab
  78 
  79 % Note, below we have added a addtitional paths. You can add multiple paths
  80 % in a cell array. You need to add the paths for each element of J. You
  81 % generally only need to add one path at a time.
  82 
  83 J = [];
  84 J(1).task = @fun1;
  85 J(1).AdditionalPaths = {'/home/dp01/teaching/cbu_parallel_workshop' '/path/two' '/path/three'}; 
  86 J(1).n_return_values = 2; % important
  87 J(1).input_args = {10, 'ID1'};
  88 J(1).depends_on = 0;
  89 
  90 J(2).task = @fun1;
  91 J(2).AdditionalPaths = '/home/dp01/teaching/cbu_parallel_workshop';
  92 J(2).n_return_values = 2; % important
  93 J(2).input_args = {10, 'ID2'};
  94 J(2).depends_on = 0;
  95 
  96 J(3).task = @fun1;
  97 J(3).AdditionalPaths = '/home/dp01/teaching/cbu_parallel_workshop';
  98 J(3).n_return_values = 2; % important
  99 J(3).input_args = {10, 'ID3'};
 100 J(3).depends_on = 0;
 101 
 102 ID = cbu_qsub(J, S);
 103 
 104 pause(20)
 105 out = load(sprintf('%s/Job%d/Task1.out.mat', S.JobStorageLocation, ID(1)));
 106 
 107 
 108 %% Create J in a loop with additional paths
 109 
 110 IDs = {'ID1' 'ID2' 'ID3'};
 111 for ii = 1:3
 112     J(ii).task = @fun1;
 113     J(ii).AdditionalPaths = {'/home/dp01/teaching/cbu_parallel_workshop'};
 114     J(ii).n_return_values = 2; % important
 115     J(ii).input_args = {10, IDs{ii}};
 116     J(ii).depends_on = 0;
 117 end
 118 
 119 ID = cbu_qsub(J, S);
 120 
 121 % Load 1 of the job output .mat files
 122 pause(20)
 123 out = load(sprintf('%s/Job%d/Task1.out.mat', S.JobStorageLocation, ID(1)))
 124 
 125 
 126 %% parfor loop (recommended for smaller jobs)
 127 clear 
 128 
 129 % cbupool is a wrapper for parpool with corrected settings
 130 delete(gcp)
 131 cbupool(3)
 132 parfor ii = 1:3
 133   % build magic squares in parallel
 134   q{ii} = magic(ii + 2);
 135 end
 136 
 137 for ii=1:length(q)
 138   % plot each magic square
 139   figure, imagesc(q{ii});
 140 end
 141 
 142 
 143 % Things that won't work
 144 
 145 % 1. Index must be consecutive integers
 146 parfor ii = [1:2:10]
 147     n = f(ii);
 148   % build magic squares in parallel
 149   q{ii} = magic(n + 2);
 150 end
 151 
 152 % Instead, use 
 153 f = 1:2:10;
 154 parfor ii = 1:5
 155     n = f(ii);
 156   % build magic squares in parallel
 157   q{ii} = magic(n + 2);
 158 end
 159 
 160 
 161 % Using an index of an index is BAD, and the error message is quite
 162 % unambiguous. "Error: The variable q in a parfor cannot be classified."
 163 cheatindex = [10:-1:1];
 164 parfor ii = 1:10
 165   % build magic squares in parallel
 166   q{cheatindex(ii)} = magic(ii + 2);
 167   disp(labindex)
 168 end
 169 
 170 
 171 % For most jobs taking a few hours, you can use parfor to run a function
 172 % taht contains your entire analysis code.
 173 IDlist = {'ID1' 'ID2' 'ID3'};
 174 NumericalInput = [1 4 6]; % just some arbitrary numbers
 175 parfor ii = 1:3
 176     [x(ii), y(ii)] = fun1(NumericalInput(ii), IDlist{ii}) % this was also save variables to disk
 177 end
 178 
 179 disp(x)
 180 disp(y)
 181 ls /imaging/dp01/output_subject_*
 182 % results in
 183 % /imaging/dp01/output_subject_ID1.mat  
 184 % /imaging/dp01/output_subject_ID2.mat
 185 % /imaging/dp01/output_subject_ID3.mat
 186 
 187 
 188 %% cbupool with Arguments (example for a very large job)
 189 % You should not request large amounts of resources unless you need them.
 190 % This increases the chances of your job crashing unexpectedly and also
 191 % takes longer to start.
 192 delete(gcp)
 193 P=cbupool(96);
 194 P.ResourceTemplate='-l nodes=^N^,mem=196GB,walltime=96:00:00';
 195 parpool(P) 
 196 
 197 
 198 % For smaller jobs (i.e. 20 subjects taking 2 hours each) use this. 2GB per worker is
 199 % usually enough, but you should calculate this before hand to ensure you
 200 % request enough memory.
 201 
 202 delete(gcp)
 203 P=cbupool(20);
 204 P.ResourceTemplate='-l nodes=^N^,mem=40GB,walltime=4:00:00';
 205 parpool(P) 
 206 
 207 % put your parfor loop here
 208 
 209 delete(gcp) % Important to delete it when you are finished to free up resources for other users
 210 
 211 %% Inspecting gcp
 212 % You can check it's methods
 213 methods(gcp)
 214 
 215 % addAttachedFiles       disp                   listAutoAttachedFiles  parfevalOnAll          
 216 % delete                 display                parfeval               updateAttachedFiles    
 217 
 218 % in order to run any methods of gcp you need to assign it to a variable
 219 
 220 g = gcp;
 221 
 222 g.listAutoAttachedFiles
 223 
 224 
 225 %% Other examples from the intranet page http://intranet.mrc-cbu.cam.ac.uk/computing/cluster-demo
 226 cd /hpc-software/example_scripts/workshop/
 227 edit matlab_scheduler.m
 228 edit matlab_analysis.m 

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2015-10-14 11:15:44, 737.5 KB) [[attachment:1_CBUComputing.pdf]]
  • [get | view] (2017-06-20 13:12:05, 1993.2 KB) [[attachment:1_CBUComputing2016.pdf]]
  • [get | view] (2017-10-11 18:16:13, 1942.0 KB) [[attachment:1_CBU_Computing.pdf]]
  • [get | view] (2017-10-11 18:11:29, 3455.5 KB) [[attachment:1_CBU_Computing.ppt]]
  • [get | view] (2015-10-09 15:29:53, 319.2 KB) [[attachment:2_Linux.pdf]]
  • [get | view] (2015-10-09 15:30:29, 5009.7 KB) [[attachment:2_Linux.zip]]
  • [get | view] (2014-12-04 10:03:39, 3958.5 KB) [[attachment:3_MATLAB.zip]]
  • [get | view] (2014-12-04 10:04:18, 212.0 KB) [[attachment:3_Parallel.pdf]]
  • [get | view] (2021-10-12 09:44:32, 1427.1 KB) [[attachment:Computing_11Oct21.pdf]]
  • [get | view] (2012-11-19 16:35:37, 2434.4 KB) [[attachment:Demo.zip]]
  • [get | view] (2015-12-02 13:45:02, 26.8 KB) [[attachment:ExampleScripts.zip]]
  • [get | view] (2013-12-17 18:24:09, 11.1 KB) [[attachment:Examples and exercises]]
  • [get | view] (2012-12-04 09:06:39, 9.8 KB) [[attachment:Examples.zip]]
  • [get | view] (2012-11-27 17:48:52, 11344.7 KB) [[attachment:ExamplesOlaf.zip]]
  • [get | view] (2013-12-17 18:28:39, 11.1 KB) [[attachment:Examples_and_exercises.zip]]
  • [get | view] (2015-11-24 17:37:13, 11.9 KB) [[attachment:Examples_and_exercises_2015.zip]]
  • [get | view] (2016-11-29 15:02:26, 11.8 KB) [[attachment:Examples_and_exercises_2016.zip]]
  • [get | view] (2016-11-30 11:57:48, 12.2 KB) [[attachment:Examples_and_exercises_2016_djm.zip]]
  • [get | view] (2017-11-14 12:21:56, 3158.3 KB) [[attachment:Examples_and_exercises_2017_djm.zip]]
  • [get | view] (2019-11-05 14:51:00, 12.7 KB) [[attachment:Examples_and_exercises_2019_djm.zip]]
  • [get | view] (2019-11-05 15:23:31, 12.9 KB) [[attachment:Examples_and_exercises_2019_djm2.zip]]
  • [get | view] (2019-11-05 15:28:25, 13.0 KB) [[attachment:Examples_and_exercises_2019_djm3.zip]]
  • [get | view] (2019-11-08 09:38:24, 11.2 KB) [[attachment:Examples_and_exercises_2019_djm4.zip]]
  • [get | view] (2017-11-14 12:24:35, 12.1 KB) [[attachment:Examples_and_exercises_Nov2017_djm.zip]]
  • [get | view] (2019-11-11 10:01:12, 903.9 KB) [[attachment:Getting Started Nov2019.pdf]]
  • [get | view] (2012-11-13 13:34:09, 386.8 KB) [[attachment:Handouts.pdf]]
  • [get | view] (2014-11-04 16:08:20, 1455.9 KB) [[attachment:IntroMatrixAlgebra.zip]]
  • [get | view] (2015-11-18 17:14:47, 932.3 KB) [[attachment:IntroMatrixAlgebra_Matlab_18Nov15.pdf]]
  • [get | view] (2015-11-18 17:18:06, 1513.6 KB) [[attachment:IntroMatrixAlgebra_Matlab_18Nov15.zip]]
  • [get | view] (2013-10-30 18:24:19, 571.7 KB) [[attachment:Intro_to_CBU_computing_2013.pdf]]
  • [get | view] (2013-12-11 12:54:22, 1994.6 KB) [[attachment:JTaylorMatlabLectureDemo.zip]]
  • [get | view] (2012-11-19 16:37:32, 1024.9 KB) [[attachment:Lecture.pdf]]
  • [get | view] (2012-11-19 16:36:10, 1024.9 KB) [[attachment:Lecture.zip]]
  • [get | view] (2012-11-13 17:46:26, 380.9 KB) [[attachment:LectureHandouts.pdf]]
  • [get | view] (2012-11-27 17:47:55, 1224.2 KB) [[attachment:LectureOlaf.pdf]]
  • [get | view] (2012-11-13 17:46:46, 830.7 KB) [[attachment:LectureSlides.pdf]]
  • [get | view] (2017-10-27 09:25:00, 201.3 KB) [[attachment:LinuxClass_JeffBerry_19Oct17.pdf]]
  • [get | view] (2018-10-22 12:48:26, 721.5 KB) [[attachment:LinuxClass_JeffBerry_22Oct18.pdf]]
  • [get | view] (2016-10-27 08:33:27, 207.2 KB) [[attachment:LinuxClass_JeffBerry_26Oct16.pdf]]
  • [get | view] (2016-10-27 09:01:40, 4178.5 KB) [[attachment:LinuxClass_JeffBerry_26Oct16_material.tar.gz]]
  • [get | view] (2021-10-12 09:44:37, 829.5 KB) [[attachment:LinuxPart1_11Oct21.pdf]]
  • [get | view] (2021-10-12 09:44:42, 684.8 KB) [[attachment:LinuxPart2_11Oct21.pdf]]
  • [get | view] (2016-11-21 10:18:53, 488.3 KB) [[attachment:MATLAB Basics 16Nov16.pdf]]
  • [get | view] (2019-11-06 17:33:48, 16.8 KB) [[attachment:MATLAB_basic_commands_2_exercises.docx]]
  • [get | view] (2019-11-06 17:36:48, 480.3 KB) [[attachment:MATLAB_basic_commands_2_exercises.pdf]]
  • [get | view] (2014-07-31 12:11:41, 357.8 KB) [[attachment:Matlab - SPM-AA.pdf]]
  • [get | view] (2017-11-01 11:22:33, 466.9 KB) [[attachment:Matlab Basic Commands - handouts]]
  • [get | view] (2017-11-01 11:22:45, 576.0 KB) [[attachment:Matlab Basic Commands - slides]]
  • [get | view] (2018-10-23 16:10:59, 2.4 KB) [[attachment:MatlabBasicsCommands_ Delia_23Oct18.zip]]
  • [get | view] (2018-10-23 16:12:21, 1192.1 KB) [[attachment:MatlabBasicsCommands_Delia_23Oct18.pdf]]
  • [get | view] (2014-11-14 11:28:06, 269.4 KB) [[attachment:MatlabVisualisation-Handouts.pdf]]
  • [get | view] (2014-11-14 11:28:21, 314.9 KB) [[attachment:MatlabVisualisation-Slides.pdf]]
  • [get | view] (2014-11-14 11:28:41, 541.0 KB) [[attachment:MatlabVisualisation_examples.zip]]
  • [get | view] (2018-10-23 16:09:45, 695.6 KB) [[attachment:Matlab_Basic_introduction_AndreaGreve_23Oct18.pdf]]
  • [get | view] (2012-11-26 19:00:14, 11342.9 KB) [[attachment:OlafExamples.zip]]
  • [get | view] (2012-11-26 18:59:49, 1224.0 KB) [[attachment:OlafLecture.pdf]]
  • [get | view] (2013-12-12 12:02:04, 878.5 KB) [[attachment:OlafMatrixAlgebraPresentation.pdf]]
  • [get | view] (2013-12-12 12:01:39, 3.6 KB) [[attachment:OlafMatrixAlgebraScript.m]]
  • [get | view] (2012-11-13 13:35:16, 2.5 KB) [[attachment:Scripts.zip]]
  • [get | view] (2015-12-02 12:17:56, 26.8 KB) [[attachment:ScriptsVisualization]]
  • [get | view] (2012-11-13 13:35:07, 647.1 KB) [[attachment:Slides.pdf]]
  • [get | view] (2017-11-01 11:28:12, 466.9 KB) [[attachment:YaaraMatlabBasicCommandsHandouts.pdf]]
  • [get | view] (2017-11-01 11:24:21, 4.7 KB) [[attachment:YaaraMatlabBasicCommandsScripts.zip]]
  • [get | view] (2017-11-01 11:28:25, 576.0 KB) [[attachment:YaaraMatlabBasicCommandsSlides.pdf]]
  • [get | view] (2015-10-27 15:04:53, 2.6 KB) [[attachment:YaaraMatlabBasics.zip]]
  • [get | view] (2015-10-27 15:05:37, 448.8 KB) [[attachment:YaaraMatlabBasicsHandouts.pdf]]
  • [get | view] (2015-10-27 15:06:10, 564.9 KB) [[attachment:YaaraMatlabBasicsSlides.pdf]]
  • [get | view] (2019-11-06 17:35:08, 718.0 KB) [[attachment:basic_commands_2_2019.pdf]]
  • [get | view] (2019-11-06 17:33:38, 2583.2 KB) [[attachment:basic_commands_2_2019.pptx]]
  • [get | view] (2013-03-18 18:22:07, 5.0 KB) [[attachment:batch_spm5_1stlevel(1).m]]
  • [get | view] (2017-11-30 12:46:52, 6.0 KB) [[attachment:cbu_scheduler_example.m]]
  • [get | view] (2014-12-10 13:21:59, 292.6 KB) [[attachment:computing_pres2014_cluster.pdf]]
  • [get | view] (2013-12-17 18:33:34, 11.1 KB) [[attachment:examples_and_exercises]]
  • [get | view] (2013-12-17 18:34:35, 11.1 KB) [[attachment:examples_and_exercises.zip]]
  • [get | view] (2015-11-24 17:32:11, 11.9 KB) [[attachment:examples_and_exercises_2015.zip]]
  • [get | view] (2012-12-04 16:11:47, 1338.4 KB) [[attachment:lecture]]
  • [get | view] (2016-12-07 17:33:36, 1032.1 KB) [[attachment:matlabWorkshop_viz_Kate.pdf]]
  • [get | view] (2017-11-22 11:10:30, 1310.9 KB) [[attachment:matlabWorkshop_viz_Kate2017.pdf]]
  • [get | view] (2019-11-07 17:32:52, 2156.8 KB) [[attachment:matlab_lecture_Nov_2019_post.pdf]]
  • [get | view] (2019-11-08 09:40:29, 3155.9 KB) [[attachment:matlab_lecture_Nov_2019_post.pptx]]
  • [get | view] (2013-02-04 14:58:14, 3.2 KB) [[attachment:mrclogo.gif]]
  • [get | view] (2019-11-08 19:18:11, 7.5 KB) [[attachment:plottingExampleCode_ljb_20191108.m]]
  • [get | view] (2019-11-08 19:16:48, 1604.9 KB) [[attachment:visualisationInMatlab_ljb_slides_20191109.pdf]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.