Matlab gets stuck - No error message

Dear All,
I am stuck with a real problem. I have written a script (please see blow the summary) that works well, but sometimes for no apparent reason it gets stuck. I firmly say it is not an endless loop.
I would be very thankful if you provide me with any suggestions, as I tried many and it didn't work. The main reason that I can think of is that the problem could be associated with the RAMs! Especially when the number of iterations within the while loops become large. If this is the case, is there a way to keep refreshing the memory?
Kindly note that the machine I am using is QuadCore, Intel Xeon® @ 2.4 GHz, 64 GB Rams
Since I already started the post; does anyone know a way that makes this simulation faster? (any hints, or toolbox that does the trick)
Thanks Experts, I really appreciate your help.
%script - kindly note that afterwards, I export the results to excel
%defining No of Simulations & the steps
ii = 10000000;
Min = 100;
Max = 110;
Steps = 1;
Observations = ((Max - Min)/Steps)+1;
PriceVector = Min:Steps:Max;
%Defining Parameters
...
...
StartingTime=0;
StartingDay=0;
EndingDay=20;
...
...
%Creating Matrices for speeding up the process
frequency = zeros(ii,Observations);
Count = zeros (ii,Observations);
DayX = zeros(1,Observations);
DayY = zeros(1,Observations);
SurV = zeros(1,Observations);
ONX = zeros(1,Observations);
PriceVec = zeros(1,Observations);
%Initiating the progress bar
multiWaitbar('Main for loop',0,'Color', 'b'); %progress bar of #Ben Tordoff.
multiWaitbar('Sub for loop',0, 'Color', 'g');
for j=1:Observations
values = zeros(1,ii);%to speed up the process
Incremental=PriceVector(j);%this gets the next incremental value
for i=1:ii;
StockPrice=Incremental;
Days=StartingDay; %start day
Hours=StartingTime;
knockout = 0; %This variable will terminate the while loop when false
while knockout == 0
%some calculations
knockoutNew = 0;
while (knockout == 0 && knockout2 == 0)
%some calculations
Hours = Hours + SS %SS is the sampling interval
if true %first condition
value = 0;
knockout = 1;
elseif true %sub condition
knockout2 = 1;
end
end
if true %first condition - Breaking the first while loop since the condition is met in the sub loop
break
end
if Hours < 1
%some calculations
if true %second condition
value = 0;
knockout = 2;
end
else
if Days==EndingDay; %third condition
value = 5;
knockout=3;
else
%some calculations
if true %fourth condition
value = 0;
knockout = 4;
else
Days=Days+1; %this ensure the termination of the loop
Hours = 0;
end
end
end
end
values(i) = value; %gets the values of the knockout i.e, either zero or 5
frequency(i) = knockout;
%progress of the sub for loop
multiWaitbar('Sub for loop',i/ii);
end
Kvalue=%somecalulations;
kboV(j)=KBO;
%collecting the count of each case of each price element
DaytimeKO(j)=sum(histc(frequency,1));
DayJumpKO(j)=sum(histc(frequency,2));
Survival(j)=sum(histc(frequency,3));
ONightKO(j)=sum(histc(frequency,4));
PriceVec(j) = Incremental;
%progress of the main for loop
multiWaitbar('Main for loop',j/TotalProcess);
end

Answers (1)

dpb
dpb on 23 Sep 2013
I firmly say it likely (given the pseudo-code if...end conditional blocks above) IS occasionally finding a set of conditions that are an infinite loop.
Memory refresh is a hardware-implemented feature of the machine; that or similar things is not in any way at all a feasible/likely cause for your problem, it is almost certainly a logic problem for a certain set of data inputs.
There's insufficient detail to tell for certain but I'd suspect if one had the overall actual code one could vectorize the loop and logic almost if not entirely from the way it appears structured overall.

4 Comments

Thanks #dpb for the answer,
I would relax if it is a coding issue. The only way that I get into an infinite loop is through the 2 while-loops I have, this if I am not wrong.
I tried the main-while loop, and it is not forsure causing any problem. While I am writing this comment, I am actually running the code, and looking if there is a problem with the sub-while loop.
Since this doesn't occur frequently, I have to wait and see how it goes. To debug the problem, I am printing a counter, but this slows the process; do you know a faster method?
I apologize for not stating the sub condition of the inner loop as it is fundamental, it goes:
else if Hours > 1 %this makes sure there is an end to this loop, as Hours is increasing in every loop
The following ensures the termination of the main-while loop:
Days=Days+1; % it starts at 0, once it becomes 20, it terminates
I thought it is a memory issue, because Matlab blocks the memory for creating the matrices.
I shall keep you updated anyways,
Thanks again & best if regards//
I thought it is a memory issue, because Matlab blocks the memory for creating the matrices.
??? What is this supposed to mean? If Matlab has an out-of-memory problem it'll abort and tell you so.
If there's something in there that's not pre-allocated and you're doing allocation on the fly that haven't caught that'll definitely bog it down with increasing sizes.
The mlint and code editor ought to be able to spot that if so altho there are some ways that are harder to find than others...
Again, I'd look real hard at vectorizing the whole thing--looks to me like you should be able to get rid of the loop structure almost entirely. Perhaps you'll need to restructure slightly to eliminate all but it just looks to me like reorganizing is the first place to look.
It again needs more of the pertinent details of the code an perhaps less of the not-so-much to really get anywhere for anybody here in that regard...
The problem only occurs, when I run another process(s) without restarting Matlab. Of course it is not an out-of-memory problem, otherwise I would get the message.
I tried the mlint function but it retrieved no errors or recommendations.
All what I can do to solve the problem now is to restart Matlab after every 1 or 2 simulation processes.
Anyways #dpb, many thanks for your contributions; I do appreciate it.
Best of Regards//
dpb
dpb on 24 Sep 2013
Edited: dpb on 24 Sep 2013
The problem only occurs, when I run another process(s) without restarting Matlab....
So, the problem likely isn't Matlab at all, iow.
I still think you should look at restructuring your code and vectorizing it extensively and would find marked improvement in performance if did so...
ADDENDUM:
I pasted your code in the editor w/ the intent of trying to actually look at the above but the missing conditionals even w/ the incomplete description you added make doing anything useful impossible as far as seeing how vectorization might solve your problems. :(

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

AND
on 23 Sep 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!