#!/usr/bin/env python # coding: utf-8 # # Project: Azimuthal integration # https://github.com/silx-kit/pyFAI # # Copyright (C) 2015-2019 European Synchrotron Radiation Facility, Grenoble, France # # Principal author: Jérôme Kieffer (Jerome.Kieffer@ESRF.eu) # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # 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 Software. # # THE SOFTWARE IS 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 SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. "test suite for OpenCL code" from __future__ import absolute_import, division, print_function __author__ = "Jérôme Kieffer" __contact__ = "Jerome.Kieffer@ESRF.eu" __license__ = "MIT" __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" __date__ = "01/08/2019" import unittest import numpy import logging import platform logger = logging.getLogger(__name__) try: import pyopencl except ImportError as error: logger.warning("OpenCL module (pyopencl) is not present, skip tests. %s.", error) pyopencl = None from .. import ocl if ocl is not None: from ..utils import read_cl_file from .. import pyopencl import pyopencl.array from ...test.utils import test_options class TestKahan(unittest.TestCase): """ Test the kernels for compensated math in OpenCL """ @classmethod def setUpClass(cls): if not test_options.WITH_OPENCL_TEST: raise unittest.SkipTest("User request to skip OpenCL tests") if pyopencl is None or ocl is None: raise unittest.SkipTest("OpenCL module (pyopencl) is not present or no device available") cls.ctx = ocl.create_context(devicetype="GPU") cls.queue = pyopencl.CommandQueue(cls.ctx, properties=pyopencl.command_queue_properties.PROFILING_ENABLE) # this is running 32 bits OpenCL woth POCL if (platform.machine() in ("i386", "i686", "x86_64") and (tuple.__itemsize__ == 4) and cls.ctx.devices[0].platform.name == 'Portable Computing Language'): cls.args = "-DX87_VOLATILE=volatile" else: cls.args = "" @classmethod def tearDownClass(cls): cls.queue = None cls.ctx = None @staticmethod def dummy_sum(ary, dtype=None): "perform the actual sum in a dummy way " if dtype is None: dtype = ary.dtype.type sum_ = dtype(0) for i in ary: sum_ += i return sum_ def test_kahan(self): # simple test N = 26 data = (1 << (N - 1 - numpy.arange(N))).astype(numpy.float32) ref64 = numpy.sum(data, dtype=numpy.float64) ref32 = self.dummy_sum(data) if (ref64 == ref32): logger.warning("Kahan: invalid tests as float32 provides the same result as float64") # Dummy kernel to evaluate src = """ kernel void summation(global float* data, int size, global float* result) { float2 acc = (float2)(0.0f, 0.0f); for (int i=0; i