Example #1
0
    def build(self, options=[], devices=None, cache_dir=None):
        if isinstance(options, str):
            options = [options]

        options = options + ["-I", _find_pyopencl_include_path()]

        import os
        forced_options = os.environ.get("PYOPENCL_BUILD_OPTIONS")
        if forced_options:
            options = options + forced_options.split()

        if os.environ.get("PYOPENCL_NO_CACHE") and self._prg is None:
            self._prg = _cl._Program(self._context, self._source)

        if self._prg is not None:
            # uncached

            self._build_and_catch_errors(
                    lambda: self._prg.build(" ".join(options), devices),
                    options=options)

        else:
            # cached

            from pyopencl.cache import create_built_program_from_source_cached
            self._prg = self._build_and_catch_errors(
                    lambda: create_built_program_from_source_cached(
                        self._context, self._source, options, devices,
                        cache_dir=cache_dir),
                    options=options, source=self._source)

            del self._context

        return self
Example #2
0
    def build(self, options=[], devices=None, cache_dir=None):
        options_bytes, include_path = self._process_build_options(
                self._context, options)

        if cache_dir is None:
            cache_dir = getattr(self._context, 'cache_dir', None)

        import os
        if os.environ.get("PYOPENCL_NO_CACHE") and self._prg is None:
            self._prg = _cl._Program(self._context, self._source)

        if self._prg is not None:
            # uncached

            self._build_and_catch_errors(
                    lambda: self._prg.build(options_bytes, devices),
                    options_bytes=options_bytes)

        else:
            # cached

            from pyopencl.cache import create_built_program_from_source_cached
            self._prg = self._build_and_catch_errors(
                    lambda: create_built_program_from_source_cached(
                        self._context, self._source, options_bytes, devices,
                        cache_dir=cache_dir, include_path=include_path),
                    options_bytes=options_bytes, source=self._source)

            del self._context

        return self
Example #3
0
    def build(self, options=[], devices=None, cache_dir=None):
        if isinstance(options, str):
            options = [options]

        options = options + ["-I", _find_pyopencl_include_path()]

        import os
        if os.environ.get("PYOPENCL_NO_CACHE") and self._prg is None:
            self._prg = _cl._Program(self._context, self._source)

        if self._prg is not None:
            if isinstance(options, list):
                options = " ".join(options)

            self._prg._build(options, devices)
        else:
            from pyopencl.cache import create_built_program_from_source_cached

            err = None
            try:
                self._prg = create_built_program_from_source_cached(
                    self._context,
                    self._source,
                    options,
                    devices,
                    cache_dir=cache_dir)
            except _cl.RuntimeError, e:
                from pytools import Record

                class ErrorRecord(Record):
                    pass

                from tempfile import NamedTemporaryFile
                srcfile = NamedTemporaryFile(mode="wt",
                                             delete=False,
                                             suffix=".cl")
                try:
                    srcfile.write(self._source)
                finally:
                    srcfile.close()

                what = e.what + "\n(source saved as %s)" % srcfile.name
                code = e.code
                routine = e.routine

                err = _cl.RuntimeError(
                    ErrorRecord(what=lambda: what,
                                code=lambda: code,
                                routine=lambda: routine))

            if err is not None:
                # Python 3.2 outputs the whole list of currently active exceptions
                # This serves to remove one (redundant) level from that nesting.
                raise err

            del self._context
            del self._source
Example #4
0
    def build(self, options=[], devices=None, cache_dir=None):
        if self._prg is not None:
            self._prg._build(options, devices)
        else:
            from pyopencl.cache import create_built_program_from_source_cached
            self._prg = create_built_program_from_source_cached(
                    self._context, self._source, options, devices,
                    cache_dir=cache_dir)

        return self
Example #5
0
    def build(self, options=[], devices=None, cache_dir=None):
        if isinstance(options, str):
            options = [options]

        options = options + ["-I", _find_pyopencl_include_path()]

        import os
        if os.environ.get("PYOPENCL_NO_CACHE") and self._prg is None:
            self._prg = _cl._Program(self._context, self._source)

        if self._prg is not None:
            if isinstance(options, list):
                options = " ".join(options)

            self._prg._build(options, devices)
        else:
            from pyopencl.cache import create_built_program_from_source_cached

            err = None
            try:
                self._prg = create_built_program_from_source_cached(
                        self._context, self._source, options, devices,
                        cache_dir=cache_dir)
            except _cl.RuntimeError, e:
                from pytools import Record
                class ErrorRecord(Record):
                    pass

                from tempfile import NamedTemporaryFile
                srcfile = NamedTemporaryFile(mode="wt", delete=False, suffix=".cl")
                try:
                    srcfile.write(self._source)
                finally:
                    srcfile.close()

                what = e.what + "\n(source saved as %s)" % srcfile.name
                code = e.code
                routine = e.routine

                err = _cl.RuntimeError(
                        ErrorRecord(
                            what=lambda : what,
                            code=lambda : code,
                            routine=lambda : routine))

            if err is not None:
                # Python 3.2 outputs the whole list of currently active exceptions
                # This serves to remove one (redundant) level from that nesting.
                raise err

            del self._context
            del self._source
