cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

Preallocation of Arrays

wayne
12-Amethyst

Preallocation of Arrays

Ref: quicksheets>mathcad techniques>improving calcultion speed.

This artical discusses the importance of preallocation of matrices.

I think I have this right, but just in case I don't:

If I initialize a matrix, say Working and make it a 100x100 matrix, I am in effect assigning a block of memory for this matrix.

I then can use and re-use the Wroking matrix, setting Working=0 before each use, and as long as each use does not exceed the maximum, then the allocation is maintained and being used efficiently. (Different sizes each time).

Is this correct?

7 REPLIES 7

wayne wrote:

Ref: quicksheets>mathcad techniques>improving calcultion speed.

This artical discusses the importance of preallocation of matrices.

I think I have this right, but just in case I don't:

If I initialize a matrix, say Working and make it a 100x100 matrix, I am in effect assigning a block of memory for this matrix.

I then can use and re-use the Wroking matrix, setting Working=0 before each use, and as long as each use does not exceed the maximum, then the allocation is maintained and being used efficiently. (Different sizes each time).

Is this correct?

This is how I read it.

Calculation speed can be improved when dealing with large matrices or vectors by creating a zero entry at the last index of the array. Which means you are telling the computer how much space should be set aside to fill the array with the calculated values.

It also recommends performing intermediate array operations inside a program. This way the arrays are only created once the program is executed.

There are loads of tricks to speed up calculations.

  • Remove units from program loops
  • Try to work with column vectors
  • Avoid using the stack function
  • Avoid unnecessary vectorization

I'm sure there's more, but can't think of them at the moment.

Mike

Hi

IN a couple of earlier posts, Mike Armstrong explained something that is very important for a user of large arrays in Mathcad. This is not documented anywhere in Mathcad manuals or help areas etc. It should be formally documented by Mathcad so users understand what is happening.

Suppose you do the following in a Mathcad worksheet:

1. create an array TF:= file read from disc, or a matrix you create under programme control in Mathcad etc

2. TF:= TF (manipulated in someway by you under programme control in Mathcad, so an written back into array TF).

3. TF:= TF (manipulated by you in another way under programme control, and so again written back to you into array TF)

Mike explained that within a programme block, temporary files are created and destroyed after leaving the block. But Mathcad needs to hold TF as three separate versions in memory in the above example as otehrwise it cannot compute or process the earlir versions of TF if the memory for Tf is overwritten by a later version of TF. So the memory requirements in the above worksheet example are roughly 3 times the size of TF (depending on what you do for TF at each of the three steps).

A lot of people who programme using other languages try to use memory efficiently and re-use memory if possible. As explained above, this cannot be done inside a Mathcad worksheet.

The end result is if you, like me, input and/or process very large data files, then Mathcad quickly chews through large volumes of RAM and Mathcad will hit a wall and stop working. Switching to full 64-bit operatings system will help in this, but the principle of memory repeats and overflow still remains.

Regards

David

Hi

My apologises, I understand it was Richard Jackson who presented the earlier posts on memory usage.

Regards

David

wayne
12-Amethyst
(To:ptc-1371556)

David,

Interesting, can you point me to the earlier post by Richard?


The information is helpful, but slightly different than the intent of my question. There is a related thread "How to find the square roof of a matrix" that is on going which is sort of realted, Richard is providing insite here to.

I am primarily interested in using the same matrix name over and over for repeated calculations; same type of problem, but different domain each time. May reuse up to 30 or 40 times, maybe more depending on how I reference files together. If I preallocate a size for a working matrix of small to moderate size, the I would expect that the same memory block is used each time (maybe twice? but not 30 ro 40 times) Form thread referenced above, it appears that with a program using a matrix as small as 10x10 is helped by preallocation.

In your answer, you talk about a very large matrix, which on occasion I have used as well, but not in normal workflow. I did not know this was the case, but it seems to make sense. What if I operate on the matrix 6 times, how many blocks of memory might I have? I think limited to 3, the current, and 2 blocks no longer used? i.e., does Mathcad know to reuse the other blocks again, or does it just keep creating new ones?

What if I made a matirx LM1 and operated on that matirx to create LM2. (2 blocks of memory)

