Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #3931 > unrolled thread

call Fortran subroutine from Mathematica // use NETLink and DLL

Started by"Scot T. Martin" <smartin@seas.harvard.edu>
First post2011-07-23 06:30 +0000
Last post2011-07-23 06:30 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  call Fortran subroutine from Mathematica // use NETLink and DLL "Scot T. Martin" <smartin@seas.harvard.edu> - 2011-07-23 06:30 +0000

#3931 — call Fortran subroutine from Mathematica // use NETLink and DLL

From"Scot T. Martin" <smartin@seas.harvard.edu>
Date2011-07-23 06:30 +0000
Subjectcall Fortran subroutine from Mathematica // use NETLink and DLL
Message-ID<j0dppu$1lq$1@smc.vnet.net>
This post explains how to call a Fortran subroutine as a DLL from Mathematica. I don't think the hints in this post exist in explicit form in the Wolfram documentation or in the MathArchive, at least so far as I could find.  

There is information in a post from Wolfram for how to use Mathlink via C wrappers to get to Fortran subroutines (version 3 of Mathematica? http://library.wolfram.com/infocenter/TechNotes/174/). *BUT* I think since the introduction of NETLink into Mathematica (version 6?), the matter is now much more straightforward, at least in a Windows environment. 

The purpose of this post is therefore to a future user who might want to follow what I have done.

>From Windows7, 64-bit, and using the gnu compiler (http://gcc.gnu.org/wiki/GFortranUsage), here is the trace of the CMD commands:

C:\Demo>dir
      22-Jul-11 21:16 336 SumAndDifference.f90

C:\Demo>more SumAndDifference.f90

subroutine sum_and_difference(i,j,resultArray)
      implicit none
      integer*4, intent (in) :: i 
      integer*4, intent (in) :: j 
      integer*4, dimension(2), intent (out) :: resultArray 
      resultArray(1) = i + j 
      resultArray(2) = i - j 
      return
end subroutine sum_and_difference

C:\Demo>gfortran -c SumAndDifference.f90

C:\Demo>gfortran -s -shared -mrtd -o SumAndDifference.dll SumAndDifference.
o

C:\Demo>dir
      22-Jul-11 21:21 12,800 SumAndDifference.dll
      22-Jul-11 21:16 336 SumAndDifference.f90
      22-Jul-11 21:20 429 SumAndDifference.o


Now follows the Mathematica part:

In[1]:= Needs["NETLink`"]

In[2]:= SetDirectory@NotebookDirectory[]
Out[2]= "C:\\MyFortran"

In[3]:= $pathToDLL = FileNameJoin[{Directory[], "SumAndDifference.dll"}]
Out[3]= "C:\\MyFortran\\SumAndDifference.dll"

[To understand below, read:  http://www.wolfram.com/learningcenter/tutorialcollection/NETLinkUserGuide/ ]
[Note addition of "_" to "sum_and_difference" that is done by compiler.]
In[4]:= SumAndDifference =  DefineDLLFunction["sum_and_difference_", $pathToDLL,  "void", {"Int32*", "Int32*", "Int32[]"}]

In[5]:= results = MakeNETObject[{0, 0}, "System.Int32[]"]

In[6]:= SumAndDifference[10, 20, results]

In[7]:= NETObjectToExpression@results
Out[7]= {30, -10}


[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web