Example #6
0
    def build(self, options=[], devices=None, cache_dir=None):
        options_bytes, include_path = self._process_build_options(
                self._context, options)

        if cache_dir is None:
            cache_dir = getattr(self._context, 'cache_dir', None)

        import os
        build_descr = None

        if os.environ.get("PYOPENCL_NO_CACHE") and self._prg is None:
            build_descr = "uncached source build (cache disabled by user)"
            self._prg = _cl._Program(self._context, self._source)

        from time import time
        start_time = time()
        was_cached = False

        if self._prg is not None:
            # uncached

            if build_descr is None:
                build_descr = "uncached source build"

            self._build_and_catch_errors(
                    lambda: self._prg.build(options_bytes, devices),
                    options_bytes=options_bytes)

        else:
            # cached

            from pyopencl.cache import create_built_program_from_source_cached
            self._prg, was_cached = self._build_and_catch_errors(
                    lambda: create_built_program_from_source_cached(
                        self._context, self._source, options_bytes, devices,
                        cache_dir=cache_dir, include_path=include_path),
                    options_bytes=options_bytes, source=self._source)

            if was_cached:
                build_descr = "cache retrieval"
            else:
                build_descr = "source build resulting from a binary cache miss"

            del self._context

        end_time = time()

        self._build_duration_info = (build_descr, was_cached, end_time-start_time)

        return self
Example #7
0
    def build(self, options=[], devices=None, cache_dir=None):
        if isinstance(options, str):
            options = [options]

        options = options + ["-I", _find_pyopencl_include_path()]

        if self._prg is not None:
            if isinstance(options, list):
                options = " ".join(options)

            self._prg._build(options, devices)
        else:
            from pyopencl.cache import create_built_program_from_source_cached
            self._prg = create_built_program_from_source_cached(
                    self._context, self._source, options, devices,
                    cache_dir=cache_dir)

        return self
Example #8
0
    def build(self, options=[], devices=None, cache_dir=None):
        if isinstance(options, str):
            options = [options]

        options = options + ["-I", _find_pyopencl_include_path()]

        import os
        forced_options = os.environ.get("PYOPENCL_BUILD_OPTIONS")
        if forced_options:
            options = options + forced_options.split()

        do_del_source = False
        if os.environ.get("PYOPENCL_NO_CACHE") and self._prg is None:
            self._prg = _cl._Program(self._context, self._source)

            do_del_source = True

        if self._prg is not None:
            # uncached

            self._build_and_catch_errors(
                lambda: self._prg.build(" ".join(options), devices),
                options=options)

        else:
            # cached

            from pyopencl.cache import create_built_program_from_source_cached
            self._prg = self._build_and_catch_errors(
                lambda: create_built_program_from_source_cached(self._context,
                                                                self._source,
                                                                options,
                                                                devices,
                                                                cache_dir=
                                                                cache_dir),
                options=options)

            del self._context
            do_del_source = True

        if do_del_source:
            del self._source

        return self
Example #9
0
    def build(self, options=[], devices=None, cache_dir=None):
        if isinstance(options, str):
            options = [options]
        elif isinstance(options, unicode):
            options = [options.encode("utf8")]

        options = (
            options
            + _DEFAULT_BUILD_OPTIONS
            + _DEFAULT_INCLUDE_OPTIONS
            + _PLAT_BUILD_OPTIONS.get(self._context.devices[0].platform.name, [])
        )

        import os

        forced_options = os.environ.get("PYOPENCL_BUILD_OPTIONS")
        if forced_options:
            options = options + forced_options.split()

        if os.environ.get("PYOPENCL_NO_CACHE") and self._prg is None:
            self._prg = _cl._Program(self._context, self._source)

        if self._prg is not None:
            # uncached

            self._build_and_catch_errors(lambda: self._prg.build(" ".join(options), devices), options=options)

        else:
            # cached

            from pyopencl.cache import create_built_program_from_source_cached

            self._prg = self._build_and_catch_errors(
                lambda: create_built_program_from_source_cached(
                    self._context, self._source, options, devices, cache_dir=cache_dir
                ),
                options=options,
                source=self._source,
            )

            del self._context

        return self
Example #10
0
    def build(self, options=[], devices=None, cache_dir=None):
        if isinstance(options, str):
            options = [options]
        elif isinstance(options, unicode):
            options = [options.encode("utf8")]

        options = (options + _DEFAULT_BUILD_OPTIONS +
                   _DEFAULT_INCLUDE_OPTIONS + _PLAT_BUILD_OPTIONS.get(
                       self._context.devices[0].platform.name, []))

        import os
        forced_options = os.environ.get("PYOPENCL_BUILD_OPTIONS")
        if forced_options:
            options = options + forced_options.split()

        if os.environ.get("PYOPENCL_NO_CACHE") and self._prg is None:
            self._prg = _cl._Program(self._context, self._source)

        if self._prg is not None:
            # uncached

            self._build_and_catch_errors(
                lambda: self._prg.build(" ".join(options), devices),
                options=options)

        else:
            # cached

            from pyopencl.cache import create_built_program_from_source_cached
            self._prg = self._build_and_catch_errors(
                lambda: create_built_program_from_source_cached(self._context,
                                                                self._source,
                                                                options,
                                                                devices,
                                                                cache_dir=
                                                                cache_dir),
                options=options,
                source=self._source)

            del self._context

        return self
Example #11
0
    def build(self, options=[], devices=None, cache_dir=None):
        if isinstance(options, str):
            options = [options]

        options = options + ["-I", _find_pyopencl_include_path()]

        if self._prg is not None:
            if isinstance(options, list):
                options = " ".join(options)

            self._prg._build(options, devices)
        else:
            from pyopencl.cache import create_built_program_from_source_cached
            self._prg = create_built_program_from_source_cached(
                self._context,
                self._source,
                options,
                devices,
                cache_dir=cache_dir)

        return self