Parfor on a cluster : failure to recognize a function - matlab

I try to run a simple task on different machines of a cluster.
My configuration has been validated (it's ok). When I run the code
on the 'local' configuration, it works. But when I use the cluster
configuration, I get the following error :
Error using parallel_function (line 598)
Undefined function 'lafunc' for input arguments of type 'double'.
Error Stack : (No remote error stack)
Error in petittest (line 6) --ยป (petittest is my program's name)
parfor it=1:200
I try to modify the code to use "dfeval" instead of the parfor loop,
but I got the same kind of result (unrecognize function lafunc).
How do I get the other workers in the cluster to recognize the function
lafunc that I manually defined ?
The code is the following:
%%%%%%%%%%%%%
laconfig='/home/matlab/fred/LACED_DC1.mat';
setmcruserdata('ParallelConfigurationFile',laconfig);
matlabpool open
parfor it=1:200
yo=lafunc(it);
disp(yo)
end
matlabpool close
%%%%%%%%%%%
where the lafunc function is
%%%%%%%%%%%%%%
function [y]=lafunc(x)
y=x*x;
end
%%%%%%%%%%%%%%%%%%%%%%
Thanks a lot, every piece of info is useful to me!!

Make sure that your MATLAB script on all worker nodes running in the same working directory, and that other directories with functions you need are included in the path. So you can specifically set the working directory and path in your script:
matlabpool open
cd workdir
addpath funcdir
...
After you run matlabpool open it will run all path related commands on all workers. See here.
You can also open matlabpool with 'FileDependencies' parameter so all the workers will know where to look for required files. See the documentation for MATLABPOOL.

Related

Matlab 2019 gives abnormal errors when launching

I have just downloaded Matlab toolbox and I think I did a mistake somewhere but don't know where. I have two versions of Matlab on my machine and I when I launch Matlab 19, the one I want to use, I get this two lines of error:
Unable to resolve the name slreq.utils.loadLinkSet.
Error in rmiml.getAll
When I run the first section, which is merely
clear all; close all; clc;
I get this error
Cannot find builtin function '_LiveEvaluate'
I really don't know what happened and how I could solve it. I have checked the environment variables and they seem okay.
When I run the whole script (without doing by section), I have this error occurring
Attempt to execute SCRIPT mrdivide as a function:
C:\Program Files\MATLAB\R2019b\toolbox\matlab\ops\mrdivide.m
Error in Sandbox (line 25)
X=1/2*eye(n);

How can I execute a simulink diagram running in external mode from a Matlab script?

Matlab provides the sim command that can be used in a Matlab script to call and execute a Simulink model. But the function seems to be restricted to models that only run in Normal mode. When I try to call a Simulink model that runs in External mode, Matlab halts the script and flags it as an error. Besides being a nuisance it seems to me an unnecessary restriction on what could be a very useful application.
In any event is there a work around, perhaps a different command that I can use to run the Simulink diagram in External mode from the Matlab script?
I did try using the Matlab DOS shell command (using !) but it requires opening another instance of Matlab.
External Mode doesn't run a simulation, rather it is a mechanism for using the Simulink model's front end as a way of changing and viewing data that is running elsewhere (e.g. an executable running on the same machine as the model, or code running on an external processor).
To do that from the command line (or within code) you need to use a combination of the following commands:
>> set_param(gcs,'SimulationMode','external') % put model into External Mode
>> set_param(gcs,'SimulationCommand','connect') % connect to the executable
>> set_param(gcs,'SimulationCommand','start') % Start the executable
>> set_param(gcs,'SimulationCommand','stop') % Stop the executable

Running external program in Matlab fails but program works outside it

I need to use a toolbox in Matlab that depends on a external program written in C and using OpenCV. I'm using Ubuntu 14.04 64 bits and Matlab R2014b.
The m-script calls it with the system function and after the C program runs, it reads some output files that the C program should generate. This is failing to happen. I observed that the files are not being generated.
Then, the first thing I did was verify if the C program was working properly, by directly calling it in the terminal, with the same parameters that the m-script has used to call it. Then it worked and the files where generated. Thus, the C program is correct and working.
Coming back to Matlab, it failed again. Then, I started debugging the C program and had find out that the OpenCV function cvLoadImage is failing to open the file, when called from Matlab. It works if I call it outside.
I verified the string that is passed to the function both running from Matlab and externally and it is rigorously the same, but the Matlab call fails ever. I verified with ldconfig -p | grep opencv from the Matlab command window and it shows the libraries. More amusing, there are other calls to other ocv functions before the problematic one that are working. Just this is failing.
The question is: is this a problem of some misconfiguration of my part or maybe it is a bug in Matlab?
Thanks in advance.
EDIT
Actually, calling from inside Matlab with sudo appended to the system call got the job done. What is strange, because I used chmod 777 -Rf as last resource in the whole folder and verified that my user was the owner of both the folder and Matlab processes. Strange... It stills bothers me having to type my password whenever I use the script though, but my schedule is tight, I'll consider this workaround as satisfactory by now.
Instead of calling the C function from MATLAB, you can try to call MATLAB from your C code with MATLAB Engine API for C:
https://es.mathworks.com/help/matlab/calling-matlab-engine-from-c-programs-1.html

scoping on parallel thread in matlab

I have 4 distinct large files I'd like to load into memory simultaneously. The following example is not exactly what I'm doing -- but it illustrates to problem:
matlabpool open local 4
spmd
if labindex==1
R = rand(9,9);
else
R = rand(4,4);
end
end
size(R)
when I copy and paste this into the command prompt, R pops up in my work space. but if i save this to a *.m file, size(R) doesn't evaluate -- it gives me an error that R doesn't exist. I've tried using gather and initializing R as a global to no avail. Any ideas?
Okay:
I was saving this script as test.m.
Then I was calling it from the command line as:
run test.m
But then I ran it as-is from command line: test
'run' must spawn a subprocess which it kills after running or something. this must have been killing the variables. just calling the script straight correctly shares the scope of the workspace with the script.

New to Matlab - Not sure how to run these files

The files I would like to run are found here:
http://www.mathworks.co.uk/matlabcentral/fileexchange/12552-multicanonical-monte-carlo-scheme-for-finding-rare-growth-factors/content/mcmc2.m
it consists of 3 files, one of which is called a driver.
I have tried running each individual one in Matlab and each one gets an error, I think this is because I need to run them all simultaneously or something?
At the beginning of the driver script it says "This is the driver script to be run from the command line. Also requires functions mcmc1 and mcmc2". Where mcmc1 and mcmc2 are the other two files I tried running it in command line but I kept getting error messages..
Any idea how I get these files to run?
You should just be able to run gf_mmc_driver from the command line in Matlab. The mcmc1.m and mcmc2.m files need to be in your matlab path, but do not need to be called directly.
However, I do notice in the driver file the following comment:
% Telling the Distributed Computing Toolbox to complete one job with
% 'numberchain' tasks. Each task is comprised of running a MCMC
% for the burnin time (mcmc1.m) with a different initial matrix.
Do you have the Distributed Computing Toolbox? The driver file appears to require it.