nifty_mpi_data.py 35.2 KB
Newer Older
ultimanet's avatar
ultimanet committed
1
# -*- coding: utf-8 -*-
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
## NIFTY (Numerical Information Field Theory) has been developed at the
## Max-Planck-Institute for Astrophysics.
##
## Copyright (C) 2015 Max-Planck-Society
##
## Author: Theo Steininger
## Project homepage: <http://www.mpa-garching.mpg.de/ift/nifty/>
##
## 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/>.


ultimanet's avatar
ultimanet committed
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

##initialize the 'found-packages'-dictionary 
found = {}

import numpy as np
import nifty_core

try:
    from mpi4py import MPI
    found[MPI] = True
except(ImportError): 
#    from mpi4py_dummy import MPI
    found[MPI] = False

try:
    import pyfftw
    found['pyfftw'] = True
except(ImportError):       
    found['pyfftw'] = False

try:
    import h5py
    found['h5py'] = True
    found['h5py_parallel'] = h5py.get_config().mpi
except(ImportError):
    found['h5py'] = False
    found['h5py_parallel'] = False

   


class distributed_data_object(object):
    """

        NIFTY class for distributed data

        Parameters
        ----------
        global_data : {tuple, list, numpy.ndarray} *at least 1-dimensional*
            Initial data which will be casted to a numpy.ndarray and then 
            stored according to the distribution strategy. The global_data's
            shape overwrites global_shape.
        global_shape : tuple of ints, *optional*
            If no global_data is supplied, global_shape can be used to
            initialize an empty distributed_data_object
        dtype : type, *optional*
            If an explicit dtype is supplied, the given global_data will be 
            casted to it.            
        distribution_strategy : {'fftw' (default), 'not'}, *optional*
            Specifies the way, how global_data will be distributed to the 
            individual nodes. 
            'fftw' follows the distribution strategy of pyfftw.
            'not' does not distribute the data at all. 
            

        Attributes
        ----------
        data : numpy.ndarray
            The numpy.ndarray in which the individual node's data is stored.
        dtype : type
            Data type of the data object.
        distribution_strategy : string
            Name of the used distribution_strategy
        distributor : distributor
            The distributor object which takes care of all distribution and 
            consolidation of the data. 
        shape : tuple of int
            The global shape of the data
            
        Raises
        ------
        TypeError : 
            If the supplied distribution strategy is not known. 
        
    """
    def __init__(self,  global_data=None, global_shape=None, dtype=None, distribution_strategy='fftw', *args, **kwargs):
        if global_data != None:
            global_data_input = np.array(global_data, copy=False)
        else:
            global_data_input = None
            
        self.distributor = self._get_distributor(distribution_strategy)(global_data=global_data_input, global_shape=global_shape, dtype=dtype, *args, **kwargs)
        self.set_full_data(data=global_data_input, *args, **kwargs)
        
        self.distribution_strategy = distribution_strategy
        self.dtype = self.distributor.dtype
        self.shape = self.distributor.global_shape
        
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        self.init_args = args 
        self.init_kwargs = kwargs
    
        
    def copy(self):
        temp_d2o = self.copy_empty()        
        temp_d2o.set_local_data(self.get_local_data())
        return temp_d2o
    
    def copy_empty(self):
        temp_d2o = distributed_data_object(global_shape=self.shape,
                                           dtype=self.dtype,
                                           distribution_strategy=self.distribution_strategy,
                                           *self.init_args,
                                           **self.init_kwargs)
        return temp_d2o
    
    def apply_function(self, function):
        self.data[:] = np.vectorize(function)(self.data)
        
ultimanet's avatar
ultimanet committed
132
133
134
135
136
137
    def __str__(self):
        return self.data.__str__()
    
    def __repr__(self):
        return '<distributed_data_object>\n'+self.data.__repr__()
    
138
139
140
141
142
143
144
    def __pos__(self):
        temp_d2o = distributed_data_object(global_shape=self.shape, 
                                           dtype=self.dtype,
                                           distribution_strategy=self.distribution_strategy)
        temp_d2o.set_local_data(data = self.get_local_data())
        return temp_d2o
        
