def testPackUnpack(self): for array, typecode1 in arrayimpl.subTest(self): if typecode1 in self.skipdtype: continue for typecode2 in array.TypeMap: if typecode2 in self.skipdtype: continue datatype1 = array.TypeMap[typecode1] datatype2 = array.TypeMap[typecode2] for items in range(10): # input and output arrays iarray1 = array(range(items), typecode1).as_raw() iarray2 = array(range(items), typecode2).as_raw() oarray1 = array(items, typecode1, items).as_raw() oarray2 = array(items, typecode2, items).as_raw() # temp array for packing size1 = datatype1.Pack_size(len(iarray1), self.COMM) size2 = datatype2.Pack_size(len(iarray2), self.COMM) tmpbuf = array(0, 'b', size1 + size2 + 1).as_raw() # pack input arrays position = 0 position = datatype1.Pack(iarray1, tmpbuf, position, self.COMM) position = datatype2.Pack(iarray2, tmpbuf, position, self.COMM) # unpack output arrays position = 0 position = datatype1.Unpack(tmpbuf, position, oarray1, self.COMM) position = datatype2.Unpack(tmpbuf, position, oarray2, self.COMM) # test self.assertTrue(allclose(iarray1, oarray1)) self.assertTrue(allclose(iarray2, oarray2))
def testExscan(self): size = self.COMM.Get_size() rank = self.COMM.Get_rank() for array, typecode in arrayimpl.subTest(self): for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN): if skip_op(typecode, op): continue buf = array(range(size), typecode) try: self.COMM.Exscan(MPI.IN_PLACE, buf.as_mpi(), op) except NotImplementedError: self.skipTest('mpi-exscan') if rank == 1: for i, value in enumerate(buf): self.assertEqual(value, i) elif rank > 1: max_val = maxvalue(buf) for i, value in enumerate(buf): if op == MPI.SUM: if (i * rank) < max_val: self.assertAlmostEqual(value, i * rank) elif op == MPI.PROD: if (i ** rank) < max_val: self.assertAlmostEqual(value, i ** rank) elif op == MPI.MAX: self.assertEqual(value, i) elif op == MPI.MIN: self.assertEqual(value, i)
def testReduce(self): size = self.COMM.Get_size() rank = self.COMM.Get_rank() for array, typecode in arrayimpl.subTest(self): for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN): if skip_op(typecode, op): continue for root in range(size): sbuf = array(range(size), typecode) rbuf = array(-1, typecode, size) self.COMM.Reduce(sbuf.as_mpi(), rbuf.as_mpi(), op, root) max_val = maxvalue(rbuf) for i, value in enumerate(rbuf): if rank != root: self.assertEqual(value, -1) continue if op == MPI.SUM: if (i * size) < max_val: self.assertAlmostEqual(value, i*size) elif op == MPI.PROD: if (i ** size) < max_val: self.assertAlmostEqual(value, i**size) elif op == MPI.MAX: self.assertEqual(value, i) elif op == MPI.MIN: self.assertEqual(value, i)
def testPackSize(self): for array, typecode in arrayimpl.subTest(self): if typecode in self.skipdtype: continue datatype = array.TypeMap[typecode] itemsize = datatype.Get_size() overhead = datatype.Pack_external_size(EXT32, 0) for count in range(10): pack_size = datatype.Pack_external_size(EXT32, count) real_size = pack_size - overhead
def testPackSize(self): for array, typecode in arrayimpl.subTest(self): if typecode in self.skipdtype: continue datatype = array.TypeMap[typecode] itemsize = datatype.Get_size() overhead = datatype.Pack_size(0, self.COMM) for count in range(10): pack_size = datatype.Pack_size(count, self.COMM) self.assertEqual(pack_size - overhead, count * itemsize)
def testReduceLocalBadCount(self): for array, typecode in arrayimpl.subTest(self): if unittest.is_mpi_gpu('openmpi', array): continue for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN): sbuf = array(range(3), typecode) rbuf = array(range(3), typecode) def f(): op.Reduce_local(sbuf.as_mpi_c(2), rbuf.as_mpi_c(3)) self.assertRaises(ValueError, f) def f(): op.Reduce_local([sbuf.as_raw(), 1, MPI.INT], [rbuf.as_raw(), 1, MPI.SHORT]) self.assertRaises(ValueError, f)
def testGather(self): size = self.COMM.Get_size() rank = self.COMM.Get_rank() for array, typecode in arrayimpl.subTest(self): for root in range(size): sbuf = array(root, typecode, root+1) if rank == root: rbuf = array(-1, typecode, (size,root+1)) else: rbuf = array([], typecode) self.COMM.Gather(sbuf.as_mpi(), rbuf.as_mpi(), root=root) if rank == root: for value in rbuf.flat: self.assertEqual(value, root)
def testScatterv2(self): size = self.COMM.Get_size() rank = self.COMM.Get_rank() for array, typecode in arrayimpl.subTest(self): for root in range(size): for count in range(size): sbuf = array(root, typecode, size*size) rbuf = array( -1, typecode, size) sendbuf = sbuf.as_mpi_v(count, size) recvbuf = rbuf.as_mpi_c(count) if rank != root: sendbuf = None self.COMM.Scatterv(sendbuf, recvbuf, root) a, b = rbuf[:count], rbuf[count:] for va in a: self.assertEqual(va, root) for vb in b: self.assertEqual(vb, -1)