Reshape and memory allocation

Reshape and memory allocation

This is mainly a question from Fortran development team, but you are more than welcome to contribute ...

I am workiing on an application dealing with huge matrices. For some reason, I have to use the reshape function to change the shape of the arrays. It is very critical that no allocation, reallocation of memory is taking place during reshape operation (because of speed considerations). My specific question is whether the Fortran implementation allocates and reallocates memory in order to reshape a matrix, or does it without.

Ali Asi

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

I assume you mean the question is FOR the development team.

In the current release of CVF, RESHAPE creates its result on the stack, and does not do any dynamic allocation. This may change in the future, as for large arrays, the stack isn't the best place for temporaries. So, no guarantees.

Note that RESHAPE creates a new copy of its argument with the desired shape. Depending on what you do with it, the compiled code may not actually create a temporary copy but might move the data directly to the array being assigned to.


Retired 12/31/2016

You are right Steve. Just bear in mind that English is my third language, after C++ and Fortran :)

Let me rephrase the question. May be there is a more elegant way of doing what I intend to do. The problem is that I would like to allocate a huge amount of space in meory and then use it as a scratch space. I do not want to keep allocating and deallocating huge arrays (the speed is very crucial here). So, I would like to allocate a big chuck of memory at the beginning of my computations and then map different arrays into it. Assume that I allocate a complex array of c(100, 100) (my actual numbers are bigger than this). After I am done with that array, I won't deallocate it. Instead, I may choose to map an array of real*8 a(20,30) into it and start filling it with useful information and manipulating it. Of course, the actual numbers that I intend to use are much bigger than this.

I already came up with some ideas on how to do this with pointers, ... But I am very interested in knowing what solution you would suggest.

1) If you don't need to reuse the info stored previously, I suggest you do a deallocate/allocate. RTL should be smart enough not to return all memory allocated to the system but to reuse it immediately after.

2) If suggestion 1 is out, Cray (Integer) pointers can be used to map anything to anything else.

3) You can still use F77-style "cheating" with argument mismatches, i.e. pass the first element of the mapped array to a subroutine, where it is declared with another type and/or shape. That requires not to have explicit interfaces, of course.

4) I assume EQUIVALENCE/COMMON is out :-).

Note also that you should be able to have a full control over allocated memory using Win32 APIs (e.g. GlobalAlloc, VirtualAlloc, GlobalLock) directly instead of ALLOCATE statements (but you should do it only if you're not satisfied with performance of Fortran RTL). There's a rich family of memory-manipulation functions which I'm not very familiar with so I suggest you take a look at the docs.


Integer pointers are a good way to do this, as long as you know the bounds of all the arrays you're going to use on entry to the routine. This sort of thing was often done in F66/F77 with COMMON and EQUIVALENCE, though I don't recommend that. RESHAPE is NOT what you want - it does not actually change the shape of a particular array (it returns an array of a specified shape with certain data).

But really, I suggest you give ALLOCATABLE a try first - the performance may not be an issue in real life (except that anything with run-time bounds is going to result in slower code.)


Retired 12/31/2016

Leave a Comment

Please sign in to add a comment. Not a member? Join today