mathjax

Wednesday, February 26, 2014

File Locking in MATLAB

File locking in MATLAB becomes a problem if you're running a parallel computation -- amongst other things. Say you're running a parameter sweep that requires some files to be loaded into memory. If all your processes are trying to load the same file into their memories at the same time, your program will crash. I couldn't find a nice function in MATLAB that would lock files. But I found a nice hack online used mkdir in shell scripts and incorporated it into my MATLAB scripts using system.

mkdir is a nice function because its atomic, i.e., it creates a folder if one doesn't exist and returns an error if one already exists with the same name. You can leverage this to implement a hacky file lock & release function. Create a directory somewhere and use the system command in MATLAB to mkdir. The name of the created directory will be the name of the file you loaded. If the file already exists, mkdir will throw an error. system returns the exit status of bash scripts as an integer: 0 if successful and 1 if failed.

With all this in mind check out the code below.

function getLock(lockPath,fName)
  d = strcat('mkdir',{' '},lockPath,fName,{' '},'2>/dev/null');
  while system(d{1})
    e = strcat(fName,{' '},'is locked...waiting 10 seconds');
    disp(e{1})
    pause(10)
  end
end

function releaseLock(lockPath,fName)
  d = strcat('rmdir',{' '},lockPath,fName,{' '},'2>/dev/null');
  system(d{1});
end

So this would be implemented in the following way:

dirStruct = dir('/path/to/data/myData.mat')
getLock(lockPath,dirStrcut.name)
load(myData.mat)
releaseLock(lockPath,dirStruct.name)