小编Ian*_*ush的帖子

将字符串数组从 C 返回到 Fortran

我正在尝试使用“iso_c_binding”将一个字符串数组从 C 返回到 Fortran。该程序编译,但给出一个运行时错误。我的 C 程序:

#include <stdio.h>    
void ret_array(int * numStrings, char **arr2 ) {                                
    int dim1=5;    
    char *arr1[5]={"name1","name2","name3","name4","name5"};    
    arr2 = &arr1;    
    printf("%s\n",arr2[0]);    
    printf("%s\n",arr2[1]);    
    printf("%s\n",arr2[2]);    
    printf("%s\n",arr2[3]);    
    printf("%s\n",arr2[4]);    
    numStrings = &dim1;    
    printf("%s","Ending  interface :");    
    fflush(stdout);    
 }  
Run Code Online (Sandbox Code Playgroud)

我的调用 Fortran 程序

program main
  implicit none
  CHARACTER(LEN = 255), dimension(:), allocatable:: str2
  integer(kind = 4):: istr
  call get_arr(istr, str2)
  PRINT *, str2(1)
contains
  subroutine get_arr(n, str1)
    use iso_c_binding
    implicit none
    INTEGER(KIND = 4):: n
    CHARACTER(LEN = 255), dimension(:), ALLOCATABLE:: str1
    character(kind = c_char), …
Run Code Online (Sandbox Code Playgroud)

c fortran pointers fortran-iso-c-binding

5
推荐指数
1
解决办法
92
查看次数

C++ 和 Fortran CUDA 基本示例之间 nvprof 输出的差异

我正在自学CUDA。我的最终目标是将其应用到 Fortran,但因为很多课程/视频都是基于 C/C++,所以我经常最终在两者中执行相同的练习(这是一件好事)。目前,我正在尝试运行一个基本练习,在 GPU 上执行 a(i) = b(i) + c(i) 。为了完整起见,我发布了两个代码以进行比较:

  1. 下面是C代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "cuda.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include "cuda_common.cuh"
#include "common.h"

//assume grid is 1D and block is 1D then nx = size
__global__ void sum_arrays_1Dgrid_1Dblock(float* a, float* b, float *c, int nx)
{
   int gid = blockIdx.x * blockDim.x + threadIdx.x;

   if (gid < nx)
      c[gid] = a[gid] + b[gid];
}


