From 4b975f676a3a1ade41c31b700ae1e3bfacab9fc3 Mon Sep 17 00:00:00 2001 From: Marko Bencun <mbencun@gmail.com> Date: Sat, 7 Sep 2013 16:27:01 +0200 Subject: [PATCH] distribution, cffi caching --- pyopencl/_cffi.py | 103 ++++++++++++++++++++++++++++++++++++++++++++ pyopencl/cffi_cl.py | 92 ++------------------------------------- setup.py | 63 ++++++++++++--------------- 3 files changed, 134 insertions(+), 124 deletions(-) create mode 100644 pyopencl/_cffi.py diff --git a/pyopencl/_cffi.py b/pyopencl/_cffi.py new file mode 100644 index 00000000..6a0159f1 --- /dev/null +++ b/pyopencl/_cffi.py @@ -0,0 +1,103 @@ +from cffi import FFI + +import os.path + +current_directory = os.path.dirname(__file__) + +_ffi = FFI() +_cl_header = """ + +/* cl.h */ +/* scalar types */ +typedef int8_t cl_char; +typedef uint8_t cl_uchar; +typedef int16_t cl_short; +typedef uint16_t cl_ushort; +typedef int32_t cl_int; +typedef uint32_t cl_uint; +typedef int64_t cl_long; +typedef uint64_t cl_ulong; + +typedef uint16_t cl_half; +typedef float cl_float; +typedef double cl_double; + + +typedef struct _cl_platform_id * cl_platform_id; +typedef struct _cl_device_id * cl_device_id; +typedef struct _cl_context * cl_context; +typedef struct _cl_command_queue * cl_command_queue; +typedef struct _cl_mem * cl_mem; +typedef struct _cl_program * cl_program; +typedef struct _cl_kernel * cl_kernel; +typedef struct _cl_event * cl_event; +typedef struct _cl_sampler * cl_sampler; + +typedef cl_uint cl_bool; /* WARNING! Unlike cl_ types in cl_platform.h, cl_bool is not guaranteed to be the same size as the bool in kernels. */ +typedef cl_ulong cl_bitfield; +typedef cl_bitfield cl_device_type; +typedef cl_uint cl_platform_info; +typedef cl_uint cl_device_info; +typedef cl_bitfield cl_device_fp_config; +typedef cl_uint cl_device_mem_cache_type; +typedef cl_uint cl_device_local_mem_type; +typedef cl_bitfield cl_device_exec_capabilities; +typedef cl_bitfield cl_command_queue_properties; +typedef intptr_t cl_device_partition_property; +typedef cl_bitfield cl_device_affinity_domain; + +typedef intptr_t cl_context_properties; +typedef cl_uint cl_context_info; +typedef cl_uint cl_command_queue_info; +typedef cl_uint cl_channel_order; +typedef cl_uint cl_channel_type; +typedef cl_bitfield cl_mem_flags; +typedef cl_uint cl_mem_object_type; +typedef cl_uint cl_mem_info; +typedef cl_bitfield cl_mem_migration_flags; +typedef cl_uint cl_image_info; +typedef cl_uint cl_buffer_create_type; +typedef cl_uint cl_addressing_mode; +typedef cl_uint cl_filter_mode; +typedef cl_uint cl_sampler_info; +typedef cl_bitfield cl_map_flags; +typedef cl_uint cl_program_info; +typedef cl_uint cl_program_build_info; +typedef cl_uint cl_program_binary_type; +typedef cl_int cl_build_status; +typedef cl_uint cl_kernel_info; +typedef cl_uint cl_kernel_arg_info; +typedef cl_uint cl_kernel_arg_address_qualifier; +typedef cl_uint cl_kernel_arg_access_qualifier; +typedef cl_bitfield cl_kernel_arg_type_qualifier; +typedef cl_uint cl_kernel_work_group_info; +typedef cl_uint cl_event_info; +typedef cl_uint cl_command_type; +typedef cl_uint cl_profiling_info; + +""" + +with open(os.path.join(current_directory, 'wrap_cl_core.h')) as _f: + _wrap_cl_header = _f.read() + +_ffi.cdef('%s\n%s' % (_cl_header, _wrap_cl_header)) + +def _get_verifier(**kwargs): + # called by setup.py at build-time, with the relevant sources/include dirs/defines. + # called by pyopencl at runtime with no kwargs, as we do not want to build at runtime, + # but only get the cached version. + + from cffi.verifier import Verifier + return Verifier( + _ffi, + """ + #include <wrap_cl.h> + """, + modulename='wrapcl', + **kwargs) + + +def _get_lib(): + # should + return _ffi, _get_verifier().load_library() + diff --git a/pyopencl/cffi_cl.py b/pyopencl/cffi_cl.py index d36c30a4..d6d8f69a 100644 --- a/pyopencl/cffi_cl.py +++ b/pyopencl/cffi_cl.py @@ -1,95 +1,9 @@ from pyopencl._cl import PooledBuffer, MemoryPool +from _cffi import _get_lib import warnings -import os.path -current_directory = os.path.dirname(__file__) - -from cffi import FFI -_ffi = FFI() -_cl_header = """ - -/* cl.h */ -/* scalar types */ -typedef int8_t cl_char; -typedef uint8_t cl_uchar; -typedef int16_t cl_short; -typedef uint16_t cl_ushort; -typedef int32_t cl_int; -typedef uint32_t cl_uint; -typedef int64_t cl_long; -typedef uint64_t cl_ulong; - -typedef uint16_t cl_half; -typedef float cl_float; -typedef double cl_double; - - -typedef struct _cl_platform_id * cl_platform_id; -typedef struct _cl_device_id * cl_device_id; -typedef struct _cl_context * cl_context; -typedef struct _cl_command_queue * cl_command_queue; -typedef struct _cl_mem * cl_mem; -typedef struct _cl_program * cl_program; -typedef struct _cl_kernel * cl_kernel; -typedef struct _cl_event * cl_event; -typedef struct _cl_sampler * cl_sampler; - -typedef cl_uint cl_bool; /* WARNING! Unlike cl_ types in cl_platform.h, cl_bool is not guaranteed to be the same size as the bool in kernels. */ -typedef cl_ulong cl_bitfield; -typedef cl_bitfield cl_device_type; -typedef cl_uint cl_platform_info; -typedef cl_uint cl_device_info; -typedef cl_bitfield cl_device_fp_config; -typedef cl_uint cl_device_mem_cache_type; -typedef cl_uint cl_device_local_mem_type; -typedef cl_bitfield cl_device_exec_capabilities; -typedef cl_bitfield cl_command_queue_properties; -typedef intptr_t cl_device_partition_property; -typedef cl_bitfield cl_device_affinity_domain; - -typedef intptr_t cl_context_properties; -typedef cl_uint cl_context_info; -typedef cl_uint cl_command_queue_info; -typedef cl_uint cl_channel_order; -typedef cl_uint cl_channel_type; -typedef cl_bitfield cl_mem_flags; -typedef cl_uint cl_mem_object_type; -typedef cl_uint cl_mem_info; -typedef cl_bitfield cl_mem_migration_flags; -typedef cl_uint cl_image_info; -typedef cl_uint cl_buffer_create_type; -typedef cl_uint cl_addressing_mode; -typedef cl_uint cl_filter_mode; -typedef cl_uint cl_sampler_info; -typedef cl_bitfield cl_map_flags; -typedef cl_uint cl_program_info; -typedef cl_uint cl_program_build_info; -typedef cl_uint cl_program_binary_type; -typedef cl_int cl_build_status; -typedef cl_uint cl_kernel_info; -typedef cl_uint cl_kernel_arg_info; -typedef cl_uint cl_kernel_arg_address_qualifier; -typedef cl_uint cl_kernel_arg_access_qualifier; -typedef cl_bitfield cl_kernel_arg_type_qualifier; -typedef cl_uint cl_kernel_work_group_info; -typedef cl_uint cl_event_info; -typedef cl_uint cl_command_type; -typedef cl_uint cl_profiling_info; - -""" - -with open(os.path.join(current_directory, 'wrap_cl_core.h')) as _f: - _wrap_cl_header = _f.read() - -_ffi.cdef('%s\n%s' % (_cl_header, _wrap_cl_header)) - -_lib = _ffi.verify( - """ - #include <wrap_cl.h> - """, - include_dirs=[os.path.join(current_directory, "../src/c_wrapper/")], - library_dirs=[current_directory], - libraries=["wrapcl", "OpenCL"]) + +_ffi, _lib = _get_lib() bitlog2 = _lib.bitlog2 diff --git a/setup.py b/setup.py index 55f8fcfd..e33a9406 100644 --- a/setup.py +++ b/setup.py @@ -4,8 +4,8 @@ def get_config_schema(): from aksetup_helper import ConfigSchema, Option, \ - IncludeDir, LibraryDir, Libraries, BoostLibraries, \ - Switch, StringListOption, make_boost_base_options + IncludeDir, LibraryDir, Libraries, \ + Switch, StringListOption import sys if 'darwin' in sys.platform: @@ -35,11 +35,7 @@ def get_config_schema(): default_cxxflags = [] default_ldflags = [] - return ConfigSchema(make_boost_base_options() + [ - BoostLibraries("python"), - - Switch("USE_SHIPPED_BOOST", True, "Use included Boost library"), - + return ConfigSchema([ Switch("CL_TRACE", False, "Enable OpenCL API tracing"), Switch("CL_ENABLE_GL", False, "Enable OpenCL<->OpenGL interoperability"), Switch("CL_ENABLE_DEVICE_FISSION", True, @@ -60,7 +56,7 @@ def get_config_schema(): def main(): from aksetup_helper import (hack_distutils, get_config, setup, - NumpyExtension, set_up_shipped_boost_if_requested, + NumpyExtension, check_git_submodules) check_git_submodules() @@ -68,13 +64,7 @@ def main(): hack_distutils() conf = get_config(get_config_schema(), warn_about_no_config=False) - EXTRA_OBJECTS, EXTRA_DEFINES = \ - set_up_shipped_boost_if_requested("pyopencl", conf) - - LIBRARY_DIRS = conf["BOOST_LIB_DIR"] - LIBRARIES = conf["BOOST_PYTHON_LIBNAME"] - - EXTRA_INCLUDE_DIRS = [] + EXTRA_DEFINES = {} EXTRA_DEFINES["PYGPU_PACKAGE"] = "pyopencl" EXTRA_DEFINES["PYGPU_PYOPENCL"] = "1" @@ -82,7 +72,6 @@ def main(): if conf["CL_TRACE"]: EXTRA_DEFINES["PYOPENCL_TRACE"] = 1 - INCLUDE_DIRS = conf["BOOST_INC_DIR"] + conf["CL_INC_DIR"] if conf["CL_ENABLE_GL"]: EXTRA_DEFINES["HAVE_GL"] = 1 @@ -165,6 +154,13 @@ def main(): else: pvt_struct_source = "src/wrapper/_pvt_struct_v2.cpp" + # wrap_cl_core.h needs to be available in pyopencl/ + # the cffi verifier depends on it. + import shutil + shutil.copyfile("src/c_wrapper/wrap_cl_core.h", "pyopencl/wrap_cl_core.h") + + from pyopencl._cffi import _get_verifier + setup(name="pyopencl", # metadata version=ver_dic["VERSION_TEXT"], @@ -204,32 +200,28 @@ def main(): "pytools>=2013.5.2", "pytest>=2", "decorator>=3.2.0", + "cffi>=0.7.2", # "Mako>=0.3.6", ], - + ext_package="pyopencl", ext_modules=[ - NumpyExtension("_cl", - [ - "src/wrapper/wrap_cl.cpp", - "src/wrapper/wrap_cl_part_1.cpp", - "src/wrapper/wrap_cl_part_2.cpp", - "src/wrapper/wrap_constants.cpp", - "src/wrapper/wrap_mempool.cpp", - "src/wrapper/bitlog.cpp", - ]+EXTRA_OBJECTS, - include_dirs=INCLUDE_DIRS + EXTRA_INCLUDE_DIRS, - library_dirs=LIBRARY_DIRS + conf["CL_LIB_DIR"], - libraries=LIBRARIES + conf["CL_LIBNAME"], + _get_verifier( + ext_package='pyopencl', # needs to be the same as above + sources=[ + "src/c_wrapper/wrap_cl.cpp", + "src/c_wrapper/wrap_constants.cpp", + #"src/c_wrapper/wrap_mempool.cpp", + "src/c_wrapper/bitlog.cpp", + ], + include_dirs=conf["CL_INC_DIR"] + ["src/c_wrapper/"], + library_dirs=conf["CL_LIB_DIR"], + libraries=conf["CL_LIBNAME"], define_macros=list(EXTRA_DEFINES.items()), extra_compile_args=conf["CXXFLAGS"], extra_link_args=conf["LDFLAGS"], - ), - NumpyExtension("_pvt_struct", - [pvt_struct_source], - extra_compile_args=conf["CXXFLAGS"], - extra_link_args=conf["LDFLAGS"], - ), + + ).get_extension() ], include_package_data=True, @@ -237,6 +229,7 @@ def main(): "pyopencl": [ "cl/*.cl", "cl/*.h", + "wrap_cl_core.h", ] }, -- GitLab