summaryrefslogtreecommitdiff
path: root/silx/opencl/sparse.py
blob: 8bfaea81c37c6130cd2cdd8613fe56fefa37095e (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2019 European Synchrotron Radiation Facility
#
# 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.
#
# ###########################################################################*/
"""Module for data sparsification on CPU/GPU."""

from __future__ import absolute_import, print_function, with_statement, division

__authors__ = ["P. Paleo"]
__license__ = "MIT"
__date__ = "07/06/2019"

import numpy
import pyopencl.array as parray
from collections import namedtuple
from pyopencl.scan import GenericScanKernel
from .common import pyopencl as cl
from .processing import OpenclProcessing, EventDescription, BufferDescription
mf = cl.mem_flags


CSRData = namedtuple("CSRData", ["data", "indices", "indptr"])

def tuple_to_csrdata(arrs):
    """
    Converts a 3-tuple to a CSRData namedtuple.
    """
    if arrs is None:
        return None
    return CSRData(data=arrs[0], indices=arrs[1], indptr=arrs[2])



# only float32 arrays are supported for now
class CSR(OpenclProcessing):
    kernel_files = ["sparse.cl"]

    def __init__(self, shape, max_nnz=None, ctx=None, devicetype="all",
                 platformid=None, deviceid=None, block_size=None, memory=None,
                 profile=False):
        """
        Compute Compressed Sparse Row format of an image (2D matrix).
        It is designed to be compatible with scipy.sparse.csr_matrix.

        :param shape: tuple
            Matrix shape.
        :param max_nnz: int, optional
            Maximum number of non-zero elements. By default, the arrays "data"
            and "indices" are allocated with prod(shape) elements, but
            in practice a much lesser space is needed.
            The number of non-zero items cannot be known in advance, but one can
            estimate an upper-bound with this parameter to save memory.

        Opencl processing parameters
        -----------------------------
        Please refer to the documentation of silx.opencl.processing.OpenclProcessing
        for information on the other parameters.
        """

        OpenclProcessing.__init__(self, ctx=ctx, devicetype=devicetype,
                                  platformid=platformid, deviceid=deviceid,
                                  profile=profile)
        self._set_parameters(shape, max_nnz)
        self._allocate_memory()
        self._setup_kernels()

    # --------------------------------------------------------------------------
    # -------------------------- Initialization --------------------------------
    # --------------------------------------------------------------------------

    def _set_parameters(self, shape, max_nnz):
        self.shape = shape
        self.size = numpy.prod(shape)
        self.indice_dtype = numpy.int32 #
        assert len(shape) == 2 #
        if max_nnz is None:
            self.max_nnz = numpy.prod(shape) # worst case
        else:
            self.max_nnz = int(max_nnz)


    def _allocate_memory(self):
        self.is_cpu = (self.device.type == "CPU") # move to OpenclProcessing ?
        self.buffers = [
            BufferDescription("array", (self.size,), numpy.float32, mf.READ_ONLY),
            BufferDescription("data", (self.max_nnz,), numpy.float32, mf.READ_WRITE),
            BufferDescription("indices", (self.max_nnz,), self.indice_dtype, mf.READ_WRITE),
            BufferDescription("indptr", (self.shape[0]+1,), self.indice_dtype, mf.READ_WRITE),
        ]
        self.allocate_buffers(use_array=True)
        for arr_name in ["array", "data", "indices", "indptr"]:
            setattr(self, arr_name, self.cl_mem[arr_name])
            self.cl_mem[arr_name].fill(0) # allocate_buffers() uses empty()
        self._old_array = self.array
        self._old_data = self.data
        self._old_indices = self.indices
        self._old_indptr = self.indptr


    def _setup_kernels(self):
        self._setup_compaction_kernel()
        self._setup_decompaction_kernel()


    def _setup_compaction_kernel(self):
        self.scan_kernel = GenericScanKernel(
            self.ctx, self.indice_dtype,
            arguments="__global float* data, __global float *data_compacted, __global int *indices, __global int* indptr",
            input_expr="(fabs(data[i]) > 0.0f) ? 1 : 0",
            scan_expr="a+b", neutral="0",
            output_statement="""
                // item is the running sum of input_expr(i), i.e the cumsum of "nonzero"
                if (prev_item != item) {
                    data_compacted[item-1] = data[i];
                    indices[item-1] = GET_INDEX(i);
                }
                // The last cumsum element of each line of "nonzero" goes to inptr[i]
                if ((i+1) % IMAGE_WIDTH == 0) {
                    indptr[(i/IMAGE_WIDTH)+1] = item;
                }
                """,
            options="-DIMAGE_WIDTH=%d" % self.shape[1],
            preamble="#define GET_INDEX(i) (i % IMAGE_WIDTH)",
        )


    def _setup_decompaction_kernel(self):
        OpenclProcessing.compile_kernels(
            self,
            self.kernel_files,
            compile_options=["-DIMAGE_WIDTH=%d" % self.shape[1]]
        )
        device = self.ctx.devices[0]
        wg_x = min(
            device.max_work_group_size,
            32,
            self.kernels.max_workgroup_size("densify_csr")
        )
        self._decomp_wg = (wg_x, 1)
        self._decomp_grid = (self._decomp_wg[0], self.shape[0])


    # --------------------------------------------------------------------------
    # -------------------------- Array utils -----------------------------------
    # --------------------------------------------------------------------------

    # TODO handle pyopencl Buffer
    def check_array(self, arr):
        """
        Check that provided array is compatible with current context.

        :param arr: numpy.ndarray or pyopencl.array.Array
            2D array in dense format.
        """
        assert arr.size == self.size
        assert arr.dtype == numpy.float32


    # TODO handle pyopencl Buffer
    def check_sparse_arrays(self, csr_data):
        """
        Check that the provided sparse arrays are compatible with the current
        context.

        :param arrays: namedtuple CSRData.
            It contains the arrays "data", "indices", "indptr"
        """
        assert isinstance(csr_data, CSRData)
        for arr in [csr_data.data, csr_data.indices, csr_data.indptr]:
            assert arr.ndim == 1
        assert csr_data.data.size == self.max_nnz
        assert csr_data.indices.size == self.max_nnz
        assert csr_data.indptr.size == self.shape[0]+1
        assert csr_data.data.dtype == numpy.float32
        assert csr_data.indices.dtype == self.indice_dtype
        assert csr_data.indptr.dtype == self.indice_dtype


    def set_array(self, arr):
        """
        Set the provided array as the current context 2D matrix.

        :param arr: numpy.ndarray or pyopencl.array.Array
            2D array in dense format.
        """
        if arr is None:
            return
        self.check_array(arr)
        # GenericScanKernel only supports 1D data
        if isinstance(arr, parray.Array):
            self._old_array = self.array
            self.array = arr
        elif isinstance(arr, numpy.ndarray):
            self.array[:] = arr.ravel()[:]
        else:
            raise ValueError("Expected pyopencl array or numpy array")


    def set_sparse_arrays(self, csr_data):
        if csr_data is None:
            return
        self.check_sparse_arrays(csr_data)
        for name, arr in {"data": csr_data.data, "indices": csr_data.indices, "indptr": csr_data.indptr}.items():
            # The current array is a device array. Don't copy, use it directly
            if isinstance(arr, parray.Array):
                setattr(self, "_old_" + name, getattr(self, name))
                setattr(self, name, arr)
            # The current array is a numpy.ndarray: copy H2D
            elif isinstance(arr, numpy.ndarray):
                getattr(self, name)[:] = arr[:]
            else:
                raise ValueError("Unsupported array type: %s" % type(arr))


    def _recover_arrays_references(self):
        """
        Recover the previous arrays references, and return the references of the
        "current" arrays.
        """
        array = self.array
        data = self.data
        indices = self.indices
        indptr = self.indptr
        for name in ["array", "data", "indices", "indptr"]:
            # self.X = self._old_X
            setattr(self, name, getattr(self, "_old_" + name))
        return array, (data, indices, indptr)


    def get_sparse_arrays(self, output):
        """
        Get the 2D dense array of the current context.

        :param output: tuple or None
            tuple in the form (data, indices, indptr). These arrays have to be
            compatible with the current context (size and data type).
            The content of these arrays will be overwritten with the result of
            the previous computation.
        """
        numels = self.max_nnz
        if output is None:
            data = self.data.get()[:numels]
            ind = self.indices.get()[:numels]
            indptr = self.indptr.get()
            res = (data, ind, indptr)
        else:
            res = output
        return res


    def get_array(self, output):
        if output is None:
            res = self.array.get().reshape(self.shape)
        else:
            res = output
        return res

    # --------------------------------------------------------------------------
    # -------------------------- Compaction ------------------------------------
    # --------------------------------------------------------------------------

    def sparsify(self, arr, output=None):
        """
        Convert an image (2D matrix) into a CSR representation.

        :param arr: numpy.ndarray or pyopencl.array.Array
            Input array.
        :param output: tuple of pyopencl.array.Array, optional
            If provided, this must be a tuple of 3 arrays (data, indices, indptr).
            The content of each array is overwritten by the computation result.
        """
        self.set_array(arr)
        self.set_sparse_arrays(tuple_to_csrdata(output))
        evt = self.scan_kernel(
            self.array,
            self.data,
            self.indices,
            self.indptr,
        )
        #~ evt.wait()
        self.profile_add(evt, "sparsification kernel")
        res = self.get_sparse_arrays(output)
        self._recover_arrays_references()
        return res

    # --------------------------------------------------------------------------
    # -------------------------- Decompaction ----------------------------------
    # --------------------------------------------------------------------------

    def densify(self, data, indices, indptr, output=None):
        self.set_sparse_arrays(
            CSRData(data=data, indices=indices, indptr=indptr)
        )
        self.set_array(output)
        evt = self.kernels.densify_csr(
            self.queue,
            self._decomp_grid,
            self._decomp_wg,
            self.data.data,
            self.indices.data,
            self.indptr.data,
            self.array.data,
            numpy.int32(self.shape[0]),
        )
        #~ evt.wait()
        self.profile_add(evt, "desparsification kernel")
        res = self.get_array(output)
        self._recover_arrays_references()
        return res