function mcr_run %=============================================================================== % MATLAB MPMD EXAMPLE: RUNTIME CODE % % This function will be compiled by Matlab Compiler. This function % - summarizes some configuration steps, e. g. defining dependencies % - calls the Matlab function implemented by the user % % This function calls matmul_serial.m (matrix-matrix-multiplication with % threading support) as dependency 1 and a plotting function as dependency 2. % % Each MPI process will run this code by evaluating its own rank. %=============================================================================== %% ============================================================================= % Each process will use the MPI rank as task identifier. % Get my MPI rank from the MPI environment. %% ============================================================================= myrank = str2num(getenv('PMI_RANK')); %% ============================================================================= % Create a log file for this task to avoid cluttered output of all MPI tasks in % the Slurm job output file. %% ============================================================================= fp = fopen(sprintf('data/JOB_%s_TaskID_%d.log', getenv('SLURM_JOB_ID'), myrank), 'w'); %=============================================================================== % All processes say hello. % Show some information, also useful for troubleshooting %=============================================================================== fprintf(fp, 'MPI process rank=%d: I run on compute node %s and will handle task %d.\n', ... myrank, getenv('HOSTNAME'), myrank); fprintf(fp, 'MATLAB temporary directory = %s\n', tempdir); fprintf(fp, 'MCR_CACHE_ROOT = %s\n', getenv('MCR_CACHE_ROOT')); fprintf(fp, 'MCR root dir = %s\n', ctfroot); %=============================================================================== % Run user code. % Scale result by taskID to show MPMD effect. %=============================================================================== % get sizes of input matrices for computation of C = A*B from commandline % arguments size_A = str2num(getenv('SIZE_MATRIX_A')); size_B = str2num(getenv('SIZE_MATRIX_B')); [C, comptime] = matmul_serial(size_A, size_B); C = C * myrank; fprintf(fp, '(rank=%d) computation time = %.4f s.\n', myrank, comptime); fprintf(fp, '\tcheck (mean of elements scaled by MPI rank): %f\n', mean(C(:))); fprintf('myrank=%d: check (mean of elements of C = A*B scaled by MPI rank): %f\n', myrank, mean(C(:))); %% ============================================================================= % close log %% ============================================================================= fclose(fp);