OpenMP with CFFI

47 views
Skip to first unread message

Jeppe Druedahl

unread,
Jan 27, 2017, 7:25:50 PM1/27/17
to python-cffi
I get an load library error when a try to use OpenMP with cffi. Compiling the c-file to an exe-file works just fine.
Any ideas what the problem is?
I am on a Windows 10 machine, I believe I have done something very similar on a Windows 8.1 before where it worked.

test_openmp.c:

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

void test_openmp()
{
    FILE
* log = fopen("log_openmp.txt","w");
    fprintf
(log, "I am working (openmp with %d threads)\n",
    omp_get_max_threads
());
    fclose
(log);
}

int main()
{
 test_openmp
();
 
return 1;
}


python-fil:

import os
from cffi import FFI

# 1. test that it works (i)
os
.system('gcc -fopenmp test_openmp.c -o test_openmp.exe')
os
.system('test_openmp.exe')
         
# 2. cffi
cmd
= 'gcc -shared -fopenmp test_openmp.c -o test_openmp.so'
os
.system(cmd)                            

ffi
= FFI()
ffi
.cdef(r''' void test_openmp(); ''')

cfuncs
= ffi.dlopen("test_openmp.so")
cfuncs
.test_openmp()


error:

OSError                                   Traceback (most recent call last)
<ipython-input-4-0b2f5b870168> in <module>()
     
8 ffi.cdef(r''' void test_openmp(); ''')
     
9
---> 10 cfuncs = ffi.dlopen("test_openmp.so")
     
11 cfuncs.test_openmp()


C
:\Users\gmf123\AppData\Local\Continuum\Anaconda3\lib\site-packages\cffi\api.py in dlopen(self, name, flags)
   
137         assert isinstance(name, basestring) or name is None
   
138         with self._lock:
--> 139             lib, function_cache = _make_ffi_library(self, name, flags)
   
140             self._function_caches.append(function_cache)
   
141             self._libraries.append(lib)


C
:\Users\gmf123\AppData\Local\Continuum\Anaconda3\lib\site-packages\cffi\api.py in _make_ffi_library(ffi, libname, flags)
   
767     import os
   
768     backend = ffi._backend
--> 769     backendlib = _load_backend_lib(backend, libname, flags)
   
770     #
   
771     def accessor_function(name):


C
:\Users\gmf123\AppData\Local\Continuum\Anaconda3\lib\site-packages\cffi\api.py in _load_backend_lib(backend, name, flags)
   
756         if '.' not in name and '/' not in name:
   
757             raise OSError("library not found: %r" % (name,))
--> 758         return backend.load_library(name, flags)
   
759     except OSError:
   
760         import ctypes.util


OSError: cannot load library test_openmp.so: error 0x45a



Armin Rigo

unread,
Jan 28, 2017, 4:45:42 AM1/28/17
to pytho...@googlegroups.com
Hi,

On 28 January 2017 at 01:25, Jeppe Druedahl <jeppe.d...@gmail.com> wrote:
> cmd = 'gcc -shared -fopenmp test_openmp.c -o test_openmp.so'
> os.system(cmd)

Are you compiling main() into the .so, too? Maybe that's the problem:
the .so is not supposed to have a main().

Otherwise, you'll have to use google to figure out what the "error
0x45a" means...


A bientôt,

Armin.

Jeppe Druedahl

unread,
Jan 28, 2017, 7:10:44 AM1/28/17
to python-cffi
Thanks for the suggestion. I have tried without the main() without any change.

I havn't been able to figure out what 0x45a means in this context... 

Armin Rigo

unread,
Jan 28, 2017, 7:16:01 AM1/28/17
to pytho...@googlegroups.com
Hi,

On 28 January 2017 at 13:10, Jeppe Druedahl <jeppe.d...@gmail.com> wrote:
> Thanks for the suggestion. I have tried without the main() without any
> change.

No clue, but maybe it should have the extension ".dll" and not ".so",
as we're on Windows?

> I havn't been able to figure out what 0x45a means in this context...

I can only recommend to look more. Maybe it means some form of
``invalid .dll``, and maybe it occurs because of the extension, as I
said above.


A bientôt,

Armin.
Reply all
Reply to author
Forward
0 new messages