Tag: fortran

从fortran写出二进制文件并在C中读取

我正在尝试读取一个由fortran程序生成的二进制文件。 我的输出中有一些奇怪的字符,难道这两个fortran和C都有不同的endianess吗?

写入Fortran和C程序之间的管道时出现问题

我有一个由其他人用Fortran编写的程序,因此从标准输入中读取一些内容然后进行一些计算并输出结果。 我想要做的是使用来自另一个程序的不同输入数据多次运行它,由我用C语言编写。 要做到这一点,我使用popen : FILE *pipe = popen(“.\\program.exe”, “wt”); if (!pipe) { exit(1); } fprintf(pipe, “%d\n”, thing1); fprintf(pipe, “%d\n”, thing2); … pclose(pipe); 问题是它不能以这种方式工作。 它与“program.exe <input.txt”完美配合,但不是这种方式。 它读取第一件事然后输出这个愚蠢的错误:“IO-09系统文件错误 – 未知错误”。 当然我不知道这意味着什么,因为我从未编写过Fortran。 我究竟做错了什么? 编辑: 不幸的是,我没有该程序的源代码

在f2py中设置fortran编译器

我试图运行f2py 示例来创建编译的扩展模块¶: # import os # os.environ[“CC”] = “gcc” # os.environ[“CXX”] = “g++” # Using post-0.2.2 scipy_distutils to display fortran compilers from scipy_distutils.fcompiler import new_fcompiler compiler = new_fcompiler() # or new_fcompiler(compiler=’intel’) compiler.dump_properties() #Generate add.f wrapper from numpy import f2py with open(“add.f”) as sourcefile: sourcecode = sourcefile.read() print ‘Fortran code’ print sourcecode # f2py.compile(sourcecode, modulename=’add’, extra_args […]

将Fortran整数数组传递给C子例程仅传递第一个元素

我试图将一个整数数组从Fortran传递给C,但我只能传递数组的第一个元素。 我有下面的测试程序再现错误。 我哪里错了? program test use foo integer (kind=c_int), allocatable :: hgmu_dose(:) allocate (hgmu_dose(0:10)) HGMU_dose(0)=22 HGMU_dose(1)=2 HGMU_dose(2)=3 HGMU_dose(3)=4 HGMU_dose(4)=5 HGMU_dose(5)=6 HGMU_dose(6)=7 HGMU_dose(7)=8 HGMU_dose(8)=9 HGMU_dose(9)=10 HGMU_dose(10)=11 print *, “HGMU_dose=”, hgmu_dose call test_interface(hgmu_dose) end program module foo use ISO_C_Binding implicit none interface subroutine test_interface(dose) bind(C,name=”INTERFACE”) import :: c_int import :: c_double import :: c_char integer (kind=c_int), allocatable […]

Fortran / C混合:如何在Fortran中访问动态分配的C数组?

我目前遇到内存问题:我有一个用Fortran编码的主程序,它调用C / C ++子程序来执行某些任务并将数据存储在动态分配的数组中。 问题是我需要在返回Fortran主程序时访问这些数据。 我试图在fortran中声明一个C指针(TYPE(C_PTR))指向数组,但它似乎不起作用。 该数组存在于C子例程中,但是当我回到Fortran主程序时尝试访问它时会出现段错误。 我在这里提供我的代码,任何想法? 谢谢你的帮忙 !! Fortran语言: PROGRAM FORT_C use iso_c_binding IMPLICIT NONE interface subroutine call_fc(pX,s) bind(C,name=’call_fc_’) import integer(c_int) :: s type(c_ptr), pointer :: pX end subroutine end interface integer(c_int) :: i integer(c_int) :: s integer(c_int), pointer :: X(:) type(C_ptr), pointer :: pX s=100 call call_fc(pX,s) call c_f_pointer(pX,X,(/s/)) ! This here […]

使用Fortran和C调用Metis API

我编写了以下代码,用于使用Fortran和C调用Metis,但仍然会出现分段错误。 代码的目标是为输入文件chanelElements执行分区。 module metis_vars use iso_c_binding ! Variables integer :: ia, ic integer(kind=c_int) :: ne, nn integer(kind=c_int) :: ncommon, objval integer(kind=c_int) :: nparts integer(kind=c_int), allocatable, dimension(:) :: eptr, eind integer(kind=c_int), allocatable, dimension(:) :: epart, npart type(c_ptr) :: vwgt, vsize, tpwgts integer :: opts(0:40) interface subroutine METIS_PartMeshDual(ne,nn,eptr,eind,vwgt,vsize,ncommon,nparts,tpwgts,opts, & objval,epart,npart)bind(C, name=”METIS_PartMeshDual”) !subroutine METIS_PartMeshDual(ne,nn,eptr,eind,vwgt,vsize,nparts,tpwgts,opts,objval,epart,npart) & ! bind(C, name=”METIS_PartMeshDual”) […]

