summaryrefslogtreecommitdiff
path: root/silx/opencl/codec/test/test_byte_offset.py
blob: e523b0f4502b630ee674324061b0bb5682fb0764 (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#    Project: Byte-offset decompression in OpenCL
#             https://github.com/silx-kit/silx
#
#    Copyright (C) 2013-2020  European Synchrotron Radiation Facility,
#                             Grenoble, France
# 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 byte-offset decompression 
"""

from __future__ import division, print_function

__authors__ = ["Jérôme Kieffer"]
__contact__ = "jerome.kieffer@esrf.eu"
__license__ = "MIT"
__copyright__ = "2013 European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "10/11/2017"

import sys
import time
import logging
import numpy
from silx.opencl.common import ocl, pyopencl
from silx.opencl.codec import byte_offset
import fabio
import unittest
logger = logging.getLogger(__name__)


@unittest.skipUnless(ocl and pyopencl,
                     "PyOpenCl is missing")
class TestByteOffset(unittest.TestCase):

    @staticmethod
    def _create_test_data(shape, nexcept, lam=200):
        """Create test (image, compressed stream) pair.

        :param shape: Shape of test image
        :param int nexcept: Number of exceptions in the image
        :param lam: Expectation of interval argument for numpy.random.poisson
        :return: (reference image array, compressed stream)
        """
        size = numpy.prod(shape)
        ref = numpy.random.poisson(lam, numpy.prod(shape))
        exception_loc = numpy.random.randint(0, size, size=nexcept)
        exception_value = numpy.random.randint(0, 1000000, size=nexcept)
        ref[exception_loc] = exception_value
        ref.shape = shape

        raw = fabio.compression.compByteOffset(ref)
        return ref, raw

    def test_decompress(self):
        """
        tests the byte offset decompression on GPU
        """
        ref, raw = self._create_test_data(shape=(91, 97), nexcept=229)
        #ref, raw = self._create_test_data(shape=(7, 9), nexcept=0)
        
        size = numpy.prod(ref.shape)

        try:
            bo = byte_offset.ByteOffset(raw_size=len(raw), dec_size=size, profile=True)
        except (RuntimeError, pyopencl.RuntimeError) as err:
            logger.warning(err)
            if sys.platform == "darwin":
                raise unittest.SkipTest("Byte-offset decompression is known to be buggy on MacOS-CPU")
            else:
                raise err
        print(bo.block_size)

        t0 = time.time()
        res_cy = fabio.compression.decByteOffset(raw)
        t1 = time.time()
        res_cl = bo.decode(raw)
        t2 = time.time()
        delta_cy = abs(ref.ravel() - res_cy).max()
        delta_cl = abs(ref.ravel() - res_cl.get()).max()

        logger.debug("Global execution time: fabio %.3fms, OpenCL: %.3fms.",
                     1000.0 * (t1 - t0),
                     1000.0 * (t2 - t1))
        bo.log_profile()
        #print(ref)
        #print(res_cl.get())
        self.assertEqual(delta_cy, 0, "Checks fabio works")
        self.assertEqual(delta_cl, 0, "Checks opencl works")

    def test_many_decompress(self, ntest=10):
        """
        tests the byte offset decompression on GPU, many images to ensure there 
        is not leaking in memory 
        """
        shape = (991, 997)
        size = numpy.prod(shape)
        ref, raw = self._create_test_data(shape=shape, nexcept=0, lam=100)

        try:
            bo = byte_offset.ByteOffset(len(raw), size, profile=False)
        except (RuntimeError, pyopencl.RuntimeError) as err:
            logger.warning(err)
            if sys.platform == "darwin":
                raise unittest.SkipTest("Byte-offset decompression is known to be buggy on MacOS-CPU")
            else:
                raise err
        t0 = time.time()
        res_cy = fabio.compression.decByteOffset(raw)
        t1 = time.time()
        res_cl = bo(raw)
        t2 = time.time()
        delta_cy = abs(ref.ravel() - res_cy).max()
        delta_cl = abs(ref.ravel() - res_cl.get()).max()
        self.assertEqual(delta_cy, 0, "Checks fabio works")
        self.assertEqual(delta_cl, 0, "Checks opencl works")
        logger.debug("Global execution time: fabio %.3fms, OpenCL: %.3fms.",
                     1000.0 * (t1 - t0),
                     1000.0 * (t2 - t1))

        for i in range(ntest):
            ref, raw = self._create_test_data(shape=shape, nexcept=2729, lam=200)

            t0 = time.time()
            res_cy = fabio.compression.decByteOffset(raw)
            t1 = time.time()
            res_cl = bo(raw)
            t2 = time.time()
            delta_cy = abs(ref.ravel() - res_cy).max()
            delta_cl = abs(ref.ravel() - res_cl.get()).max()
            self.assertEqual(delta_cy, 0, "Checks fabio works #%i" % i)
            self.assertEqual(delta_cl, 0, "Checks opencl works #%i" % i)

            logger.debug("Global execution time: fabio %.3fms, OpenCL: %.3fms.",
                         1000.0 * (t1 - t0),
                         1000.0 * (t2 - t1))

    def test_encode(self):
        """Test byte offset compression"""
        ref, raw = self._create_test_data(shape=(2713, 2719), nexcept=2729)

        try:
            bo = byte_offset.ByteOffset(len(raw), ref.size, profile=True)
        except (RuntimeError, pyopencl.RuntimeError) as err:
            logger.warning(err)
            raise err

        t0 = time.time()
        compressed_array = bo.encode(ref)
        t1 = time.time()

        compressed_stream = compressed_array.get().tobytes()
        self.assertEqual(raw, compressed_stream)

        logger.debug("Global execution time: OpenCL: %.3fms.",
                     1000.0 * (t1 - t0))
        bo.log_profile()

    def test_encode_to_array(self):
        """Test byte offset compression while providing an out array"""

        ref, raw = self._create_test_data(shape=(2713, 2719), nexcept=2729)

        try:
            bo = byte_offset.ByteOffset(profile=True)
        except (RuntimeError, pyopencl.RuntimeError) as err:
            logger.warning(err)
            raise err
        # Test with out buffer too small
        out = pyopencl.array.empty(bo.queue, (10,), numpy.int8)
        with self.assertRaises(ValueError):
            bo.encode(ref, out)

        # Test with out buffer too big
        out = pyopencl.array.empty(bo.queue, (len(raw) + 10,), numpy.int8)

        compressed_array = bo.encode(ref, out)

        # Get size from returned array
        compressed_size = compressed_array.size
        self.assertEqual(compressed_size, len(raw))

        # Get data from out array, read it from bo object queue
        out_bo_queue = out.with_queue(bo.queue)
        compressed_stream = out_bo_queue.get().tobytes()[:compressed_size]
        self.assertEqual(raw, compressed_stream)

    def test_encode_to_bytes(self):
        """Test byte offset compression to bytes"""
        ref, raw = self._create_test_data(shape=(2713, 2719), nexcept=2729)

        try:
            bo = byte_offset.ByteOffset(profile=True)
        except (RuntimeError, pyopencl.RuntimeError) as err:
            logger.warning(err)
            raise err

        t0 = time.time()
        res_fabio = fabio.compression.compByteOffset(ref)
        t1 = time.time()
        compressed_stream = bo.encode_to_bytes(ref)
        t2 = time.time()

        self.assertEqual(raw, compressed_stream)

        logger.debug("Global execution time: fabio %.3fms, OpenCL: %.3fms.",
                     1000.0 * (t1 - t0),
                     1000.0 * (t2 - t1))
        bo.log_profile()

    def test_encode_to_bytes_from_array(self):
        """Test byte offset compression to bytes from a pyopencl array.
        """
        ref, raw = self._create_test_data(shape=(2713, 2719), nexcept=2729)

        try:
            bo = byte_offset.ByteOffset(profile=True)
        except (RuntimeError, pyopencl.RuntimeError) as err:
            logger.warning(err)
            raise err

        d_ref = pyopencl.array.to_device(
            bo.queue, ref.astype(numpy.int32).ravel())

        t0 = time.time()
        res_fabio = fabio.compression.compByteOffset(ref)
        t1 = time.time()
        compressed_stream = bo.encode_to_bytes(d_ref)
        t2 = time.time()

        self.assertEqual(raw, compressed_stream)

        logger.debug("Global execution time: fabio %.3fms, OpenCL: %.3fms.",
                     1000.0 * (t1 - t0),
                     1000.0 * (t2 - t1))
        bo.log_profile()

    def test_many_encode(self, ntest=10):
        """Test byte offset compression with many image"""
        shape = (991, 997)
        ref, raw = self._create_test_data(shape=shape, nexcept=0, lam=100)

        try:
            bo = byte_offset.ByteOffset(profile=False)
        except (RuntimeError, pyopencl.RuntimeError) as err:
            logger.warning(err)
            raise err

        bo_durations = []

        t0 = time.time()
        res_fabio = fabio.compression.compByteOffset(ref)
        t1 = time.time()
        compressed_stream = bo.encode_to_bytes(ref)
        t2 = time.time()
        bo_durations.append(1000.0 * (t2 - t1))

        self.assertEqual(raw, compressed_stream)
        logger.debug("Global execution time: fabio %.3fms, OpenCL: %.3fms.",
                     1000.0 * (t1 - t0),
                     1000.0 * (t2 - t1))

        for i in range(ntest):
            ref, raw = self._create_test_data(shape=shape, nexcept=2729, lam=200)

            t0 = time.time()
            res_fabio = fabio.compression.compByteOffset(ref)
            t1 = time.time()
            compressed_stream = bo.encode_to_bytes(ref)
            t2 = time.time()
            bo_durations.append(1000.0 * (t2 - t1))

            self.assertEqual(raw, compressed_stream)
            logger.debug("Global execution time: fabio %.3fms, OpenCL: %.3fms.",
                         1000.0 * (t1 - t0),
                         1000.0 * (t2 - t1))

        logger.debug("OpenCL execution time: Mean: %fms, Min: %fms, Max: %fms",
                     numpy.mean(bo_durations),
                     numpy.min(bo_durations),
                     numpy.max(bo_durations))


def suite():
    test_suite = unittest.TestSuite()
    test_suite.addTest(TestByteOffset("test_decompress"))
    test_suite.addTest(TestByteOffset("test_many_decompress"))
    test_suite.addTest(TestByteOffset("test_encode"))
    test_suite.addTest(TestByteOffset("test_encode_to_array"))
    test_suite.addTest(TestByteOffset("test_encode_to_bytes"))
    test_suite.addTest(TestByteOffset("test_encode_to_bytes_from_array"))
    test_suite.addTest(TestByteOffset("test_many_encode"))
    return test_suite