<?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 Fortran-C++ interoperation of 2D array in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-C-interoperation-of-2D-array/m-p/1538769#M168925</link>
    <description>&lt;P&gt;I have a C++ pgm that uses Fortran subroutines and functions, but I am having trouble converting one function call to multi-threaded, due to confusion as to how to pass an array reference.&lt;/P&gt;&lt;P&gt;This is the original code, which works.&lt;/P&gt;&lt;P&gt;! Fortran function, called from C++&lt;BR /&gt;FUNCTION Compute(nPoints)&lt;BR /&gt;INTEGER, INTENT(OUT), DIMENSION(n, m) :: nPoints&lt;BR /&gt;-------------------------------------------------------&lt;BR /&gt;// C++ prototype&lt;BR /&gt;extern "C" int __cdecl COMPUTE(int nP[m][n]);&lt;/P&gt;&lt;P&gt;// Declaration of C++ array&lt;BR /&gt;int nPoints[m][n];&lt;/P&gt;&lt;P&gt;// Call of Fortran function&lt;BR /&gt;int error = COMPUTE(nPoints);&lt;/P&gt;&lt;P&gt;The above compiles, and works as I expect.&lt;BR /&gt;The C++ array nPoints receives the output from Fortran function Compute.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;====================== End of original code =================================&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I'm trying to modify the above so Compute runs in a separate thread. So I need to pass the array from the C++ function that declares the nPoints array to another thread.&lt;/P&gt;&lt;P&gt;I create a new structure to hold something that describes the C++ array.&lt;/P&gt;&lt;P&gt;struct ParamsForThread&lt;BR /&gt;{&lt;BR /&gt;int *nPoints;&lt;BR /&gt;};&lt;BR /&gt;ParamsForThread p;&lt;BR /&gt;p.nPoints = &amp;amp;nPoints[]0[0];&lt;/P&gt;&lt;P&gt;// Call of Fortran function from thread; p is the ParamsForThread structure.&lt;BR /&gt;int error = COMPUTE(p-&amp;gt;nPoints);&lt;/P&gt;&lt;P&gt;The C++ compiler gives an error:&lt;BR /&gt;error C2664: 'int COMPUTE(int [][n])': cannot convert argument 1 from 'int *' to 'int [][n]'&lt;BR /&gt;Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or parenthesized function-style cast&lt;/P&gt;&lt;P&gt;How should I define the fields in the ParamsForThread structure so I can use it in the Fortran function call?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 30 Oct 2023 21:18:11 GMT</pubDate>
    <dc:creator>AONym</dc:creator>
    <dc:date>2023-10-30T21:18:11Z</dc:date>
    <item>
      <title>Fortran-C++ interoperation of 2D array</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-C-interoperation-of-2D-array/m-p/1538769#M168925</link>
      <description>&lt;P&gt;I have a C++ pgm that uses Fortran subroutines and functions, but I am having trouble converting one function call to multi-threaded, due to confusion as to how to pass an array reference.&lt;/P&gt;&lt;P&gt;This is the original code, which works.&lt;/P&gt;&lt;P&gt;! Fortran function, called from C++&lt;BR /&gt;FUNCTION Compute(nPoints)&lt;BR /&gt;INTEGER, INTENT(OUT), DIMENSION(n, m) :: nPoints&lt;BR /&gt;-------------------------------------------------------&lt;BR /&gt;// C++ prototype&lt;BR /&gt;extern "C" int __cdecl COMPUTE(int nP[m][n]);&lt;/P&gt;&lt;P&gt;// Declaration of C++ array&lt;BR /&gt;int nPoints[m][n];&lt;/P&gt;&lt;P&gt;// Call of Fortran function&lt;BR /&gt;int error = COMPUTE(nPoints);&lt;/P&gt;&lt;P&gt;The above compiles, and works as I expect.&lt;BR /&gt;The C++ array nPoints receives the output from Fortran function Compute.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;====================== End of original code =================================&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I'm trying to modify the above so Compute runs in a separate thread. So I need to pass the array from the C++ function that declares the nPoints array to another thread.&lt;/P&gt;&lt;P&gt;I create a new structure to hold something that describes the C++ array.&lt;/P&gt;&lt;P&gt;struct ParamsForThread&lt;BR /&gt;{&lt;BR /&gt;int *nPoints;&lt;BR /&gt;};&lt;BR /&gt;ParamsForThread p;&lt;BR /&gt;p.nPoints = &amp;amp;nPoints[]0[0];&lt;/P&gt;&lt;P&gt;// Call of Fortran function from thread; p is the ParamsForThread structure.&lt;BR /&gt;int error = COMPUTE(p-&amp;gt;nPoints);&lt;/P&gt;&lt;P&gt;The C++ compiler gives an error:&lt;BR /&gt;error C2664: 'int COMPUTE(int [][n])': cannot convert argument 1 from 'int *' to 'int [][n]'&lt;BR /&gt;Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or parenthesized function-style cast&lt;/P&gt;&lt;P&gt;How should I define the fields in the ParamsForThread structure so I can use it in the Fortran function call?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 21:18:11 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-C-interoperation-of-2D-array/m-p/1538769#M168925</guid>
      <dc:creator>AONym</dc:creator>
      <dc:date>2023-10-30T21:18:11Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran-C++ interoperation of 2D array</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-C-interoperation-of-2D-array/m-p/1538829#M168927</link>
      <description>&lt;P&gt;Try&lt;/P&gt;&lt;LI-CODE lang="cpp"&gt;// C++ prototype
extern "C" int __cdecl COMPUTE(void*);

int ret = COMPUTE(&amp;amp;nPoints);
int ret = COMPUTE(&amp;amp;nPoints[0][0]);
&lt;/LI-CODE&gt;&lt;P&gt;Presumably m and n are fixed parameters and are in agreement between the C++ side and Fortran side.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Most 2D arrays on the C++ side use type**, and the data may be discongiguous. You can have a contiguous block of data with literal dimensions and a contiguous block of data (row major for Fortran other way for C/C++).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jim Dempsey&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 23:45:26 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-C-interoperation-of-2D-array/m-p/1538829#M168927</guid>
      <dc:creator>jimdempseyatthecove</dc:creator>
      <dc:date>2023-10-30T23:45:26Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran-C++ interoperation of 2D array</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-C-interoperation-of-2D-array/m-p/1538831#M168928</link>
      <description>&lt;P&gt;Use the debugger, place a signature value in the 1st element [0][0] (e.g. 123456789), and say -123456789 in [1][0] and step into the called function. See what you get for (1,1) and (1,2). You can experiment with small numbers for m and n as it will be easire to examing the entire blob being passed. When you get it right, up the values for m and n&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jim Dempsey&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 23:50:21 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-C-interoperation-of-2D-array/m-p/1538831#M168928</guid>
      <dc:creator>jimdempseyatthecove</dc:creator>
      <dc:date>2023-10-30T23:50:21Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran-C++ interoperation of 2D array</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-C-interoperation-of-2D-array/m-p/1538896#M168930</link>
      <description>&lt;P&gt;Fortran needs to know the array extents. That's why the Fortran function contains the (fixed) values for m and n.&lt;/P&gt;&lt;P&gt;In the original code, I just prototyped the Fortran function as int nPoints[m][n], and, as I said, this works fine.&lt;/P&gt;&lt;P&gt;The problem is, how to transfer the extents to the structure that transmits info from my C++ pgm to the thread (&lt;SPAN class=""&gt;ParamsForThread&lt;/SPAN&gt; in my example). In other words, I want to match the prototype's arg int nPoints[m][n].&lt;/P&gt;&lt;P&gt;As far as using the type int **, how do I get from the C++ declaration int nPoints[m][n] to a variable of type int **? And, even if this could be done, the C++ code would be obscured, since the prototype of the Fortran function would now have to be COMPUTE(int **something).&lt;/P&gt;&lt;P&gt;Since the C++ array is declared with constant dimensions, it will be contiguous.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 04:42:14 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-C-interoperation-of-2D-array/m-p/1538896#M168930</guid>
      <dc:creator>AONym</dc:creator>
      <dc:date>2023-10-31T04:42:14Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran-C++ interoperation of 2D array</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-C-interoperation-of-2D-array/m-p/1539069#M168933</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimdempseyatthecove_0-1698765930562.png" style="width: 400px;"&gt;&lt;img src="https://community.intel.com/t5/image/serverpage/image-id/47437i0B823978ACCB7DF5/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400&amp;amp;whitelist-exif-data=Orientation%2CResolution%2COriginalDefaultFinalSize%2CCopyright" role="button" title="jimdempseyatthecove_0-1698765930562.png" alt="jimdempseyatthecove_0-1698765930562.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;LI-CODE lang="cpp"&gt;// ConsoleApplication11.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include &amp;lt;iostream&amp;gt;

