0

I am trying to pass large amount of data (double numbers) from a FORTRAN program to a C++ Program using pipe method.

I followed http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx for the C++ part. But for the FORTRAN part (which is the child processor) I do not know how to properly write data. Do I have to write all the numbers on the console using WRITE(*,*) and then read it with the C++ program?

My FORTRAN code to write data:

        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! (It even becomes slower than passing data by file!!!) Any suggestion is appreciated.

VecTor
  • 83
  • 3
  • 13
  • 1
    Please post some code in C++, the part where you fork and setup the pipe [This question](http://stackoverflow.com/questions/5554263/problem-writing-to-a-pipe-between-fortran-and-c-programs?rq=1) may be of some help – rath May 18 '13 at 01:35
  • See this [link](http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx) – VecTor May 18 '13 at 01:40
  • 1
    possible duplicate of [Passing large amount of data from FORTRAN to C++](http://stackoverflow.com/questions/16619662/passing-large-amount-of-data-from-fortran-to-c) -- please don't reask the same question 1 hour later. – M. S. B. May 18 '13 at 09:46

1 Answers1

0

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? 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. To check your fortran output

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. 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 C I/O does, so you may as well just use C I/O and scanf from stdin. If you are doing this in binary, the whole scanf part can be left out. 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.

Another alternative is to convert the Fortran program into a library and call it from C++. That way, you just mess with arrays instead of I/O.

cup
  • 7,589
  • 4
  • 19
  • 42
  • What is `type xxx.txt | cpp`? Wouldn't it be better to use `cpp < xxx.txt`? – mgilson May 19 '13 at 01:08
  • Yes, but the op is asking about pipes, so, I'm giving an example using pipes. The problem with Windows is that when someone mentions *pipe*, you wonder whether they mean anonymous pipes, which is similar to *nix (example above) or named pipes, which requires a server, security access etc. Normally it is the former but on very rare occasions, you get the latter. – cup May 19 '13 at 18:06