Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for out-of-line CFFI modules #1454

Merged
merged 2 commits into from Oct 5, 2015
Merged

Conversation

gmarkall
Copy link
Member

@gmarkall gmarkall commented Oct 2, 2015

Out-of-line CFFI modules appear differently to inline ones - they are all of type builtin_function_or_method. This PR allows registering an out-of-line CFFI module with Numba so that it can build a mapping of types for all functions in the module, and therefore compile calls to them in jitted functions.

- Update tests to test with an out-of-line module, and mixing
  calls to inline and out-of-line functions
- Update documentation
# We have no record of the function's address in an OOL module, so we
# resort to looking up the function by name in the interpreter's
# symbol table using ctypes
cfunc = getattr(ctypes.pythonapi, cffi_func.__name__)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only worked the test is using sin and cos symbols, which are in the "C" namespace. This will not work for other user defined symbols; for example:

from cffi import FFI

ffi = FFI()


ffi.cdef("int foo(int a, int b, int c);")

ffi.set_source("_example",
"""
static int foo(int a, int b, int c)
{
    return a + b * c;
}
""")


ffi.compile()

import _example

from numba import cffi_support, njit


cffi_support.register_module(_example)


foo = _example.lib.foo

print(foo(1, 2, 3))

@njit
def bar(x, y, z):
    return foo(x, y, z)

print(bar(1, 2, 3))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you need is:

addr = mod.ffi.addressof(mod.lib, 'foo')  # get function pointer
ptr = int(mod.ffi.cast("uintptr_t", addr))    # cast to uintptr_t and then to python int 

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great suggestion, many thanks! Will update the PR and add your example as a test.

@gmarkall
Copy link
Member Author

gmarkall commented Oct 5, 2015

I've updated this to work with user-defined functions as per your patch, and added a test calling the foo function.

seibert added a commit that referenced this pull request Oct 5, 2015
Support for out-of-line CFFI modules
@seibert seibert merged commit f788235 into numba:master Oct 5, 2015
@gmarkall gmarkall deleted the cffi_ool branch December 12, 2019 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants