包含多个数组的FORTRAN指针 [英] FORTRAN pointer encompassing multiple arrays
问题描述
我正在一个项目中,尽管在第4维中大小可能有所不同,但在第1维,第2维和第3维中有许多相同大小的数组.
I'm working on a project where I have a number of arrays of the same size in the 1st, 2nd and 3rd dimension, although the sizes may vary in the 4th dimension.
我想通过构造一个连接这些数组的指针来对这些数组进行分组.
I would like to group these arrays by constructing a pointer which concatenates these arrays.
为了使这一点变得不那么抽象,假设我有2个数组:
To make this less abstract, let's say I have 2 arrays:
A (size: N1 x N2 x N3 x N4a)
B (size: N1 x N2 x N3 x N4b)
在项目的早期版本中,
将这些数组复制到大小为N1 x N2 x N3 x (N4a + N4b)
的数组C中,然后将其传递给子例程以对该数组执行ffts.
in previous versions of the project these arrays where copied to an array C of size N1 x N2 x N3 x (N4a + N4b)
which would then be passed to a subroutine to perform ffts on this array.
我想避免这种复制操作,并构造一个指针p
,该指针将包含与先前版本中的数组C相同的数据,但没有显式复制和额外的内存分配.
I would like to avoid this copying operation and construct a pointer p
which would contain the same data as the array C in the previous version but without the explicit copying and additional memory allocation.
在Fortran中有可能吗?
Is this possible in Fortran?
推荐答案
否.指针不能同时指向两个原本独立的对象.
No. A pointer cannot point across two otherwise independent objects like that at the same time.
根据您的情况,可行的方法是从维数为(N1,N2,N3,N4a + N4b)的数组开始,然后将A和B关联(指针,存储或参数)初始大数组的某些部分.
Depending on your situation, what might be workable is to start with an array that is of dimension (N1,N2,N3,N4a+N4b) and then make A and B associated (pointer, storage or argument) to the relevant parts of that initial big array in some way.
REAL, TARGET :: c(N1,N2,N3,N4a+N4b)
REAL, POINTER :: a(:,:,:,:)
REAL, POINTER :: b(:,:,:,:)
a => c(:,:,:,:n4a)
b => c(:,:,:,n4a+1:)
! Go forth and do things with a and b.
! Then later do things with c.
在黑暗时期,在Fortran进行任何类型的动态内存分配之前,通常将这种一元化到规则化的方法分解开来.
In the dark times, before Fortran had dynamic memory allocation of any sort, this sort of one-array-to-rule-them-all that then got divvied out was in common usage.
这篇关于包含多个数组的FORTRAN指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!