void run_sum_array_1d(int argc, char** argv)
{
   printf("Runing 1D grid \n");
   int …
Run Code Online (Sandbox Code Playgroud)

c malloc fortran cuda

4
推荐指数
1
解决办法
112
查看次数

我什么时候必须在模块中使用 CONTAINS?

我定义了以下模块:

module data_model_2

        implicit none

        ! -------------------
        ! TYPE DEFINITION
        ! -------------------
        type :: type1_record
                integer                 :: month
                integer                 :: day
                integer                 :: year
                integer                 :: hour
                integer                 :: minute
                integer                 :: second
                integer                 :: value1
                integer                 :: value2
        end type
        type :: timestamp_record
                integer                 :: year
                integer                 :: month
                integer                 :: day
                integer                 :: hour
                integer                 :: minute
                integer                 :: second
        end type

end module
Run Code Online (Sandbox Code Playgroud)

我已经看到,modules应使用条款contains。当模块仅包含派生类型定义时是否需要该子句?

如果是这样,它应该包括在implicit none什么地方,在?之后还是之前?

如果模块还包括函数或过程,派生类型定义是在contains子句之前还是之后?

我很想知道这在 Fortran …

fortran

3
推荐指数
1
解决办法
49
查看次数

fortran:检测作为虚拟参数传递的空指针

我想从子例程内部检测到传递的虚拟参数intent(in)实际上是空指针:

program testPTR
  
implicit none
  
integer, target :: ii
integer, pointer :: iPtr
  
  iPtr => ii
  iPtr = 2
  
  print *, "passing ii"
  call pointer_detect(ii)
  
  print *, "passing iPtr"
  call pointer_detect(iPtr)
  
  iPtr => null()
  print *, "passing iPtr => null()"
  call pointer_detect(iPtr)
  
contains
                                                                                                                                                                      
  subroutine pointer_detect(iVal)
      implicit none
      integer, intent(in), target :: iVal
      integer, pointer :: iPtr
      character(len = *), parameter :: sub_name = 'pointer_detect'
    
      iPtr => iVal
      if (associated(iPtr)) then
        print *, "Pointer associated. Val=", iVal, ", iPtr …
Run Code Online (Sandbox Code Playgroud)

standards fortran pointers

3
推荐指数
1
解决办法
309
查看次数

gfortran -std=f2008 如果调用 fseek 存在则编译标志错误

我正在尝试使用 gfortran 强制执行 Fortran 2008 一致性检查。当我使用 -std=f2008 编译时,出现以下错误:

Undefined symbols for architecture x86_64:
  "_fseek_", referenced from:
      _MAIN__ in ccJtOcKa.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

以下简单的测试程序显示了该问题。它可以在没有 -std=f2008 的情况下正常编译和运行,但在设置此标志时不会编译。

program main
  implicit none
  integer :: i
  open(unit=10, file='test.bin', access='stream')
  write(10) 100

  !fseek works, but will not compile with -std=f2008
  call fseek(10, -4, 1)
  read(10) i
  print *, i
end program
Run Code Online (Sandbox Code Playgroud)

这有效: gfortran main.f90

这给出了错误: gfortran -std=f2008 main.f90

我正在使用 gfortran 8.2 的 MacPorts 版本 …

fortran gfortran

2
推荐指数
1
解决办法
392
查看次数

在 FORTRAN-77 中传递二维数组的子数组

我有二维数组

real triangle(0:2, 0:1)
Run Code Online (Sandbox Code Playgroud)

其中“三角形”是一个向量数组(一维数组),我也有子程序

subroutine vecSub(lhs, rhs, result)
real lhs(0:1), rhs(0:1), result(0:1)
    result(0) = lhs(0) - rhs(0)
    result(1) = lhs(1) - rhs(1)
    return
end
Run Code Online (Sandbox Code Playgroud)

有没有办法将“三角形”变量中的向量之一传递给这个子程序?Fortran-90 可以做到这一点:triangle(0, :)它给出了第一个三角形数组,但我只能使用 FORTRAN-77,所以这不行,有什么建议吗?

arrays fortran fortran77

2
推荐指数
1
解决办法
330
查看次数

其他语言中是否有类似的“COMMON”实现,Fortran 的引用环境?

Fortran 语言有一个称为 COMMON 的引用环境。

正如下面网站中所定义的,COMMON 语句定义了一个主内存存储块,以便不同的程序单元可以共享相同的数据,而无需使用参数。

https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vn7v/index.html

示例实现如下所示:

在此处输入图片说明

我想知道在其他语言(如 C、Python 或 Java)中是否有此类环境的类似实现,以及它与 Global 环境有何不同。

fortran global-variables

2
推荐指数
1
解决办法
92
查看次数

链接 2 个具有相同模块名称和子例程名称的库

我在一个 Fortran 项目中,我必须链接 2 个具有相同模块名称的库,在它们下具有相同的子例程名称。我正在使用 Intel Fortran 编译器,当我导入模块并调用子例程时,它总是转到第一个链接的模块。

有没有一种方法可以专门从特定库调用子例程?

这是一些伪代码:

Lib1 和 Lib 2 都有这个:

module foo
  subroutine func()
    write (*, *) "Hello from Lib1" ! or Lib2
  end subroutine()
end module
Run Code Online (Sandbox Code Playgroud)

主要的

program Main
  use foo, only: func

  call func()
end program
Run Code Online (Sandbox Code Playgroud)

CMakeLists.txt


target_link_libraries(Main PRIVATE libLib1.so libLib2.so)

Run Code Online (Sandbox Code Playgroud)

fortran

2
推荐指数
1
解决办法
254
查看次数

Fortran 指针赋值,“=&gt;”和“=”的区别

我努力理解的不同的行为=>=使用Fortran 95即分配一个指针到另一个时说,我有一个导出数据类型foo,那么,什么是在下面的代码片段,它们是等价的最后两行之间的区别?

type(foo), target :: f
type(foo), pointer :: p1, p2

f = foo(...)
p1 => foo

p2 => p1
p2 = p1
Run Code Online (Sandbox Code Playgroud)

fortran pointers

1
推荐指数
1
解决办法
94
查看次数

如何在 Fortran 中将 real(kind=8) 类型的数组正确转换为 real(kind=4) 类型之一

这个问题适用于 Fortran,更准确地说是编译器 gfortran。

我需要知道如何将 real(kind=8) 数组转换为 real(kind=4) 数组,以便保留数组每个条目中的所有合理数字。

显然,从 kind=8 到 kind=4 的类型转换意味着信息丢失。因此,我正在寻找的是丢失尽可能多的信息的正确方法 - 但不是更多。

fortran types casting gfortran

1
推荐指数
1
解决办法
85
查看次数