<?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 The Intel Fortran in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-allocate-a-Fortran-dynamic-array-in-a-dynamic-linked/m-p/1090597#M124137</link>
    <description>&lt;P&gt;The Intel Fortran documentation has topics on shared libraries.&amp;nbsp;https://software.intel.com/en-us/node/579667 is a current (16.0) documentation link - if that link doesn't work in the future, look under Compiler Reference &amp;gt; Libraries &amp;gt; Creating Shared Libraries.&lt;/P&gt;</description>
    <pubDate>Thu, 21 Jan 2016 15:35:16 GMT</pubDate>
    <dc:creator>Steven_L_Intel1</dc:creator>
    <dc:date>2016-01-21T15:35:16Z</dc:date>
    <item>
      <title>How to allocate a Fortran dynamic array in a dynamic-linked library written in C++?</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-allocate-a-Fortran-dynamic-array-in-a-dynamic-linked/m-p/1090592#M124132</link>
      <description>&lt;P&gt;&lt;BR /&gt;
	Hello,&lt;/P&gt;

&lt;P&gt;I'm trying to allocate a Fortran dynamic array in a member function of&lt;BR /&gt;
	a dynamic-linked library written in C.&lt;BR /&gt;
	The compiling and linking is success, but the exacution afterwards has&lt;BR /&gt;
	some problems.&lt;BR /&gt;
	It get stuck at the "new" sentance in the dynamic-linked library member function.&lt;/P&gt;

&lt;P&gt;The C++ codes (secso.cpp) for dynamic-linked library:&lt;/P&gt;

&lt;P&gt;#include &amp;lt;iostream&amp;gt;&lt;BR /&gt;
	extern "C"&lt;BR /&gt;
	{&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp; int f( double ** array)&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int j,k;&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; std::cout &amp;lt;&amp;lt; "The allocation 1"&amp;lt;&amp;lt;std::endl;&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; array[0] = new double [9*3];&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; std::cout &amp;lt;&amp;lt; "The allocation 2"&amp;lt;&amp;lt;std::endl;&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (j=0;j&amp;lt;9;j++)&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (k=0;k&amp;lt;3;k++)&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; array[0] [j*3+k] = double((j+1)*10+k+1);&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;
	}&lt;/P&gt;

&lt;P&gt;The Fortran main function code (test.f90):&lt;/P&gt;

&lt;P&gt;program test&lt;BR /&gt;
	implicit none&lt;/P&gt;

&lt;P&gt;real(kind=8),&amp;nbsp; pointer :: array(:,:)&lt;/P&gt;

&lt;P&gt;! External C style function&lt;BR /&gt;
	call f(array)&lt;/P&gt;

&lt;P&gt;write (*,*) array&lt;BR /&gt;
	deallocate (array)&lt;/P&gt;

&lt;P&gt;end program test&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;Make file (makefile):&lt;/P&gt;

&lt;P&gt;SHELL = /bin/bash&lt;BR /&gt;
	test:&lt;BR /&gt;
	&amp;nbsp;icpc -shared -fPIC -o libsec.so secso.cpp&lt;BR /&gt;
	&amp;nbsp;ifort -assume nounderscore -o test.exe test.f90 -L. -lsec&lt;/P&gt;

&lt;P&gt;What should I do?&lt;BR /&gt;
	Any advise would be helpful, thanks in advance.&lt;/P&gt;</description>
      <pubDate>Sun, 17 Jan 2016 03:24:02 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-allocate-a-Fortran-dynamic-array-in-a-dynamic-linked/m-p/1090592#M124132</guid>
      <dc:creator>Port__Phillip</dc:creator>
      <dc:date>2016-01-17T03:24:02Z</dc:date>
    </item>
    <item>
      <title>To some extent, what your</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-allocate-a-Fortran-dynamic-array-in-a-dynamic-linked/m-p/1090593#M124133</link>
      <description>&lt;P&gt;To some extent, what your code does is similar to two persons writing checks on a single account without either being aware of what the other person is doing. Having Fortran de-allocate memory that was allocated in C, or vice versa, is sure to cause trouble. The following modification of your code works on Windows 32-bit (but it is a quick-and-dirty fix-up, which you should sanitize and adapt for Linux symbol naming conventions, and consistently use only ISO-C-Interoperability methods or only "legacy" methods):&lt;/P&gt;

&lt;P&gt;File csub.cpp:&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;#include &amp;lt;iostream&amp;gt;
extern "C"
{
    int F( double ** array)
    {
        int j,k;

        std::cout &amp;lt;&amp;lt; "The allocation 1"&amp;lt;&amp;lt;std::endl;
        array[0] = new double [9*3];
        std::cout &amp;lt;&amp;lt; "The allocation 2"&amp;lt;&amp;lt;std::endl;
        for (j=0;j&amp;lt;9;j++)
        {
            for (k=0;k&amp;lt;3;k++)
            {
                array[0] [j*3+k] = double((j+1)*10+k+1);
            }
        }

        return 0;
    }
}