从Fortran传递2d数组到C

我很难将Fortard中的二维数组传递给C函数。 但是,在所有支持之后,以下代码100%正常运行。 以下是我的C函数: #include void print2(void *p, int n) { printf(“Array from C is \n”); double *dptr; dptr = (double *)p; for (int i = 0; i < n; i++) { for (int j = 0; j<n; j++) printf("%.6g \t",dptr[i*n+j]); printf("\n"); } } 以下是我的Fortran代码: program linkFwithC use iso_c_binding implicit none interface subroutine my_routine(p,r) bind(c,name=’print2′) import […]

返回指向设备分配矩阵的指针从C到Fortran

首先,我是Fortran / C / CUDA的新手。 其次,我正在研究一个使用cuBLAS在GPU上执行矩阵向量乘法的Fortran / C程序。 在需要更新矩阵内容之前,我需要将多个(最多1000个)向量与一个矩阵相乘。 但是,每当新的向量发送到GPU时,我必须重新分配矩阵的当前版本(由于矩阵没有改变,这非常浪费和缓慢)。 我希望能够将矩阵与向量相乘,而无需为每个向量重新分配矩阵。 我所涉及的一个想法是调用一个单独的C函数,它将矩阵分配给GPU,返回指向Fortran主程序的指针,然后调用执行矩阵向量乘法的另一个C函数。 使用ISO_C_BINDING,我向变量返回一个指向浮点数的指针: type(C_PTR) :: ptr 当我尝试将其传递给矩阵向量C函数时: 在Fortran call cudaFunction(ptr,vector, N) 在C. extern “C” void cudaFunction_(float *mat, float *vector, int *N) 一切都编译并运行,但是cublasSgemv的执行无法执行。 关于为什么会发生这种情况的任何想法? 我已经看到了一些相关的post,但他们从未尝试将返回的指针发送回C,这就是(我相信)我遇到的问题。 提前致谢!

FORTRAN调用一个C dll,后者又调用另一个FORTRAN dll

首先是免责声明,我不是C和FORTRAN之间互操作性的专家。 我在FORTRAN(GNU FORTRAN编译器)中有我的主程序。 它需要调用C dll进行一些计算。 但是,C dll需要调用另一个用FORTRAN编写的第三方dll。 所以基本上调用的是: 主FORTRAN程序 – > C dll – >第三方FORTRAN dll。 我根本无法使用它。 我无法再获得调试信息。 我看到的唯一错误是“程序接收信号SIGSEGV,分段故障”。 主FORTRAN – > C dll工作正常。 我通过评论对FORTRAN dll的调用来测试。 我进一步做了以下事情: 测试程序C ++ – > C dll – >第三方FORTRAN dll。 上述情况完全正常。 所以它迫使我得出结论,错误是由FORTRAN – > C – > FORTRAN的调用序列引起的 我的问题是,对这样的呼叫序列有什么特别的考虑吗? 谢谢您的帮助。 问候,snkp 编辑:问题代码 Fortran部分 !Interface declaration INTERFACE SUBROUTINE SetupProgram(Num,NumComponents,ComponentConc,ConcPhase,ErrNum,Name,hr,herr,hfm)BIND(C,NAME=’SetupProgram’) USE […]

如果可能的话,如何进行fftw3 MPI“转置”2D变换?

考虑从复杂数组src到实数数组tgt的formsL x M(列主要设置)的2D变换。 或者,在Fortranese, complex(C_DOUBLE_COMPLEX), pointer :: src(:,:) real(8), pointer :: tgt(:,:) . 相应的指针是 type(C_PTR) :: csrc,ctgt . 我会按以下方式分配它们: ! The complex array first alloc_local = fftw_mpi_local_size_2d(M,L/2+1,MPI_COMM_WORLD,local_M,local_offset1) csrc = fftw_alloc_complex(alloc_local) call c_f_pointer(csrc, src, [L/2,local_M]) ! Now the real array alloc_local = fftw_mpi_local_size_2d(2*(L/2+1),M, & MPI_COMM_WORLD,local_L,local_offset2) ctgt = fftw_alloc_real(alloc_local) call c_f_pointer(ctgt, tgt, [M,local_L]) 现在,该计划将创建为: ! Create c–>r […]