def test_parallel_pdf(self): """check parallel PDFCalculator """ from diffpy.srreal.pdfcalculator import PDFCalculator pdfc = PDFCalculator() r0, g0 = pdfc(self.cdse) ppdfc1 = createParallelCalculator(PDFCalculator(), 3, map) r1, g1 = ppdfc1(self.cdse) self.assertTrue(numpy.array_equal(r0, r1)) self.assertTrue(numpy.allclose(g0, g1)) ppdfc2 = createParallelCalculator(PDFCalculator(), self.ncpu, self.pool.imap_unordered) r2, g2 = ppdfc2(self.cdse) self.assertTrue(numpy.array_equal(r0, r2)) self.assertTrue(numpy.allclose(g0, g2)) pdfc.rmax = ppdfc1.rmax = ppdfc2.rmax = 5 pdfc.qmax = ppdfc1.qmax = ppdfc2.qmax = 15 r0a, g0a = pdfc() self.assertTrue(numpy.all(r0a <= 5)) self.assertFalse(numpy.allclose(g0a, numpy.interp(r0a, r0, g0))) r1a, g1a = ppdfc1() self.assertTrue(numpy.array_equal(r0a, r1a)) self.assertTrue(numpy.allclose(g0a, g1a)) r2a, g2a = ppdfc2() self.assertTrue(numpy.array_equal(r0a, r2a)) self.assertTrue(numpy.allclose(g0a, g2a)) return
def test_parallel_bonds(self): """check parallel BondCalculator """ from diffpy.srreal.bondcalculator import BondCalculator nickel = self.nickel bc = BondCalculator() d0 = bc(nickel) pbc1 = createParallelCalculator(BondCalculator(), 3, map) d1 = pbc1(nickel) self.assertTrue(numpy.array_equal(d0, d1)) pbc2 = createParallelCalculator(BondCalculator(), self.ncpu, self.pool.imap_unordered) d2 = pbc2(nickel) self.assertTrue(numpy.array_equal(d0, d2)) bc.rmax = pbc1.rmax = pbc2.rmax = 2.5 for bci in (bc, pbc1, pbc2): bci.maskAllPairs(False) bci.setPairMask(0, 'all', True) bci.filterCone([1, 0, 0], 48) d0a = bc(nickel) self.assertEqual(8, len(d0a)) d1a = pbc1(nickel) self.assertTrue(numpy.array_equal(d0a, d1a)) d2a = pbc2(nickel) self.assertTrue(numpy.array_equal(d0a, d2a)) return
def test_parallel_bonds(self): """check parallel BondCalculator """ from diffpy.srreal.bondcalculator import BondCalculator nickel = self.nickel bc = BondCalculator() d0 = bc(nickel) pbc1 = createParallelCalculator(BondCalculator(), 3, map) d1 = pbc1(nickel) self.failUnless(numpy.array_equal(d0, d1)) pbc2 = createParallelCalculator(BondCalculator(), self.ncpu, self.pool.imap_unordered) d2 = pbc2(nickel) self.failUnless(numpy.array_equal(d0, d2)) bc.rmax = pbc1.rmax = pbc2.rmax = 2.5 for bci in (bc, pbc1, pbc2): bci.maskAllPairs(False) bci.setPairMask(0, 'all', True) bci.filterCone([1, 0, 0], 48) d0a = bc(nickel) self.assertEqual(8, len(d0a)) d1a = pbc1(nickel) self.failUnless(numpy.array_equal(d0a, d1a)) d2a = pbc2(nickel) self.failUnless(numpy.array_equal(d0a, d2a)) return
def test_parallel_pdf(self): """check parallel PDFCalculator """ from diffpy.srreal.pdfcalculator import PDFCalculator pdfc = PDFCalculator() r0, g0 = pdfc(self.cdse) ppdfc1 = createParallelCalculator(PDFCalculator(), 3, map) r1, g1 = ppdfc1(self.cdse) self.failUnless(numpy.array_equal(r0, r1)) self.failUnless(numpy.allclose(g0, g1)) ppdfc2 = createParallelCalculator(PDFCalculator(), self.ncpu, self.pool.imap_unordered) r2, g2 = ppdfc2(self.cdse) self.failUnless(numpy.array_equal(r0, r2)) self.failUnless(numpy.allclose(g0, g2)) pdfc.rmax = ppdfc1.rmax = ppdfc2.rmax = 5 pdfc.qmax = ppdfc1.qmax = ppdfc2.qmax = 15 r0a, g0a = pdfc() self.failUnless(numpy.all(r0a <= 5)) self.assertFalse(numpy.allclose(g0a, numpy.interp(r0a, r0, g0))) r1a, g1a = ppdfc1() self.failUnless(numpy.array_equal(r0a, r1a)) self.failUnless(numpy.allclose(g0a, g1a)) r2a, g2a = ppdfc2() self.failUnless(numpy.array_equal(r0a, r2a)) self.failUnless(numpy.allclose(g0a, g2a)) return
def parallel(self, ncpu, mapfunc = None): """Run calculation in parallel. ncpu -- Number of parallel processes. Revert to serial mode when 1. mapfunc -- A mapping function to use. If this is None (default), multiprocessing.Pool.imap_unordered will be used. No return value. """ from diffpy.srreal.parallel import createParallelCalculator calc_serial = self._calc if hasattr(calc_serial, 'pqobj'): calc_serial = calc_serial.pqobj # revert to serial calculator for ncpu <= 1 if ncpu <= 1: self._calc = calc_serial self._pool = None return # Why don't we let the user shoot his foot or test on single CPU? # ncpu = min(ncpu, multiprocessing.cpu_count()) if mapfunc is None: import multiprocessing self._pool = multiprocessing.Pool(ncpu) mapfunc = self._pool.imap_unordered self._calc = createParallelCalculator(calc_serial, ncpu, mapfunc) return
def parallel(self, ncpu, mapfunc=None): """Run calculation in parallel. ncpu -- Number of parallel processes. Revert to serial mode when 1. mapfunc -- A mapping function to use. If this is None (default), multiprocessing.Pool.imap_unordered will be used. No return value. """ from diffpy.srreal.parallel import createParallelCalculator calc_serial = self._calc if hasattr(calc_serial, 'pqobj'): calc_serial = calc_serial.pqobj # revert to serial calculator for ncpu <= 1 if ncpu <= 1: self._calc = calc_serial self._pool = None return # Why don't we let the user shoot his foot or test on single CPU? # ncpu = min(ncpu, multiprocessing.cpu_count()) if mapfunc is None: import multiprocessing self._pool = multiprocessing.Pool(ncpu) mapfunc = self._pool.imap_unordered self._calc = createParallelCalculator(calc_serial, ncpu, mapfunc) return
def test_parallel_evaluatortype(self): """check handling of the evaluatortype property """ from diffpy.srreal.pdfcalculator import PDFCalculator pdfc = PDFCalculator() self.assertEqual('OPTIMIZED', pdfc.evaluatortype) ppdfc = createParallelCalculator(pdfc, 2, map) self.assertEqual('BASIC', ppdfc.evaluatortype) self.assertEqual('BASIC', pdfc.evaluatortype) ppdfc.evaluatortype = 'BASIC' self.assertRaises(ValueError, setattr, ppdfc, 'evaluatortype', 'OPTIMIZED') return
def test_parallel(self): """check parallel run of OverlapCalculator """ import multiprocessing from diffpy.srreal.parallel import createParallelCalculator ncpu = 4 self.pool = multiprocessing.Pool(processes=ncpu) olc = self.olc polc = createParallelCalculator(OverlapCalculator(), ncpu, self.pool.imap_unordered) olc.atomradiitable.fromString('Ti:1.6, O:0.66') polc.atomradiitable = olc.atomradiitable self.assertTrue(numpy.array_equal(olc(self.rutile), polc(self.rutile))) self.assertTrue(olc.totalsquareoverlap > 0.0) self.assertEqual(olc.totalsquareoverlap, polc.totalsquareoverlap) self.assertEqual(sorted(zip(olc.sites0, olc.sites1)), sorted(zip(polc.sites0, polc.sites1))) olc.atomradiitable.resetAll() self.assertEqual(0.0, sum(olc(self.rutile))) self.assertEqual(0.0, sum(polc(self.rutile))) return
# number of CPUs for parallel calculation ncpu = multiprocessing.cpu_count() # calculate PDF on a single core t0 = time.time() pc0 = PDFCalculator(**cfg) r0, g0 = pc0(menthol) t0 = time.time() - t0 print "Calculation time on 1 CPU: %g" % t0 # create a pool of workers pool = multiprocessing.Pool(processes=ncpu) t1 = time.time() # create a proxy parrallel calculator to PDFCalculator pc0, # that uses ncpu parallel jobs submitted via pool.imap_unordered pc1 = createParallelCalculator(pc0, ncpu, pool.imap_unordered) r1, g1 = pc1(menthol) t1 = time.time() - t1 print "Calculation time on %i CPUs: %g" % (ncpu, t1) print "Time ratio: %g" % (t0 / t1) # plot both results and the difference curve from pylab import plot, show, clf, draw clf() gd = g0 - g1 plot(r0, g0, r1, g1, r0, gd - 3) show()
} # number of CPUs for parallel calculation ncpu = multiprocessing.cpu_count() # calculate PDF on a single core t0 = time.time() pc0 = PDFCalculator(**cfg) r0, g0 = pc0(menthol) t0 = time.time() - t0 print "Calculation time on 1 CPU: %g" % t0 # create a pool of workers pool = multiprocessing.Pool(processes=ncpu) t1 = time.time() # create a proxy parrallel calculator to PDFCalculator pc0, # that uses ncpu parallel jobs submitted via pool.imap_unordered pc1 = createParallelCalculator(pc0, ncpu, pool.imap_unordered) r1, g1 = pc1(menthol) t1 = time.time() - t1 print "Calculation time on %i CPUs: %g" % (ncpu, t1) print "Time ratio: %g" % (t0 / t1) # plot both results and the difference curve from matplotlib.pyplot import plot, show, clf, draw clf() gd = g0 - g1 plot(r0, g0, r1, g1, r0, gd - 3) show()