简体   繁体   English

在FORTRAN和C ++之间使用管道

[英]Use Pipe between FORTRAN and C++

I am trying to pass large amount of data (double numbers) from a FORTRAN program to a C++ Program using pipe method. 我试图使用管道方法将大量数据(双数)从FORTRAN程序传递到C ++程序。

I followed http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx for the C++ part. 我遵循了http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx的C ++部分。 But for the FORTRAN part (which is the child processor) I do not know how to properly write data. 但是对于FORTRAN部分(即子处理器),我不知道如何正确写入数据。 Do I have to write all the numbers on the console using WRITE(*,*) and then read it with the C++ program? 我是否必须使用WRITE(*,*)在控制台上写所有数字,然后使用C ++程序读取它?

My FORTRAN code to write data: 我的FORTRAN代码写入数据:

        DO 281 I=1,NDOF 
        DO 280 J=1,UBW              
        IF (S(I,J).NE.0) THEN      
          WRITE (*, 2770) I,(J+I-1)
          WRITE (*,2760) (S(I,J))          
        ENDIF        
 280    CONTINUE         
 281    CONTINUE

I do not think my FORTRAN part is correct because writing on the console takes a lot of time! 我认为我的FORTRAN部分不正确,因为在控制台上编写会花费很多时间! (It even becomes slower than passing data by file!!!) Any suggestion is appreciated. (它甚至比通过文件传递数据还要慢!!!)任何建议都值得赞赏。

You mentioned writing to files. 您提到写入文件。 Have you thought of using binary output from the fortran program and binary input on the C++ side using fread? 您是否考虑过使用fortran程序的二进制输出以及使用fread在C ++端使用二进制输入? That is significantly faster since there is no parsing involved. 由于不涉及解析,因此速度明显加快。 If that is still slow, then pipes won't really solve your speed problem. 如果那仍然很慢,那么管道将无法真正解决您的速度问题。

If the powers that be insist you use formatted output and pipes then so be it. 如果坚持要使用格式化的输出和管道,那么就这样吧。 Say your fortran program is called fort and your c++ program cpp. 假设您的fortran程序称为fort,而您的c ++程序cpp。 To check your fortran output 检查您的fortran输出

fort > xxx.txt

On the C++ side, you don't really need to use createpipe if it is just one way communication, you could just use stdin. 在C ++方面,如果它只是一种通信方式,则实际上并不需要使用createpipe,而只需使用stdin。 That way it is quite easy to test using 这样很容易测试使用

type xxx.txt | cpp

When everything is working use 当一切正常时使用

fort | cpp

Using MS file/pipe handling may be faster but you still need to read the data into a buffer and sscanf it which is what CI/O does, so you may as well just use CI/O and scanf from stdin. 使用MS文件/管道处理可能会更快,但是您仍然需要将数据读入缓冲区并对其进行sscanf扫描(这是CI / O的作用),因此您也可以仅使用stdin中的CI / O和scanf。 If you are doing this in binary, the whole scanf part can be left out. 如果以二进制形式执行此操作,则可以忽略整个scanf部分。 The advantage of keeping to the standard stuff is you can then port it to Unix or other operating system quite easily at a later date. 保留标准内容的优点是,您以后可以很轻松地将其移植到Unix或其他操作系统。

Another alternative is to convert the Fortran program into a library and call it from C++. 另一种选择是将Fortran程序转换为库,然后从C ++调用它。 That way, you just mess with arrays instead of I/O. 这样,您只会搞乱数组而不是I / O。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM