<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic 1. is there a better way to in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Within-Fortran-Call-Matlab/m-p/996274#M102435</link>
    <description>&lt;BLOCKQUOTE&gt;
	&lt;P&gt;1. is there a better way to let Fortran handle over to Matlab to process, wait and get back the results besides Call System() ?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;I guess that you're using an extension to the standard.&amp;nbsp; Fortran 2008 has the new intrinsic subroutine:&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;execute_command_line(command [, wait] [, exitstat] [, cmdstat] [, cmdmsg])&lt;/PRE&gt;

&lt;P&gt;So, your Matlab script could be executed by&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;call execute_command_line(" matlab -nodesktop -nojvm -r 'Matlab_script; exit' ")&lt;/PRE&gt;

&lt;P&gt;and by default (wait = .true.) the program would wait for the Matlab script to finish the execution before resuming.&lt;/P&gt;

&lt;P&gt;However, apart from changing from an intel-specific extension to a standard instruction, you would be doing exactly the same thing...&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;P&gt;2. Why call system does not implement the matlab parfor function? &amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;I don't know why you Matlab code would take longer to execute when called from fortran than calling it directly.&amp;nbsp; Moreover, I think that the answer to your question would be the lack of interoperability between fortran and Matlab.&amp;nbsp; So far, the standard only provides interoperability between fortran and c/c++ via iso_c_binding.&lt;/P&gt;

&lt;P&gt;I had a similar problem.&amp;nbsp; I wanted not only to execute a python script from fortran, but also transfer the result from python to F.&amp;nbsp; The simplest solution was something similar to your approach: call execute_command_line to run the python script, write the result to a known file and then read the file in F.&amp;nbsp; After a lot of reading and tweaking I managed to access a python script&amp;nbsp; via c (F &amp;lt;-&amp;gt; C/C++ &amp;lt;-&amp;gt; Python).&amp;nbsp; The interface between C and python is provided by the Py/C API.&lt;/P&gt;

&lt;P&gt;So, if you know that a Matlab script can be run from C/C++, then you could do the same...&lt;/P&gt;</description>
    <pubDate>Wed, 26 Aug 2015 17:09:21 GMT</pubDate>
    <dc:creator>rudi-gaelzer</dc:creator>
    <dc:date>2015-08-26T17:09:21Z</dc:date>
    <item>
      <title>Within Fortran, Call Matlab</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Within-Fortran-Call-Matlab/m-p/996273#M102434</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I have a question about how to call Matlab script &lt;SPAN style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;within Fortran,&amp;nbsp;&lt;/SPAN&gt;which performs Matlab parallel computing "parfor" syntax. Basically, I am solving a problem in Fortran which is mainly a Do-Loop. For each iteration, I write out a series from Fortran for a Matlab script to read in, process through "parfor", and write back to Fortran. Fortran iteration continues until convergence.&lt;/P&gt;

&lt;P&gt;In my Fortran codes, I use call system to let the shell return to Linux commands and call matlab, run matlab script, exit matlab and return to Fortran.&lt;/P&gt;

&lt;P&gt;&lt;SPAN style="font-size: 1em; line-height: 1.5;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; CALL system(" matlab -nodesktop -nojvm -r 'Matlab_script&lt;/SPAN&gt;&lt;SPAN style="font-size: 1em; line-height: 1.5;"&gt;; exit' ")&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;&lt;SPAN style="font-size: 1em; line-height: 1.5;"&gt;The codes look like the above. ifort compiles the entire program with no problem. Also, the entire Fortran program runs fine and the results are correct.&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;&lt;SPAN style="font-size: 1em; line-height: 1.5;"&gt;However, the problem is that within&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;Matlab_script.m, a matlab parfor loop should be executed. Rather, the loop was executed in plain "for, ..., end" loop. It takes so long for Fortran to get back the matlab processed results. I tested the Matlab_script.m without Fortran call system() and the parfor loop goes fine.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;&lt;SPAN style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;Does anyone know &lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;&lt;SPAN style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;1. is there a better way to let Fortran handle over to Matlab to process, wait and get back the results besides Call System() ?&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;&lt;SPAN style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;2. Why call system does not implement the matlab parfor function? &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;&lt;SPAN style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;Please advise. Thank you so much!&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;Calvin&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Aug 2015 19:09:50 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Within-Fortran-Call-Matlab/m-p/996273#M102434</guid>
      <dc:creator>Jia__Dun</dc:creator>
      <dc:date>2015-08-25T19:09:50Z</dc:date>
    </item>
    <item>
      <title>1. is there a better way to</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Within-Fortran-Call-Matlab/m-p/996274#M102435</link>
      <description>&lt;BLOCKQUOTE&gt;
	&lt;P&gt;1. is there a better way to let Fortran handle over to Matlab to process, wait and get back the results besides Call System() ?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;I guess that you're using an extension to the standard.&amp;nbsp; Fortran 2008 has the new intrinsic subroutine:&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;execute_command_line(command [, wait] [, exitstat] [, cmdstat] [, cmdmsg])&lt;/PRE&gt;

&lt;P&gt;So, your Matlab script could be executed by&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;call execute_command_line(" matlab -nodesktop -nojvm -r 'Matlab_script; exit' ")&lt;/PRE&gt;

&lt;P&gt;and by default (wait = .true.) the program would wait for the Matlab script to finish the execution before resuming.&lt;/P&gt;

&lt;P&gt;However, apart from changing from an intel-specific extension to a standard instruction, you would be doing exactly the same thing...&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;P&gt;2. Why call system does not implement the matlab parfor function? &amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;I don't know why you Matlab code would take longer to execute when called from fortran than calling it directly.&amp;nbsp; Moreover, I think that the answer to your question would be the lack of interoperability between fortran and Matlab.&amp;nbsp; So far, the standard only provides interoperability between fortran and c/c++ via iso_c_binding.&lt;/P&gt;

&lt;P&gt;I had a similar problem.&amp;nbsp; I wanted not only to execute a python script from fortran, but also transfer the result from python to F.&amp;nbsp; The simplest solution was something similar to your approach: call execute_command_line to run the python script, write the result to a known file and then read the file in F.&amp;nbsp; After a lot of reading and tweaking I managed to access a python script&amp;nbsp; via c (F &amp;lt;-&amp;gt; C/C++ &amp;lt;-&amp;gt; Python).&amp;nbsp; The interface between C and python is provided by the Py/C API.&lt;/P&gt;

&lt;P&gt;So, if you know that a Matlab script can be run from C/C++, then you could do the same...&lt;/P&gt;</description>
      <pubDate>Wed, 26 Aug 2015 17:09:21 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Within-Fortran-Call-Matlab/m-p/996274#M102435</guid>
      <dc:creator>rudi-gaelzer</dc:creator>
      <dc:date>2015-08-26T17:09:21Z</dc:date>
    </item>
    <item>
      <title>Matlab has an "engine" API</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Within-Fortran-Call-Matlab/m-p/996275#M102436</link>
      <description>&lt;P&gt;Matlab has an "engine" API that you can use to invoke it from within another application.&lt;/P&gt;

&lt;P&gt;See &lt;A href="http://mathworks.com/help/matlab/calling-matlab-engine-from-c-c-and-fortran-programs.html" target="_blank"&gt;http://mathworks.com/help/matlab/calling-matlab-engine-from-c-c-and-fortran-programs.html&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;I have no idea whether this will alter in anyway what you are seeing with parfor though.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2015 01:45:37 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Within-Fortran-Call-Matlab/m-p/996275#M102436</guid>
      <dc:creator>IanH</dc:creator>
      <dc:date>2015-08-27T01:45:37Z</dc:date>
    </item>
    <item>
      <title>Hi Rudi,</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Within-Fortran-Call-Matlab/m-p/996276#M102437</link>
      <description>&lt;P&gt;Hi Rudi,&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;Thank you so so much for giving me the insights. I tried the execute_command_line with the wait option. This works fine and well reaches my goal. Therefore I guess this one is an equivalent to call system() syntax.&lt;/P&gt;

&lt;P&gt;About bullet 2, I actually figured out why matlab parfor was not called. In order to reduce the matlab system time, I used the option -nojvm to turn off the&amp;nbsp;&lt;SPAN style="color: rgb(64, 64, 64); font-family: Arial, Helvetica, sans-serif; font-size: 12.75px; line-height: 18.6149997711182px;"&gt;Java&amp;nbsp;&lt;/SPAN&gt;functionality. It turned out that this can prevent Matlab to create parpool. So instead, I used&amp;nbsp;&lt;/P&gt;

&lt;PRE class="programlisting" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; padding: 0px; margin-top: 0px; margin-bottom: 10px; margin-left: 2em; line-height: 17.0400009155273px; color: rgb(64, 64, 64); word-break: break-all; word-wrap: break-word; border: none; border-radius: 0px; transition: none !important; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;matlab -nosplash&lt;/PRE&gt;

&lt;P&gt;in my call system () syntax.&lt;/P&gt;

&lt;P&gt;I think what you said in bullet 2 is really helpful. All these problems arise from the limited interactive API among these applications. But it seems we at least find two shortcuts to achieve the goals as long as the interactions are limited.&lt;/P&gt;

&lt;P&gt;Thank you so much!&lt;/P&gt;

