我一直在努力寻找有关设计派生类型的任何具体信息。我认为讨论这个问题的最佳方式是通过几个选项。我用派生类型的不同应用编写了一些代码部分。我更愿意为 、 和 使用nparts
动态index
数组refs
。我省略了实际使用该结构的代码部分(没有任何代码,因为我编造了它),但显示了一个示例,并且在例程中我打算至少使用该结构中的所有值一次。
选项 A:在派生类型中使用静态数组。缺点是我必须在编译时猜测数组大小。
! Known before compile time.
nboxes = 5000
max_parts = 2000
packs = 10
Type Boxes
Sequence
Integer :: location, date
Integer, Dimension(0:packs) :: nparts
Integer, Dimension(max_parts,packs) :: index
Real(Kind=8), Dimension(packs,packs) :: refs
End Type Boxes
type(boxes), dimension(:), allocatable :: assembly
allocate(assembly(nboxes))
! Perform some operations on assembly...
do i = 1,nboxes
do j = 1,packs
do k = j,packs
example = assembly(i)%nparts(k) - assembly(i)%nparts(j)
.
.
do m = 1,max_parts
example = assembly(i)%index(m,j) + assembly(i)%refs(k,j) * assembly(i)%nparts(j)
.
.
end do
end do
end do
end do
选项 B:在派生类型中使用动态数组。
! Defined during execution. Much better.
nboxes = 5000
max_parts = 2000
packs = 10
Type Boxes
Sequence
Integer :: location, date
Integer, Dimension(:), Allocatable :: nparts
Integer, Dimension(:,:), Allocatable :: index
Real(Kind=8), Dimension(:,:), Allocatable :: refs
End Type Boxes
type(boxes), dimension(:), allocatable :: assembly
allocate(assembly(nboxes))
do i = 1,nboxes
allocate(assembly(i)%nparts(0:packs))
allocate(assembly(i)%index(max_parts,packs))
allocate(assembly(i)%refs(packs,packs))
end do
! Perform some operations on assembly...
do i = 1,nboxes
do j = 1,packs
do k = j,packs
example = assembly(i)%nparts(k) - assembly(i)%nparts(j)
.
.
do m = 1,max_parts
example = assembly(i)%index(m,j) + assembly(i)%refs(k,j) * assembly(i)%nparts(j)
.
.
end do
end do
end do
end do
选项 C:最小化派生类型中使用的动态数组的数量,并强制assembly
成为数组。请注意,在这个版本中,我们有一堆未使用的内存。例如,nparts
并且index
需要内存packs
时间,因为assembly(packs,packs,nboxes)
.
! Defined during execution. Much better.
nboxes = 5000
max_parts = 2000
packs = 10
Type Boxes
Sequence
Integer :: location, date, nparts, index
Real(Kind=8) :: refs
Integer, Dimension(:), Allocatable :: index
End Type Boxes
type(boxes), dimension(:,:,:), allocatable :: assembly
allocate(assembly(packs,packs,nboxes))
do i = 1,nboxes
do j = 1,packs
do k = 1,packs
allocate(assembly(k,j,i)%index(max_parts))
end do
end do
end do
! Perform some operations on assembly...
do i = 1,nboxes
do j = 1,packs
do k = j,packs
example = assembly(k,j,i)%nparts - assembly(k,j,i)%nparts
.
do m = 1,max_parts
example = assembly(k,j,i)%index(m) + assembly(k,j,i)%refs * assembly(k,j,i)%nparts
.
.
end do
end do
end do
end do
选项 D:选项 C 的另一种排列。
问题:
do
哪个版本是为所示循环示例设计派生类型的正确/预期方法?考虑到我想要动态数组功能,哪个版本最优化?- 可能和上面有关。内存是如何分配和访问的?使用是否
SEQUENCE
值得?我认为分配的数组无论如何都不会按顺序显示。这难道不是表明选项 C 是最好的,因为每个部分assembly
都较小吗? - 我应该将这个派生类型拆分为多个派生类型,还是完全摆脱它而只使用变量?我将在多个例程中使用这个派生类型,并将它放在一个模块中。