ultimanet's avatar
ultimanet committed
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
    def __neg__(self):
        temp_d2o = distributed_data_object(global_shape=self.shape, 
                                           dtype=self.dtype,
                                           distribution_strategy=self.distribution_strategy)
        temp_d2o.set_local_data(data = self.get_local_data().__neg__()) 
        return temp_d2o
    
            
    def __builtin_helper__(self, operator, other):
        temp_d2o = distributed_data_object(global_shape=self.shape, 
                                           dtype=self.dtype, 
                                           distribution_strategy=self.distribution_strategy)
        if isinstance(other, distributed_data_object):        
            temp_data = operator(other.get_local_data())
        else:
            temp_data = operator(other)
        temp_d2o.set_local_data(data=temp_data)
        return temp_d2o

    def __add__(self, other):
        return self.__builtin_helper__(self.get_local_data().__add__, other)

    def __radd__(self, other):
        return self.__builtin_helper__(self.get_local_data().__radd__, other)
    
    def __sub__(self, other):
        return self.__builtin_helper__(self.get_local_data().__sub__, other)
    
    def __rsub__(self, other):
        return self.__builtin_helper__(self.get_local_data().__rsub__, other)
    
    def __isub__(self, other):
        return self.__builtin_helper__(self.get_local_data().__isub__, other)
        
    def __div__(self, other):
        return self.__builtin_helper__(self.get_local_data().__div__, other)
    
    def __rdiv__(self, other):
        return self.__builtin_helper__(self.get_local_data().__rdiv__, other)

    def __floordiv__(self, other):
        return self.__builtin_helper__(self.get_local_data().__floordiv__, other)
    
    def __rfloordiv__(self, other):
        return self.__builtin_helper__(self.get_local_data().__rfloordiv__, other)
    
    def __mul__(self, other):
        return self.__builtin_helper__(self.get_local_data().__mul__, other)
    
    def __rmul__(self, other):
        return self.__builtin_helper__(self.get_local_data().__rmul__, other)

    def __imul__(self, other):
        return self.__builtin_helper__(self.get_local_data().__imul__, other)
    
    def __pow__(self, other):
        return self.__builtin_helper__(self.get_local_data().__pow__, other)
 
    def __rpow__(self, other):
        return self.__builtin_helper__(self.get_local_data().__rpow__, other)

    def __ipow__(self, other):
        return self.__builtin_helper__(self.get_local_data().__ipow__, other)

    def __getitem__(self, key):
        return self.get_data(key)
    
    def __setitem__(self, key, data):
        self.set_data(data, key)
        
    def set_local_data(self, data):
        """
            Stores data directly in the local data attribute. No distribution 
            is done. The shape of the data must fit the local data attributes
            shape.

            Parameters
            ----------
            data : tuple, list, numpy.ndarray 
                The data which should be stored in the local data attribute.
            
            Returns
            -------
            None
        
        """
        self.data = np.array(data).astype(self.dtype, copy=False)
    
    def set_data(self, data, key, *args, **kwargs):
        """
            Stores the supplied data in the region which is specified by key. 
            The data is distributed according to the distribution strategy. If
            the individual nodes get different key-arguments. Their data is 
            processed one-by-one.
            
            Parameters
            ----------
            data : tuple, list, numpy.ndarray 
                The data which should be distributed.
            key : int, slice, tuple of int or slice
                The key is the object which specifies the region, where data 
                will be stored in.                
            
            Returns
            -------
            None
        
        """
        (slices, sliceified) = self.__sliceify__(key)        
        self.distributor.disperse_data(self.data, self.__enfold__(data, sliceified), slices, *args, **kwargs)        
    
    def set_full_data(self, data, *args, **kwargs):
        """
            Distributes the supplied data to the nodes. The shape of data must 
            match the shape of the distributed_data_object.
            
            Parameters
            ----------
            data : tuple, list, numpy.ndarray 
                The data which should be distributed.
            
            Notes
            -----
            set_full_data(foo) is equivalent to set_data(foo,slice(None)) but 
            faster.
        
            Returns
            -------
            None
        
        """
        self.data = self.distributor.distribute_data(data=data, *args, **kwargs)
    

    def get_local_data(self, key=(slice(None),)):
        """
            Loads data directly from the local data attribute. No consolidation 
            is done. 

            Parameters
            ----------
            key : int, slice, tuple of int or slice
                The key which will be used to access the data. 
            
            Returns
            -------
            self.data[key] : numpy.ndarray
        
        """        
        return self.data[key]        
        
    def get_data(self, key, *args, **kwargs):
        """
            Loads data from the region which is specified by key. The data is 
            consolidated according to the distribution strategy. If the 
            individual nodes get different key-arguments, they get individual
            data. 
            
            Parameters
            ----------
        
            key : int, slice, tuple of int or slice
                The key is the object which specifies the region, where data 
                will be loaded from.                 
            
            Returns
            -------
            global_data[key] : numpy.ndarray
        
        """
        (slices, sliceified) = self.__sliceify__(key)        
        result= self.distributor.collect_data(self.data, slices, *args, **kwargs)        
        return self.__defold__(result, sliceified)
        
    
    
    def get_full_data(self, target_rank='all'):
        """
            Fully consolidates the distributed data. 
            
            Parameters
            ----------
            target_rank : 'all' (default), int *optional*
                If only one node should recieve the full data, it can be 
                specified here.
            
            Notes
            -----
            get_full_data() is equivalent to get_data(slice(None)) but 
            faster.
        
            Returns
            -------
            None
        """

        return self.distributor.consolidate_data(self.data, target_rank)

  
    def _get_distributor(self, distribution_strategy):
        '''
            Comments:
              - The distributor's get_data and set_data functions MUST be 
                supplied with a tuple of slice objects. In case that there was 
                a direct integer involved, the unfolding will be done by the
                helper functions __sliceify__, __enfold__ and __unfold__.
        '''
        
        distributor_dict={
            'fftw':     _fftw_distributor,
            'not':      _not_distributor
        }
        if not distributor_dict.has_key(distribution_strategy):
            raise TypeError(nifty_core.about._errors.cstring("ERROR: Unknown distribution strategy supplied."))
        return distributor_dict[distribution_strategy]
      
    def save(self, alias, path=None, overwriteQ=True):
        
        """
            Saves a distributed_data_object to disk utilizing h5py.
            
            Parameters
            ----------
            alias : string
                The name for the dataset which is saved within the hdf5 file.
         
            path : string *optional*
                The path to the hdf5 file. If no path is given, the alias is 
                taken as filename in the current path.
            
            overwriteQ : Boolean *optional*
                Specifies whether a dataset may be overwritten if it is already
                present in the given hdf5 file or not.
        """
        self.distributor.save_data(self.data, alias, path, overwriteQ)

    def load(self, alias, path=None):
        """
            Loads a distributed_data_object from disk utilizing h5py.
            
            Parameters
            ----------
            alias : string
                The name of the dataset which is loaded from the hdf5 file.
 
            path : string *optional*
                The path to the hdf5 file. If no path is given, the alias is 
                taken as filename in the current path.
        """
        self.data = self.distributor.load_data(alias, path)
           
    def __sliceify__(self, inp):
        sliceified = []
        result = []
        if isinstance(inp, tuple):
            x = inp
        else:
            x = (inp, )
        
        for i in range(len(x)):
            if isinstance(x[i], slice):
                result += [x[i], ]
                sliceified += [False, ]
            else:
                result += [slice(x[i], x[i]+1), ]
                sliceified += [True, ]
    
        return (tuple(result), sliceified)
                
                
    def __enfold__(self, in_data, sliceified):
        data = np.array(in_data, copy=False)    
        temp_shape = ()
        j=0
        for i in sliceified:
            if i == True:
                temp_shape += (1,)
            else:
                temp_shape += (data.shape[j],)
                j += 1
        ## take into account that the sliceified tuple may be too short, because 
        ## of a non-exaustive list of slices
        for i in range(len(data.shape)-j):
            temp_shape += (data.shape[j],)
            j += 1
            
        return data.reshape(temp_shape)
    
    def __defold__(self, data, sliceified):
        temp_slice = ()
        for i in sliceified:
            if i == True:
                temp_slice += (0,)
            else:
                temp_slice += (slice(None),)
        return data[temp_slice]

    

   
