rg_transforms.py 22.4 KB
Newer Older
1
2
3
4
5
6
import warnings

import numpy as np
from d2o import distributed_data_object, STRATEGIES
from nifty.config import about, dependency_injector as gdi
import nifty.nifty_utilities as utilities
Jait Dixit's avatar
Jait Dixit committed
7
from nifty import nifty_configuration
8
9
10
11

pyfftw = gdi.get('pyfftw')


Jait Dixit's avatar
Jait Dixit committed
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
class Transform(object):
    """
        A generic fft object without any implementation.
    """

    def __init__(self, domain, codomain):
        pass

    def transform(self, val, axes, **kwargs):
        """
            A generic ff-transform function.

            Parameters
            ----------
            field_val : distributed_data_object
                The value-array of the field which is supposed to
                be transformed.

            domain : nifty.rg.nifty_rg.rg_space
                The domain of the space which should be transformed.

            codomain : nifty.rg.nifty_rg.rg_space
                The taget into which the field should be transformed.
        """
        raise NotImplementedError

38

Jait Dixit's avatar
Jait Dixit committed
39
class FFTW(Transform):
40
41
42
43
44
    """
        The pyfftw pendant of a fft object.
    """

    def __init__(self, domain, codomain):
Jait Dixit's avatar
Jait Dixit committed
45
46
        self.domain = domain
        self.codomain = codomain
47
48
49
50
51
52
53

        if 'pyfftw' not in gdi:
            raise ImportError("The module pyfftw is needed but not available.")

        # Enable caching for pyfftw.interfaces
        pyfftw.interfaces.cache.enable()

Jait Dixit's avatar
Jait Dixit committed
54
55
56
57
58
59
60
61
62
        # The plan_dict stores the FFTWTransformInfo objects which correspond
        # to a certain set of (field_val, domain, codomain) sets.
        self.info_dict = {}

        # initialize the dictionary which stores the values from
        # get_centering_mask
        self.centering_mask_dict = {}

    def get_centering_mask(self, to_center_input, dimensions_input,
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
                           offset_input=False):
        """
            Computes the mask, used to (de-)zerocenter domain and target
            fields.

            Parameters
            ----------
            to_center_input : tuple, list, numpy.ndarray
                A tuple of booleans which dimensions should be
                zero-centered.

            dimensions_input : tuple, list, numpy.ndarray
                A tuple containing the mask's desired shape.

            offset_input : int, boolean
                Specifies whether the zero-th dimension starts with an odd
                or and even index, i.e. if it is shifted.

            Returns
            -------
            result : np.ndarray
                A 1/-1-alternating mask.
        """
        # cast input
        to_center = np.array(to_center_input)
        dimensions = np.array(dimensions_input)

        # if none of the dimensions are zero centered, return a 1
        if np.all(to_center == 0):
            return 1

        if np.all(dimensions == np.array(1)) or \
                np.all(dimensions == np.array([1])):
            return dimensions
        # The dimensions of size 1 must be sorted out for computing the
        # centering_mask. The depth of the array will be restored in the
        # end.
        size_one_dimensions = []
        temp_dimensions = []
        temp_to_center = []
        for i in range(len(dimensions)):
            if dimensions[i] == 1:
                size_one_dimensions += [True]
            else:
                size_one_dimensions += [False]
                temp_dimensions += [dimensions[i]]
                temp_to_center += [to_center[i]]
        dimensions = np.array(temp_dimensions)
        to_center = np.array(temp_to_center)
        # cast the offset_input into the shape of to_center
        offset = np.zeros(to_center.shape, dtype=int)
        offset[0] = int(offset_input)
        # check for dimension match
        if to_center.size != dimensions.size:
            raise TypeError(
                'The length of the supplied lists does not match.')

        # build up the value memory
        # compute an identifier for the parameter set
        temp_id = tuple(
            (tuple(to_center), tuple(dimensions), tuple(offset)))
Jait Dixit's avatar
Jait Dixit committed
124
        if temp_id not in self.centering_mask_dict:
