Output from fortran application not showing up in Matlab

☆樱花仙子☆ 提交于 2019-12-25 05:18:28

问题


I'm having some issues with output from a fortran application being executed from within Matlab. We use Matlab to call a number of fortran applications and to display output and results.

I'm using gfortran on OSX to build one of these programs, which does a large amount of file output and a little output to stdout to track progress. stdout output is accomplished mainly through print * statements, but I've tried write( * , * ) as well. The program uses OpenMP, but none of the print * or write( * , * ) statements are performed within OpenMP parallel sections.Everything works fine when the program is executed from a terminal. However, when the program is executed from within matlab, there is no output from stdout. The file output works fine though.

Additionally, the same code, when compiled with Intel's ifort, displays its output in matlab without issue. Unfortunately I don't have regular access to the Intel compiler.

I'm positive that the output is going to stdout (not stderr), and I've tried flushing both from within the code (call flush(6) & call flush(0)), but this doesn't seem to make a difference.

I'm not sure what could be causing this. Any thoughts?

some relevant information: OS: OSX 10.6.8 (64bit mode)

Matlab: R2012b

gfortran: 4.7.2 (obtained via fink)

compile flags: -cpp -fopenmp -ffree-line-length-0 -fno-range-check -m64 -static-libgfortran -fconvert=little-endian -fstrict-aliasing

EDIT:

I've done some more testing, creating a simple 'hello' program:

program printTest
write (*,*) 'hello'
end program

compiled with...

gfortran test.f90 -o test

which exhibits the same behavior.

I've also tried compiling with an earlier version of gfortran (4.2.1), which produced some interesting results. it executes fine in terminal, but in matlab I get the following:

!./test dyld: lazy symbol binding failed: Symbol not found: __gfortran_set_std Referenced from: /Users/sah/Desktop/./test Expected in: /Applications/MATLAB_R2012b.app/sys/os/maci64/libgfortran.2.dylib

dyld: Symbol not found: __gfortran_set_std Referenced from: /Users/sah/Desktop/./test Expected in: /Applications/MATLAB_R2012b.app/sys/os/maci64/libgfortran.2.dylib

./test: Trace/breakpoint trap

This leads me to believe its a library issue. using -static-libgfortran produces the same result in this case.


回答1:


I believe Matlab is a single threaded application. When you invoke a multithreaded executive, I have seen various issues with piping the output back to Matlab. Have you considered recompiling into a Fortran mex file?




回答2:


I am not sure a mex file would print to stdout any better than a standalone executable.

There are other options. One is to write(append) all your diagnostics to a file and just look at the file when you want to. Emacs, for example, automatically "revert"s the contents of a file every second or whatever you set the interval to. Another option might be to convert the fortran source into matlab source (see f2matlab) and keep it all in matlab.

bb




回答3:


According to the system function documentation

[status, result] = system('command') returns completion status to the status variable and returns the result of the command to the result variable.

[status,result] = system('command','-echo') also forces the output to the Command Window.

So you should use '-echo' parameter to the system call to see the output directly in the command window

system(['cd "',handles.indir,'";chmod u+x ./qp.exe',... ';./qp.exe'], '-echo')

or you can assign the stdout to a variable:

[ret txt] = system(['cd "',handles.indir,'";chmod u+x ./qp.exe',... ';./qp.exe'])


来源:https://stackoverflow.com/questions/12629742/output-from-fortran-application-not-showing-up-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!