Difference between revisions of "Polyglot"

From SourceWiki
Jump to navigation Jump to search
Line 4: Line 4:
 
=Introduction=
 
=Introduction=
  
Languages have their pros and cons.
+
All languages have their pros and cons.  None is perfect.
 +
 
 +
Imagine that you've found a very useful third-party library written in C.  Alas, you're most comfortable writing Fortran.  It would take you a ''very'' long time to re-write the C code in Fortran and would be tedious to boot.  Worst of all, you'd have to understand the C to make the translation and lo, we've come full circle.  Far better then to leave the library as it is and to call the routines from your favoured language.  In this workshop, we'll look at ways in which we can mix languages and in the process create a useful end product which plays to the strengths of each component. 
 +
 
 +
To get the examples for the practicals, log into your preferred Linux machine and cut and paste the following into your terminal.
  
 
<pre>
 
<pre>
Line 10: Line 14:
 
</pre>
 
</pre>
  
The first example is calling a Fortran routine from C.
+
= Wrapping up some Fortran with C =
 +
 
 +
In the first example we'll call a Fortran subroutine from C program:
 +
 
 +
<pre>
 +
cd examples/example1
 +
</pre>
 +
 
 +
To compile the example, type:
 +
 
 +
<pre>
 +
make
 +
</pre>
 +
 
 +
and to run it, type:
 +
 
 +
<pre>
 +
call_fort.exe
 +
</pre>
 +
 
 +
Tada!  We've mixed our languages into a single executable and it works!  Cool.  Very cool.  OK, so much for the magic, let's take a look inside the files.  Open up '''sub.f90'''.
  
 
Things to get right:
 
Things to get right:
Line 18: Line 42:
 
* fortran indexes arrays from 1, by defualt; C from 0.
 
* fortran indexes arrays from 1, by defualt; C from 0.
 
* The rows/columns are reversed for 2-dimensional arrays in Fortran to C.  Arrays in C are 'row-major' and arrays in Fortran are 'column major'.
 
* The rows/columns are reversed for 2-dimensional arrays in Fortran to C.  Arrays in C are 'row-major' and arrays in Fortran are 'column major'.
 +
 +
= Turning the Parcel Inside-Out =

Revision as of 17:43, 26 May 2008

Mixing Languages

Introduction

All languages have their pros and cons. None is perfect.

Imagine that you've found a very useful third-party library written in C. Alas, you're most comfortable writing Fortran. It would take you a very long time to re-write the C code in Fortran and would be tedious to boot. Worst of all, you'd have to understand the C to make the translation and lo, we've come full circle. Far better then to leave the library as it is and to call the routines from your favoured language. In this workshop, we'll look at ways in which we can mix languages and in the process create a useful end product which plays to the strengths of each component.

To get the examples for the practicals, log into your preferred Linux machine and cut and paste the following into your terminal.

svn export http://source.ggy.bris.ac.uk/subversion-open/polyglot/trunk ./polyglot

Wrapping up some Fortran with C

In the first example we'll call a Fortran subroutine from C program:

cd examples/example1

To compile the example, type:

make

and to run it, type:

call_fort.exe

Tada! We've mixed our languages into a single executable and it works! Cool. Very cool. OK, so much for the magic, let's take a look inside the files. Open up sub.f90.

Things to get right:

  • use 'nm' or a similar program to investigate the name mangling used by your Fortran compiler, so that you can match the name in your C program.
  • matching the sizes of variables
  • pass by reference, pass by value
  • fortran indexes arrays from 1, by defualt; C from 0.
  • The rows/columns are reversed for 2-dimensional arrays in Fortran to C. Arrays in C are 'row-major' and arrays in Fortran are 'column major'.

Turning the Parcel Inside-Out