OpenMP within a C DLL

General OpenMP discussion

OpenMP within a C DLL

Postby BAGZZlash » Mon Sep 30, 2013 1:12 pm

Hi there,

I've recently started to play around with OpenMP and like it very much.

I am a just-for-fun Classic-VB programmer and like coding functions for my VB programs in C. As such, I use Windows 7 x64 and GCC 4.7.2.
I usually set up all my C functions in one large C file and then compile a DLL out of it. Now I would like to use OpenMP in my DLL.

First of all, I set up a simple example and compiled an exe file from it:

Code: Select all
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   int n = 520000;
   int i;
   int a[n];
   int NumThreads;
   
   omp_set_num_threads(4);

   #pragma omp parallel for
   for (i = 0; i < n; i++)
   {
      a[i] = 2 * i;
      NumThreads = omp_get_num_threads();
   }
   
   printf("Value = %d.\n", a[77]);
   printf("Number of threads = %d.", NumThreads);
   return(0);
}


I compile that using
Code: Select all
gcc -fopenmp !MyC.c -o !MyC.exe
and it works like a charm.

However, when I try to use OpenMP in my DLL, it fails. For example, I set up this function:

Code: Select all
__declspec(dllexport) int __stdcall TestAdd3i(struct SAFEARRAY **InArr1, struct SAFEARRAY **InArr2, struct SAFEARRAY **OutArr) //OpenMP Test
{
   int LengthArr;
   int i;
   int *InArrElements1;
   int *InArrElements2;
   int *OutArrElements;
   
   LengthArr = (*InArr1)->rgsabound[0].cElements;
   
   InArrElements1 = (int*) (**InArr1).pvData;
   InArrElements2 = (int*) (**InArr2).pvData;
   OutArrElements = (int*) (**OutArr).pvData;
   
   omp_set_num_threads(4);
   
   #pragma omp parallel for private(i)
   for (i = 0; i < LengthArr; i++)
   {
      OutArrElements[i] = InArrElements1[i] + InArrElements2[i];
   }
   
   return(omp_get_num_threads());
}


The structs are defined, of course. I compile that using

Code: Select all
gcc -fopenmp -c -DBUILD_DLL dll.c -o dll.o
gcc -fopenmp -shared -o mydll.dll dll.o -lgomp -Wl,--add-stdcall-alias


The compiler and linker do not complain (not even warnings come up) and the dll file is actually being built. But as I try to call the function from within VB, the VB compiler claims the the DLL file could not be found (run-time error 53). The strange thing about that is that as soon as one single OpenMP "command" is present inside the .c file, the VB compiler claims a missing DLL even if I call a function that does not even contain a single line of OpenMP code. When I comment all OpenMP stuff out, the function works as expected, but doesn't use OpenMP for parallelization, of course.

What is wrong here?
BAGZZlash
 
Posts: 2
Joined: Mon Sep 30, 2013 11:44 am

Re: OpenMP within a C DLL

Postby BAGZZlash » Tue Oct 01, 2013 7:59 am

Found it, the problem was the not provided path to the respective dependencies "libgomp-1.dll" and "pthreadGC2.dll".
Thanks for reading anyway! :)
BAGZZlash
 
Posts: 2
Joined: Mon Sep 30, 2013 11:44 am


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 3 guests

cron