开发者

Fortran pointer for reordered array to pass to procedure

开发者 https://www.devze.com 2023-03-12 15:49 出处:网络
I\'m trying to integrate two Fortran 9x codes which contain data arrays with opposite array开发者_JS百科 ordering.One code (I\'ll call it the old code) has an established library of subroutines and I

I'm trying to integrate two Fortran 9x codes which contain data arrays with opposite array开发者_JS百科 ordering. One code (I'll call it the old code) has an established library of subroutines and I am trying to take advantage of these with the other (new) code as efficiently as possible (i.e. not having to create temporary arrays just to reorder an array and pass it to a subroutine and then have to replace the old array with the new reordered result). For example,

Old code:

oldarray(1:n,1) -> variable 1 for n elements

oldarray(1:n,2) -> variable 2 for n elements

.. and so on

new code:

newarray(1,1:n) -> variable 1 for n elements

newarray(1,1:n) -> variable 2 for n elements

.. and so on

The variable indices do not necessarily relate between the two codes. If I only need one variable to pass to a procedure, I just pass newarray(1,1:n) and the procedure doesn't know the difference. However, if a procedure from the old code requires variables 1-6 of oldarray which might correspond to variables 2,6,8,1,4,3 (I just picked arbitrary numbers) of newarray, is it possible to create a pointer that I could pass to the procedure? On a simpler side, would it be possible to just create pointer for the tranpose of the new array? As an example, pointer(1000,6) points to newarray(6,1000).

Note: It is not possible to rewrite the new code to use the same array ordering because both codes use an array ordering that best suits its loop structures which cannot be changed.

Also, I have very little experience with pointers. I know I can create a derived datatype which consists of an array of pointers but I don't think I would be able to pass that to a procedure in the manner required (I could be wrong as I also have very little experience with derived datatypes). The reference book I have (Fortran 95/2003 for Scientists and Engineers) only explores advanced applications of pointers in terms of linked lists and trees. I have also found little Fortran pointer information outside of what is covered in this book on the internet.

Thank you for your help.


I think the answer is no, you can't do this, and it wouldn't help anyway.

You can do all sorts of super-cool things with array pointers, with strides across arrays, etc, but I don't see on the face of it how you can change the order of the data.

So I could be wrong on this and it is possible, but then the question is: how would it help you? Presumably you want to user pointers to re-arrange the data without copying; but when you're passing such a thing around, the compiler is allowed to do copy-in, copy-out; eg, create a temporary array, copy the data in, pass it to the subroutine, and copy the data out upon return. And in fact that would almost certainly be the right thing to do in this case, performance-wise; that way the old code could be accessing memory in the fast order, and the transpose-copy could be done in a fast way, as well.

So I suspect the right way to treat this problem is to do the copy-in/copy-out approach yourself explicitly.

0

精彩评论

暂无评论...
验证码 换一张
取 消