Calling the NAG C Library Mark 24 from C#

The information here is applicable to the DLLs supplied with CLW3224DAL.

Please note that we recommend using the NAG Library for .NET from within C#. If this does not contain the NAG routines that you require, the NAG Fortran Library (DLLs) or the NAG C Library (as described below) may be used.

The NAG C Library uses the following data types as parameters. These are:

  1. scalars of type double, int and complex. These are passed either by value or by reference (as pointers to the particular type).
  2. enum types.
  3. arrays of type double, int and complex.
  4. a large number of structures, generally passed as pointers.
  5. in a few instances arrays (type double**) that are allocated within NAG routines and have to be freed by users. Also, in one instance, an array (type char***) of strings which is allocated internally.
  6. function parameters, also known as call-backs, that are pointers to functions with particular signatures.

As a first step towards making the C Library functions available to the .NET environment, an assembly of imports is needed. This contains:

  1. signatures of all the functions we want to call from the NAG C Library.
  2. structures with their attributes specified both in terms of their layout and their properties when passed as parameters (in, out or both). This is necessary as structures are a value type in .NET while in C they can be reference types also. Also pointers are more or less a taboo in C#. We avoid the explicit use of pointers by using instead the C# IntPtr type which is in fact equivalent to void * in C. The IntPtr fields can be handled later using marshalling methods.
  3. signatures of all call-back types.
  4. enum types derived from the C header file, nag_types.h.

The file clcsdnet.cs contains signatures of NAG C Library routines and call-back functions. It also contains layouts of structures used in the library, e.g. the NagError structure which is used by almost all the routines in the library. One of the most useful areas of the library is optimization. We have provided the layout for the Nag_E04_Opt structure along with the signatures for nag_opt_nlp (e04ucc) and the call-backs, e04ucc_objfun_DELEGATE and e04ucc_confun_DELEGATE, which are required by this routine, for example. enum types have also been provided.

We have also provided basic C# classes for illustrative purposes. These can be found in the files listed below. For example a sample C# class calling e04ucc can be found in e04ucce.cs. Each of the illustrative classes has a Main method exercising the NAG C example as shown in the C Library manual.

These examples may be compiled from the command line (e.g. from a Visual Studio Command Prompt) in the following manner:

csc /unsafe d01sjce.cs clcsdnet.cs

Note that because calling NAG C Library functions involves the use of pointers (see above), the /unsafe compiler option must be specified. On 64-bit machines, you will also need to specify the /platform:x86 compiler option.

The function declarations in clcsdnet.cs are based on the stand-alone version of the NAG C Library DLL (CLW3224DA_nag.dll); to specify the version of the DLL which uses the MKL BLAS/LAPACK instead (CLW3224DA_mkl.dll), replace CLW3224DA_nag.dll by CLW3224DA_mkl.dll in the DllImport attribute.

Remember also that to be able to run the program after you have compiled it, the NAG C Library DLL will need to appear somewhere in your current path. For example, if the DLLs are in C:\Program Files\NAG\CL24\clw3224dal\bin, then your PATH environment variable must contain this folder. You will also need the folder containing the Intel run-time libraries on your path (unless these are already present). If you are using the MKL-based version of the NAG C Library (CLW3224DA_mkl.dll), then the folder containing the MKL DLLs should also be on your path, but should appear later in the path than the bin folder for the NAG C Library DLLs, e.g.

C:\Program Files\NAG\CL24\clw3224dal\bin;
C:\Program Files\NAG\CL24\clw3224dal\rtl\bin;
C:\Program Files\NAG\CL24\clw3224dal\mkl_ia32_11.1\bin;<rest of path>

Tested with Visual Studio 2008 (Visual C# 2008 Compiler version 3.5 for .NET Framework version 3.5), Visual Studio 2010 and 2012 (both Visual C# Compiler version 4.0 for .NET Framework 4.5) and Visual Studio 2013 (Visual C# Compiler version 12.0 for C# 5).

Website Feedback

If you would like a response from NAG please provide your e-mail address below.

(If you're a human, don't change the following field)
Your first name.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.