extern "C" int COMPUTE(int* array, int m, int n);

int main()
{
    std::cout &amp;lt;&amp;lt; "Hello World!\n";
    int m = 10;
    int n = 20;
    int* blob = (int*)malloc(m*n*sizeof(int));
    int ret = COMPUTE(blob, m, n);
}&lt;/LI-CODE&gt;&lt;LI-CODE lang="fortran"&gt;function Compute(array, m, n) bind(C, name="COMPUTE") result(ret)
    implicit none
    integer, intent(inout) :: array(n,m)
    integer, value, intent(in) :: m,n
    integer :: ret
    
    integer :: i,j
    
    do i=1,m
        do j=1,n
            array(j,i) = j*i
        end do
    end do
    ret = sum(array)
end function Compute&lt;/LI-CODE&gt;&lt;P&gt;I will let you work out the details of accessing the array on the C++ side (e.g. either passing the address of array[0][0] or the blob)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is also a chapter on how to use Fortran descriptors in the user manual.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jim Dempsey&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 15:29:11 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-C-interoperation-of-2D-array/m-p/1539069#M168933</guid>
      <dc:creator>jimdempseyatthecove</dc:creator>
      <dc:date>2023-10-31T15:29:11Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran-C++ interoperation of 2D array</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-C-interoperation-of-2D-array/m-p/1539116#M168940</link>
      <description>&lt;P&gt;Jim -&lt;/P&gt;&lt;P&gt;Thanks for your detailed answer; I understand how this will work. I was hoping for something simpler (like my original code), not having to pass the extents explicitly.&lt;/P&gt;&lt;P&gt;I have realized that my difficulty is with the C++ side, namely, a way to store the extents and array address in a single entity, to match the original Fortran prototype.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 16:41:26 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-C-interoperation-of-2D-array/m-p/1539116#M168940</guid>
      <dc:creator>AONym</dc:creator>
      <dc:date>2023-10-31T16:41:26Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran-C++ interoperation of 2D array</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-C-interoperation-of-2D-array/m-p/1539148#M168943</link>
      <description>&lt;P&gt;If, on the C++ side, you create (or find) an array type/class, it will contain a pointer (or a member function to get a pointer) to the memory block and the index extents for each dimension).&lt;/P&gt;&lt;P&gt;You can then write a shell function that accepts this type, extracts the base, and index extents, then pass it on to the Fortran routine.&lt;/P&gt;&lt;P&gt;An alternative is to pass the address of the C++ type to the Fortran routine as an interoperable type (generally a Fortran type with BIND(C)) then use a call to C_TO_F_POINTER to build an array descriptor for use on the Fortran side.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jim Dempsey&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 18:11:10 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-C-interoperation-of-2D-array/m-p/1539148#M168943</guid>
      <dc:creator>jimdempseyatthecove</dc:creator>
      <dc:date>2023-10-31T18:11:10Z</dc:date>
    </item>
  </channel>
</rss>

