Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28920 Discussions

Improving efficiency of a Fortran to MATLAB operation

Gao__Wu
Novice
668 Views

Hi all,

I am writing to ask for some advice to optimize my Fortran codes.

A Fortran subroutine is running on the Visual Studio 2013 with Intel Parallel Studio XE 2016, Windows 10. In this program, an m-file of MATLAB is executed first to generate a data-file for the subsequent Fortran codes. It takes some time (varied from a few seconds to ten seconds) for MATLAB to finish its task. There is a risk that the subsequent Fortran codes would start to run before the MATLAB process finishes. I wrote a section of code as follows. It works but is not efficient considering the 15 seconds sleep time. I would like some advice as to improve the efficiency of this operation, in general. Thanks in advance.

logical (4)      :: result_matlab  
logical (4)      :: exists
real (kind=dbl),dimension(1000) :: dataoutofmatlab

result_matlab = SYSTEMQQ("directory of matlab\matlab.exe" -nodesktop -nosplash -r "my_mfile")  
INQUIRE (FILE = dataoutofmatlab, EXIST = exists)    % MATLAB generates ‘dataoutofmatlab.dat’  
if (.NOT. exists) then  
    call sleep(15)  
end if

open(2, file = 'dataoutofmatlab.dat', form = 'formatted')   % Fortran codes read ‘dataoutofmaltab.dat’  
do i = 1, 1000  
    read(2,*) dataoutofmatlab(i)  
end do  
close(2)

 

0 Kudos
2 Replies
David_Billinghurst
New Contributor III
668 Views

Even if the file exist, there is no guarantee that MATLAB has finished generating it when your Fortran program starts to read it.

The SYSTEMQQ function waits until the command terminates.  If you run matlab non-interactively then all should be good.  From memory you need something like

result_matlab = SYSTEMQQ(' "directory of matlab\matlab.exe"  -batch "my_mfile" ' )

You may need -nodesktop or -nosplash - too long ago to recall all the details.  I suggest you Read The Fine Manual and experiment running Matlab from the command line.

0 Kudos
jimdempseyatthecove
Honored Contributor III
668 Views

Additionally, it is not unusual for an Antivirus program to scan a newly created file for potential virus. When it does so, this new file might not be opened in a shared mode (by AV and/or user program).

To correct for this possibility, add your program's output folder to your AV's exclusion list.

Jim Dempsey

0 Kudos
Reply