Thread Subject: Cleaning code (so Matlab won't crash)

Subject: Cleaning code (so Matlab won't crash)

From: susan

Date: 20 Sep, 2009 19:08:00

Message: 1 of 10



Hi, anyway to make this more efficient?

ratemat=zeros([1000,21,52]);
for i=1:1000
    [Cc]=calculate values; %size(21x52)
    ratemat(i,:,:)=Cc;
    clear Cc
end

Matlab crashes after ~ i=60..I have a 2.99Ghz machine with 3GB ram, so I am really hoping this is just an efficiency issue..

Thanks,
S

Subject: Cleaning code (so Matlab won't crash)

From: per isakson

Date: 20 Sep, 2009 19:30:21

Message: 2 of 10

"susan" <suetibu@hotmail.com> wrote in message <h95uig$t0q$1@fred.mathworks.com>...
>
>
> Hi, anyway to make this more efficient?
>
> ratemat=zeros([1000,21,52]);
> for i=1:1000
> [Cc]=calculate values; %size(21x52)
> ratemat(i,:,:)=Cc;
> clear Cc
> end
>
> Matlab crashes after ~ i=60..I have a 2.99Ghz machine with 3GB ram, so I am really hoping this is just an efficiency issue..
>
> Thanks,
> S

Is there an error message?

/ per

Subject: Cleaning code (so Matlab won't crash)

From: Matt Fig

Date: 20 Sep, 2009 19:31:01

Message: 3 of 10

What actually happens when MATLAB "crashes?" Is there an error message? Be specific.
As this works:


ratemat = zeros(1000,21,52);
for i=1:1000
    Cc = rand(21,52);
    ratemat(i,:,:) = Cc; % Why clear Cc every time through the loop???
end


I suspect your problem is in the calculation of Cc. Perhaps if you show some of that calculation.

Subject: Cleaning code (so Matlab won't crash)

From: Jan Simon

Date: 21 Sep, 2009 08:22:01

Message: 4 of 10

Dear susan!

> ratemat=zeros([1000,21,52]);
> for i=1:1000
> [Cc]=calculate values; %size(21x52)
> ratemat(i,:,:)=Cc;
> clear Cc
> end
>
> Matlab crashes after ~ i=60..I have a 2.99Ghz machine with 3GB ram, so I am really hoping this is just an efficiency issue..

As long as your RAM and the processor is not damaged, it is not possible to crash Matlab with a correct program by a kind of overworking or exhaustion. Matlab does not get tired.

So beside the stylistic tips, the error must be located in "calculate values".
  ratemat=zeros(1000, 21, 52); % No square brakets here
  for i = 1:1000
      ratemat(i, :, :) = calculate values; % No need for temp variable Cc
  end

Good luck, Jan

Subject: Cleaning code (so Matlab won't crash)

From: Sebastiaan

Date: 21 Sep, 2009 09:36:03

Message: 5 of 10


> So beside the stylistic tips, the error must be located in "calculate values".
> ratemat=zeros(1000, 21, 52); % No square brakets here
> for i = 1:1000
> ratemat(i, :, :) = calculate values; % No need for temp variable Cc
> end
>
> Good luck, Jan
Talking about style and efficiency, reshape your matrix if possible:
ratemat=zeros(21, 52, 1000); % No square brakets here
for i = 1:1000
    ratemat(:, :, i) = calculate values;
end

Memory is continuous in the first 2 dimensions. Depending on the time calculate_values takes, this may or may not be a significant gain in speed.

Subject: Cleaning code (so Matlab won't crash)

From: susan

Date: 21 Sep, 2009 16:08:20

Message: 6 of 10

"Sebastiaan" <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl> wrote in message <h97he3$jqj$1@fred.mathworks.com>...
>
> > So beside the stylistic tips, the error must be located in "calculate values".
> > ratemat=zeros(1000, 21, 52); % No square brakets here
> > for i = 1:1000
> > ratemat(i, :, :) = calculate values; % No need for temp variable Cc
> > end
> >
> > Good luck, Jan
> Talking about style and efficiency, reshape your matrix if possible:
> ratemat=zeros(21, 52, 1000); % No square brakets here
> for i = 1:1000
> ratemat(:, :, i) = calculate values;
> end
>
> Memory is continuous in the first 2 dimensions. Depending on the time calculate_values takes, this may or may not be a significant gain in speed.

Error message:
------------------------------------------------------------------------
       Segmentation violation detected at Mon Sep 21 12:04:04 2009
------------------------------------------------------------------------
Configuration:
  MATLAB Version: 7.5.0.342 (R2007b)
  MATLAB License: 79639
  Operating System: Microsoft Windows XP
  Window System: Version 5.1 (Build 2600: Service Pack 3)
  Processor ID: x86 Family 6 Model 15 Stepping 6, GenuineIntel
  Virtual Machine: Java 1.6.0 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
  Default Charset: windows-1252
Register State:
  EAX = 0000023c EBX = 0792a560
  ECX = 0a8c1700 EDX = 0a8c17c0
  ESI = 00ced0ac EDI = 00000001
  EBP = 00ced23c ESP = 00ced010
  EIP = 00000000 FLG = 00210202
Stack Trace:
  [0] 0x01beee20(0, 0, 2, 0)

Subject: Cleaning code (so Matlab won't crash)

From: susan

Date: 21 Sep, 2009 16:23:03

Message: 7 of 10

"susan" <suetibu@hotmail.com> wrote in message <h988dk$o4t$1@fred.mathworks.com>...
> "Sebastiaan" <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl> wrote in message <h97he3$jqj$1@fred.mathworks.com>...
> >
> > > So beside the stylistic tips, the error must be located in "calculate values".
> > > ratemat=zeros(1000, 21, 52); % No square brakets here
> > > for i = 1:1000
> > > ratemat(i, :, :) = calculate values; % No need for temp variable Cc
> > > end
> > >
> > > Good luck, Jan
> > Talking about style and efficiency, reshape your matrix if possible:
> > ratemat=zeros(21, 52, 1000); % No square brakets here
> > for i = 1:1000
> > ratemat(:, :, i) = calculate values;
> > end
> >
> > Memory is continuous in the first 2 dimensions. Depending on the time calculate_values takes, this may or may not be a significant gain in speed.
>
> Error message:
> ------------------------------------------------------------------------
> Segmentation violation detected at Mon Sep 21 12:04:04 2009
> ------------------------------------------------------------------------
> Configuration:
> MATLAB Version: 7.5.0.342 (R2007b)
> MATLAB License: 79639
> Operating System: Microsoft Windows XP
> Window System: Version 5.1 (Build 2600: Service Pack 3)
> Processor ID: x86 Family 6 Model 15 Stepping 6, GenuineIntel
> Virtual Machine: Java 1.6.0 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
> Default Charset: windows-1252
> Register State:
> EAX = 0000023c EBX = 0792a560
> ECX = 0a8c1700 EDX = 0a8c17c0
> ESI = 00ced0ac EDI = 00000001
> EBP = 00ced23c ESP = 00ced010
> EIP = 00000000 FLG = 00210202
> Stack Trace:
> [0] 0x01beee20(0, 0, 2, 0)

I don't think it is my calculation (very complicated to explain in a couple of sentences)
1) The crash happens at different i's each time.
2) I am running the for loop 1000 times, because I am calculating each time on random shuffles of the orig data. I have tested the random shuffle of the crash to see if there is something funny about my calculation about that particular random shuffle, but it runs fine..

Help!

Subject: Cleaning code (so Matlab won't crash)

From: dpb

Date: 21 Sep, 2009 16:25:22

Message: 8 of 10

susan wrote:
...
> Error message:
> ------------------------------------------------------------------------
> Segmentation violation detected at Mon Sep 21 12:04:04 2009
> ------------------------------------------------------------------------
> Configuration:
> MATLAB Version: 7.5.0.342 (R2007b)
> MATLAB License: 79639
> Operating System: Microsoft Windows XP
> Window System: Version 5.1 (Build 2600: Service Pack 3)
...

Still, this indicates an error in calculate_values() (assuming it was
the above line or similar that preceded the error; otherwise all bets
are off and need to see what it was).

One might venture to guess this is a mex-function as a segment violation
is indicative of an indexing error in array bounds addressing. Pretty
rare in TMW-supplied code; not impossible but wouldn't be my first guess
(just as in an error in compiling w/ a C or Fortran compiler my first
inclination would be user error before blaming the compiler).

Need more info on calculate_values(). Nothing to be said otherwise than
there's a problem there.

--

Subject: Cleaning code (so Matlab won't crash)

From: Jan Simon

Date: 21 Sep, 2009 20:45:08

Message: 9 of 10

Dear susan!

> > ------------------------------------------------------------------------
> > Segmentation violation detected at Mon Sep 21 12:04:04 2009
> > ------------------------------------------------------------------------

> I don't think it is my calculation (very complicated to explain in a couple of sentences)
> 1) The crash happens at different i's each time.
> 2) I am running the for loop 1000 times, because I am calculating each time on random shuffles of the orig data. I have tested the random shuffle of the crash to see if there is something funny about my calculation about that particular random shuffle, but it runs fine..

I do think, it is your calculation. But you can test this easily:
  for i = 1:1000
     fprintf('In>');
     Cc = <calculate values>
     fprintf('<Out\n');
     ratemat(:, :, i) = Cc
  end
Then look in the last line before the segementation fault message. I bet
you find an "In>". Then move the "fprint" lines inside your "calculate values" function, such that they include less and less lines. Finally you find the line, which damages the segemention.

The fact that the crash happens at different times displays the ability of C code to create non-reproducible errors. If the program uses a pointer, which points to the wrong address, the result can be random.

Good luck, Jan

Subject: Cleaning code (so Matlab won't crash)

From: per isakson

Date: 21 Sep, 2009 21:38:04

Message: 10 of 10

"susan" <suetibu@hotmail.com> wrote in message <h98997$l3t$1@fred.mathworks.com>...
> "susan" <suetibu@hotmail.com> wrote in message <h988dk$o4t$1@fred.mathworks.com>...
> > "Sebastiaan" <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl> wrote in message <h97he3$jqj$1@fred.mathworks.com>...
> > >
> > > > So beside the stylistic tips, the error must be located in "calculate values".
> > > > ratemat=zeros(1000, 21, 52); % No square brakets here
> > > > for i = 1:1000
> > > > ratemat(i, :, :) = calculate values; % No need for temp variable Cc
> > > > end
> > > >
> > > > Good luck, Jan
> > > Talking about style and efficiency, reshape your matrix if possible:
> > > ratemat=zeros(21, 52, 1000); % No square brakets here
> > > for i = 1:1000
> > > ratemat(:, :, i) = calculate values;
> > > end
> > >
> > > Memory is continuous in the first 2 dimensions. Depending on the time calculate_values takes, this may or may not be a significant gain in speed.
> >
> > Error message:
> > ------------------------------------------------------------------------
> > Segmentation violation detected at Mon Sep 21 12:04:04 2009
> > ------------------------------------------------------------------------
> > Configuration:
> > MATLAB Version: 7.5.0.342 (R2007b)
> > MATLAB License: 79639
> > Operating System: Microsoft Windows XP
> > Window System: Version 5.1 (Build 2600: Service Pack 3)
> > Processor ID: x86 Family 6 Model 15 Stepping 6, GenuineIntel
> > Virtual Machine: Java 1.6.0 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
> > Default Charset: windows-1252
> > Register State:
> > EAX = 0000023c EBX = 0792a560
> > ECX = 0a8c1700 EDX = 0a8c17c0
> > ESI = 00ced0ac EDI = 00000001
> > EBP = 00ced23c ESP = 00ced010
> > EIP = 00000000 FLG = 00210202
> > Stack Trace:
> > [0] 0x01beee20(0, 0, 2, 0)
>
> I don't think it is my calculation (very complicated to explain in a couple of sentences)
> 1) The crash happens at different i's each time.
> 2) I am running the for loop 1000 times, because I am calculating each time on random shuffles of the orig data. I have tested the random shuffle of the crash to see if there is something funny about my calculation about that particular random shuffle, but it runs fine..
>
> Help!

Did you send the crash report to segv@mathworks.com?

If your code is plain Matlab you should ask Mathwork support for help!

If there is a mex-function, which is not part of Matlab, then that function is propably causing the problem.

/ per

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
for Sprinceana 20 Sep, 2009 15:57:30
for loop Sprinceana 20 Sep, 2009 15:57:30
code Sprinceana 20 Sep, 2009 15:55:05
rssFeed for this Thread

Contact us at files@mathworks.com