Interfacing NAP to a DLL based on C or Fortran Code

Table of Contents

  1. Introduction
  2. make_dll options newCommand argDec argDec argDec
  3. make_dll_i options newCommand argDec argDec argDec

Introduction

The file make_dll.tcl defines procedures for automatically producing an interface from NAP to a DLL (dynamic-link library or shared library) based on C or Fortran Code. This process defines a new tcl command which can either be used directly or via another interface (written in Tcl) defining a NAP function.

make_dll options newCommand argDec argDec argDec

This is the standard procedure used to create a DLL.

newCommand is name of new command.

Each argument-declaration argDec is a list with the form {name dataType intent} where

options are:
-quiet: Do not echo commands.
-compile command: C compile-command with options
-dll fileName: output filename for DLL (default: newCommand.dll for windows, newCommand.so for unix)
-entry : User-routine entry-point (default: newCommand). Note that fortran entry points often include suffix '_'.
-header fileName: header (*.h) filename (default: none)
-libs fileNames: filenames of extra binary libraries (default: none)
-link command: Link-command with options
-object fileName: User-routine object-file (default: newCommand.obj for windows, newCommand.o for unix)
-source fileName: Output file containing C source code of interface (default: newCommand_i.c)
-version : Version number (default: 1.0)

The following example (under Linux) defines a new NAP function partialProd which calculates partial-products. This is analogous to the standard nap function psum which calculates partial sums. The new function is based on the following C file pprod.c:

void pprod(int *n, float *x, float *result) {
   int         i;
   float       prod = 1;

   for (i = 0; i < *n; i++) {
       result[i] = prod = prod * x[i];
   }
}
% exec cc -c -o pprod.o pprod.c
% make_dll pprod {n i32 in} {x f32} {y f32 inout}
cc -I/home/dav480/tcl/include -c pprod_i.c
ld -shared -o libpprod.so pprod_i.o pprod.o  
% load ./libpprod.so
% proc partialProd x {
    set result [nap "reshape(0f, shape(x))"]
    pprod  "nels(x)" x result
    return $result
}
% [nap "partialProd({2 1.5 3 0.5})"]
2 3 9 4.5

You can do the same thing in fortran 90 using the following source code:

subroutine pprod(n, x, result)
   integer, intent(in) :: n
   real, intent(in) :: x(n)
   real, intent(out) :: result(n)
   integer :: i
   real :: prod
   prod = 1.0
   do i = 1, n
       prod = prod * x(i)
       result(i) = prod
   end do
end subroutine pprod

The following log was produced using the Linux Lahey f95 compiler. (Note that the entry point is pprod_)

% exec lf95 -c pprod.f90
Compiling file pprod.f90.
Compiling program unit pprod at line 1:
/home/dav480/tmp/asm03529aaa.s: Assembler messages:
/home/dav480/tmp/asm03529aaa.s:86: Warning: translating to `fstp %st'
/home/dav480/tmp/asm03529aaa.s:90: Warning: translating to `fstp %st'
% make_dll -entry pprod_ pprod {n i32 in} {x f32} {y f32 inout}
cc -I/home/dav480/tcl/include -c pprod_i.c
ld -shared -o libpprod.so pprod_i.o pprod.o  
% load ./libpprod.so
% proc partialProd x {
    set result [nap "reshape(0f, shape(x))"]
    pprod  "nels(x)" x result
    return $result
}
% [nap "partialProd({2 1.5 3 0.5})"]
2 3 9 4.5

make_dll_i options newCommand argDec argDec argDec

Make NAP C interface to user's C function or Fortran subroutine. This procedure is normally used via make_dll, but may be used directly if you prefer to do your own compiling and linking. The result of make_dll_i is the C code.

The arguments are similar to make_dll, except that the only options are:
-entry : User-routine entry-point (default: newCommand). Note that fortran entry points often include suffix '_'.
-header fileName: header (*.h) filename (default: none)
-version : Version number (default: 1.0)

Author: Harvey Davies       © 2002, CSIRO Australia.       Legal Notice and Disclaimer
CVS Version Details: $Id: make_dll.html,v 1.2 2003/03/17 05:12:27 dav480 Exp $