class _fftw_distributor(object):
    def __init__(self, global_data=None, global_shape=None, dtype=None, comm=MPI.COMM_WORLD, alias=None, path=None):
        
        if alias != None:
            file_path = path if path != None else alias 
            if found['h5py_parallel']:
                f = h5py.File(file_path, 'r', driver='mpio', comm=comm)
            else:
                f= h5py.File(file_path, 'r')        
            dset = f[alias]        

        
        if comm.rank == 0:        
            if alias != None:
                self.global_shape = dset.shape
            else:                
                if global_data == None:
                    if global_shape == None:
                        raise TypeError(nifty_core.about._errors.cstring("ERROR: Neither data nor shape supplied!"))
                    else:
                        self.global_shape = global_shape
                else:
                    self.global_shape = global_data.shape


        else:
            self.global_shape = None
        
        
        self.global_shape = comm.bcast(self.global_shape, root = 0)
        self.global_shape = tuple(self.global_shape)
        
        if comm.rank == 0:        
            if alias != None:
                self.dtype = dset.dtype.type
            else:    
                if dtype != None:        
                    self.dtype = dtype
                elif global_data != None:
                    self.dtype = np.array(global_data).dtype.type
                else:
                    raise TypeError(nifty_core.about._errors.cstring("ERROR: Failed setting datatype. Neither data, nor datatype supplied."))
        else:
            self.dtype=None
        
        self.dtype = comm.bcast(self.dtype, root=0)
        if alias != None:        
            f.close()        
        
        self._my_dtype_converter = dtype_converter()
        
        if not self._my_dtype_converter.known_np_Q(self.dtype):
            raise TypeError(nifty_core.about._errors.cstring("ERROR: The data type is not known to mpi4py."))

        self.mpi_dtype  = self._my_dtype_converter.to_mpi(self.dtype)
        
        self._local_size = pyfftw.local_size(self.global_shape)
        self.local_start = self._local_size[2]
        self.local_end = self.local_start + self._local_size[1]
        self.local_length = self.local_end-self.local_start        
        self.local_shape = (self.local_length,) + tuple(self.global_shape[1:])
        self.local_dim = np.product(self.local_shape)
        self.local_dim_list = np.empty(comm.size, dtype=np.int)
        comm.Allgather([np.array(self.local_dim,dtype=np.int), MPI.INT], [self.local_dim_list, MPI.INT])
        self.local_dim_offset = np.sum(self.local_dim_list[0:comm.rank])
        
        self.local_slice = np.array([self.local_start, self.local_end, self.local_length, self.local_dim, self.local_dim_offset], dtype=np.int)
        ## collect all local_slices 
        ## [start, stop, length=stop-start, dimension, dimension_offset]
        self.all_local_slices = np.empty((comm.size,5),dtype=np.int)
        comm.Allgather([np.array((self.local_slice,),dtype=np.int), MPI.INT], [self.all_local_slices, MPI.INT])
        
        
    def distribute_data(self, data=None, comm = MPI.COMM_WORLD, alias=None, path=None):
        '''
        distribute data checks 
        - whether the data is located on all nodes or only on node 0
        - that the shape of 'data' matches the global_shape
        '''
        if data == None and found['h5py']:
            try: 
                file_path = path if path != None else alias 
                if found['h5py_parallel']:
                    f = h5py.File(file_path, 'r', driver='mpio', comm=comm)
                else:
                    f= h5py.File(file_path, 'r')        
                dset = f[alias]
                if dset.shape == self.global_shape and dset.dtype.type == self.dtype:
                    temp_data = dset[self.local_start:self.local_end]
                    f.close()
                    return temp_data
                else:
                    raise TypeError(nifty_core.about._errors.cstring("ERROR: Input data has the wrong shape or wrong dtype!"))                 
            except(IOError, AttributeError):
                pass
            
        rank = comm.Get_rank()
        size = comm.Get_size()        
        local_data_available_Q = np.array((int(data != None), ))
        data_available_Q = np.empty(size,dtype=int)
        comm.Allgather([local_data_available_Q, MPI.INT], [data_available_Q, MPI.INT])
        ## if all nodes got data, we assume that it is the right data and 
        ## store it individually. If not, take the data on node 0 and scatter it.
        if np.all(data_available_Q):
            return data[self.local_start:self.local_end].astype(self.dtype, copy=False)
        else:
            if data == None:
                data = np.empty(self.global_shape)            
            if rank == 0:
                if np.all(data.shape != self.global_shape):
                    raise TypeError(nifty_core.about._errors.cstring("ERROR: Input data has the wrong shape!"))
            ## Scatter the data!            
            _scattered_data = np.zeros(self.local_shape, dtype = self.dtype)
            _dim_list = self.all_local_slices[:,3]
            _dim_offset_list = self.all_local_slices[:,4]
            #comm.Scatterv([data.astype(np.float64, copy=False), _dim_list, _dim_offset_list, MPI.DOUBLE], [_scattered_data, MPI.DOUBLE], root=0)
            comm.Scatterv([data, _dim_list, _dim_offset_list, self.mpi_dtype], [_scattered_data, self.mpi_dtype], root=0)
            return _scattered_data
        return None
    
    def _disperse_data_primitive(self, data, data_update, slice_objects, source_rank='all', comm=MPI.COMM_WORLD):
        ## compute the part of the slice which is relevant for the individual node      
        localized_start, localized_stop = self._backshift_and_decycle(
            slice_objects[0], self.local_start)
        local_slice = (slice(localized_start,localized_stop,slice_objects[0].step),) + slice_objects[1:]
        
        ## compute the parameter sets and list for the data splitting
        local_slice_shape = data[local_slice].shape        
        local_affected_data_length = local_slice_shape[0]
        local_affected_data_length_list=np.empty(comm.size, dtype=np.int)        
        comm.Allgather([np.array(local_affected_data_length, dtype=np.int), MPI.INT], [local_affected_data_length_list, MPI.INT])        
        local_affected_data_length_offset_list = np.append([0],np.cumsum(local_affected_data_length_list)[:-1])
        
        
        if source_rank == 'all':
            ## only take the relevant part out of data_update and plug it into 
            ## data[local_slice]
            r = comm.rank
            o = local_affected_data_length_offset_list
            l = local_affected_data_length
            update_slice = (slice(o[r], o[r]+l),) 
            data[local_slice] = np.array(data_update[update_slice], copy=False).astype(self.dtype)
            
        else:
            ## Scatterv the relevant part from the source_rank to the others 
            ## and plug it into data[local_slice]
            local_affected_data_dim_list= np.array(local_affected_data_length_list) * np.product(local_slice_shape[1:])                    
            local_affected_data_dim_offset_list = np.append([0],np.cumsum(local_affected_data_dim_list)[:-1])
            local_dispersed_data = np.zeros(local_slice_shape, dtype=self.dtype)
            comm.Scatterv([np.array(data_update, copy=False).astype(self.dtype), local_affected_data_dim_list, local_affected_data_dim_offset_list, self.mpi_dtype],
                          [local_dispersed_data, self.mpi_dtype], 
                          root=source_rank)                            
            print (comm.rank, local_dispersed_data, local_slice)
            data[local_slice] = local_dispersed_data
        return None
        
    
    
    def disperse_data(self, data, data_update, slice_objects, comm=MPI.COMM_WORLD):
        
        slice_objects_list = comm.allgather(slice_objects)
        ## check if all slices are the same. 
        if all(x == slice_objects_list[0] for x in slice_objects_list):
            ## in this case, the _disperse_data_primitive can simply be called 
            ##with target_rank = 'all'
            self._disperse_data_primitive(data=data, data_update=data_update, slice_objects=slice_objects, source_rank='all', comm=comm)
        ## if the different nodes got different slices, disperse the data individually
        else:
            i = 0        
            for temp_slices in slice_objects_list:
                ## make the collect_data call on all nodes            
                self._disperse_data_primitive(data=data, data_update=data_update, slice_objects=temp_slices, source_rank=i, comm=comm)
                i += 1
                 
        
    def _collect_data_primitive(self, data, slice_objects, target_rank='all', comm=MPI.COMM_WORLD):
        localized_start, localized_stop = self._backshift_and_decycle(
            slice_objects[0], self.local_start)
        local_slice = (slice(localized_start,localized_stop,slice_objects[0].step),)+slice_objects[1:]
        local_collected_data = np.ascontiguousarray(data[local_slice])

        local_collected_data_length = local_collected_data.shape[0]
        local_collected_data_length_list=np.empty(comm.size, dtype=np.int)        
        comm.Allgather([np.array(local_collected_data_length, dtype=np.int), MPI.INT], [local_collected_data_length_list, MPI.INT])        
             
        collected_data_length = np.sum(local_collected_data_length_list) 
        collected_data_shape = (collected_data_length,)+local_collected_data.shape[1:]
        local_collected_data_dim_list= np.array(local_collected_data_length_list) * np.product(local_collected_data.shape[1:])        
        
        local_collected_data_dim_offset_list = np.append([0],np.cumsum(local_collected_data_dim_list)[:-1])
        collected_data = np.empty(collected_data_shape, dtype=self.dtype)

        if target_rank == 'all':
            comm.Allgatherv([local_collected_data, self.mpi_dtype], 
                         [collected_data, local_collected_data_dim_list, local_collected_data_dim_offset_list, self.mpi_dtype])                
        else:
            comm.Gatherv([local_collected_data, self.mpi_dtype], 
                         [collected_data, local_collected_data_dim_list, local_collected_data_dim_offset_list, self.mpi_dtype], root=target_rank)                            
        return collected_data

    def collect_data(self, data, slice_objects, comm=MPI.COMM_WORLD):
        slice_objects_list = comm.allgather(slice_objects)
        ## check if all slices are the same. 
        if all(x == slice_objects_list[0] for x in slice_objects_list):
            ## in this case, the _collect_data_primitive can simply be called 
            ##with target_rank = 'all'
            return self._collect_data_primitive(data=data, slice_objects=slice_objects, target_rank='all', comm=comm)
        
        ## if the different nodes got different slices, collect the data individually
        i = 0        
        for temp_slices in slice_objects_list:
            ## make the collect_data call on all nodes            
            temp_data = self._collect_data_primitive(data=data, slice_objects=temp_slices, target_rank=i, comm=comm)
            ## save the result only on the pulling node            
            if comm.rank == i:
                individual_data = temp_data
            i += 1
        return individual_data
        
    
    def _backshift_and_decycle(self, slice_object, shift):
        ## initialize the step and step_size_compensation
        if slice_object.step == None:
            step = 1
        else:
            step = slice_object.step
            
        ## calculate the start index
        if slice_object.start == None:
            local_start = (-shift)%step ## step size compensation
        else:
            local_start = slice_object.start - shift
            ## if the local_start is negative, pull it up to zero
            local_start = local_start%step if local_start < 0 else local_start
        ## calculate the stop index
        if slice_object.stop == None:
            local_stop = None
        else:
            local_stop = slice_object.stop - shift
            ## if local_stop is negative, pull it up to zero
            local_stop = 0 if local_stop < 0 else local_stop
                    ## Note: if start or stop are greater than the array length,
        ## numpy will automatically cut the index value down into the 
        ## array's range 
        return local_start, local_stop        
        
    def consolidate_data(self, data, target_rank='all', comm = MPI.COMM_WORLD):
        _gathered_data = np.empty(self.global_shape, dtype=self.dtype)
        _dim_list = self.all_local_slices[:,3]
        _dim_offset_list = self.all_local_slices[:,4]
        if target_rank == 'all':
            comm.Allgatherv([data, self.mpi_dtype], 
                         [_gathered_data, _dim_list, _dim_offset_list, self.mpi_dtype])                
        else:
            comm.Gatherv([data, self.mpi_dtype], 
                         [_gathered_data, _dim_list, _dim_offset_list, self.mpi_dtype],
                         root=target_rank)
        return _gathered_data
    
    if found['h5py']:
        def save_data(self, data, alias, path=None, overwriteQ=True, comm=MPI.COMM_WORLD):
            ## if no path and therefore no filename was given, use the alias as filename        
            use_path = alias if path==None else path
            
            ## create the file-handle
            if found['h5py_parallel']:
                f = h5py.File(use_path, 'a', driver='mpio', comm=comm)
            else:
                f= h5py.File(use_path, 'a')
            ## check if dataset with name == alias already exists
            try: 
                f[alias]
                if overwriteQ == False: #if yes, and overwriteQ is set to False, raise an Error
                    raise KeyError(nifty_core.about._errors.cstring("ERROR: overwriteQ == False, but alias already in use!"))
                else: # if yes, remove the existing dataset
                    del f[alias]
            except(KeyError):
                pass
            
            ## create dataset
            dset = f.create_dataset(alias, shape=self.global_shape, dtype=self.dtype)
            ## write the data
            dset[self.local_start:self.local_end] = data
            ## close the file
            f.close()
        
        def load_data(self, alias, path):
            ## create the file-handle
            if found['h5py_parallel']:
                f = h5py.File(path, 'r', driver='mpio', comm=comm)
            else:
                f= h5py.File(path, 'r')        
            dset = f[alias]        
            ## check shape
            if dset.shape != self.global_shape:
                raise TypeError(nifty_core.about._errors.cstring("ERROR: The shape of the given dataset does not match the distributed_data_object."))
            ## check dtype
            if dset.dtype.type != self.dtype:
                raise TypeError(nifty_core.about._errors.cstring("ERROR: The datatype of the given dataset does not match the distributed_data_object."))
            ## if everything seems to fit, load the data
            data = dset[self.local_start:self.local_end]
            ## close the file
            f.close()
            return data
    else:
        def save_data(self, *args, **kwargs):
            raise ImportError(nifty_core.about._errors.cstring("ERROR: h5py was not imported")) 
        def load_data(self, *args, **kwargs):
            raise ImportError(nifty_core.about._errors.cstring("ERROR: h5py was not imported")) 
        
        
        
        

