1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Project: Fast Azimuthal Integration
# https://github.com/pyFAI/pyFAI
#
# Copyright (C) European Synchrotron Radiation Facility, Grenoble, France
#
# Principal author: Jérôme Kieffer (Jerome.Kieffer@ESRF.eu)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"test suite for OpenCL code"
__author__ = "Jérôme Kieffer"
__contact__ = "Jerome.Kieffer@ESRF.eu"
__license__ = "GPLv3+"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "22/10/2014"
import unittest
import os
import time
import sys
import fabio
import gc
from utilstest import UtilsTest, Rwp, getLogger
logger = getLogger(__file__)
try:
import pyopencl
except ImportError as error:
logger.warning("OpenCL module (pyopencl) is not present, skip tests. %s." % error)
skip = True
else:
skip = False
pyFAI = sys.modules["pyFAI"]
from pyFAI.opencl import ocl
if ocl is None:
skip = True
class test_mask(unittest.TestCase):
tmp_dir = os.environ.get("PYFAI_TEMPDIR", os.path.join(os.path.dirname(os.path.abspath(__file__)), "tmp"))
N = 1000
def setUp(self):
self.datasets = [{"img": UtilsTest.getimage("1883/Pilatus1M.edf"),
"poni": UtilsTest.getimage("1893/Pilatus1M.poni"),
"spline": None},
{"img": UtilsTest.getimage("1882/halfccd.edf"),
"poni": UtilsTest.getimage("1895/halfccd.poni"),
"spline": UtilsTest.getimage("1461/halfccd.spline")},
{"img": UtilsTest.getimage("1881/Frelon2k.edf"),
"poni": UtilsTest.getimage("1896/Frelon2k.poni"),
"spline": UtilsTest.getimage("1900/frelon.spline")},
{"img": UtilsTest.getimage("1884/Pilatus6M.cbf"),
"poni": UtilsTest.getimage("1897/Pilatus6M.poni"),
"spline": None},
]
for ds in self.datasets:
if ds["spline"] is not None:
data = open(ds["poni"], "r").read()
# spline = os.path.basename(ds["spline"])
with open(ds["poni"]) as f:
data = []
for line in f:
if line.startswith("SplineFile:"):
data.append("SplineFile: " + ds["spline"])
else:
data.append(line.strip())
ds["poni"] = os.path.join(self.tmp_dir, os.path.basename(ds["poni"]))
with open(ds["poni"], "w") as f:
f.write(os.linesep.join(data))
def tearDown(self):
unittest.TestCase.tearDown(self)
for ds in self.datasets:
if ds["spline"] is not None:
if os.path.isfile(ds["poni"]):
os.unlink(ds["poni"])
def test_OpenCL(self):
logger.info("Testing histogram-based algorithm (forward-integration)")
for devtype in ("GPU", "CPU"):
ids = ocl.select_device(devtype, extensions=["cl_khr_int64_base_atomics"])
if ids is None:
logger.error("No suitable %s OpenCL device found" % devtype)
continue
else:
logger.info("I found a suitable device %s %s: %s %s " % (devtype, ids, ocl.platforms[ids[0]], ocl.platforms[ids[0]].devices[ids[1]]))
for ds in self.datasets:
ai = pyFAI.load(ds["poni"])
data = fabio.open(ds["img"]).data
res = ai.xrpd_OpenCL(data, self.N, devicetype="all", platformid=ids[0], deviceid=ids[1], useFp64=True)
ref = ai.integrate1d(data, self.N, method="splitBBox", unit="2th_deg")
r = Rwp(ref, res)
logger.info("OpenCL histogram vs histogram SplitBBox has R= %.3f for dataset %s" % (r, ds))
self.assertTrue(r < 6, "Rwp=%.3f for OpenCL histogram processing of %s" % (r, ds))
del ai, data
gc.collect()
def test_OpenCL_LUT(self):
logger.info("Testing LUT-based algorithm (backward-integration)")
for devtype in ("GPU", "CPU"):
ids = ocl.select_device(devtype, best=True)
if ids is None:
logger.error("No suitable %s OpenCL device found" % devtype)
continue
else:
logger.info("I found a suitable device %s %s: %s %s " % (devtype, ids, ocl.platforms[ids[0]], ocl.platforms[ids[0]].devices[ids[1]]))
for ds in self.datasets:
ai = pyFAI.load(ds["poni"])
data = fabio.open(ds["img"]).data
ref = ai.integrate1d(data, self.N, method="splitBBox", unit="2th_deg")
try:
res = ai.integrate1d(data, self.N, method="ocl_lut_%i,%i" % (ids[0], ids[1]), unit="2th_deg")
except (pyFAI.opencl.pyopencl.MemoryError, MemoryError, pyFAI.opencl.pyopencl.RuntimeError, RuntimeError) as error:
logger.warning("Memory error on %s dataset %s: %s%s. Converted into warnining: device may not have enough memory." % (devtype, os.path.basename(ds["img"]), os.linesep, error))
break
else:
ref = ai.xrpd(data, self.N)
r = Rwp(ref, res)
logger.info("OpenCL CSR vs histogram SplitBBox has R= %.3f for dataset %s" % (r, ds))
self.assertTrue(r < 3, "Rwp=%.3f for OpenCL LUT processing of %s" % (r, ds))
del ai, data
gc.collect()
def test_OpenCL_CSR(self):
logger.info("Testing CSR-based algorithm (backward-integration)")
for devtype in ("GPU", "CPU"):
ids = ocl.select_device(devtype, best=True)
if ids is None:
logger.error("No suitable %s OpenCL device found" % devtype)
continue
else:
logger.info("I found a suitable device %s %s: %s %s " % (devtype, ids, ocl.platforms[ids[0]], ocl.platforms[ids[0]].devices[ids[1]]))
for ds in self.datasets:
ai = pyFAI.load(ds["poni"])
data = fabio.open(ds["img"]).data
ref = ai.integrate1d(data, self.N, method="splitBBox", unit="2th_deg")
try:
res = ai.integrate1d(data, self.N, method="ocl_csr_%i,%i" % (ids[0], ids[1]), unit="2th_deg")
except (pyFAI.opencl.pyopencl.MemoryError, MemoryError, pyFAI.opencl.pyopencl.RuntimeError, RuntimeError) as error:
logger.warning("Memory error on %s dataset %s: %s%s. Converted into Warning: device may not have enough memory." % (devtype, os.path.basename(ds["img"]), os.linesep, error))
break
else:
r = Rwp(ref, res)
logger.info("OpenCL CSR vs histogram SplitBBox has R= %.3f for dataset %s" % (r, ds))
self.assertTrue(r < 3, "Rwp=%.3f for OpenCL CSR processing of %s" % (r, ds))
del ai, data
gc.collect()
def test_suite_all_OpenCL():
testSuite = unittest.TestSuite()
if skip:
logger.warning("OpenCL module (pyopencl) is not present or no device available: skip tests")
else:
testSuite.addTest(test_mask("test_OpenCL"))
testSuite.addTest(test_mask("test_OpenCL_LUT"))
testSuite.addTest(test_mask("test_OpenCL_CSR"))
return testSuite
if __name__ == '__main__':
mysuite = test_suite_all_OpenCL()
runner = unittest.TextTestRunner()
runner.run(mysuite)
|