Scientific Computing & Visualization
Help Contact
About Accounts Computation Visualization Documentation Services

Running Multiple MATLAB Tasks

Described in the following is a procedure to run multiple independent MATLAB tasks in a single batch job. Typically, users who want to run MATLAB jobs in this mode do so for parametric studies of the same code under different conditions. These conditions are often controlled by an index or an input file. Since multiple copies of MATLAB are processed concurrently, the computed results from each must be saved in a distinct file. Shown below is a sample batch script that is sufficiently general to accomodate a great variety of situations. This method supercedes an earlier method for the same onjective. It is still available.

  1. This new batch script is called mplbatch ("l" stands for Linux to distincguish it from the Katana Cluster version, mpkbatch).
    #!/bin/csh
    # All PBS batch scheduler (on Linux Cluster) commands start with #PBS
    # The following requests 4 processors (see scv.bu.edu for details)
    # It can be overridden, for example, with "qsub mplbatch -l nodes=4:ppn=2"
    #PBS -l nodes=2:ppn=2
    # IMPORTANT: DONOT precede the above with any non-PBS statements
    unsetenv DISPLAY
    echo CWD is $PBS_O_WORKDIR
    echo Nodes assigned are :
    cat $PBS_NODEFILE
    # cd to the dir from which this script is launched; or provide your own
    cd $PBS_O_WORKDIR
    set nproc = `wc -l $PBS_NODEFILE |  awk '{print $1}'`
    echo number of procs is $nproc
    @ rank = 0
    # IMPORTANT: DONOT indent any of the below statements
    foreach node (`cat $PBS_NODEFILE`)
    matlab -nojvm  >! myoutput_$rank << MATLAB_ENV &
    myrank = $rank              % Between MATLAB_ENVs, carry on any MATLAB ops
    outname = ['output.' num2str(myrank)]; % output file name for current proc
    myfunc(myrank,outname)      % a function
    myscript                    % a script
    exit                        % don't forget to exit
    MATLAB_ENV
    @ rank++
    end
    wait
    

    myfunc.m:

    function myfunc(myrank, outname)
    %function myfunc(myrank, outname)
    disp('This is a MATLAB function')
    if myrank == 0
      disp('Perform a task')
    else
      disp('Perform a different task')
    end
    %  can also construct outname here with myrank
    save(outname);   % save data
    
    myscript.m:
    disp('This is a MATLAB script')

    In the above example, four instances of MATLAB (resulting from #PBS -l nodes=2:ppn=2) are launched. The myoutput_* files trap any output sent to the screen by each MATLAB instant.

    Please avoid combining tasks that vary significantly in compute time as the tasks that finish first will wait for the longest task to complete and therefore waste computing resources. Please call or email Kadin Tseng (kadin@bu.edu) if you'd like to explore other possibilities of multiprocessing with MATLAB.

  2. Give mplbatch execute attribute
    cootie:~ % chmod +x mplbatch 
  3. Submit batch job to the batch queue requesting four processors and a (default) two hours wallclock time.
    cootie:~ % qsub mplbatch
  4. You can override the pre-defined PBS parameters such as walltime and processor count with the following, optional, input.
    cootie:~ % qsub mplbatch -l nodes=4:ppn=2 -l walltime=24:00:00
Boston University
Boston University
 
OIT | CCS | September 16, 2008  
Scientific Computing & Visualization Boston University home page Boston University home page