% create a local cluster object pc = parcluster('local') % explicitly set the JobStorageLocation to % the temp directory that is unique to each cluster job pc.JobStorageLocation = getenv('TMPDIR') % start the matlabpool with maximum available workers parpool (pc, str2num(getenv('SLURM_CPUS_ON_NODE'))) R = 1 darts = 1e7 count = 0 tic parfor i = 1:darts x = R*rand(1) y = R*rand(1) if x^2 y^2 <= R^2 count = count 1 end end myPI = 4*count/darts T = toc fprintf('The computed value of pi is %8.7f\n',myPI) fprintf('The parallel Monte-Carlo method is executed in %8.2f seconds\n', T) delete(gcp) exit