&lt;P&gt;Calvin&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;rudi-gaelzer wrote:&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;P&gt;1. is there a better way to let Fortran handle over to Matlab to process, wait and get back the results besides Call System() ?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;I guess that you're using an extension to the standard.&amp;nbsp; Fortran 2008 has the new intrinsic subroutine:&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;execute_command_line(command [, wait] [, exitstat] [, cmdstat] [, cmdmsg])&lt;/PRE&gt;

&lt;P&gt;So, your Matlab script could be executed by&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;call execute_command_line(" matlab -nodesktop -nojvm -r 'Matlab_script; exit' ")&lt;/PRE&gt;

&lt;P&gt;and by default (wait = .true.) the program would wait for the Matlab script to finish the execution before resuming.&lt;/P&gt;

&lt;P&gt;However, apart from changing from an intel-specific extension to a standard instruction, you would be doing exactly the same thing...&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;P&gt;2. Why call system does not implement the matlab parfor function? &amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;I don't know why you Matlab code would take longer to execute when called from fortran than calling it directly.&amp;nbsp; Moreover, I think that the answer to your question would be the lack of interoperability between fortran and Matlab.&amp;nbsp; So far, the standard only provides interoperability between fortran and c/c++ via iso_c_binding.&lt;/P&gt;

&lt;P&gt;I had a similar problem.&amp;nbsp; I wanted not only to execute a python script from fortran, but also transfer the result from python to F.&amp;nbsp; The simplest solution was something similar to your approach: call execute_command_line to run the python script, write the result to a known file and then read the file in F.&amp;nbsp; After a lot of reading and tweaking I managed to access a python script&amp;nbsp; via c (F &amp;lt;-&amp;gt; C/C++ &amp;lt;-&amp;gt; Python).&amp;nbsp; The interface between C and python is provided by the Py/C API.&lt;/P&gt;

&lt;P&gt;So, if you know that a Matlab script can be run from C/C++, then you could do the same...&lt;/P&gt;

&lt;P&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2015 03:17:57 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Within-Fortran-Call-Matlab/m-p/996276#M102437</guid>
      <dc:creator>Jia__Dun</dc:creator>
      <dc:date>2015-08-27T03:17:57Z</dc:date>
    </item>
    <item>
      <title>Thank you very much Ian, I</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Within-Fortran-Call-Matlab/m-p/996277#M102438</link>
      <description>&lt;P&gt;Thank you very much Ian, I noticed that this API engine is available but some the learning curve can be steep. Plus, I read about compiling using Matlab mex function which I hate the most somehow. I will definitely look into these some time later.&amp;nbsp;&lt;/P&gt;

&lt;P&gt;Best&lt;/P&gt;

&lt;P&gt;Calvin&lt;/P&gt;

&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;ianh wrote:&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;

&lt;P&gt;Matlab has an "engine" API that you can use to invoke it from within another application.&lt;/P&gt;

&lt;P&gt;See &lt;A href="http://mathworks.com/help/matlab/calling-matlab-engine-from-c-c-and-fortran-programs.html"&gt;http://mathworks.com/help/matlab/calling-matlab-engine-from-c-c-and-fort...&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;I have no idea whether this will alter in anyway what you are seeing with parfor though.&lt;/P&gt;

&lt;P&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2015 03:20:51 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Within-Fortran-Call-Matlab/m-p/996277#M102438</guid>
      <dc:creator>Jia__Dun</dc:creator>
      <dc:date>2015-08-27T03:20:51Z</dc:date>
    </item>
    <item>
      <title>It has been a few years now,</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Within-Fortran-Call-Matlab/m-p/996278#M102439</link>
      <description>&lt;P&gt;It has been a few years now, but when I was interfacing with Matlab I had (and still have) a very strong distaste for their approaches they provided for interfacing Fortran and Matlab.&amp;nbsp; I wrote my own wrappers, using the C interop facilities of Fortran 2003, against the specification of their API in their documentation and provided C header files.&amp;nbsp; After the initial investment I found this approach far superior.&lt;/P&gt;

&lt;P&gt;As far as I can tell, my wrappers didn't cover their engine API (which surprises me, because I recall using it, but perhaps I wasn't calling it from Fortran).&amp;nbsp; But that API is not particularly large - writing F2003 wrappers for it shouldn't take very long.&amp;nbsp; If you do go down the path of using the API, I suggest you consider this wrapper approach.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2015 03:38:30 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Within-Fortran-Call-Matlab/m-p/996278#M102439</guid>
      <dc:creator>IanH</dc:creator>
      <dc:date>2015-08-27T03:38:30Z</dc:date>
    </item>
  </channel>
</rss>

