Jump to content

Run a function of a dll in multiple threads


HeLo

Recommended Posts

As the faddeeva function is not available in LabVIEW, I have compiled the code of Steven G. Johnson  into a DLL using the GCC compiler in Code::Blocks - and it works (the same code is used also in SciPy, Matlab and others).

In order to improve in speed, I would like to run the function in a for-loop "enabling loop parallelism" or have it called by multiple clones of a reentrant "Faddeeva.vi".

However calling the function that way produces erroneous results - it looks like there is some interference between the different calls.

As native LabVIEW functions like "Integral x(t).vi" can do exactly this and as these functions also call a dll, I must have some statements in my dll that are not thread save.

Can anybody point me to the mistake, that prevents a correct multi threaded call?

Herbert

 

P.S.: Please find below a part of the c-file attached

#include <math.h>
#include <windows.h>
/* The Faddeeva.cc file contains macros to let it compile as C code
   (assuming C99 complex-number support), so just #include it. */
#include "Faddeeva.cc"
BOOL WINAPI DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                       DWORD reason        /* Reason this function is being called. */ ,
                       LPVOID reserved     /* Not used. */ )
{
    switch (reason)
    {
      case DLL_PROCESS_ATTACH:
        break;

      case DLL_PROCESS_DETACH:
        break;

      case DLL_THREAD_ATTACH:
        break;

      case DLL_THREAD_DETACH:
        break;
    }

    /* Returns TRUE on success, FALSE on failure */
    return TRUE;
}

/* LabVIEW created typedefs */
#pragma pack (1)
typedef struct {
    long dimSize;
    double elt[1];
    } TD1;
typedef TD1 **dArr;
double *Re_zArr, *Im_zArr, *RelErr_Arr, *Re_wArr, *Im_wArr;
double complex z, w;
int Npts;
int i;

_declspec(dllexport) void FaddeevaW(dArr x, dArr y, dArr RelErr, dArr Re_w, dArr Im_w);
_declspec(dllexport) void FaddeevaW(dArr x, dArr y, dArr RelErr, dArr Re_w, dArr Im_w)
{
  Npts = (*x)->dimSize;
  Re_zArr = & (*x)->elt[0];
  Im_zArr = & (*y)->elt[0];
  RelErr_Arr = & (*RelErr)->elt[0];
  Re_wArr = & (*Re_w)->elt[0];
  Im_wArr = & (*Im_w)->elt[0];
    for (i=0; i<Npts; i++)
        {
            w = Faddeeva_w((*Re_zArr + *Im_zArr*I), *RelErr_Arr);
            *Re_wArr = creal(w);
            *Im_wArr = cimag(w);
            Re_zArr++;
            Im_zArr++;
            RelErr_Arr++;
            Re_wArr++;
            Im_wArr++;
        }
}

 

 

 

Faddeeva.c Faddeeva.cc Faddeeva.h

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.