class _not_distributor(object):
    def __init__(self, global_data=None, global_shape=None, dtype=None, *args,  **kwargs):
        if dtype != None:        
            self.dtype = dtype
        elif global_data != None:
            self.dtype = np.array(global_data).dtype.type
            
        if global_data != None:
            self.global_shape = np.array(global_data).shape
        elif global_shape != None:
            self.global_shape = global_shape
        else:
            raise TypeError(nifty_core.about._errors.cstring("ERROR: Neither data nor shape supplied!")) 
    def distribute_data(self, data, **kwargs):
        return np.array(data).astype(self.dtype, copy=False)
    
    def disperse_data(self, data, data_update, key):
        
        data[key] = np.array(data_update, copy=False).astype(self.dtype)
                     
    def collect_data(self, data, slice_object, *args,  **kwargs):
        return data[slice_object]
        
    def consolidate_data(self, data, *args, **kwargs):
        return data





class dtype_converter:
    """
        NIFTY class for dtype conversion between python/numpy dtypes and MPI
        dtypes.
    """
    
    def __init__(self):
        pre_dict = [
                    #[, MPI_CHAR],
                    #[, MPI_SIGNED_CHAR],
                    #[, MPI_UNSIGNED_CHAR],                                        
                    [np.int16, MPI.SHORT],
                    [np.uint16, MPI.UNSIGNED_SHORT],
                    [np.uint32, MPI.UNSIGNED_INT],                    
                    [np.int, MPI.INT],                    
                    [np.int32, MPI.INT],
                    [np.int64, MPI.LONG],
                    [np.uint64, MPI.UNSIGNED_LONG],
                    [np.int64, MPI.LONG_LONG],
                    [np.uint64, MPI.UNSIGNED_LONG_LONG],
                    [np.float32, MPI.FLOAT],
                    [np.float, MPI.DOUBLE],
                    [np.float64, MPI.DOUBLE],
                    [np.float128, MPI.LONG_DOUBLE],
                    [np.complex64, MPI.COMPLEX],
                    [np.complex, MPI.DOUBLE_COMPLEX],
                    [np.complex128, MPI.DOUBLE_COMPLEX]]
                    
        to_mpi_pre_dict = np.array(pre_dict)
        to_mpi_pre_dict[:,0] = map(self.dictionize_np, to_mpi_pre_dict[:,0])
        self._to_mpi_dict = dict(to_mpi_pre_dict)
        
        to_np_pre_dict = np.array(pre_dict)[:,::-1]
        to_np_pre_dict[:,0] = map(self.dictionize_mpi, to_np_pre_dict[:,0])
        self._to_np_dict = dict(to_np_pre_dict)

    def dictionize_np(self, x):
        return frozenset(x.__dict__.items())
        
    def dictionize_mpi(self, x):
        return x.name
    
    def to_mpi(self, dtype):
        return self._to_mpi_dict[self.dictionize_np(dtype)]

    def to_np(self, dtype):
        return self._to_np_dict[self.dictionize_mpi(dtype)]
    
    def known_mpi_Q(self, dtype):
        return self._to_np_dict.has_key(self.dictionize_mpi(dtype))
    
    def known_np_Q(self, dtype):
        return self._to_mpi_dict.has_key(self.dictionize_np(dtype))