125
126
127
128
            # use np.tile in order to stack the core alternation scheme
            # until the desired format is constructed.
            core = np.fromfunction(
                lambda *args: (-1) **
Jait Dixit's avatar
Jait Dixit committed
129
130
131
132
133
134
135
                              (np.tensordot(to_center,
                                            args +
                                            offset.reshape(offset.shape +
                                                           (1,) *
                                                           (np.array(
                                                               args).ndim - 1)),
                                            1)),
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
                (2,) * to_center.size)
            # Cast the core to the smallest integers we can get
            core = core.astype(np.int8)

            centering_mask = np.tile(core, dimensions // 2)
            # for the dimensions of odd size corresponding slices must be
            # added
            for i in range(centering_mask.ndim):
                # check if the size of the certain dimension is odd or even
                if (dimensions % 2)[i] == 0:
                    continue
                # prepare the slice object
                temp_slice = (slice(None),) * i + (slice(-2, -1, 1),) + \
                             (slice(None),) * (centering_mask.ndim - 1 - i)
                # append the slice to the centering_mask
                centering_mask = np.append(centering_mask,
                                           centering_mask[temp_slice],
                                           axis=i)
            # Add depth to the centering_mask where the length of a
            # dimension was one
            temp_slice = ()
            for i in range(len(size_one_dimensions)):
                if size_one_dimensions[i]:
                    temp_slice += (None,)
                else:
                    temp_slice += (slice(None),)
            centering_mask = centering_mask[temp_slice]
Jait Dixit's avatar
Jait Dixit committed
163
164
            self.centering_mask_dict[temp_id] = centering_mask
        return self.centering_mask_dict[temp_id]
165

Jait Dixit's avatar
Jait Dixit committed
166
    def _get_transform_info(self, domain, codomain, local_shape,
167
168
169
170
171
172
173
174
                            local_offset_Q, is_local, transform_shape=None,
                            **kwargs):
        # generate a id-tuple which identifies the domain-codomain setting
        temp_id = (domain.__hash__() ^
                   (101 * codomain.__hash__()) ^
                   (211 * transform_shape.__hash__()))

        # generate the plan_and_info object if not already there
Jait Dixit's avatar
Jait Dixit committed
175
        if temp_id not in self.info_dict:
176
            if is_local:
Jait Dixit's avatar
Jait Dixit committed
177
                self.info_dict[temp_id] = FFTWLocalTransformInfo(
178
                    domain, codomain, local_shape,
Jait Dixit's avatar
Jait Dixit committed
179
                    local_offset_Q, self, **kwargs
180
181
                )
            else:
Jait Dixit's avatar
Jait Dixit committed
182
                self.info_dict[temp_id] = FFTWMPITransfromInfo(
183
                    domain, codomain, local_shape,
Jait Dixit's avatar
Jait Dixit committed
184
                    local_offset_Q, self, transform_shape, **kwargs
185
186
                )

Jait Dixit's avatar
Jait Dixit committed
187
        return self.info_dict[temp_id]
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

    def _apply_mask(self, val, mask, axes):
        """
            Apply centering mask to an array.

            Parameters
            ----------
            val: distributed_data_object or numpy.ndarray
                The value-array on which the mask should be applied.

            mask: numpy.ndarray
                The mask to be applied.

            axes: tuple
                The axes which are to be transformed.

            Returns
            -------
            distributed_data_object or np.nd_array
                Mask input array by multiplying it with the mask.
        """
        # reshape mask if necessary
        if axes:
            mask = mask.reshape(
                [y if x in axes else 1
Jait Dixit's avatar
Jait Dixit committed
213
                 for x, y in enumerate(val.shape)]
214
215
216
217
218
            )

        return val * mask

    def _atomic_mpi_transform(self, val, info, axes):
Jait Dixit's avatar
Jait Dixit committed
219

220
        # Apply codomain centering mask
221
        if reduce(lambda x, y: x + y, self.codomain.zerocenter):
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
            temp_val = np.copy(val)
            val = self._apply_mask(temp_val, info.cmask_codomain, axes)

        p = info.plan
        # Load the value into the plan
        if p.has_input:
            p.input_array[:] = val
        # Execute the plan
        p()

        if p.has_output:
            result = p.output_array
        else:
            return None

        # Apply domain centering mask
238
        if reduce(lambda x, y: x + y, self.domain.zerocenter):
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
            result = self._apply_mask(result, info.cmask_domain, axes)

        # Correct the sign if needed
        result *= info.sign

        return result

    def _local_transform(self, val, axes, **kwargs):
        ####
        # val must be numpy array or d2o with slicing distributor
        ###

        local_offset_Q = False
        try:
            local_val = val.get_local_data(copy=False)
            if axes is None or 0 in axes:
                local_offset_Q = val.distributor.local_shape[0] % 2
        except(AttributeError):
            local_val = val
        current_info = self._get_transform_info(self.domain,
                                                self.codomain,
                                                local_shape=local_val.shape,
                                                local_offset_Q=local_offset_Q,
                                                is_local=True,
                                                **kwargs)

        # Apply codomain centering mask
266
        if reduce(lambda x, y: x + y, self.codomain.zerocenter):
267
268
269
270
271
272
273
274
275
276
277
            temp_val = np.copy(local_val)
            local_val = self._apply_mask(temp_val,
                                         current_info.cmask_codomain, axes)

        local_result = current_info.fftw_interface(
            local_val,
            axes=axes,
            planner_effort='FFTW_ESTIMATE'
        )

        # Apply domain centering mask
278
        if reduce(lambda x, y: x + y, self.domain.zerocenter):
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
332
333
334
335
336
337
338
339
340
341
            local_result = self._apply_mask(local_result,
                                            current_info.cmask_domain, axes)

        # Correct the sign if needed
        if current_info.sign != 1:
            local_result *= current_info.sign

        try:
            # Create return object and insert results inplace
            return_val = val.copy_empty(global_shape=val.shape,
                                        dtype=self.codomain.dtype)
            return_val.set_local_data(data=local_result, copy=False)
        except(AttributeError):
            return_val = local_result

        return return_val

    def _repack_to_fftw_and_transform(self, val, axes, **kwargs):
        temp_val = val.copy_empty(distribution_strategy='fftw')
        about.warnings.cprint('WARNING: Repacking d2o to fftw \
                                distribution strategy')
        temp_val.set_full_data(val, copy=False)

        # Recursive call to transform
        result = self.transform(temp_val, axes, **kwargs)

        return_val = result.copy_empty(
            distribution_strategy=val.distribution_strategy)
        return_val.set_full_data(data=result, copy=False)

        return return_val

    def _mpi_transform(self, val, axes, **kwargs):

        if axes is None or 0 in axes:
            local_offset_list = np.cumsum(
                np.concatenate([[0, ], val.distributor.all_local_slices[:, 2]])
            )
            local_offset_Q = bool(
                local_offset_list[val.distributor.comm.rank] % 2)
        else:
            local_offset_Q = False

        return_val = val.copy_empty(global_shape=val.shape,
                                    dtype=self.codomain.dtype)

        # Extract local data
        local_val = val.get_local_data(copy=False)

        # Create temporary storage for slices
        temp_val = None

        # If axes tuple includes all axes, set it to None
        if axes is not None:
            if set(axes) == set(range(len(val.shape))):
                axes = None

        current_info = None
        for slice_list in utilities.get_slice_list(local_val.shape, axes):
            if slice_list == [slice(None, None)]:
                inp = local_val
            else:
                if temp_val is None:
Jait Dixit's avatar
Jait Dixit committed
342
343
344
345
                    temp_val = np.empty_like(
                        local_val,
                        dtype=self.codomain.dtype
                    )
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
                inp = local_val[slice_list]

            # This is in order to make FFTW behave properly when slicing input
            # over MPI ranks when the input is 1-dimensional. The default
            # behaviour is to optimize to take advantage of byte-alignment,
            # which doesn't match the slicing strategy for multi-dimensional
            # data.
            original_shape = None
            if len(inp.shape) == 1:
                original_shape = inp.shape
                inp = inp.reshape(inp.shape[0], 1)

            if current_info is None:
                current_info = self._get_transform_info(
                    self.domain,
                    self.codomain,
                    local_shape=val.local_shape,
                    local_offset_Q=local_offset_Q,
                    is_local=False,
Jait Dixit's avatar
Jait Dixit committed
365
                    transform_shape=inp.shape,
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
                    **kwargs
                )

            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                result = self._atomic_mpi_transform(inp, current_info, axes)

            if result is None:
                temp_val = np.empty_like(local_val)
            elif slice_list == [slice(None, None)]:
                temp_val = result
            else:
                # Reverting to the original shape i.e. before the input was
                # augmented with 1 to make FFTW behave properly.
                if original_shape is not None:
                    result = result.reshape(original_shape)
                temp_val[slice_list] = result

        return_val.set_local_data(data=temp_val, copy=False)

        return return_val

Jait Dixit's avatar
Jait Dixit committed
388
    def transform(self, val, axes, **kwargs):
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
        """
            The pyfftw transform function.

            Parameters
            ----------
            val : distributed_data_object or numpy.ndarray
                The value-array of the field which is supposed to
                be transformed.

            axes: tuple, None
                The axes which should be transformed.

            **kwargs : *optional*
                Further kwargs are passed to the create_mpi_plan routine.

            Returns
            -------
            result : np.ndarray or distributed_data_object
                Fourier-transformed pendant of the input field.
        """
        # Check if the axes provided are valid given the shape
        if axes is not None and \
                not all(axis in range(len(val.shape)) for axis in axes):
            raise ValueError("ERROR: Provided axes does not match array shape")

        # If the input is a numpy array we transform it locally
        if not isinstance(val, distributed_data_object):
            # Cast to a np.ndarray
            temp_val = np.asarray(val)

            # local transform doesn't apply transforms inplace
            return_val = self._local_transform(temp_val, axes)
        else:
            if val.distribution_strategy in STRATEGIES['slicing']:
                if axes is None or 0 in axes:
                    if val.distribution_strategy != 'fftw':
                        return_val = \
                            self._repack_to_fftw_and_transform(
                                val, axes, **kwargs
                            )
                    else:
                        return_val = self._mpi_transform(
                            val, axes, **kwargs
                        )
                else:
                    return_val = self._local_transform(
                        val, axes, **kwargs
                    )
            else:
                return_val = self._repack_to_fftw_and_transform(
                    val, axes, **kwargs
                )

        return return_val


class FFTWTransformInfo(object):
    def __init__(self, domain, codomain, local_shape,
Jait Dixit's avatar
Jait Dixit committed
447
                 local_offset_Q, fftw_context, **kwargs):
448
449
450
        if pyfftw is None:
            raise ImportError("The module pyfftw is needed but not available.")

Jait Dixit's avatar
Jait Dixit committed
451
        self.cmask_domain = fftw_context.get_centering_mask(
452
            domain.zerocenter,
453
454
455
            local_shape,
            local_offset_Q)

Jait Dixit's avatar
Jait Dixit committed
456
        self.cmask_codomain = fftw_context.get_centering_mask(
457
            codomain.zerocenter,
458
459
460
461
462
            local_shape,
            local_offset_Q)

        # If both domain and codomain are zero-centered the result,
        # will get a global minus. Store the sign to correct it.
463
464
        self.sign = (-1) ** np.sum(np.array(domain.zerocenter) *
                                   np.array(codomain.zerocenter) *
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
                                   (np.array(domain.shape) // 2 % 2))

    @property
    def cmask_domain(self):
        return self._domain_centering_mask

    @cmask_domain.setter
    def cmask_domain(self, cmask):
        self._domain_centering_mask = cmask

    @property
    def cmask_codomain(self):
        return self._codomain_centering_mask

    @cmask_codomain.setter
    def cmask_codomain(self, cmask):
        self._codomain_centering_mask = cmask

    @property
    def sign(self):
        return self._sign

    @sign.setter
    def sign(self, sign):
        self._sign = sign


class FFTWLocalTransformInfo(FFTWTransformInfo):
    def __init__(self, domain, codomain, local_shape,
Jait Dixit's avatar
Jait Dixit committed
494
                 local_offset_Q, fftw_context, **kwargs):
495
496
497
498
        super(FFTWLocalTransformInfo, self).__init__(domain,
                                                     codomain,
                                                     local_shape,
                                                     local_offset_Q,
Jait Dixit's avatar
Jait Dixit committed
499
                                                     fftw_context,
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
                                                     **kwargs)
        if codomain.harmonic:
            self._fftw_interface = pyfftw.interfaces.numpy_fft.fftn
        else:
            self._fftw_interface = pyfftw.interfaces.numpy_fft.ifftn

    @property
    def fftw_interface(self):
        return self._fftw_interface

    @fftw_interface.setter
    def fftw_interface(self, interface):
        about.warnings.cprint('WARNING: FFTWLocalTransformInfo fftw_interface \
                               cannot be modified')


class FFTWMPITransfromInfo(FFTWTransformInfo):
    def __init__(self, domain, codomain, local_shape,
Jait Dixit's avatar
Jait Dixit committed
518
                 local_offset_Q, fftw_context, transform_shape, **kwargs):
519
520
521
522
        super(FFTWMPITransfromInfo, self).__init__(domain,
                                                   codomain,
                                                   local_shape,
                                                   local_offset_Q,
Jait Dixit's avatar
Jait Dixit committed
523
                                                   fftw_context,
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
                                                   **kwargs)
        self._plan = pyfftw.create_mpi_plan(
            input_shape=transform_shape,
            input_dtype='complex128',
            output_dtype='complex128',
            direction='FFTW_FORWARD' if codomain.harmonic else 'FFTW_BACKWARD',
            flags=["FFTW_ESTIMATE"],
            **kwargs
        )

    @property
    def plan(self):
        return self._plan

    @plan.setter
    def plan(self, plan):
        about.warnings.cprint('WARNING: FFTWMPITransfromInfo plan \
                               cannot be modified')
Jait Dixit's avatar
Jait Dixit committed
542
543
544
545
546
547
548
549
550
551
552
553
554


class GFFT(Transform):
    """
        The gfft pendant of a fft object.

        Parameters
        ----------
        fft_module_name : String
            Switch between the gfft module used: 'gfft' and 'gfft_dummy'

    """

555
    def __init__(self, domain, codomain, fft_module=None):
Jait Dixit's avatar
Jait Dixit committed
556
        if fft_module is None:
557
            fft_module = gdi['gfft_dummy']
Jait Dixit's avatar
Jait Dixit committed
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614

        self.domain = domain
        self.codomain = codomain
        self.fft_machine = fft_module

    def transform(self, val, axes, **kwargs):
        """
            The gfft transform function.

            Parameters
            ----------
            val : numpy.ndarray or distributed_data_object
                The value-array of the field which is supposed to
                be transformed.

            axes : None or tuple
                The axes which should be transformed.

            **kwargs : *optional*
                Further kwargs are not processed.

            Returns
            -------
            result : np.ndarray or distributed_data_object
                Fourier-transformed pendant of the input field.
        """
        # Check if the axes provided are valid given the shape
        if axes is not None and \
                not all(axis in range(len(val.shape)) for axis in axes):
            raise ValueError("ERROR: Provided axes does not match array shape")

        # GFFT doesn't accept d2o objects as input. Consolidate data from
        # all nodes into numpy.ndarray before proceeding.
        if isinstance(val, distributed_data_object):
            temp_inp = val.get_full_data()
        else:
            temp_inp = val

        # Array for storing the result
        return_val = None

        for slice_list in utilities.get_slice_list(temp_inp.shape, axes):

            # don't copy the whole data array
            if slice_list == [slice(None, None)]:
                inp = temp_inp
            else:
                # initialize the return_val object if needed
                if return_val is None:
                    return_val = np.empty_like(temp_inp)
                inp = temp_inp[slice_list]

            inp = self.fft_machine.gfft(
                inp,
                in_ax=[],
                out_ax=[],
                ftmachine='fft' if self.codomain.harmonic else 'ifft',
615
616
617
618
                in_zero_center=map(bool, self.domain.zerocenter),
                out_zero_center=map(bool, self.codomain.zerocenter),
                # enforce_hermitian_symmetry=bool(self.codomain.complexity),
                enforce_hermitian_symmetry=False,
Jait Dixit's avatar
Jait Dixit committed
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
                W=-1,
                alpha=-1,
                verbose=False
            )
            if slice_list == [slice(None, None)]:
                return_val = inp
            else:
                return_val[slice_list] = inp

        if isinstance(val, distributed_data_object):
            new_val = val.copy_empty(dtype=self.codomain.dtype)
            new_val.set_full_data(return_val, copy=False)
            return_val = new_val
        else:
            return_val = return_val.astype(self.codomain.dtype, copy=False)

        return return_val