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 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 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 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 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 _converged(self, it, p, m, n): if(self.isMaster()): converged = super(MpiParticleSwarmOptimizer, self)._converged(it, p, m, n) else: converged = False converged = mpiBCast(converged) return converged
def _converged(self, it, p, m, n): if (self.isMaster()): converged = super(MpiParticleSwarmOptimizer, self)._converged(it, p, m, n) else: converged = False converged = mpiBCast(converged) return converged
def _get_fitness(self, swarm): mapFunction = self.pool.map mpiSwarm = mpiBCast(swarm) pos = numpy.array([part.position for part in mpiSwarm]) results = mapFunction(self.func, pos) lnprob = numpy.array([l[0] for l in results]) for i, particle in enumerate(swarm): particle.fitness = lnprob[i] particle.position = pos[i]
def _get_fitness(self,swarm): mapFunction = self.pool.map mpiSwarm = mpiBCast(swarm) pos = numpy.array([part.position for part in mpiSwarm]) results = mapFunction(self.func, pos) lnprob = numpy.array([l[0] for l in results]) for i, particle in enumerate(swarm): particle.fitness = lnprob[i] particle.position = pos[i]
for i, cswarm in enumerate( pso.sample(maxIter=maxIter, p=fracPart, m=tol, n=meanNorm)): if (pso.isMaster()): for particle in cswarm: f.write("%s\t%f\t" % (i, particle.fitness)) f.write("\t".join( ['{:025.20f}'.format(p) for p in particle.position]) + "\n") f.flush() #--------- compute differences ------------ if (pso.isMaster()): log(logger, "Iteration: %s .Time: %s seconds" % (count, time() - t1)) log(logger, "Best found: %f \n %s" % (pso.gbest.fitness, pso.gbest.position)) p0 = np.array([part.position for part in pso.swarm]) p0 = mpiBCast(p0) smp.reset() print "emcee" for pos, prob, rstate, datas in smp.sample(p0, iterations=mcmc_it, storechain=False): if (pso.isMaster()): for i in range(partCount): f.write("%s\t%f\t" % (-count, prob[i])) f.write("\t".join(['{:025.20f}'.format(q) for q in pos[i]]) + "\n") f.flush() if (pso.isMaster()): delta = np.abs(1.0 - np.abs(pso.gbest.fitness / ofly_lnp)) ofly_lnp = pso.gbest.fitness
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())