extern "C"{
void C_FREE(void **p){
   free(*p);
   }
}&lt;/PRE&gt;

&lt;P&gt;File fmain.f90:&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;program test
use, intrinsic :: iso_c_binding
implicit none

real(kind=8),  pointer :: array(:,:)
type(C_PTR) :: cptr_to_array

! External C style function
call f(cptr_to_array)
call C_F_POINTER (cptr_to_array, array, [3,9])

write (*,*) array
call c_free (cptr_to_array)

end program test
&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Jan 2016 13:51:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-allocate-a-Fortran-dynamic-array-in-a-dynamic-linked/m-p/1090593#M124133</guid>
      <dc:creator>mecej4</dc:creator>
      <dc:date>2016-01-17T13:51:00Z</dc:date>
    </item>
    <item>
      <title>I would recommend using the</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-allocate-a-Fortran-dynamic-array-in-a-dynamic-linked/m-p/1090594#M124134</link>
      <description>&lt;P&gt;I would recommend using the extended C interoperability features from draft Fortran 2015, supported in the 16.0 compiler. I'll work up a version of your code done this way.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2016 17:17:16 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-allocate-a-Fortran-dynamic-array-in-a-dynamic-linked/m-p/1090594#M124134</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2016-01-18T17:17:16Z</dc:date>
    </item>
    <item>
      <title>This isn't your program, but</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-allocate-a-Fortran-dynamic-array-in-a-dynamic-linked/m-p/1090595#M124135</link>
      <description>&lt;P&gt;This isn't your program, but it illustrates how to pass deferred-shape and allocatable arrays from Fortran to C and to have the C code do the allocation.&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;#include "ISO_Fortran_binding.h"
extern int c_alloc (CFI_cdesc_t * descr) {
	int ret, i;
	float * array;
	CFI_index_t lower = 0, upper = 10;
	ret = CFI_allocate (descr, &amp;amp;lower, &amp;amp;upper, 0); // No elem_len
	if (ret == CFI_SUCCESS) {
		array = descr-&amp;gt;base_addr;
		for (i=lower;i&amp;lt;=upper;i++) {
			array&lt;I&gt; = (float) i;
		}
	}
	return ret;
}&lt;/I&gt;&lt;/PRE&gt;

&lt;PRE class="brush:fortran;"&gt;use, intrinsic :: iso_c_binding
interface
  function c_alloc (array) bind(C)
  import
  integer(C_INT) :: c_alloc
  real(C_FLOAT), intent(out), allocatable, dimension(:) :: array
  end function c_alloc
end interface

real(C_FLOAT), allocatable, dimension(:) :: my_array
if (c_alloc(my_array) == 0) then
  print *, lbound(my_array), ubound(my_array); print *, my_array
  end if
end&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2016 13:46:27 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-allocate-a-Fortran-dynamic-array-in-a-dynamic-linked/m-p/1090595#M124135</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2016-01-19T13:46:27Z</dc:date>
    </item>
    <item>
      <title>Thank Mr. Lionel and Mr(s)</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-allocate-a-Fortran-dynamic-array-in-a-dynamic-linked/m-p/1090596#M124136</link>
      <description>&lt;P&gt;Thank Mr. Lionel and Mr(s) mecej4 for your time and kind reply.&lt;/P&gt;

&lt;P&gt;The codes really help me a lot.&lt;/P&gt;

&lt;P&gt;My fortran codes now could processing arrays from C style shared library.&lt;/P&gt;

&lt;P&gt;Is there any tutorials about shared libraries for beginners like me?&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2016 14:05:23 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-allocate-a-Fortran-dynamic-array-in-a-dynamic-linked/m-p/1090596#M124136</guid>
      <dc:creator>Port__Phillip</dc:creator>
      <dc:date>2016-01-21T14:05:23Z</dc:date>
    </item>
    <item>
      <title>The Intel Fortran</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-allocate-a-Fortran-dynamic-array-in-a-dynamic-linked/m-p/1090597#M124137</link>
      <description>&lt;P&gt;The Intel Fortran documentation has topics on shared libraries.&amp;nbsp;https://software.intel.com/en-us/node/579667 is a current (16.0) documentation link - if that link doesn't work in the future, look under Compiler Reference &amp;gt; Libraries &amp;gt; Creating Shared Libraries.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2016 15:35:16 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-allocate-a-Fortran-dynamic-array-in-a-dynamic-linked/m-p/1090597#M124137</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2016-01-21T15:35:16Z</dc:date>
    </item>
    <item>
      <title>thank you mr. Steve for reply</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-allocate-a-Fortran-dynamic-array-in-a-dynamic-linked/m-p/1090598#M124138</link>
      <description>&lt;P&gt;thank you mr.&amp;nbsp;Steve for reply&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Mar 2016 17:57:50 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-allocate-a-Fortran-dynamic-array-in-a-dynamic-linked/m-p/1090598#M124138</guid>
      <dc:creator>Adel_S_</dc:creator>
      <dc:date>2016-03-19T17:57:50Z</dc:date>
    </item>
  </channel>
</rss>