Then I operated on LM2 to create a new LM1. (still two blocks of memory?)

And then did both operations again, Do I have two blocks of memory, LM1 and LM2? OR do I have 2 blocks for the current LM1 and LM2 and at least 2 more blocks of memory which had the old LM1 and LM2 that are no longer used but taking up RAM?

(I am taxing my sanity) When I build a matrix element by element, either inside or outside a program I am changing the matrix each time, but I am not creatng a new block of memory for that matirx, as long as it is within the preallocated space? (I hope I have that much right) If I change one or more elements, I am also pretty sure I don't then cause another copy in RAM for that matrix. If that is true, whats the difference whether I change one element or all the elements.

It seems acadamic, but a better insite may make me change how I work

Thanks,

Wayne

Hi

Some earlier threads in the forum which you can look up (or putting "memory" in the search field above):

23/10/2010 - What new features do you want to see in Mathcad Prime? - Richard Jackson

12/02/2009 - Memory Limits - Richard Jackson

12/01/2008 - Error 'not enough memory- Tom Gutman

All users should look back at that last thread. There are comments from Tom Gutman, Philip Oakley etc on this whole issue of memory allocation and using the same variable name within a worksheet. A summary of this important topic should be placed in Mathcad's User Manual for all users to easily find and see.

So the idea is to write a single programme block, function etc that does as as much local processing as possible on a large matrix before moving to the next item in the worksheet. Having intermediate separate programming blocks which re-uses the matrix name may be easier to test and understand by the human user, but they may not be optimum for Mathcad. Of course, peak memory usage within a single major programming block may still surge very high so this may not be a final solution (I have not tested the limits of this approach with my worksheets).

We can work out the absolute size of the memory block for any variable or matrix we set up may not be vary large (ie number of elements times relevant number of bytes for each element). I suggest this is why users become frustrated and believe something is wrong with Mathcad. There seems to be a lot of resources overhead (for each element in a matrix etc) which is part of the internal workings of Mathcad. This may be the real reason why users find their worksheets stop working much sooner than expected due to "insufficient memory" even when they know they have large slabs of RAM. The resources overhead seems to be greater within say Mathcad 15 than Mathcad 11, which seems to explain why the recent Mathcad versions have a slower runtime and fall over sooner than Mathcad 11 when processing large arrays etc. I suppose XML has become part of this overhead?

Is there a way to switch of any of the resources overhead? I cannot find a way do so and it may be impossible, but perhaps Mathcad could provide users with an option switch which enables a worksheet to run "lean and mean" with as little bloat as possible.

The regular contributors to this forum (and the previous one) are much more knowledgeable than I on the detailed workings of Mathcad. Hopefully they might comment further on this issue and perhaps share any possible approaches or strategies they have found to improve matters?

Regards

David

There is also some good examples, worksheets and text found in the following thread on this topic.

http://communities.ptc.com/message/154997#154997

Mike

RichardJ
19-Tanzanite
(To:ptc-1371556)

We can work out the absolute size of the memory block for any variable or matrix we set up may not be vary large (ie number of elements times relevant number of bytes for each element). I suggest this is why users become frustrated and believe something is wrong with Mathcad. There seems to be a lot of resources overhead (for each element in a matrix etc) which is part of the internal workings of Mathcad. This may be the real reason why users find their worksheets stop working much sooner than expected due to "insufficient memory" even when they know they have large slabs of RAM.

No. Mathcad versions 12 and later are based on the Microsoft .NET framework. That handles all memory management, and requires that an array be allocated a single, contiguous, block of memory. So it's not the total amount of RAM that matters, it's the size of the largest available contiguous block of RAM. If you launch, for example, program1, program 2, program3, then Mathcad I assume Windows allocates the memory in a way that minimizes fragmentation. But if you then get an "insufficient memory" message closing program2 is not likely to make any difference. You will have more total RAM available, but the new block freed by program2 will not be contiguous with the rest.

Is there a way to switch of any of the resources overhead?

No, but overhead is not the problem. You need tominimize memory fragmentation. So reboot Windows, and don't launch anything other than Mathcad.

Announcements

Top Tags