Problem CPU
2.2. Multicore for MATLAB Library
MATLAB’s native capabilities can be used to execute parallel jobs in a multicore installation [2.8]-[2.10]; however this usually requires purchasing special toolbox licenses. The “Multicore for MATLAB” library (developed by M. Buehren [2.11]) consists of a set of .m files which allow the user access to all cores in a multicore installation. This library can be used to execute embarrassingly parallel jobs by distributing task executions among system cores in an equivalent manner to MATLAB’s parfor function. In order to access all available cores one MATLAB session must be run for each parallel execution; one session will serve as master, whereas the rest will work as slave sessions. The maximum number of concurrent MATLAB sessions must be equal to the number of cores at the user’s disposal.
The “Multicore for MATLAB” library may exhibit a lower efficiency than MATLAB’s native capabilities in terms of task distribution and information gathering times; however it does not require a special toolbox license and can be readily expanded for its use with a computer cluster.
As the name implies, the “Multicore for MATLAB” library runs entirely on MATLAB (i.e. it requires no additional software tool to access the available cores in the multicore installation). This Section describes in detail the master function and the main parameters that control the library execution; it also presents how the task chosen for execution must be defined in the MATLAB environment. Additionally, some comments on the execution of the “Multicore for MATLAB” library in a computer cluster are made.
2.2.1. Job-Task and Input Data
The task to be executed must be defined as a function within the MATLAB environment. The input parameters will be stored in a cell structure and the output parameters will also be returned as a cell. Input and output parameters may contain any type of variable format (double, matrix, string, cells, etc.).
The testfun function has been defined to be used as an example of how the library works:
Analysis of Power Distribution Systems Using a Multicore Environment
32
[Out1,Out2]=testfun(Param1,Param2,Param3);
Input parameters are stored in a cell structure using the code lines shown in Figure 2.5. paramCell = cell(1,N);
for k = 1:N
paramCell{1,k} = {Param1(k),Param2(k),Param3(k)}; end
Figure 2.5. Library input parameters. where N is the total number of executions.
The task-function is a standard MATLAB function, namely it only has access to locally defined variables and only returns the variables predefined as outputs; however the function can make use of auxiliary scripts saved as .m files.
2.2.2. Master Function and Main Parameters
As it was previously mentioned, one MATLAB session must serve as master; it is in the master session that the main script must be run. The main script must include the so- called startmulticoremaster function, which will be in charge for creating a set of .mat files containing the information required for the tasks to be executed. The master session is also in charge of collecting information produced by slave sessions.
Continuing with the testfun example, the following line presents the command to be executed in order to run the master function:
resultCell=startmulticoremaster(@testfun,paramCell,settings); testfun: task-function to be executed
paramCell: cell structure with input data
settings: library settings, overwrite default values. resultCell: cell structure with output data
In this scenario testfun outputs are assumed to be single values using double format;
Out1 and Out2 are retrieved using the code presented in Figure 2.6.
for n=1:N
resu=resultCell{n};
Out1(n)=cell2mat(resu(1)); Out2(n)=cell2mat(resu(2)); end
Figure 2.6. Output variables collection.
The library execution is controlled by a group of settings that can be overwritten to adapt to the user’s needs. Settings must be defined before the startmulticoremaster function is executed. The main settings are:
Chapter 2: Application of Parallel Computing to Distribution System Analysis
33
settings.nrOfEvalsAtOnce
The .mat files generated by the library contain the information related to the tasks to be executed. Slave sessions read these .mat files and execute the tasks defined in them. This field determines the number of tasks that will be included in a single .mat file, thus the number of consecutive executions performed by a slave session.
settings.maxEvalTimeSingle
It is the maximum expected time for each individual execution. If the individual execution time expires and the master session has not received the output information from the slave session, it will assumed there has been an error and will execute the task itself.
settings.multicoreDir
This setting specifies the folder where the .mat files will be created. All slave sessions must have access to this directory.
settings.masterIsWorker
Defines whether the master session will be used to perform task executions or stay idle, waiting to gather information produced by slave sessions (“1” master sessions will perform some task executions, “0” master session will not perform any tasks).
2.2.3. Slave Function
The so-called startmulticoreslave function must be executed in every slave session; this function will load the .mat files created by the master function and execute the task- function with information contained in those files. While it is being executed, it will continuously read the specified folder searching for .mat files and execute the tasks contained in those files.
The slave function will be run executing the following command: startmulticoreslave('C:\testdir')
testdir: folder where the slave function will search for .mat files; it must match the folder specified in settings.multicoreDir
2.2.4. Library Execution in a Computer Cluster
The use of the “Multicore for MATLAB” library is not constrained to a single multicore computer; its capabilities can also be used to control a group of computers (i.e. a computer cluster); this possibility will grant the user access to larger computational
Analysis of Power Distribution Systems Using a Multicore Environment
34
resources. A computer cluster can be easily expanded by adding more nodes to its original structure, thus increasing the number of available cores for task executions. This increment in available cores will allow the user to achieve a greater reduction in execution times, since to number of task executions per core will be drastically reduced. The implementation of the “Multicore for MATLAB” library in a cluster requires access to a folder that can be reached by all nodes that are scheduled to take part in the job execution. The .mat files and other files related to each task execution (e.g. task- function file and other auxiliary files) must be placed in this folder, so all slave sessions can reach them. The target folder must be defined using the complete access path (including host’s name or IP address); this is a requirement for the nodes that will remotely access the folder. No additional actions are required for the library’s cluster implementation.