class test(object):
844
    def __init__(self,x=None, *args, **kwargs):
ultimanet's avatar
ultimanet committed
845
        self.x =x
846
847
        print args
        print kwargs
ultimanet's avatar
ultimanet committed
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
    @property
    def val(self):
        return self.x
    
    @val.setter
    def val(self, x):
        self.x = x


if __name__ == '__main__':    
    comm = MPI.COMM_WORLD
    rank = comm.rank
    
    if rank == 0:
        x = np.arange(48).reshape((12,4)).astype(np.float64)
        print x
        #x = np.arange(3)
    else:
        x = None
    obj = distributed_data_object(global_data=x, distribution_strategy='fftw')
    
    
    #obj.load('myalias', 'mpitest.hdf5')
    if MPI.COMM_WORLD.rank==0:
        print ('rank', rank, vars(obj.distributor))
    MPI.COMM_WORLD.Barrier()
    print ('rank', rank, vars(obj))
    
    MPI.COMM_WORLD.Barrier()
    temp_erg =obj.get_full_data(target_rank='all')
    print ('rank', rank, 'full data', temp_erg, temp_erg.shape)
    
    MPI.COMM_WORLD.Barrier()
    if rank == 0:    
        print ('erwuenscht', x[slice(1,10,2)])
    sl = slice(1,2+rank,1)
    print ('slice', rank, sl, obj[sl,2])
    print obj[1:5:2,1:3]
    if rank == 0:
        sl = (slice(1,9,2), slice(1,5,2))
        d = [[111, 222],[333,444],[111, 222],[333,444]]
    else:
        sl = (slice(6,10,2), slice(1,5,2))
        d = [[555, 666],[777,888]]
    obj[sl] = d
    print obj.get_full_data()