From 05af02c5ff8135754addfa5e7eff19405b1bfce8 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Wed, 11 Mar 2009 13:20:34 -0400 Subject: [PATCH] Initial bits of PyOpenCL. --- .gitignore | 43 ++ README_SETUP.txt | 34 ++ aksetup_helper.py | 581 ++++++++++++++++++++++++ ez_setup.py | 272 +++++++++++ setup.py | 137 ++++++ src/cl/CL/cl.h | 849 +++++++++++++++++++++++++++++++++++ src/cl/CL/cl_platform.h | 167 +++++++ src/cpp/cl.hpp | 111 +++++ src/wrapper/wrap_cl.cpp | 57 +++ src/wrapper/wrap_helpers.hpp | 56 +++ 10 files changed, 2307 insertions(+) create mode 100644 .gitignore create mode 100644 README_SETUP.txt create mode 100644 aksetup_helper.py create mode 100644 ez_setup.py create mode 100644 setup.py create mode 100644 src/cl/CL/cl.h create mode 100644 src/cl/CL/cl_platform.h create mode 100644 src/cpp/cl.hpp create mode 100644 src/wrapper/wrap_cl.cpp create mode 100644 src/wrapper/wrap_helpers.hpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..3abefc774 --- /dev/null +++ b/.gitignore @@ -0,0 +1,43 @@ +.pydevproject +.project +.settings +*~ +.*.sw[po] +*.dat +*.pyc +build +*.prof +siteconf.py +doc/hedge-notes.pdf +*.vtk +*.silo +*.session +dump.py +*.orig +/Makefile +*.png +tags +*.vtu +*.pvtu +*.pvd +doc/user-reference +doc/dev-reference +*.poly +*.node +*.bak +*.pdf +*.tif +*.mpeg +*-journal +visitlog.py +*.log +.figleaf +dist +*.egg* +MANIFEST +*.patch +*.LOCAL.[0-9]* +*.REMOTE.[0-9]* +*.BASE.[0-9]* +tmp +temp* diff --git a/README_SETUP.txt b/README_SETUP.txt new file mode 100644 index 000000000..07cbb551e --- /dev/null +++ b/README_SETUP.txt @@ -0,0 +1,34 @@ +Hi, welcome. + +This Python package uses aksetup for installation, which means that +installation should be easy and quick. + +If you don't want to continue reading, just try the regular + + ./configure.py --help + ./configure.py --some-options + make + sudo make install + +That should do the trick. (By the way: If a config option says "several ok", +then you may specify several values, separated by commas.) + +aksetup also supports regular distutils installation, without using +configure: + + python setup.py build + sudo python setup.py install + +In this case, configuration is obtained from files in this order: + +/etc/aksetup-defaults.py +$HOME/.aksetup-defaults.py +$PACKAGEDIR/siteconf.py + +Once you've run configure, you can copy options from your siteconf.py file to +one of these files, and you won't ever have to configure them again manually. +In fact, you may pass the options "--update-user" and "--update-global" to +configure, and it will automatically update these files for you. + +This is particularly handy if you want to perform an unattended or automatic +installation via easy_install. diff --git a/aksetup_helper.py b/aksetup_helper.py new file mode 100644 index 000000000..a52976dc0 --- /dev/null +++ b/aksetup_helper.py @@ -0,0 +1,581 @@ +# dealings with ez_setup ------------------------------------------------------ +import ez_setup + +ez_setup.use_setuptools() + +from setuptools import Extension + +def setup(*args, **kwargs): + from setuptools import setup + import traceback + try: + setup(*args, **kwargs) + except KeyboardInterrupt: + raise + except SystemExit: + raise + except: + print "--------------------------------------------------------------------------" + print "Sorry, your build failed. Try rerunning configure with different options." + print "--------------------------------------------------------------------------" + raise + + + + +class NumpyExtension(Extension): + # nicked from + # http://mail.python.org/pipermail/distutils-sig/2007-September/008253.html + # solution by Michael Hoffmann + def __init__(self, *args, **kwargs): + Extension.__init__(self, *args, **kwargs) + self._include_dirs = self.include_dirs + del self.include_dirs # restore overwritten property + + def get_numpy_incpath(self): + from imp import find_module + # avoid actually importing numpy, it screws up distutils + file, pathname, descr = find_module("numpy") + from os.path import join + return join(pathname, "core", "include") + + @property + def include_dirs(self): + return self._include_dirs + [self.get_numpy_incpath()] + + + + +class PyUblasExtension(NumpyExtension): + def get_module_include_path(self, name): + from imp import find_module + file, pathname, descr = find_module(name) + from os.path import join + return join(pathname, "..", "include") + + @property + def include_dirs(self): + return self._include_dirs + [ + self.get_numpy_incpath(), + self.get_module_include_path("pyublas"), + ] + + + + +class HedgeExtension(PyUblasExtension): + @property + def include_dirs(self): + return self._include_dirs + [ + self.get_numpy_incpath(), + self.get_module_include_path("pyublas"), + self.get_module_include_path("hedge"), + ] + + + + +# tools ----------------------------------------------------------------------- +def flatten(list): + """For an iterable of sub-iterables, generate each member of each + sub-iterable in turn, i.e. a flattened version of that super-iterable. + + Example: Turn [[a,b,c],[d,e,f]] into [a,b,c,d,e,f]. + """ + for sublist in list: + for j in sublist: + yield j + + + + +def humanize(sym_str): + words = sym_str.lower().replace("_", " ").split(" ") + return " ".join([word.capitalize() for word in words]) + + + + +# siteconf handling ----------------------------------------------------------- +def get_config(schema=None): + if schema is None: + from setup import get_config_schema + schema = get_config_schema() + + if not schema.have_config() and not schema.have_global_config(): + print "********************************************************" + print "*** I have detected that you have not run configure." + print "********************************************************" + print "*** Additionally, no global config files were found." + print "*** I will go ahead with the default configuration." + print "*** In all likelihood, this will not work out." + print "*** " + print "*** See README_SETUP.txt for more information." + print "*** " + print "*** If the build does fail, just re-run configure with the" + print "*** correct arguments, and then retry. Good luck!" + print "********************************************************" + print "*** HIT Ctrl-C NOW IF THIS IS NOT WHAT YOU WANT" + print "********************************************************" + + delay = 10 + + from time import sleep + import sys + while delay: + sys.stdout.write("Continuing in %d seconds... \r" % delay) + sys.stdout.flush() + delay -= 1 + sleep(1) + + return schema.read_config() + + + + +def hack_distutils(debug=False, fast_link=True): + # hack distutils.sysconfig to eliminate debug flags + # stolen from mpi4py + + def remove_prefixes(optlist, bad_prefixes): + for bad_prefix in bad_prefixes: + for i, flag in enumerate(optlist): + if flag.startswith(bad_prefix): + optlist.pop(i) + break + return optlist + + import sys + if not sys.platform.lower().startswith("win"): + from distutils import sysconfig + + cvars = sysconfig.get_config_vars() + cflags = cvars.get('OPT') + if cflags: + cflags = remove_prefixes(cflags.split(), + ['-g', '-O', '-Wstrict-prototypes', '-DNDEBUG']) + if debug: + cflags.append("-g") + else: + cflags.append("-O3") + cflags.append("-DNDEBUG") + cvars['OPT'] = str.join(' ', cflags) + cvars["CFLAGS"] = cvars["BASECFLAGS"] + " " + cvars["OPT"] + + if fast_link: + for varname in ["LDSHARED", "BLDSHARED"]: + ldsharedflags = cvars.get(varname) + if ldsharedflags: + ldsharedflags = remove_prefixes(ldsharedflags.split(), + ['-Wl,-O']) + cvars[varname] = str.join(' ', ldsharedflags) + + + +# configure guts -------------------------------------------------------------- +def default_or(a, b): + if a is None: + return b + else: + return a + + + +def expand_str(s, options): + import re + + def my_repl(match): + sym = match.group(1) + try: + repl = options[sym] + except KeyError: + from os import environ + repl = environ[sym] + + return expand_str(repl, options) + + return re.subn(r"\$\{([a-zA-Z0-9_]+)\}", my_repl, s)[0] + +def expand_value(v, options): + if isinstance(v, (str, unicode)): + return expand_str(v, options) + elif isinstance(v, list): + return [expand_value(i, options) for i in v] + else: + return v + + +def expand_options(options): + for k in options.keys(): + options[k] = expand_value(options[k], options) + return options + + + + + + +class ConfigSchema: + def __init__(self, options, conf_file="siteconf.py", conf_dir="."): + self.optdict = dict((opt.name, opt) for opt in options) + self.options = options + self.conf_dir = conf_dir + self.conf_file = conf_file + + from os.path import expanduser + self.user_conf_file = expanduser("~/.aksetup-defaults.py") + + import sys + if not sys.platform.lower().startswith("win"): + self.global_conf_file = "/etc/aksetup-defaults.py" + else: + self.global_conf_file = None + + def get_conf_file(self): + import os + return os.path.join(self.conf_dir, self.conf_file) + + def set_conf_dir(self, conf_dir): + self.conf_dir = conf_dir + + def get_default_config(self): + return dict((opt.name, opt.default) + for opt in self.options) + + def read_config_from_pyfile(self, filename): + result = {} + filevars = {} + execfile(filename, filevars) + + for key, value in filevars.iteritems(): + if key in self.optdict: + result[key] = value + + return result + + def update_conf_file(self, filename, config): + result = {} + filevars = {} + + try: + execfile(filename, filevars) + except IOError: + pass + + del filevars["__builtins__"] + + for key, value in config.iteritems(): + if value is not None: + filevars[key] = value + + keys = filevars.keys() + keys.sort() + + outf = open(filename, "w") + for key in keys: + outf.write("%s = %s\n" % (key, repr(filevars[key]))) + outf.close() + + return result + + def update_user_config(self, config): + self.update_conf_file(self.user_conf_file, config) + + def update_global_config(self, config): + if self.global_conf_file is not None: + self.update_conf_file(self.global_conf_file, config) + + def get_default_config_with_files(self): + result = self.get_default_config() + + import os + + confignames = [] + if self.global_conf_file is not None: + confignames.append(self.global_conf_file) + confignames.append(self.user_conf_file) + + for fn in confignames: + if os.access(fn, os.R_OK): + result.update(self.read_config_from_pyfile(fn)) + + return result + + def have_global_config(self): + import os + result = os.access(self.user_conf_file, os.R_OK) + + if self.global_conf_file is not None: + result = result or os.access(self.global_conf_file, os.R_OK) + + return result + + def have_config(self): + import os + return os.access(self.get_conf_file(), os.R_OK) + + def read_config(self, warn_if_none=True): + import os + cfile = self.get_conf_file() + + result = self.get_default_config_with_files() + if os.access(cfile, os.R_OK): + filevars = {} + execfile(cfile, filevars) + + for key, value in filevars.iteritems(): + if key in self.optdict: + result[key] = value + elif key == "__builtins__": + pass + else: + raise KeyError, "invalid config key in %s: %s" % ( + cfile, key) + + expand_options(result) + + return result + + def add_to_configparser(self, parser, def_config=None): + if def_config is None: + def_config = self.get_default_config_with_files() + + for opt in self.options: + default = default_or(def_config.get(opt.name), opt.default) + opt.add_to_configparser(parser, default) + + def get_from_configparser(self, options): + result = {} + for opt in self.options: + result[opt.name] = opt.take_from_configparser(options) + expand_options(result) + return result + + def write_config(self, config): + import os + outf = open(self.get_conf_file(), "w") + for opt in self.options: + value = config[opt.name] + if value is not None: + outf.write("%s = %s\n" % (opt.name, repr(config[opt.name]))) + outf.close() + + def make_substitutions(self, config): + return dict((opt.name, opt.value_to_str(config[opt.name])) + for opt in self.options) + + + + + + + + +class Option(object): + def __init__(self, name, default=None, help=None): + self.name = name + self.default = default + self.help = help + + def as_option(self): + return self.name.lower().replace("_", "-") + + def metavar(self): + last_underscore = self.name.rfind("_") + return self.name[last_underscore+1:] + + def get_help(self, default): + result = self.help + if self.default: + result += " (default: %s)" % self.value_to_str( + default_or(default, self.default)) + return result + + def value_to_str(self, default): + return default + + def add_to_configparser(self, parser, default=None): + default = default_or(default, self.default) + default_str = self.value_to_str(default) + parser.add_option( + "--" + self.as_option(), dest=self.name, + default=default_str, + metavar=self.metavar(), help=self.get_help(default)) + + def take_from_configparser(self, options): + return getattr(options, self.name) + +class Switch(Option): + def add_to_configparser(self, parser, default=None): + option = self.as_option() + + if not isinstance(self.default, bool): + raise ValueError, "Switch options must have a default" + + if default is None: + default = self.default + + if default: + action = "store_false" + else: + action = "store_true" + + parser.add_option( + "--" + self.as_option(), + dest=self.name, + help=self.get_help(default), + default=default, + action=action) + +class StringListOption(Option): + def value_to_str(self, default): + if default is None: + return None + + return ",".join([str(el) for el in default]) + + def get_help(self, default): + return Option.get_help(self, default) + " (several ok)" + + def take_from_configparser(self, options): + opt = getattr(options, self.name) + if opt is None: + return None + else: + if opt: + return opt.split(",") + else: + return [] + + +class IncludeDir(StringListOption): + def __init__(self, lib_name, default=None, human_name=None, help=None): + StringListOption.__init__(self, "%s_INC_DIR" % lib_name, default, + help=help or ("Include directories for %s" + % (human_name or humanize(lib_name)))) + +class LibraryDir(StringListOption): + def __init__(self, lib_name, default=None, human_name=None, help=None): + StringListOption.__init__(self, "%s_LIB_DIR" % lib_name, default, + help=help or ("Library directories for %s" + % (human_name or humanize(lib_name)))) + +class Libraries(StringListOption): + def __init__(self, lib_name, default=None, human_name=None, help=None): + StringListOption.__init__(self, "%s_LIBNAME" % lib_name, default, + help=help or ("Library names for %s (without lib or .so)" + % (human_name or humanize(lib_name)))) + +class BoostLibraries(Libraries): + def __init__(self, lib_base_name): + Libraries.__init__(self, "BOOST_%s" % lib_base_name.upper(), + ["boost_%s-${BOOST_COMPILER}-mt" % lib_base_name], + help="Library names for Boost C++ %s library (without lib or .so)" + % humanize(lib_base_name)) + +def make_boost_base_options(): + return [ + IncludeDir("BOOST", []), + LibraryDir("BOOST", []), + Option("BOOST_COMPILER", default="gcc43", + help="The compiler with which Boost C++ was compiled, e.g. gcc43"), + ] + + + + + + + +def configure_frontend(): + from optparse import OptionParser + + from setup import get_config_schema + schema = get_config_schema() + if schema.have_config(): + print "************************************************************" + print "*** I have detected that you have already run configure." + print "*** I'm taking the configured values as defaults for this" + print "*** configure run. If you don't want this, delete the file" + print "*** %s." % schema.get_conf_file() + print "************************************************************" + + import sys + + description = "generate a configuration file for this software package" + parser = OptionParser(description=description) + parser.add_option( + "--python-exe", dest="python_exe", default=sys.executable, + help="Which Python interpreter to use", metavar="PATH") + + parser.add_option("--prefix", default=None, + help="Ignored") + parser.add_option("--enable-shared", help="Ignored", action="store_false") + parser.add_option("--disable-static", help="Ignored", action="store_false") + parser.add_option("--update-user", help="Update user config file (%s)" % schema.user_conf_file, + action="store_true") + parser.add_option("--update-global", + help="Update global config file (%s)" % schema.global_conf_file, + action="store_true") + + schema.add_to_configparser(parser, schema.read_config()) + + options, args = parser.parse_args() + + config = schema.get_from_configparser(options) + schema.write_config(config) + + if options.update_user: + schema.update_user_config(config) + + if options.update_global: + schema.update_global_config(config) + + import os + if os.access("Makefile.in", os.F_OK): + substs = schema.make_substitutions(config) + substs["PYTHON_EXE"] = options.python_exe + + substitute(substs, "Makefile") + + + + +def substitute(substitutions, fname): + import re + var_re = re.compile(r"\$\{([A-Za-z_0-9]+)\}") + string_var_re = re.compile(r"\$str\{([A-Za-z_0-9]+)\}") + + fname_in = fname+".in" + lines = open(fname_in, "r").readlines() + new_lines = [] + for l in lines: + made_change = True + while made_change: + made_change = False + match = var_re.search(l) + if match: + varname = match.group(1) + l = l[:match.start()] + str(substitutions[varname]) + l[match.end():] + made_change = True + + match = string_var_re.search(l) + if match: + varname = match.group(1) + subst = substitutions[varname] + if subst is None: + subst = "" + else: + subst = '"%s"' % subst + + l = l[:match.start()] + subst + l[match.end():] + made_change = True + new_lines.append(l) + new_lines.insert(1, "# DO NOT EDIT THIS FILE -- it was generated by configure.py\n") + import sys + new_lines.insert(2, "# %s\n" % (" ".join(sys.argv))) + open(fname, "w").write("".join(new_lines)) + + from os import stat, chmod + infile_stat_res = stat(fname_in) + chmod(fname, infile_stat_res.st_mode) diff --git a/ez_setup.py b/ez_setup.py new file mode 100644 index 000000000..89cf056d9 --- /dev/null +++ b/ez_setup.py @@ -0,0 +1,272 @@ +#!python +"""Bootstrap setuptools installation + +If you want to use setuptools in your package's setup.py, just include this +file in the same directory with it, and add this to the top of your setup.py:: + + from ez_setup import use_setuptools + use_setuptools() + +If you want to require a specific version of setuptools, set a download +mirror, or use an alternate download directory, you can do so by supplying +the appropriate options to ``use_setuptools()``. + +This file can also be run as a script to install or upgrade setuptools. +""" +import sys +DEFAULT_VERSION = "0.6c8" +DEFAULT_URL = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3] + +md5_data = { + 'setuptools-0.6b1-py2.3.egg': '8822caf901250d848b996b7f25c6e6ca', + 'setuptools-0.6b1-py2.4.egg': 'b79a8a403e4502fbb85ee3f1941735cb', + 'setuptools-0.6b2-py2.3.egg': '5657759d8a6d8fc44070a9d07272d99b', + 'setuptools-0.6b2-py2.4.egg': '4996a8d169d2be661fa32a6e52e4f82a', + 'setuptools-0.6b3-py2.3.egg': 'bb31c0fc7399a63579975cad9f5a0618', + 'setuptools-0.6b3-py2.4.egg': '38a8c6b3d6ecd22247f179f7da669fac', + 'setuptools-0.6b4-py2.3.egg': '62045a24ed4e1ebc77fe039aa4e6f7e5', + 'setuptools-0.6b4-py2.4.egg': '4cb2a185d228dacffb2d17f103b3b1c4', + 'setuptools-0.6c1-py2.3.egg': 'b3f2b5539d65cb7f74ad79127f1a908c', + 'setuptools-0.6c1-py2.4.egg': 'b45adeda0667d2d2ffe14009364f2a4b', + 'setuptools-0.6c2-py2.3.egg': 'f0064bf6aa2b7d0f3ba0b43f20817c27', + 'setuptools-0.6c2-py2.4.egg': '616192eec35f47e8ea16cd6a122b7277', + 'setuptools-0.6c3-py2.3.egg': 'f181fa125dfe85a259c9cd6f1d7b78fa', + 'setuptools-0.6c3-py2.4.egg': 'e0ed74682c998bfb73bf803a50e7b71e', + 'setuptools-0.6c3-py2.5.egg': 'abef16fdd61955514841c7c6bd98965e', + 'setuptools-0.6c4-py2.3.egg': 'b0b9131acab32022bfac7f44c5d7971f', + 'setuptools-0.6c4-py2.4.egg': '2a1f9656d4fbf3c97bf946c0a124e6e2', + 'setuptools-0.6c4-py2.5.egg': '8f5a052e32cdb9c72bcf4b5526f28afc', + 'setuptools-0.6c5-py2.3.egg': 'ee9fd80965da04f2f3e6b3576e9d8167', + 'setuptools-0.6c5-py2.4.egg': 'afe2adf1c01701ee841761f5bcd8aa64', + 'setuptools-0.6c5-py2.5.egg': 'a8d3f61494ccaa8714dfed37bccd3d5d', + 'setuptools-0.6c6-py2.3.egg': '35686b78116a668847237b69d549ec20', + 'setuptools-0.6c6-py2.4.egg': '3c56af57be3225019260a644430065ab', + 'setuptools-0.6c6-py2.5.egg': 'b2f8a7520709a5b34f80946de5f02f53', + 'setuptools-0.6c7-py2.3.egg': '209fdf9adc3a615e5115b725658e13e2', + 'setuptools-0.6c7-py2.4.egg': '5a8f954807d46a0fb67cf1f26c55a82e', + 'setuptools-0.6c7-py2.5.egg': '45d2ad28f9750e7434111fde831e8372', + 'setuptools-0.6c8-py2.3.egg': '50759d29b349db8cfd807ba8303f1902', + 'setuptools-0.6c8-py2.4.egg': 'cba38d74f7d483c06e9daa6070cce6de', + 'setuptools-0.6c8-py2.5.egg': '1721747ee329dc150590a58b3e1ac95b', +} + +import sys, os + +def _validate_md5(egg_name, data): + if egg_name in md5_data: + from md5 import md5 + digest = md5(data).hexdigest() + if digest != md5_data[egg_name]: + print >>sys.stderr, ( + "md5 validation of %s failed! (Possible download problem?)" + % egg_name + ) + sys.exit(2) + return data + + +def use_setuptools( + version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, + download_delay=15 +): + """Automatically find/download setuptools and make it available on sys.path + + `version` should be a valid setuptools version number that is available + as an egg for download under the `download_base` URL (which should end with + a '/'). `to_dir` is the directory where setuptools will be downloaded, if + it is not already available. If `download_delay` is specified, it should + be the number of seconds that will be paused before initiating a download, + should one be required. If an older version of setuptools is installed, + this routine will print a message to ``sys.stderr`` and raise SystemExit in + an attempt to abort the calling script. + """ + was_imported = 'pkg_resources' in sys.modules or 'setuptools' in sys.modules + def do_download(): + egg = download_setuptools(version, download_base, to_dir, download_delay) + sys.path.insert(0, egg) + import setuptools; setuptools.bootstrap_install_from = egg + try: + import pkg_resources + except ImportError: + return do_download() + try: + pkg_resources.require("setuptools>="+version); return + except pkg_resources.VersionConflict, e: + if was_imported: + print >>sys.stderr, ( + "The required version of setuptools (>=%s) is not available, and\n" + "can't be installed while this script is running. Please install\n" + " a more recent version first, using 'easy_install -U setuptools'." + "\n\n(Currently using %r)" + ) % (version, e.args[0]) + sys.exit(2) + else: + del pkg_resources, sys.modules['pkg_resources'] # reload ok + return do_download() + except pkg_resources.DistributionNotFound: + return do_download() + +def download_setuptools( + version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, + delay = 15 +): + """Download setuptools from a specified location and return its filename + + `version` should be a valid setuptools version number that is available + as an egg for download under the `download_base` URL (which should end + with a '/'). `to_dir` is the directory where the egg will be downloaded. + `delay` is the number of seconds to pause before an actual download attempt. + """ + import urllib2, shutil + egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3]) + url = download_base + egg_name + saveto = os.path.join(to_dir, egg_name) + src = dst = None + if not os.path.exists(saveto): # Avoid repeated downloads + try: + from distutils import log + if delay: + log.warn(""" +--------------------------------------------------------------------------- +This script requires setuptools version %s to run (even to display +help). I will attempt to download it for you (from +%s), but +you may need to enable firewall access for this script first. +I will start the download in %d seconds. + +(Note: if this machine does not have network access, please obtain the file + + %s + +and place it in this directory before rerunning this script.) +---------------------------------------------------------------------------""", + version, download_base, delay, url + ); from time import sleep; sleep(delay) + log.warn("Downloading %s", url) + src = urllib2.urlopen(url) + # Read/write all in one block, so we don't create a corrupt file + # if the download is interrupted. + data = _validate_md5(egg_name, src.read()) + dst = open(saveto,"wb"); dst.write(data) + finally: + if src: src.close() + if dst: dst.close() + return os.path.realpath(saveto) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +def main(argv, version=DEFAULT_VERSION): + """Install or upgrade setuptools and EasyInstall""" + try: + import setuptools + except ImportError: + egg = None + try: + egg = download_setuptools(version, delay=0) + sys.path.insert(0,egg) + from setuptools.command.easy_install import main + return main(list(argv)+[egg]) # we're done here + finally: + if egg and os.path.exists(egg): + os.unlink(egg) + else: + if setuptools.__version__ == '0.0.1': + print >>sys.stderr, ( + "You have an obsolete version of setuptools installed. Please\n" + "remove it from your system entirely before rerunning this script." + ) + sys.exit(2) + + req = "setuptools>="+version + import pkg_resources + try: + pkg_resources.require(req) + except pkg_resources.VersionConflict: + try: + from setuptools.command.easy_install import main + except ImportError: + from easy_install import main + main(list(argv)+[download_setuptools(delay=0)]) + sys.exit(0) # try to force an exit + else: + if argv: + from setuptools.command.easy_install import main + main(argv) + else: + print "Setuptools version",version,"or greater has been installed." + print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)' + +def update_md5(filenames): + """Update our built-in md5 registry""" + + import re + from md5 import md5 + + for name in filenames: + base = os.path.basename(name) + f = open(name,'rb') + md5_data[base] = md5(f.read()).hexdigest() + f.close() + + data = [" %r: %r,\n" % it for it in md5_data.items()] + data.sort() + repl = "".join(data) + + import inspect + srcfile = inspect.getsourcefile(sys.modules[__name__]) + f = open(srcfile, 'rb'); src = f.read(); f.close() + + match = re.search("\nmd5_data = {\n([^}]+)}", src) + if not match: + print >>sys.stderr, "Internal error!" + sys.exit(2) + + src = src[:match.start(1)] + repl + src[match.end(1):] + f = open(srcfile,'w') + f.write(src) + f.close() + + +if __name__=='__main__': + if len(sys.argv)>2 and sys.argv[1]=='--md5update': + update_md5(sys.argv[2:]) + else: + main(sys.argv[1:]) + + + + + diff --git a/setup.py b/setup.py new file mode 100644 index 000000000..941d36bac --- /dev/null +++ b/setup.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python +# -*- coding: latin-1 -*- + + + +def get_config_schema(): + from aksetup_helper import ConfigSchema, Option, \ + IncludeDir, LibraryDir, Libraries, BoostLibraries, \ + Switch, StringListOption, make_boost_base_options + + return ConfigSchema(make_boost_base_options() + [ + BoostLibraries("python"), + BoostLibraries("thread"), + + Switch("CL_TRACE", False, "Enable OpenCL API tracing"), + + IncludeDir("CL", []), + LibraryDir("CL", []), + Libraries("CL", ["cl"]), + + StringListOption("CXXFLAGS", [], + help="Any extra C++ compiler options to include"), + StringListOption("LDFLAGS", [], + help="Any extra linker options to include"), + ]) + + + + +def main(): + import glob + from aksetup_helper import hack_distutils, get_config, setup, \ + NumpyExtension + + hack_distutils() + conf = get_config(get_config_schema()) + + LIBRARY_DIRS = conf["BOOST_LIB_DIR"] + LIBRARIES = conf["BOOST_PYTHON_LIBNAME"] + conf["BOOST_THREAD_LIBNAME"] + + from os.path import dirname, join, normpath + + EXTRA_DEFINES = { } + EXTRA_INCLUDE_DIRS = [] + EXTRA_LIBRARY_DIRS = [] + EXTRA_LIBRARIES = [] + + if conf["CL_TRACE"]: + EXTRA_DEFINES["CLPP_TRACE_CL"] = 1 + + INCLUDE_DIRS = ['src/cpp', 'src/cl'] + conf["BOOST_INC_DIR"] + conf["CL_INC_DIR"] + + import sys + + ext_kwargs = dict() + + setup(name="pyopencl", + # metadata + version="0.90", + description="Python wrapper for OpenCL", + long_description=""" + PyOpenCL lets you access GPUs and other massively parallel compute + devices from Python. It tries to offer computing goodness in the + spirit of its sister project `PyCUDA `_: + + * Object cleanup tied to lifetime of objects. This idiom, often + called + `RAII `_ + in C++, makes it much easier to write correct, leak- and + crash-free code. PyOpenCL knows about dependencies, too, so (for + example) it won't detach from a context before all memory + allocated in it is also freed. + + * Convenience. Abstractions like pyopencl.SourceModule and + pycuda.gpuarray.GPUArray make OpenCL programming much more + convenient than the default C API. + + * Completeness. PyOpenCL puts the full power of OpenCL's API at + your disposal, if you wish. + + * Automatic Error Checking. All CL errors are automatically + translated into Python exceptions. + + * Speed. PyOpenCL's base layer is written in C++, so all the niceties + above are virtually free. + + * Helpful `Documentation `_. + """, + author=u"Andreas Kloeckner", + author_email="inform@tiker.net", + license = "MIT", + url="http://mathema.tician.de/software/pyopencl", + classifiers=[ + 'Environment :: Console', + 'Development Status :: 2 - Pre-Alpha', + 'Intended Audience :: Developers', + 'Intended Audience :: Other Audience', + 'Intended Audience :: Science/Research', + 'License :: OSI Approved :: Apache Software License', + 'Natural Language :: English', + 'Programming Language :: C++', + 'Programming Language :: Python', + 'Topic :: Scientific/Engineering', + 'Topic :: Scientific/Engineering :: Mathematics', + 'Topic :: Scientific/Engineering :: Physics', + ], + + # build info + packages=["pyopencl"], + zip_safe=False, + + install_requires=[ + "pytools>=7", + ], + + package_dir={"pyopencl": "src/python"}, + ext_package="pyopencl", + ext_modules=[ + NumpyExtension("_cl", + [ + #"src/cpp/cl.cpp", + "src/wrapper/wrap_cl.cpp", + ], + include_dirs=INCLUDE_DIRS + EXTRA_INCLUDE_DIRS, + library_dirs=LIBRARY_DIRS + conf["CL_LIB_DIR"], + libraries=LIBRARIES + conf["CL_LIBNAME"], + define_macros=list(EXTRA_DEFINES.iteritems()), + extra_compile_args=conf["CXXFLAGS"], + extra_link_args=conf["LDFLAGS"], + ), + ]) + + + + +if __name__ == '__main__': + main() diff --git a/src/cl/CL/cl.h b/src/cl/CL/cl.h new file mode 100644 index 000000000..7832c954d --- /dev/null +++ b/src/cl/CL/cl.h @@ -0,0 +1,849 @@ +/******************************************************************************* + * Copyright (c) 2008-2009 The Khronos Group Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and/or associated documentation files (the + * "Materials"), to deal in the Materials without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Materials, and to + * permit persons to whom the Materials are furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Materials. + * + * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. + ******************************************************************************/ + +#ifndef __OPENCL_CL_H +#define __OPENCL_CL_H + +#ifdef __APPLE__ +#include +#else +#include +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/******************************************************************************/ + +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_address_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_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_uint cl_image_info; +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_int cl_build_status; +typedef cl_uint cl_kernel_info; +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; + +typedef struct _cl_image_format { + cl_channel_order image_channel_order; + cl_channel_type image_channel_data_type; +} cl_image_format; + +/******************************************************************************/ + +// Error Codes +#define CL_SUCCESS 0 +#define CL_DEVICE_NOT_FOUND -1 +#define CL_DEVICE_NOT_AVAILABLE -2 +#define CL_DEVICE_COMPILER_NOT_AVAILABLE -3 +#define CL_MEM_OBJECT_ALLOCATION_FAILURE -4 +#define CL_OUT_OF_RESOURCES -5 +#define CL_OUT_OF_HOST_MEMORY -6 +#define CL_PROFILING_INFO_NOT_AVAILABLE -7 +#define CL_MEM_COPY_OVERLAP -8 +#define CL_IMAGE_FORMAT_MISMATCH -9 +#define CL_IMAGE_FORMAT_NOT_SUPPORTED -10 + +#define CL_INVALID_VALUE -30 +#define CL_INVALID_DEVICE_TYPE -31 +#define CL_INVALID_DEVICE_LIST -32 +#define CL_INVALID_DEVICE -33 +#define CL_INVALID_CONTEXT -34 +#define CL_INVALID_QUEUE_PROPERTIES -35 +#define CL_INVALID_COMMAND_QUEUE -36 +#define CL_INVALID_HOST_PTR -37 +#define CL_INVALID_MEM_OBJECT -38 +#define CL_INVALID_IMAGE_FORMAT_DESCRIPTOR -39 +#define CL_INVALID_IMAGE_SIZE -40 +#define CL_INVALID_SAMPLER -41 +#define CL_INVALID_BINARY -42 +#define CL_INVALID_BUILD_OPTIONS -43 +#define CL_INVALID_PROGRAM -44 +#define CL_INVALID_PROGRAM_EXECUTABLE -45 +#define CL_INVALID_KERNEL_NAME -46 +#define CL_INVALID_KERNEL_DEFINITION -47 +#define CL_INVALID_KERNEL -48 +#define CL_INVALID_ARG_INDEX -49 +#define CL_INVALID_ARG_VALUE -50 +#define CL_INVALID_ARG_SIZE -51 +#define CL_INVALID_KERNEL_ARGS -52 +#define CL_INVALID_WORK_DIMENSION -53 +#define CL_INVALID_WORK_GROUP_SIZE -54 +#define CL_INVALID_WORK_ITEM_SIZE -55 +#define CL_INVALID_GLOBAL_OFFSET -56 +#define CL_INVALID_EVENT_WAIT_LIST -57 +#define CL_INVALID_EVENT -58 +#define CL_INVALID_OPERATION -59 +#define CL_INVALID_GL_OBJECT -60 +#define CL_INVALID_BUFFER_SIZE -61 +#define CL_INVALID_MIP_LEVEL -62 + +// OpenCL Version +#define CL_VERSION_1_0 1 + +// cl_bool +#define CL_FALSE 0 +#define CL_TRUE 1 + +// cl_platform_info +#define CL_PLATFORM_PROFILE 0x0900 +#define CL_PLATFORM_VERSION 0x0901 + +// cl_device_type - bitfield +#define CL_DEVICE_TYPE_DEFAULT (1 << 0) +#define CL_DEVICE_TYPE_CPU (1 << 1) +#define CL_DEVICE_TYPE_GPU (1 << 2) +#define CL_DEVICE_TYPE_ACCELERATOR (1 << 3) +#define CL_DEVICE_TYPE_ALL 0xFFFFFFFF + +// cl_device_info +#define CL_DEVICE_TYPE 0x1000 +#define CL_DEVICE_VENDOR_ID 0x1001 +#define CL_DEVICE_MAX_COMPUTE_UNITS 0x1002 +#define CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS 0x1003 +#define CL_DEVICE_MAX_WORK_GROUP_SIZE 0x1004 +#define CL_DEVICE_MAX_WORK_ITEM_SIZES 0x1005 +#define CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR 0x1006 +#define CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT 0x1007 +#define CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT 0x1008 +#define CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG 0x1009 +#define CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT 0x100A +#define CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE 0x100B +#define CL_DEVICE_MAX_CLOCK_FREQUENCY 0x100C +#define CL_DEVICE_ADDRESS_BITS 0x100D +#define CL_DEVICE_MAX_READ_IMAGE_ARGS 0x100E +#define CL_DEVICE_MAX_WRITE_IMAGE_ARGS 0x100F +#define CL_DEVICE_MAX_MEM_ALLOC_SIZE 0x1010 +#define CL_DEVICE_IMAGE2D_MAX_WIDTH 0x1011 +#define CL_DEVICE_IMAGE2D_MAX_HEIGHT 0x1012 +#define CL_DEVICE_IMAGE3D_MAX_WIDTH 0x1013 +#define CL_DEVICE_IMAGE3D_MAX_HEIGHT 0x1014 +#define CL_DEVICE_IMAGE3D_MAX_DEPTH 0x1015 +#define CL_DEVICE_IMAGE_SUPPORT 0x1016 +#define CL_DEVICE_MAX_PARAMETER_SIZE 0x1017 +#define CL_DEVICE_MAX_SAMPLERS 0x1018 +#define CL_DEVICE_MEM_BASE_ADDR_ALIGN 0x1019 +#define CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE 0x101A +#define CL_DEVICE_SINGLE_FP_CONFIG 0x101B +#define CL_DEVICE_GLOBAL_MEM_CACHE_TYPE 0x101C +#define CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE 0x101D +#define CL_DEVICE_GLOBAL_MEM_CACHE_SIZE 0x101E +#define CL_DEVICE_GLOBAL_MEM_SIZE 0x101F +#define CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE 0x1020 +#define CL_DEVICE_MAX_CONSTANT_ARGS 0x1021 +#define CL_DEVICE_LOCAL_MEM_TYPE 0x1022 +#define CL_DEVICE_LOCAL_MEM_SIZE 0x1023 +#define CL_DEVICE_ERROR_CORRECTION_SUPPORT 0x1024 +#define CL_DEVICE_PROFILING_TIMER_RESOLUTION 0x1025 +#define CL_DEVICE_ENDIAN_LITTLE 0x1026 +#define CL_DEVICE_AVAILABLE 0x1027 +#define CL_DEVICE_COMPILER_AVAILABLE 0x1028 +#define CL_DEVICE_EXECUTION_CAPABILITIES 0x1029 +#define CL_DEVICE_QUEUE_PROPERTIES 0x102A +#define CL_DEVICE_NAME 0x102B +#define CL_DEVICE_VENDOR 0x102C +#define CL_DRIVER_VERSION 0x102D +#define CL_DEVICE_PROFILE 0x102E +#define CL_DEVICE_VERSION 0x102F +#define CL_DEVICE_EXTENSIONS 0x1030 + +// cl_device_address_info - bitfield +#define CL_DEVICE_ADDRESS_32_BITS (1 << 0) +#define CL_DEVICE_ADDRESS_64_BITS (1 << 1) + +// cl_device_fp_config - bitfield +#define CL_FP_DENORM (1 << 0) +#define CL_FP_INF_NAN (1 << 1) +#define CL_FP_ROUND_TO_NEAREST (1 << 2) +#define CL_FP_ROUND_TO_ZERO (1 << 3) +#define CL_FP_ROUND_TO_INF (1 << 4) +#define CL_FP_FMA (1 << 5) + +// cl_device_mem_cache_type +#define CL_NONE 0x0 +#define CL_READ_ONLY_CACHE 0x1 +#define CL_READ_WRITE_CACHE 0x2 + +// cl_device_local_mem_type +#define CL_LOCAL 0x1 +#define CL_GLOBAL 0x2 + +// cl_device_exec_capabilities - bitfield +#define CL_EXEC_KERNEL (1 << 0) +#define CL_EXEC_NATIVE_KERNEL (1 << 1) + +// cl_command_queue_properties - bitfield +#define CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE (1 << 0) +#define CL_QUEUE_PROFILING_ENABLE (1 << 1) + +// cl_context_info +#define CL_CONTEXT_REFERENCE_COUNT 0x1080 +#define CL_CONTEXT_NUM_DEVICES 0x1081 +#define CL_CONTEXT_DEVICES 0x1082 +#define CL_CONTEXT_PROPERTIES 0x1083 + +// cl_command_queue_info +#define CL_QUEUE_CONTEXT 0x1090 +#define CL_QUEUE_DEVICE 0x1091 +#define CL_QUEUE_REFERENCE_COUNT 0x1092 +#define CL_QUEUE_PROPERTIES 0x1093 + +// cl_mem_flags - bitfield +#define CL_MEM_READ_WRITE (1 << 0) +#define CL_MEM_WRITE_ONLY (1 << 1) +#define CL_MEM_READ_ONLY (1 << 2) +#define CL_MEM_USE_HOST_PTR (1 << 3) +#define CL_MEM_ALLOC_HOST_PTR (1 << 4) +#define CL_MEM_COPY_HOST_PTR (1 << 5) + +// cl_channel_order +#define CL_R 0x10B0 +#define CL_A 0x10B1 +#define CL_RG 0x10B2 +#define CL_RA 0x10B3 +#define CL_RGB 0x10B4 +#define CL_RGBA 0x10B5 +#define CL_BGRA 0x10B6 +#define CL_ARGB 0x10B7 + +// cl_channel_type +#define CL_SNORM_INT8 0x10D0 +#define CL_SNORM_INT16 0x10D1 +#define CL_UNORM_INT8 0x10D2 +#define CL_UNORM_INT16 0x10D3 +#define CL_UNORM_SHORT_565 0x10D4 +#define CL_UNORM_SHORT_555 0x10D5 +#define CL_UNORM_INT_101010 0x10D6 +#define CL_SIGNED_INT8 0x10D7 +#define CL_SIGNED_INT16 0x10D8 +#define CL_SIGNED_INT32 0x10D9 +#define CL_UNSIGNED_INT8 0x10DA +#define CL_UNSIGNED_INT16 0x10DB +#define CL_UNSIGNED_INT32 0x10DC +#define CL_HALF_FLOAT 0x10DD +#define CL_FLOAT 0x10DE + +// cl_mem_object_type +#define CL_MEM_OBJECT_BUFFER 0x10F0 +#define CL_MEM_OBJECT_IMAGE2D 0x10F1 +#define CL_MEM_OBJECT_IMAGE3D 0x10F2 + +// cl_mem_info +#define CL_MEM_TYPE 0x1100 +#define CL_MEM_FLAGS 0x1101 +#define CL_MEM_SIZE 0x1102 +#define CL_MEM_HOST_PTR 0x1103 +#define CL_MEM_MAP_COUNT 0x1104 +#define CL_MEM_REFERENCE_COUNT 0x1105 +#define CL_MEM_CONTEXT 0x1106 + +// cl_image_info +#define CL_IMAGE_FORMAT 0x1110 +#define CL_IMAGE_ELEMENT_SIZE 0x1111 +#define CL_IMAGE_ROW_PITCH 0x1112 +#define CL_IMAGE_SLICE_PITCH 0x1113 +#define CL_IMAGE_WIDTH 0x1114 +#define CL_IMAGE_HEIGHT 0x1115 +#define CL_IMAGE_DEPTH 0x1116 + +// cl_addressing_mode +#define CL_ADDRESS_NONE 0x1130 +#define CL_ADDRESS_CLAMP_TO_EDGE 0x1131 +#define CL_ADDRESS_CLAMP 0x1132 +#define CL_ADDRESS_REPEAT 0x1133 + +// cl_filter_mode +#define CL_FILTER_NEAREST 0x1140 +#define CL_FILTER_LINEAR 0x1141 + +// cl_sampler_info +#define CL_SAMPLER_REFERENCE_COUNT 0x1150 +#define CL_SAMPLER_CONTEXT 0x1151 +#define CL_SAMPLER_NORMALIZED_COORDS 0x1152 +#define CL_SAMPLER_ADDRESSING_MODE 0x1153 +#define CL_SAMPLER_FILTER_MODE 0x1154 + +// cl_map_flags - bitfield +#define CL_MAP_READ (1 << 0) +#define CL_MAP_WRITE (1 << 1) + +// cl_program_info +#define CL_PROGRAM_REFERENCE_COUNT 0x1160 +#define CL_PROGRAM_CONTEXT 0x1161 +#define CL_PROGRAM_NUM_DEVICES 0x1162 +#define CL_PROGRAM_DEVICES 0x1163 +#define CL_PROGRAM_SOURCE 0x1164 +#define CL_PROGRAM_BINARY_SIZES 0x1165 +#define CL_PROGRAM_BINARIES 0x1166 + +// cl_program_build_info +#define CL_PROGRAM_BUILD_STATUS 0x1181 +#define CL_PROGRAM_BUILD_OPTIONS 0x1182 +#define CL_PROGRAM_BUILD_LOG 0x1183 + +// cl_build_status +#define CL_BUILD_SUCCESS 0 +#define CL_BUILD_NONE -1 +#define CL_BUILD_ERROR -2 +#define CL_BUILD_IN_PROGRESS -3 + +// cl_kernel_info +#define CL_KERNEL_FUNCTION_NAME 0x1190 +#define CL_KERNEL_NUM_ARGS 0x1191 +#define CL_KERNEL_REFERENCE_COUNT 0x1192 +#define CL_KERNEL_CONTEXT 0x1193 +#define CL_KERNEL_PROGRAM 0x1194 + +// cl_kernel_work_group_info +#define CL_KERNEL_WORK_GROUP_SIZE 0x11B0 +#define CL_KERNEL_COMPILE_WORK_GROUP_SIZE 0x11B1 + +// cl_event_info +#define CL_EVENT_COMMAND_QUEUE 0x11D0 +#define CL_EVENT_COMMAND_TYPE 0x11D1 +#define CL_EVENT_REFERENCE_COUNT 0x11D2 +#define CL_EVENT_COMMAND_EXECUTION_STATUS 0x11D3 + +// cl_command_type +#define CL_COMMAND_NDRANGE_KERNEL 0x11F0 +#define CL_COMMAND_TASK 0x11F1 +#define CL_COMMAND_NATIVE_KERNEL 0x11F2 +#define CL_COMMAND_READ_BUFFER 0x11F3 +#define CL_COMMAND_WRITE_BUFFER 0x11F4 +#define CL_COMMAND_COPY_BUFFER 0x11F5 +#define CL_COMMAND_READ_IMAGE 0x11F6 +#define CL_COMMAND_WRITE_IMAGE 0x11F7 +#define CL_COMMAND_COPY_IMAGE 0x11F8 +#define CL_COMMAND_COPY_IMAGE_TO_BUFFER 0x11F9 +#define CL_COMMAND_COPY_BUFFER_TO_IMAGE 0x11FA +#define CL_COMMAND_MAP_BUFFER 0x11FB +#define CL_COMMAND_MAP_IMAGE 0x11FC +#define CL_COMMAND_UNMAP_MEM_OBJECT 0x11FD +#define CL_COMMAND_MARKER 0x11FE +#define CL_COMMAND_WAIT_FOR_EVENTS 0x11FF +#define CL_COMMAND_BARRIER 0x1200 +#define CL_COMMAND_ACQUIRE_GL_OBJECTS 0x1201 +#define CL_COMMAND_RELEASE_GL_OBJECTS 0x1202 + +// command execution status +#define CL_COMPLETE 0x0 +#define CL_RUNNING 0x1 +#define CL_SUBMITTED 0x2 +#define CL_QUEUED 0x3 + +// cl_profiling_info +#define CL_PROFILING_COMMAND_QUEUED 0x1280 +#define CL_PROFILING_COMMAND_SUBMIT 0x1281 +#define CL_PROFILING_COMMAND_START 0x1282 +#define CL_PROFILING_COMMAND_END 0x1283 + +/********************************************************************************************************/ + +// Platform API +extern CL_API_ENTRY cl_int CL_API_CALL +clGetPlatformInfo(cl_platform_info /* param_name */, + size_t /* param_value_size */, + void * /* param_value */, + size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0; + +// Device APIs +extern CL_API_ENTRY cl_int CL_API_CALL +clGetDeviceIDs(cl_device_type /* device_type */, + cl_uint /* num_entries */, + cl_device_id * /* devices */, + cl_uint * /* num_devices */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clGetDeviceInfo(cl_device_id /* device */, + cl_device_info /* param_name */, + size_t /* param_value_size */, + void * /* param_value */, + size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0; + +// Context APIs +typedef void (*logging_fn)(const char *, const void *, size_t, const void *); + +extern CL_API_ENTRY cl_context CL_API_CALL +clCreateContext(cl_context_properties * /* properties */, + cl_uint /* num_devices */, + const cl_device_id * /* devices */, + logging_fn /* pfn_notify */, + void * /* user_data */, + cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_context CL_API_CALL +clCreateContextFromType(cl_context_properties * /* properties */, + cl_device_type /* device_type */, + logging_fn /* pfn_notify */, + void * /* user_data */, + cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clRetainContext(cl_context /* context */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clReleaseContext(cl_context /* context */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clGetContextInfo(cl_context /* context */, + cl_context_info /* param_name */, + size_t /* param_value_size */, + void * /* param_value */, + size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0; + +// Command Queue APIs +extern CL_API_ENTRY cl_command_queue CL_API_CALL +clCreateCommandQueue(cl_context /* context */, + cl_device_id /* device */, + cl_command_queue_properties /* properties */, + cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clRetainCommandQueue(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clReleaseCommandQueue(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clGetCommandQueueInfo(cl_command_queue /* command_queue */, + cl_command_queue_info /* param_name */, + size_t /* param_value_size */, + void * /* param_value */, + size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clSetCommandQueueProperty(cl_command_queue /* command_queue */, + cl_command_queue_properties /* properties */, + cl_bool /* enable */, + cl_command_queue_properties * /* old_properties */) CL_API_SUFFIX__VERSION_1_0; + +// Memory Object APIs +extern CL_API_ENTRY cl_mem CL_API_CALL +clCreateBuffer(cl_context /* context */, + cl_mem_flags /* flags */, + size_t /* size */, + void * /* host_ptr */, + cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_mem CL_API_CALL +clCreateImage2D(cl_context /* context */, + cl_mem_flags /* flags */, + const cl_image_format * /* image_format */, + size_t /* image_width */, + size_t /* image_height */, + size_t /* image_row_pitch */, + void * /* host_ptr */, + cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_mem CL_API_CALL +clCreateImage3D(cl_context /* context */, + cl_mem_flags /* flags */, + const cl_image_format * /* image_format */, + size_t /* image_width */, + size_t /* image_height */, + size_t /* image_depth */, + size_t /* image_row_pitch */, + size_t /* image_slice_pitch */, + void * /* host_ptr */, + cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clRetainMemObject(cl_mem /* memobj */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clReleaseMemObject(cl_mem /* memobj */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clGetSupportedImageFormats(cl_context /* context */, + cl_mem_flags /* flags */, + cl_mem_object_type /* image_type */, + cl_uint /* num_entries */, + cl_image_format * /* image_formats */, + cl_uint * /* num_image_formats */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clGetMemObjectInfo(cl_mem /* memobj */, + cl_mem_info /* param_name */, + size_t /* param_value_size */, + void * /* param_value */, + size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clGetImageInfo(cl_mem /* image */, + cl_image_info /* param_name */, + size_t /* param_value_size */, + void * /* param_value */, + size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0; + +// Sampler APIs +extern CL_API_ENTRY cl_sampler CL_API_CALL +clCreateSampler(cl_context /* context */, + cl_bool /* normalized_coords */, + cl_addressing_mode /* addressing_mode */, + cl_filter_mode /* filter_mode */, + cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clRetainSampler(cl_sampler /* sampler */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clReleaseSampler(cl_sampler /* sampler */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clGetSamplerInfo(cl_sampler /* sampler */, + cl_sampler_info /* param_name */, + size_t /* param_value_size */, + void * /* param_value */, + size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0; + +// Program Object APIs +extern CL_API_ENTRY cl_program CL_API_CALL +clCreateProgramWithSource(cl_context /* context */, + cl_uint /* count */, + const char ** /* strings */, + const size_t * /* lengths */, + cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_program CL_API_CALL +clCreateProgramWithBinary(cl_context /* context */, + cl_uint /* num_devices */, + const cl_device_id * /* device_list */, + const size_t * /* lengths */, + const void ** /* binaries */, + cl_int * /* binary_status */, + cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clRetainProgram(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clReleaseProgram(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clBuildProgram(cl_program /* program */, + cl_uint /* num_devices */, + const cl_device_id * /* device_list */, + const char * /* options */, + void (*pfn_notify)(cl_program /* program */, void * /* user_data */), + void * /* user_data */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clUnloadCompiler(void) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clGetProgramInfo(cl_program /* program */, + cl_program_info /* param_name */, + size_t /* param_value_size */, + void * /* param_value */, + size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clGetProgramBuildInfo(cl_program /* program */, + cl_device_id /* device */, + cl_program_build_info /* param_name */, + size_t /* param_value_size */, + void * /* param_value */, + size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0; + +// Kernel Object APIs +extern CL_API_ENTRY cl_kernel CL_API_CALL +clCreateKernel(cl_program /* program */, + const char * /* kernel_name */, + cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clCreateKernelsInProgram(cl_program /* program */, + cl_uint /* num_kernels */, + cl_kernel * /* kernels */, + cl_uint * /* num_kernels_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clRetainKernel(cl_kernel /* kernel */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clReleaseKernel(cl_kernel /* kernel */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clSetKernelArg(cl_kernel /* kernel */, + cl_uint /* arg_index */, + size_t /* arg_size */, + const void * /* arg_value */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clGetKernelInfo(cl_kernel /* kernel */, + cl_kernel_info /* param_name */, + size_t /* param_value_size */, + void * /* param_value */, + size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clGetKernelWorkGroupInfo(cl_kernel /* kernel */, + cl_device_id /* device */, + cl_kernel_work_group_info /* param_name */, + size_t /* param_value_size */, + void * /* param_value */, + size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0; + +// Event Object APIs +extern CL_API_ENTRY cl_int CL_API_CALL +clWaitForEvents(cl_uint /* num_events */, + const cl_event * /* event_list */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clGetEventInfo(cl_event /* event */, + cl_event_info /* param_name */, + size_t /* param_value_size */, + void * /* param_value */, + size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clRetainEvent(cl_event /* event */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clReleaseEvent(cl_event /* event */) CL_API_SUFFIX__VERSION_1_0; + +// Profiling APIs +extern CL_API_ENTRY cl_int CL_API_CALL +clGetEventProfilingInfo(cl_event /* event */, + cl_profiling_info /* param_name */, + size_t /* param_value_size */, + void * /* param_value */, + size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0; + +// Flush and Finish APIs +extern CL_API_ENTRY cl_int CL_API_CALL +clFlush(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clFinish(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0; + +// Enqueued Commands APIs +extern CL_API_ENTRY cl_int CL_API_CALL +clEnqueueReadBuffer(cl_command_queue /* command_queue */, + cl_mem /* buffer */, + cl_bool /* blocking_read */, + size_t /* offset */, + size_t /* cb */, + void * /* ptr */, + cl_uint /* num_events_in_wait_list */, + const cl_event * /* event_wait_list */, + cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clEnqueueWriteBuffer(cl_command_queue /* command_queue */, + cl_mem /* buffer */, + cl_bool /* blocking_write */, + size_t /* offset */, + size_t /* cb */, + const void * /* ptr */, + cl_uint /* num_events_in_wait_list */, + const cl_event * /* event_wait_list */, + cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clEnqueueCopyBuffer(cl_command_queue /* command_queue */, + cl_mem /* src_buffer */, + cl_mem /* dst_buffer */, + size_t /* src_offset */, + size_t /* dst_offset */, + size_t /* cb */, + cl_uint /* num_events_in_wait_list */, + const cl_event * /* event_wait_list */, + cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clEnqueueReadImage(cl_command_queue /* command_queue */, + cl_mem /* image */, + cl_bool /* blocking_read */, + const size_t * /* origin[3] */, + const size_t * /* region[3] */, + size_t /* row_pitch */, + size_t /* slice_pitch */, + void * /* ptr */, + cl_uint /* num_events_in_wait_list */, + const cl_event * /* event_wait_list */, + cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clEnqueueWriteImage(cl_command_queue /* command_queue */, + cl_mem /* image */, + cl_bool /* blocking_write */, + const size_t * /* origin[3] */, + const size_t * /* region[3] */, + size_t /* input_row_pitch */, + size_t /* input_slice_pitch */, + const void * /* ptr */, + cl_uint /* num_events_in_wait_list */, + const cl_event * /* event_wait_list */, + cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clEnqueueCopyImage(cl_command_queue /* command_queue */, + cl_mem /* src_image */, + cl_mem /* dst_image */, + const size_t * /* src_origin[3] */, + const size_t * /* dst_origin[3] */, + const size_t * /* region[3] */, + cl_uint /* num_events_in_wait_list */, + const cl_event * /* event_wait_list */, + cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clEnqueueCopyImageToBuffer(cl_command_queue /* command_queue */, + cl_mem /* src_image */, + cl_mem /* dst_buffer */, + const size_t * /* src_origin[3] */, + const size_t * /* region[3] */, + size_t /* dst_offset */, + cl_uint /* num_events_in_wait_list */, + const cl_event * /* event_wait_list */, + cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clEnqueueCopyBufferToImage(cl_command_queue /* command_queue */, + cl_mem /* src_buffer */, + cl_mem /* dst_image */, + size_t /* src_offset */, + const size_t * /* dst_origin[3] */, + const size_t * /* region[3] */, + cl_uint /* num_events_in_wait_list */, + const cl_event * /* event_wait_list */, + cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY void * CL_API_CALL +clEnqueueMapBuffer(cl_command_queue /* command_queue */, + cl_mem /* buffer */, + cl_bool /* blocking_map */, + cl_map_flags /* map_flags */, + size_t /* offset */, + size_t /* cb */, + cl_uint /* num_events_in_wait_list */, + const cl_event * /* event_wait_list */, + cl_event * /* event */, + cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY void * CL_API_CALL +clEnqueueMapImage(cl_command_queue /* command_queue */, + cl_mem /* image */, + cl_bool /* blocking_map */, + cl_map_flags /* map_flags */, + const size_t * /* origin[3] */, + const size_t * /* region[3] */, + size_t * /* image_row_pitch */, + size_t * /* image_slice_pitch */, + cl_uint /* num_events_in_wait_list */, + const cl_event * /* event_wait_list */, + cl_event * /* event */, + cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clEnqueueUnmapMemObject(cl_command_queue /* command_queue */, + cl_mem /* memobj */, + void * /* mapped_ptr */, + cl_uint /* num_events_in_wait_list */, + const cl_event * /* event_wait_list */, + cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clEnqueueNDRangeKernel(cl_command_queue /* command_queue */, + cl_kernel /* kernel */, + cl_uint /* work_dim */, + const size_t * /* global_work_offset */, + const size_t * /* global_work_size */, + const size_t * /* local_work_size */, + cl_uint /* num_events_in_wait_list */, + const cl_event * /* event_wait_list */, + cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clEnqueueTask(cl_command_queue /* command_queue */, + cl_kernel /* kernel */, + cl_uint /* num_events_in_wait_list */, + const cl_event * /* event_wait_list */, + cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clEnqueueNativeKernel(cl_command_queue /* command_queue */, + void (*user_func)(void *), + void * /* args */, + size_t /* cb_args */, + cl_uint /* num_mem_objects */, + const cl_mem * /* mem_list */, + const void ** /* args_mem_loc */, + cl_uint /* num_events_in_wait_list */, + const cl_event * /* event_wait_list */, + cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clEnqueueMarker(cl_command_queue /* command_queue */, + cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clEnqueueWaitForEvents(cl_command_queue /* command_queue */, + cl_uint /* num_events */, + const cl_event * /* event_list */) CL_API_SUFFIX__VERSION_1_0; + +extern CL_API_ENTRY cl_int CL_API_CALL +clEnqueueBarrier(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0; + +#ifdef __cplusplus +} +#endif + +#endif // __OPENCL_CL_H + diff --git a/src/cl/CL/cl_platform.h b/src/cl/CL/cl_platform.h new file mode 100644 index 000000000..72b9768fc --- /dev/null +++ b/src/cl/CL/cl_platform.h @@ -0,0 +1,167 @@ +/********************************************************************************** + * Copyright (c) 2008-2009 The Khronos Group Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and/or associated documentation files (the + * "Materials"), to deal in the Materials without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Materials, and to + * permit persons to whom the Materials are furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Materials. + * + * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. + **********************************************************************************/ + +#ifndef __CL_PLATFORM_H +#define __CL_PLATFORM_H + +#ifdef __APPLE__ + /* Contains #defines for AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER below */ + #include +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#define CL_API_ENTRY +#define CL_API_CALL +#ifdef __APPLE__ +#define CL_API_SUFFIX__VERSION_1_0 AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER +#else +#define CL_API_SUFFIX__VERSION_1_0 +#endif + +#include +#include + +/* scalar types */ +typedef int8_t cl_char; +typedef uint8_t cl_uchar; +typedef int16_t cl_short __attribute__((aligned(2))); +typedef uint16_t cl_ushort __attribute__((aligned(2))); +typedef int32_t cl_int __attribute__((aligned(4))); +typedef uint32_t cl_uint __attribute__((aligned(4))); +typedef int64_t cl_long __attribute__((aligned(8))); +typedef uint64_t cl_ulong __attribute__((aligned(8))); + +typedef uint16_t cl_half __attribute__((aligned(2))); +typedef float cl_float __attribute__((aligned(4))); +typedef double cl_double __attribute__((aligned(8))); + +/* and a few goodies to go with them */ +#define CL_CHAR_BIT 8 +#define CL_SCHAR_MAX 127 +#define CL_SCHAR_MIN (-127-1) +#define CL_CHAR_MAX CL_SCHAR_MAX +#define CL_CHAR_MIN CL_SCHAR_MIN +#define CL_UCHAR_MAX 255 +#define CL_SHRT_MAX 32767 +#define CL_SHRT_MIN (-32767-1) +#define CL_USHRT_MAX 65535 +#define CL_INT_MAX 2147483647 +#define CL_INT_MIN (-2147483647-1) +#define CL_UINT_MAX 0xffffffffU +#define CL_LONG_MAX ((cl_long) 0x7FFFFFFFFFFFFFFFLL) +#define CL_LONG_MIN ((cl_long) -0x7FFFFFFFFFFFFFFFLL - 1LL) +#define CL_ULONG_MAX ((cl_ulong) 0xFFFFFFFFFFFFFFFFULL) + +#define CL_FLT_DIG 6 +#define CL_FLT_MANT_DIG 24 +#define CL_FLT_MAX_10_EXP +38 +#define CL_FLT_MAX_EXP +128 +#define CL_FLT_MIN_10_EXP -37 +#define CL_FLT_MIN_EXP -125 +#define CL_FLT_RADIX 2 +#define CL_FLT_MAX 0x1.fffffep127f +#define CL_FLT_MIN 0x1.0p-126f +#define CL_FLT_EPSILON 0x1.0p-23f + +#define CL_DBL_DIG 15 +#define CL_DBL_MANT_DIG 53 +#define CL_DBL_MAX_10_EXP +308 +#define CL_DBL_MAX_EXP +1024 +#define CL_DBL_MIN_10_EXP -307 +#define CL_DBL_MIN_EXP -1021 +#define CL_DBL_RADIX 2 +#define CL_DBL_MAX 0x1.fffffffffffffp1023 +#define CL_DBL_MIN 0x1.0p-1022 +#define CL_DBL_EPSILON 0x1.0p-52 + +/* + * Vector types + * + * Note: OpenCL requires that all types be naturally aligned. + * This means that vector types must be naturally aligned. + * For example, a vector of four floats must be aligned to + * a 16 byte boundary (calculated as 4 * the natural 4-byte + * alignment of the float). The alignment qualifiers here + * will only function properly if your compiler supports them + * and if you don't actively work to defeat them. For example, + * in order for a cl_float4 to be 16 byte aligned in a struct, + * the start of the struct must itself be 16-byte aligned. + * + * Maintaining proper alignment is the user's responsibility. + */ +typedef int8_t cl_char2[2] __attribute__((aligned(2))); +typedef int8_t cl_char4[4] __attribute__((aligned(4))); +typedef int8_t cl_char8[8] __attribute__((aligned(8))); +typedef int8_t cl_char16[16] __attribute__((aligned(16))); +typedef uint8_t cl_uchar2[2] __attribute__((aligned(2))); +typedef uint8_t cl_uchar4[4] __attribute__((aligned(4))); +typedef uint8_t cl_uchar8[8] __attribute__((aligned(8))); +typedef uint8_t cl_uchar16[16] __attribute__((aligned(16))); + +typedef int16_t cl_short2[2] __attribute__((aligned(4))); +typedef int16_t cl_short4[4] __attribute__((aligned(8))); +typedef int16_t cl_short8[8] __attribute__((aligned(16))); +typedef int16_t cl_short16[16] __attribute__((aligned(32))); +typedef uint16_t cl_ushort2[2] __attribute__((aligned(4))); +typedef uint16_t cl_ushort4[4] __attribute__((aligned(8))); +typedef uint16_t cl_ushort8[8] __attribute__((aligned(16))); +typedef uint16_t cl_ushort16[16] __attribute__((aligned(32))); + +typedef int32_t cl_int2[2] __attribute__((aligned(8))); +typedef int32_t cl_int4[4] __attribute__((aligned(16))); +typedef int32_t cl_int8[8] __attribute__((aligned(32))); +typedef int32_t cl_int16[16] __attribute__((aligned(64))); +typedef uint32_t cl_uint2[2] __attribute__((aligned(8))); +typedef uint32_t cl_uint4[4] __attribute__((aligned(16))); +typedef uint32_t cl_uint8[8] __attribute__((aligned(32))); +typedef uint32_t cl_uint16[16] __attribute__((aligned(64))); + +typedef int64_t cl_long2[2] __attribute__((aligned(16))); +typedef int64_t cl_long4[4] __attribute__((aligned(32))); +typedef int64_t cl_long8[8] __attribute__((aligned(64))); +typedef int64_t cl_long16[16] __attribute__((aligned(128))); +typedef uint64_t cl_ulong2[2] __attribute__((aligned(16))); +typedef uint64_t cl_ulong4[4] __attribute__((aligned(32))); +typedef uint64_t cl_ulong8[8] __attribute__((aligned(64))); +typedef uint64_t cl_ulong16[16] __attribute__((aligned(128))); + +typedef float cl_float2[2] __attribute__((aligned(8))); +typedef float cl_float4[4] __attribute__((aligned(16))); +typedef float cl_float8[8] __attribute__((aligned(32))); +typedef float cl_float16[16] __attribute__((aligned(64))); + +typedef double cl_double2[2] __attribute__((aligned(16))); +typedef double cl_double4[4] __attribute__((aligned(32))); +typedef double cl_double8[8] __attribute__((aligned(64))); +typedef double cl_double16[16] __attribute__((aligned(128))); + +/* There are no vector types for half */ + +#ifdef __cplusplus +} +#endif + +#endif // __CL_PLATFORM_H diff --git a/src/cpp/cl.hpp b/src/cpp/cl.hpp new file mode 100644 index 000000000..62baf8494 --- /dev/null +++ b/src/cpp/cl.hpp @@ -0,0 +1,111 @@ +#include +#include + + + + +#ifndef _AFJHAYYTA_PYOPENCL_HEADER_SEEN_CL_HPP +#define _AFJHAYYTA_PYOPENCL_HEADER_SEEN_CL_HPP + + + + +namespace cl +{ + class error : public std::runtime_error + { + private: + const char *m_routine; + cl_int m_code; + + private: + static std::string make_message(const char *rout, cl_int c, const char *msg) + { + std::string result = rout; + result += " failed: "; + result += cl_error_to_str(c); + if (msg) + { + result += " - "; + result += msg; + } + return result; + } + + public: + error(const char *rout, cl_int c, const char *msg=0) + : std::runtime_error(make_message(rout, c, msg)), + m_routine(rout), m_code(c) + { } + + const char *routine() const + { + return m_routine; + } + + cl_int code() const + { + return m_code; + } + + static const char *cl_error_to_str(cl_int e) + { + switch (e) + { + case CL_SUCCESS: return "success"; + case CL_DEVICE_NOT_FOUND: return "device not found"; + case CL_DEVICE_NOT_AVAILABLE: return "device not available"; + case CL_DEVICE_COMPILER_NOT_AVAILABLE: return "device compiler not available"; + case CL_MEM_OBJECT_ALLOCATION_FAILURE: return "mem object allocation failure"; + case CL_OUT_OF_RESOURCES: return "out of resources"; + case CL_OUT_OF_HOST_MEMORY: return "out of host memory"; + case CL_PROFILING_INFO_NOT_AVAILABLE: return "profiling info not available"; + case CL_MEM_COPY_OVERLAP: return "mem copy overlap"; + case CL_IMAGE_FORMAT_MISMATCH: return "image format mismatch"; + case CL_IMAGE_FORMAT_NOT_SUPPORTED: return "image format not supported"; + + case CL_INVALID_VALUE: return "invalid value"; + case CL_INVALID_DEVICE_TYPE: return "invalid device type"; + case CL_INVALID_DEVICE_LIST: return "invalid device list"; + case CL_INVALID_DEVICE: return "invalid device"; + case CL_INVALID_CONTEXT: return "invalid context"; + case CL_INVALID_QUEUE_PROPERTIES: return "invalid queue properties"; + case CL_INVALID_COMMAND_QUEUE: return "invalid command queue"; + case CL_INVALID_HOST_PTR: return "invalid host ptr"; + case CL_INVALID_MEM_OBJECT: return "invalid mem object"; + case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: return "invalid image format descriptor"; + case CL_INVALID_IMAGE_SIZE: return "invalid image size"; + case CL_INVALID_SAMPLER: return "invalid sampler"; + case CL_INVALID_BINARY: return "invalid binary"; + case CL_INVALID_BUILD_OPTIONS: return "invalid build options"; + case CL_INVALID_PROGRAM: return "invalid program"; + case CL_INVALID_PROGRAM_EXECUTABLE: return "invalid program executable"; + case CL_INVALID_KERNEL_NAME: return "invalid kernel name"; + case CL_INVALID_KERNEL_DEFINITION: return "invalid kernel definition"; + case CL_INVALID_KERNEL: return "invalid kernel"; + case CL_INVALID_ARG_INDEX: return "invalid arg index"; + case CL_INVALID_ARG_VALUE: return "invalid arg value"; + case CL_INVALID_ARG_SIZE: return "invalid arg size"; + case CL_INVALID_KERNEL_ARGS: return "invalid kernel args"; + case CL_INVALID_WORK_DIMENSION: return "invalid work dimension"; + case CL_INVALID_WORK_GROUP_SIZE: return "invalid work group size"; + case CL_INVALID_WORK_ITEM_SIZE: return "invalid work item size"; + case CL_INVALID_GLOBAL_OFFSET: return "invalid global offset"; + case CL_INVALID_EVENT_WAIT_LIST: return "invalid event wait list"; + case CL_INVALID_EVENT: return "invalid event"; + case CL_INVALID_OPERATION: return "invalid operation"; + case CL_INVALID_GL_OBJECT: return "invalid gl object"; + case CL_INVALID_BUFFER_SIZE: return "invalid buffer size"; + case CL_INVALID_MIP_LEVEL: return "invalid mip level"; + + default: return "invalid error code"; + } + } + }; +} + + + + + +#endif diff --git a/src/wrapper/wrap_cl.cpp b/src/wrapper/wrap_cl.cpp new file mode 100644 index 000000000..3ca0197e3 --- /dev/null +++ b/src/wrapper/wrap_cl.cpp @@ -0,0 +1,57 @@ +#include +#include "wrap_helpers.hpp" + + + + +namespace py = boost::python; + + + + +namespace +{ + py::handle<> + CLError, + CLMemoryError, + CLLogicError, + CLRuntimeError; + + + + + void translate_cl_error(const cl::error &err) + { + if (err.code() == CL_MEM_OBJECT_ALLOCATION_FAILURE) + PyErr_SetString(CLMemoryError.get(), err.what()); + else if (err.code() <= CL_INVALID_VALUE) + PyErr_SetString(CLLogicError.get(), err.what()); + else if (err.code() > CL_INVALID_VALUE && err.code() < CL_SUCCESS) + PyErr_SetString(CLRuntimeError.get(), err.what()); + else + PyErr_SetString(CLError.get(), err.what()); + } +} + + + + +BOOST_PYTHON_MODULE(_cl) +{ +#define DECLARE_EXC(NAME, BASE) \ + CL##NAME = py::handle<>(PyErr_NewException("pyopencl._cl." #NAME, BASE, NULL)); \ + py::scope().attr(#NAME) = CL##NAME; + + { + DECLARE_EXC(Error, NULL); + py::tuple memerr_bases = py::make_tuple( + CLError, + py::handle<>(py::borrowed(PyExc_MemoryError))); + DECLARE_EXC(MemoryError, memerr_bases.ptr()); + DECLARE_EXC(LogicError, CLLogicError.get()); + DECLARE_EXC(RuntimeError, CLError.get()); + + py::register_exception_translator(translate_cl_error); + } + +} diff --git a/src/wrapper/wrap_helpers.hpp b/src/wrapper/wrap_helpers.hpp new file mode 100644 index 000000000..f521c42f9 --- /dev/null +++ b/src/wrapper/wrap_helpers.hpp @@ -0,0 +1,56 @@ +#ifndef PYCUDA_WRAP_HELPERS_HEADER_SEEN +#define PYCUDA_WRAP_HELPERS_HEADER_SEEN + + + + +#include + + + + +#define PYTHON_ERROR(TYPE, REASON) \ +{ \ + PyErr_SetString(PyExc_##TYPE, REASON); \ + throw boost::python::error_already_set(); \ +} + +#define ENUM_VALUE(NAME) \ + value(#NAME, NAME) + +#define DEF_SIMPLE_METHOD(NAME) \ + def(#NAME, &cl::NAME) + +#define DEF_SIMPLE_METHOD_WITH_ARGS(NAME, ARGS) \ + def(#NAME, &cl::NAME, boost::python::args ARGS) + +#define DEF_SIMPLE_FUNCTION(NAME) \ + boost::python::def(#NAME, &NAME) + +#define DEF_SIMPLE_FUNCTION_WITH_ARGS(NAME, ARGS) \ + boost::python::def(#NAME, &NAME, boost::python::args ARGS) + +#define DEF_SIMPLE_RO_MEMBER(NAME) \ + def_readonly(#NAME, &cl::m_##NAME) + +#define DEF_SIMPLE_RW_MEMBER(NAME) \ + def_readwrite(#NAME, &cl::m_##NAME) + + + + + +namespace +{ + template + inline boost::python::handle<> handle_from_new_ptr(T *ptr) + { + return boost::python::handle<>( + typename boost::python::manage_new_object::apply::type()(ptr)); + } +} + + + + +#endif