Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

 

from cosmoHammer import CosmoHammerSampler 

 

from cosmoHammer.util.SampleFileUtil import SampleFileUtil 

from cosmoHammer.util.MpiUtil import MpiPool, mpiBCast 

 

class MpiCosmoHammerSampler(CosmoHammerSampler): 

""" 

A sampler implementation extending the regular sampler in order to allow for distributing  

the computation with MPI. 

 

:param kwargs:  

key word arguments passed to the CosmoHammerSampler 

 

""" 

def __init__(self, **kwargs): 

""" 

CosmoHammer sampler implementation 

 

""" 

self.pool = MpiPool(self._getMapFunction()) 

self.rank = self.pool.rank 

 

super(MpiCosmoHammerSampler, self).__init__(pool=self.pool, **kwargs) 

 

 

 

def _getMapFunction(self): 

""" 

Returns the build in map function 

""" 

return map 

 

def createSampleFileUtil(self): 

""" 

Returns a new instance of a File Util 

""" 

return SampleFileUtil(self.filePrefix, self.isMaster(), reuseBurnin=self.reuseBurnin) 

 

 

def sampleBurnin(self, p0): 

""" 

Starts the sampling process. The master node (mpi rank = 0) persists the result to the disk 

""" 

p0 = mpiBCast(p0) 

 

self.log("MPI Process rank "+ str(self.rank)+" starts sampling") 

return super(MpiCosmoHammerSampler, self).sampleBurnin(p0); 

 

def sample(self, burninPos, burninProb, burninRstate, datas): 

""" 

Starts the sampling process. The master node (mpi rank = 0) persists the result to the disk 

""" 

burninPos = mpiBCast(burninPos) 

burninProb = mpiBCast(burninProb) 

burninRstate = mpiBCast(burninRstate) 

 

self.log("MPI Process rank "+ str(self.rank)+" starts sampling") 

super(MpiCosmoHammerSampler, self).sample(burninPos, burninProb, burninRstate, datas); 

 

 

def loadBurnin(self): 

""" 

loads the burn in form the file system 

""" 

if(self.isMaster()): 

pos, prob, rstate = super(MpiCosmoHammerSampler, self).loadBurnin() 

else: 

pos, prob, rstate = [] 

 

pos = mpiBCast(pos) 

prob = mpiBCast(prob) 

rstate = mpiBCast(rstate) 

 

self.log("loading done") 

return pos, prob, rstate 

 

def createInitPos(self): 

""" 

Factory method to create initial positions 

""" 

#bcast the positions to ensure that all mpi nodes start at the same position 

return mpiBCast(super(MpiCosmoHammerSampler, self).createInitPos()) 

 

 

def isMaster(self): 

""" 

Returns true if the rank is 0 

""" 

return self.pool.isMaster()