jushin-pon has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am a scientist and use PERL code to connect a commercial package with my own Fortran code. The data generated from the commercial package will be the input data for my Fortran code, and then the output from my Fortran code will be the input for the commercial package. This procedure will be conducted over 1,000,000 times to finish my calculation for a scientific simulation. The way I used to exchange data between my Fortran code and the commercial package is to write it into the hard disk and then read it from the hard disk. This makes my calculation very inefficient. Is it possible by PERL code to make data communication between my Fortran code and the commercial package like a subroutine in a pure Fortran code?
  • Comment on input and output data from PERL code to my Fortran code!!!

Replies are listed 'Best First'.
Re: input and output data from Perl code to my Fortran code
by moritz (Cardinal) on Sep 16, 2009 at 07:15 UTC
    The question is: how does the commercial package provide its data?

    If it writes files, it might bring a considerable speed-up to write these files not to a real hard disk, but to a RAM Disk. This should work without modifying the program.

    You can call C code from Fortran, and you can interface C from Perl too, so you could also interface Fortran an Perl, but I don't see how that's going to help unless you can actually change the commercial package you're interfacing.

    Perl 6 - links to (nearly) everything that is Perl 6.
      Dear moritz: Thank you for your kind help. To write data both from the commerical package and my Fortran code to a RAM Disk is possible, but I don't know how to read the data from a RAM disk for a fortran code, which is embedded as a part of a PERL code. Shin-Pon
        Reading from a RAM disk is just like reading from any other file - that's the beauty of it.

        Oh, and the programming language is spelled Perl, the interpreter is spelled perl - there is no PERL.

        Perl 6 - links to (nearly) everything that is Perl 6.
Re: input and output data from PERL code to my Fortran code!!!
by dHarry (Abbot) on Sep 16, 2009 at 07:57 UTC

    Maybe there are modules available for that purpose: a quick search on CPAN gave me: Fortran::Format for F77 format and Fortran::F90Format for F90 I/O formatting. There are many more Fortran related modules however. You might be able to use them as building blocks to solve your problem or take a look at the source code for inspiration.

    Maybe you should give some more (technical) details on how you envisage the interface to work.

    HTH

    Harry

      Dear Harry: Thank you for your kind help, and I feel your comments will be very helpful for my case. Best Regards, Shin-Pon
Re: input and output data from PERL code to my Fortran code!!!
by graff (Chancellor) on Sep 16, 2009 at 17:01 UTC
    You don't say what your "commercial package" is, but if you haven't searched CPAN yet for modules that relate to that package, you should try that. If there is a module that provides direct data i/o between a perl script and your commercial package -- or if that tool can be run via any sort of "inter-process communication" already supported by perl (cf. perlipc) -- you might be able to avoid some amount of disk i/o using a process that manages in-memory data transfer from one process to the other.

    (Then again, the RAM-disk idea sounds like the easiest way to get a noticeable speed-up.)