How to prevent Matlab from hanging entire system? Disable swapping - matlab

It is very easy to hang entire system with Matlab. It is sufficient to run any operations on very large matrices to do this.
In hang situation Matlab is apparently neither give the system to work and nor works itself. I.e. this "operation mode" is completely useless and harmful.
Is it possible to disable Matlab from jumping over it's head? Is it possible to disable/minimize swapping or something?
It would be much better if Matlab just fail to complete an operation than if it is simulating some work which is not true.

This guide outlines a few strategies that you can put to use
http://in.mathworks.com/help/matlab/matlab_prog/resolving-out-of-memory-errors.html

Related

Matlab "system" command stuck

I am using matlab to automatically parameter and launch a finite element method code. I write a parameter text file that the FEM code will read, and then call for the FEM code with :
[status,cmdout]=system(['FEMApp ' current_folder '\MyFile']);
Sometimes, the FEM App will be unable to complete its task, and send an error message in the command window. Until now, I was able to detect the error message in cmdout, and proceed to the next paramter set.
For an unknown reason, the system command started to behave differently : it gets stuck for seemingly forever (Matlab is always in "busy" mode). Did I change anything without realizing it ?
For now, I am using the following solution :
[status,cmdout]=system(['FEMApp ' current_folder '\MyFile &']);
pause(45)
system(['taskkill' 'FEMProcessus')
It works correctly, but it slows my computation a lot (~ x5), because Matlab will always wait 45 secondes even when the task is completed in much less time.
Can anyone explain the change in behaviour of Matlab ?
Does anyone has a cleverer work around than mine ?
It should be noted that Matlab is an interpreter rather than a compiler. That means it performs a lot of internal operations, hidden from the developer, some of which may require a lot of CPU resources. Finite Element applications are very numerically intense in terms of using CPU and RAM resources. It might not be a good idea to use Matlab for FEM programming. Try to use some numerically-oriented language, like C or Fortran, where you will have full control over memory allocation and arithmetic operations.

Does Matlab R2015 use GPU?

Mathworks seems to be working on making better use of GPUs and I am wondering at what stage they are at in R2015. Does it automatically use GPU or do I need to program Matlab somehow to make use of my GPU? I have Nvidia GTX860M and it would be a pity for it to idle away while I sit and wait for Matlab to execute scripts. Do I still need to get an add-on for GPU for Matlab R2015?
If someone is currently using GPU in their Matlab computing, can you post some relevant links with what I have to go to set it up (preferably for R2015, if it makes any difference)? Whenever I search I come across different instructions made for Matlab from R2006 to R2012 and I wonder if they are still relevant for more recent releases.
Does it automatically use GPU?
No, you have to make the decision. Using the gpuArray function you decide to copy the data to the graphic card memory. Any data stored there is processed by the gpu (as long as the function is supported, otherwise you get an error), any data stored in your main memory is processed by the CPU.
For more information how to use it, start reading here http://de.mathworks.com/discovery/matlab-gpu.html

MATLAB gui freeze after running my program

I create a GUI that use Parallel computing for accelerating Neural network and SVM models. When I enable Parallel computing in my GUI all thing (MATLAB,My GUI and my code's window) will freeze and I can't maximize these windows either, but my program is working,using workers and sometimes I can see updates in graphs and values of GUI. When I disable Parallel computing in my program everything is normal. In first situation sometimes my GUI turns to black color (Background of GUI,buttons,text-boxes and edit-boxes are good) but values are updating in iterations of created GUI.
What is this problem?
Thanks.
Sorry for the late answer. It is hard to give a definite answer to a solution here. The problem is quite advanced and most of the code we cannot do anthing about. What happens in parfor is probably that matlab puts some kind of lock (mutex or something else) on the elements it accesses at the moment. Also, since parfor works in parallel it is unlikely that any of the data in the loop is ever untouched. The only solution I can see is that you run shorter processes. That will allow your gui to update between the prcesses.
It would probably work to put a drawnow where you want the gui to update.
Other options could also be to place the gui in a location where you does not need to move it or dock it to the main window in matlab. But these are shortcuts which will only hide the problem.

How to utilise parallel processing in Matlab

I am working on a time series based calculation. Each iteration of the calculation is independent. Could anyone share some tips / online primers on using utilising parallel processing in Matlab? How can this be specified inside the actual code?
Since you have access to the Parallel toolbox, I suggest that you first check whether you can do it the easy way.
Basically, instead of writing
for i=1:lots
out(:,i)=do(something);
end
You write
parfor i=1:lots
out(:,i)=do(something);
end
Then, you use matlabpool to create a number of workers (you can have a maximum of 8 on your local machine with the toolbox, and tons on a remote cluster if you also have a Distributed Computing Server license), and you run the code, and see nice speed gains when your iterations are run by 8 cores instead of one.
Even though the parfor route is the easiest, it may not work right out of the box, since you might do your indexing wrong, or you may be referencing an array in a problematic way etc. Look at the mlint warnings in the editor, read the documentation, and rely on good old trial and error, and you should figure it out reasonably fast. If you have nested loops, it's often best parallelize only the innermost one and ensure it does tons of iterations - this is not only good design, it also reduces the amount of code that could give you trouble.
Note that especially if you run the code on a local machine, you may run into memory issues (which might manifest in really slow execution in parallel mode because you're paging): Every worker gets a copy of the workspace, so if your calculation involves creating a 500MB array, 8 workers will need a total 4GB of RAM - and then you haven't even started counting the RAM of the parent process! In addition, it can be good to only use N-1 cores on your machine, so that there is still one core left for other processes that may run on the computer (such as a mandatory antivirus...).
Mathworks offers its own parallel computing toolbox. If you do not want to purchase that, there a few options
You could write your own mex file and use pthreads or OpenMP.
However make sure you do not call any Mex api in the parallel part of the code, because they arent thread safe
If you want coarser grained parallelism via MPI you can try pmatlab
Same with parmatlab
Edit: Adding link Parallel MATLAB with openmp mex files
I have only tried the first.
Don't forget that many Matlab functions are already multithreaded. By careful programming you may be able to take advantage of them -- check the documentation for your version as the Mathworks seem to be increasing the range and number of multithreaded functions with each new release. For example, it seems that 2010a has multithreaded ffts which may be useful for time series processing.
If the intrinsic multithreading is not what you need, then as #srean suggests, the Parallel Computing Toolbox is available. For my money (or rather, my employers' money) it's the way to go, allowing you to program in parallel in Matlab, rather than having to bolt things on. I have to admit, too, that I'm quite impressed by the toolbox and the facilities it offers.

MATLAB and using multiple cores to run calculations

Hey all. Im trying to sort out how to get MATLAB running as best as possible. I have a pretty decent new machine.
12GB RAM
Core i7 3.2Ghz Cpu
and lots of free space.
and a strong graphics card.
However when I run the benchmark test of MATLAB (command bench) it lists the computer as being near the worst, around a Windows XP single core 1.7Ghz machine.
Any ideas why and how I can improve this??
Thanks very much
Firstly, I would recommend re-running the bench command a few times to make sure MATLAB has fully loaded all the libraries etc. it needs. Much of MATLAB is loaded on demand, so it's always best to time the second or third run.
MATLAB automatically takes advantage of multiple cores when executing certain operations which are multithreaded. For example lots of elementwise operations such as +, .* and so on as well as BLAS-backed operations (and probably others). This page lists those things which are multithreaded.
Parallel Computing Toolbox is useful when MATLAB's intrinsic multithreading can't help (if it can, then it's usually the fastest way to do things). This gives you explicit parallelism via PARFOR, SPMD and distributed arrays.
You need the Parallel Processing Toolbox. A lot of MATLAB functions are multithreaded but to parallelize your own code, you'll need it. A dumb hack is to open several instances of command-line MATLAB. You could also write multithreaded MEX files but the right way to go about it would be the purchase and use the aforementioned toolbox.
This may be obvious, but make sure that you have enabled multithreaded computation in the preferences (File > Preferences > General > Multithreading). In some versions of MATLAB, it's not enabled by default.