未知等级的数组作为子例程参数 [英] Array of unknown rank as subroutine argument

查看:84
本文介绍了未知等级的数组作为子例程参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个与hdf5 Fortran库一起使用的模块.该模块包含子例程,用于在文件中读写不同类型和形状的数组.

I am designing a module which works with the hdf5 Fortran library. This module contains subroutines to read and write arrays of different types and shapes to/from a file.

例如我希望能够调用writeToHDF5(filepath,array)而不管数组的形状和类型如何.我意识到必须使用接口来实现不同类型的接口.但是,我想知道是否有可能具有假定的数组形状.

e.g. I wish to be able to call writeToHDF5(filepath, array) regardless of what the shape and type of array is. I realise that interfaces have to be used to achieve this with different types. I am however wondering if it is possible to have an assumed shape of the array.

例如

如果定义了一个数组,例如

if an array was defined such as

integer(kind=4), dimension(*),intent(in) :: array

,并且传递了二维数组,这将起作用.有什么方法可以为数组的每种形状创建单独的子例程而无需创建单独的子例程?

and a two dimensional array was passed this would work. Is there any way to do this without creating separate subroutines for each shape of the array?

推荐答案

正如弗拉基米尔F所说,Fortran 2015添加了假定等级"-此是有用的 Fortran-Fortran(这是MPI要求的) (对于Fortran绑定),但是当您收到这样的数组时,如果没有其他复杂性,就无法直接对其进行太多操作.已有几个编译器支持此功能,但是很少(如果有的话)支持新添加的SELECT RANK构造,这使得此构造更加有用.

As Vladimir F says, Fortran 2015 adds "assumed-rank" - this is useful Fortran-Fortran (it was requested by MPI for the Fortran bindings), but when you receive such an array, you can't do much with it directly without additional complications. Several compilers support this already, but few (if any?) support the newly added SELECT RANK construct that make this a bit more useful.

但是,您可以使用C_LOC和C_F_POINTER将假定排名的虚拟对象投射"到指向您喜欢的任意级别的数组的指针,所以这是有可能的.

You can, however, use C_LOC and C_F_POINTER to "cast" the assumed-rank dummy to a pointer to an array of whatever rank you like, so that's a possibility.

标准版(甚至可以回溯到Fortran 90)确实在这里给您带来帮助.如果您编写:call writeToHDF5(filepath, array(1,1))(此处假定数组为2级),则被调用过程的显式接口可以通过序列关联"的魔术方法为哑元参数指定任何级别.但是有一些限制-特别是不允许将数组假定为形状或POINTER.

The standard (even back to Fortran 90) does give you an out here. If you write:call writeToHDF5(filepath, array(1,1)) (assuming array is rank 2 here), the explicit interface of the called procedure can specify any rank for the dummy argument through the magic of "sequence association". There are some restrictions, though - in particular the array is not allowed to be assumed-shape or POINTER.

这篇关于未知等级的数组作为子例程参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