arrays - 将假定等级实体写入文件

标签 arrays multidimensional-array fortran

我正在尝试将假定的排名数组写入文件:

    subroutine write_dbl_gen(filename, array)
        Implicit None
        character(len=*), intent(in)     :: filename
        real(8), intent(in)              :: array(..)

        open(unit=p_un, file=filename, form="unformatted",&
             access="stream")
        write (p_un) array
        close(unit=p_un)
    end subroutine write_dbl_gen

ifort 然后提示:

(base) > $ make                                                                                                                                                                                     [±master ●]
[ 25%] Building Fortran object CMakeFiles/npy.x.dir/src/npy.F90.o
/Users/redies/NPY-for-Fortran/src/npy.F90(835): error #8842: An I/O list item must not be an assumed rank object.   [ARRAY]
        write (p_un) array
---------------------^
compilation aborted for /Users/redies/NPY-for-Fortran/src/npy.F90 (code 1)
make[2]: *** [CMakeFiles/npy.x.dir/src/npy.F90.o] Error 1
make[1]: *** [CMakeFiles/npy.x.dir/all] Error 2
make: *** [all] Error 2

如何将假定的等级数组写入文件?我尝试 reshape 它,但“ reshape ”功能不会采用假定的等级数组。我使用“转移”尝试了类似的事情,但它有同样的问题。

最佳答案

假定级别的实体出现的位置非常有限。特定约束(Fortran 2018,C837)说:

An assumed-rank variable name shall not appear in a designator or expression except as an actual argument that corresponds to a dummy argument that is assumed-rank, the argument of the function C_LOC or C_SIZEOF from the intrinsic module ISO_C_BINDING (18.2), the first dummy argument of an intrinsic inquiry function, or the selector of a SELECT RANK statement.

ifort 提示在问题的子程序中使用是正确的。

正如 Vladimir F 评论的那样,可以使用 SELECT RANK 构造来创建一个非假定等级实体,然后可以将其用于(可能)多个写入语句(每个等级一个)。

但是,如 an answer elsewhere 中所述,可以将一些假设级别的实体“转换”为另一个已知级别的实体,然后可以在写入语句中使用:

   real(8), pointer :: output_array(:)
   call c_f_pointer(c_loc(array), output_array, [size(array)])
   write (p_un) output_array

因为使用了c_loc来自 iso_c_binding这里array应该有 target添加了属性。


在我上面的例子中,size是一个内在的查询功能,这就是为什么 size(array)被允许。从问题中,reshapetransfer而是转换函数,解释了为什么它们不接受假定秩数组作为参数。

关于arrays - 将假定等级实体写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58938045/

相关文章:

c# - 无法对 C# 中的数组值进行算术运算

c++ - 在 Fortran 和 C++ 之间传递复杂的数据结构

linux - Fortran 内部函数 sqrt() 行为

python - 如何让我的 python 函数使用数组作为输入?

android - "java.lang.OutOfMemoryError"在 Android 中新建一个 int 数组时

javascript - $.post 总是将空值传入服务器

使用 C 的编译器选项捕获浮点异常

PHP 数组条件不起作用

algorithm - 来自采访 : Removing rows and columns in an n×n matrix to maximize the sum of remaining values

php - 计算对象数组的列中某个值的出现次数