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 string: 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 n.m: Version number (default: 1.0)

C Example

The following example (under SunOS 5.8) 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/sol/home/dav480/tcl/include -c pprod_i.c
ld -G -o libpprod.so pprod_i.o pprod.o  
% load ./libpprod.so                             
% proc partialProd x {
    nap "result = reshape(f32(_), shape(x))"
    pprod  "nels(x)" x result
    nap "result"
}
% [nap "partialProd({2 1.5 3 0.5})"]
2 3 9 4.5

Fortran 90 Example

The following fortran 90 example does the same thing as the above C example. The f90 source code in the file pprod.f90 is:
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 SunOS 5.8 f95 compiler. (Note that the entry point is "pprod_".)

% exec f95 -c pprod.f90
% make_dll -entry pprod_ pprod {n i32 in} {x f32} {y f32 inout}
cc -I/sol/home/dav480/tcl/include -c pprod_i.c
ld -G -o libpprod.so pprod_i.o pprod.o  
% load ./libpprod.so
% proc partialProd x {
    nap "result = reshape(f32(_), shape(x))"
    pprod  "nels(x)" x result
    nap "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 string: User-routine entry-point (default: newCommand). Note that fortran entry points often include suffix '_'.
-header fileName: header ( *.h) filename (default: none)
-version n.m: Version number (default: 1.0)

Author: Harvey Davies       © 2002, CSIRO Australia.       Legal Notice and Disclaimer
CVS Version Details: $Id: make_dll.html,v 1.5 2005/12/04 03:12:23 dav480 Exp $