200 likes | 214 Vues
Learn the basics of calling the NAG DLLs from LabVIEW, including similarities with C/C++ calling conventions and using LabVIEW's "Call Library Function" feature. Explore complex numbers, 2D arrays, functions and subroutines, and character strings. Discover advanced functions, utilizing LabVIEW's "Transpose 2D Array" and handling Fortran subroutines and functions. Explore curve fitting and interpolation using Chebyshev polynomials, and tackle challenges with external calls. Assistance available for using NAG DLLs from LabVIEW.
E N D
NAG Workshop and Surgery 6 December 2001 Using the NAG Library fromLabVIEWJohn KeightleyNPL, Centre for Ionising Radiation Metrology
Outline of Talk • Basics of calling the NAG DLLs from LabVIEW • Similarities with C/C++ calling conventions • Introduction to LabVIEW “Call Library Function” • Problems I’ve encountered so far !! • Complex Numbers • 2D Arrays must be TRANSPOSED ! • Functions and Subroutines • Character Strings returned via function name • Arrays MUST be large enough to hold output • Example
BASICS • Very similar approach to C/C++ • C/C++ headers are VERY useful in determining calling notation for LabVIEW • NB : Stdcall(WINAPI) calling convention • Ie: C++ header from NAG help extern void __stdcall E02ACF( CONST double x[], CONST double y[], CONST int *n, double aa[], CONST int *m1, double *ref );
“Advanced” Functions Palette from VI diagram LAbVIEW “Call Library Function”
Complex NumbersExample A02ABF :Complex Modulus • FORTRAN : • real FUNCTION A02ABF(XR,XI) • real XR,XI • Refer to C/C++ headers (type definitions) • typedef struct { double re,im; } Complex; ??? • “Call Library Function” does not allow pointer to a “cluster” !! • Refer to C/C++ header for A02ABF extern double __stdcall A02ABF( CONST double *xxr, CONST double *xxi );
2D Arrays • In FORTRAN : • Column major order • In LabVIEW • Row major order (Same as C/C++) • Thus, MUST TRANSPOSE arrays • BEFORE inputting to NAG functions in the DLLs • AFTER retrieving output from the DLLs. • Can use LabVIEW “Transpose 2D Array” function from the Array palette ! • or use the NAG routine F01CRF (more complicated as you need to know the size of the array to start with)
FORTRAN Subroutines and Functions • SUBROUTINE returns VOID • FUNCTION returns a value !! • eg: SUBROUTINE X05AAF(ITIME) INTEGER ITIME(7) time data array passed through parameter list CHARACTER*30 FUNCTION X05ABF(ITIME) INTEGER ITIME(7) function returns a character array
Returning a character string via a function name CHARACTER*30 FUNCTION X05ABF(ITIME) INTEGER ITIME(7) • Fortran has no NULL TERMINATED strings: • Instead it uses TWO parameters for strings character string AND an int for string length • cannot return TWO values !! • Refer to C headers for solution : extern void __stdcall X05ABF (char [], int, CONST int itime[] ); 1st 2 parameters refer to string which was being returned via function name ? ! ??????
A Simple Example : E02ADF and E02AEFCurve Fitting and Interpolation by Chebyshev Polynomials • Program Reads Input Data : • Least Squares Fitting by method of Chebyshev Polynomials • E02ADF • Interpolation of values • E02AEF
MY BIG PROBLEM : EXTERNAL calls • Example : C05AJF : • locates zero of a user supplied continuous function • C/C++ header • extern void __stdcall C05AJF( • double *x, • CONST double *eps, • CONST double *eta, • double (__stdcall *f)(double *), • CONST int *nfmax, • int *ifail • );
EXTERNAL calls : C05AJF #include <math.h> #include <stdio.h> #include "nagmk19.h“ main(){ double eps, eta, x; int ifail, k, nfmax; double __stdcall f(double *); for (k=1; k<=2; k++){ eps = k==1 ? 0.1e-3 : 0.1e-4; x = 1.0; eta = 0.0; nfmax = 200; ifail = 1; C05AJF(&x,&eps,&eta,f,&nfmax,&ifail); if (ifail==0) printf("With eps = %e root = %f\n",eps,x); else{ printf("ifail = %d\n",ifail); if (ifail==3 | ifail==4) printf("With eps = %e final value = %f\n",eps,x); } } return 0; } double __stdcall f(double *x) { return exp(-*x) - *x; }
EXTENAL CALLS • I DON’T KNOW HOW TO DO THIS IN LabVIEW !! • ie: I need a pointer to the user supplied function !!! • LabVIEW call library function doesn’t help me ! • Wrapper DLL in C++ required ???
Conclusion • Use LabVIEW “Call Library Function” • LabVIEW calls are VERY SIMILAR to those in C/C++ • Use NAG C headers for help ! • Still to resolve issues with EXTERNAL calls to user supplied functions !! • Nick Williamson from National Instruments is looking into this • Wrapper DLL in C/C++ ?? • I can help anyone wanting to use the NAG DLLs from LabVIEW !