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

Calling fortran executable file from matlab is much slower

NC1
Beginner
391 Views

I have a fortran code compiled into a exe file. I can run it directly through the command line, or simply call the executable from matlab. This two approaches lead to massively different execution speeds and I am unsure why.

If I run the executable file from the command line, the first part of the code runs in **1,000** seconds with all cores/threads of my computer being used:

direct_exe_cores.png

If instead I call the fortran executable from matlab it does not use all cores/threads and takes approximately **1,400** seconds to run.

I am calling the code using: `system('path\LoopFortranToMatlab.exe')`



0 Kudos
2 Replies
andrew_4619
Honored Contributor III
383 Views

I would guess that the console that the matlab version is  run from does not have the same environment settings so it is running in serial rather than parallel. Environment setting can be used to control cores settings.

0 Kudos
jimdempseyatthecove
Honored Contributor III
354 Views

I am not a Matlab user, but I do know OpenMP to some extent.

 

Possibilities:

1) Matlab at startup has set the process affinity mask to one logical processor. You can add the environment variable:

    KMP_AFFINITY=verbose

and if possible catch the console output when your program starts. That failing, use the Windows functions to extract this information directly. You can also use @andrew_4619 hint and use the Fortran GET_ENVIRONMENT_VARIABLE to test for, and what are, the expected environment variable settings. 

 

2) IIF Matlab is multi-threaded, .AND. internally use OpenMP for threading, then it is possible that Matlab is calling your program from a parallel region, .AND your program detects it is within a parallel region (I cannot confirm it can in this situation), .AND. nested parallel regions are disabled THEN your code will run with one thread. You can test for this with OpenMP runtime libraries. IIF this is the case, then at your program startup, prior to your first parallel region, use the OpenMP rtl to enable nested parallel regions (or increase the nest maximum levels). Note, if Matlab is engaging all threads when it calls your application then you will have competition for resources.

 

Jim Dempsey

0 Kudos
Reply