Some MAKE_DLL questions [message #92137] |
Mon, 19 October 2015 06:30  |
natha
Messages: 482 Registered: October 2007
|
Senior Member |
|
|
Hi guys,
I've never created a sharable library from C code to be used by IDL with CALL_EXTERNAL.
I was wondering if it would be possible to create some parallel code using C + OpenMP and then generate de DLL. I don't know if this is possible since the documentation says:
"Not every possible option supported by the C compiler or system linker is addressed, only those commonly needed by IDL-related C code."
I have some more questions related to the DLLs:
- Are they platform independent? Can they be used in Mac, Win, Linux?
- How is the memory managed? Are the variables copied to a different block?
Thank you for all your help and clarifications,
Nata
|
|
|
Re: Some MAKE_DLL questions [message #92138 is a reply to message #92137] |
Mon, 19 October 2015 08:04   |
Russell[1]
Messages: 101 Registered: August 2011
|
Senior Member |
|
|
I never got make_dll to produce a shared library that used OpenMP. If you make it work, I'd love to know how you did it. I did all this on Mac OS.
-Russell
On Monday, October 19, 2015 at 9:31:26 AM UTC-4, nata wrote:
> Hi guys,
>
> I've never created a sharable library from C code to be used by IDL with CALL_EXTERNAL.
> I was wondering if it would be possible to create some parallel code using C + OpenMP and then generate de DLL. I don't know if this is possible since the documentation says:
> "Not every possible option supported by the C compiler or system linker is addressed, only those commonly needed by IDL-related C code."
>
> I have some more questions related to the DLLs:
> - Are they platform independent? Can they be used in Mac, Win, Linux?
> - How is the memory managed? Are the variables copied to a different block?
>
> Thank you for all your help and clarifications,
> Nata
|
|
|
|
|
|
|
Re: Some MAKE_DLL questions [message #92148 is a reply to message #92143] |
Tue, 20 October 2015 08:13  |
natha
Messages: 482 Registered: October 2007
|
Senior Member |
|
|
OK, so it seems to work...
I took the example code of Michael's book and I wrote the following OpenMP version:
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
float callex_total(float arr[], int *n) {
int nthreads, i, tid;
float total = 0.;
#pragma omp parallel shared(arr) private(i,tid)
{
tid = omp_get_thread_num();
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
printf("Thread %d starting...\n",tid);
#pragma omp for
for (i = 0; i < *n; i++)
{
total += arr[i];
printf("Thread %d: total += arr[%d]\n",tid,i);
}
} /* end of parallel section */
return(total);
}
Then, I had to play a lot with the compiler but at the end I've found the way:
cfile='callex_total.c'
cfile_noext=file_basename(cfile, '.c')
srcdir = file_dirname(file_expand_path(cfile))
cc='gcc -fopenmp -lgomp -fPIC %c -c -o %o'
ld='gcc -fopenmp -shared -o %L %O %X'
make_dll, cfile_noext, 'IDL_Load', input_directory=srcdir, output_directory=srcdir, /verbose, /show_all_output, cc=cc, ld=ld ;;extra_cflag='-fopenmp -c'
result = call_external(cfile_noext+'.so', cfile_noext, findgen(10), 10, /f_value, /auto_glue)
print, result
And here is the output:
Thread 6 starting...
Thread 4 starting...
Thread 4: total += arr[8]
Thread 4: total += arr[9]
Thread 2 starting...
Thread 2: total += arr[4]
Thread 2: total += arr[5]
Thread 7 starting...
Thread 5 starting...
Thread 1 starting...
Thread 1: total += arr[2]
Thread 1: total += arr[3]
Thread 3 starting...
Thread 3: total += arr[6]
Thread 3: total += arr[7]
Number of threads = 8
Thread 0 starting...
Thread 0: total += arr[0]
Thread 0: total += arr[1]
45.0000
I am very glad to know that we can link OpenMP to IDL.
|
|
|