Python mpi4py.MPI.INT Examples

The following are 11 code examples of mpi4py.MPI.INT(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module mpi4py.MPI , or try the search function .
Example #1
Source File: distributed.py    From veros with MIT License 7 votes vote down vote up
def get_array_buffer(vs, arr):
    from mpi4py import MPI

    MPI_TYPE_MAP = {
        'int8': MPI.CHAR,
        'int16': MPI.SHORT,
        'int32': MPI.INT,
        'int64': MPI.LONG,
        'int128': MPI.LONG_LONG,
        'float32': MPI.FLOAT,
        'float64': MPI.DOUBLE,
        'bool': MPI.BOOL,
    }

    if rs.backend == 'bohrium':
        if np.check(arr):
            buf = np.interop_numpy.get_array(arr)
        else:
            buf = arr
    else:
        buf = arr

    return [buf, arr.size, MPI_TYPE_MAP[str(arr.dtype)]] 
Example #2
Source File: parallel.py    From PyDeepGP with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def numpy_to_MPI_typemap(np_type):
    from mpi4py import MPI
    typemap = {
        np.dtype(np.float64) : MPI.DOUBLE,
        np.dtype(np.float32) : MPI.FLOAT,
        np.dtype(np.int)     : MPI.INT,
        np.dtype(np.int8)    : MPI.CHAR,
        np.dtype(np.uint8)   : MPI.UNSIGNED_CHAR,
        np.dtype(np.int32)   : MPI.INT,
        np.dtype(np.uint32)  : MPI.UNSIGNED_INT,
    }
    return typemap[np_type] 
Example #3
Source File: pflee.py    From flee-release with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def CalcCommWorldTotal(self, np_array):
    assert np_array.size > 0

    total = np.zeros(np_array.size, dtype='i')

    #print(self.rank, type(total), type(np_array), total, np_array, np_array.size)
    # If you want this number on rank 0, just use Reduce.
    self.comm.Allreduce([np_array, MPI.INT], [total, MPI.INT], op=MPI.SUM)

    return total 
Example #4
Source File: nonblocking.py    From pySDC with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def main():

    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    size = comm.Get_size()

    data = None
    if rank == 0:
        data = np.array([1, 2, 3])
        req = comm.isend(data, dest=1)
        req.wait()
    elif rank == 1:
        data = comm.recv(source=0)
        print(data)
    #
    # if rank == 0:
    #     n = 10
    #     for i in range(n):
    #         tmp = np.array(i, dtype=int)
    #         # print(f'{rank} sending {i} to {1} - START')
    #         comm.send(tmp, dest=1)
    #         # req = comm.isend(tmp, dest=1, tag=i)
    #         # req = comm.Isend((tmp, MPI.INT), dest=1, tag=i)
    #         # req.wait()
    #         # print(f'{rank} sending {i} to {1} - STOP')
    # elif rank == 1:
    #     pass 
Example #5
Source File: backend_tests_mpi_model_mpi.py    From abcpy with BSD 3-Clause Clear License 5 votes vote down vote up
def test_map(self):
        def square_mpi(x, npc=None):
            local_res = numpy.array([2*(x**2)], 'i')
            #global_res = numpy.array([0], 'i')
            #MPI.COMM_WORLD.Reduce([local_res,MPI.INT], [global_res,MPI.INT], op=MPI.SUM, root=0)
            return local_res[0]
        
        data = [1,2,3,4,5]
        pds = backend_mpi.parallelize(data)
        pds_map = backend_mpi.map(square_mpi, pds)
        res = backend_mpi.collect(pds_map)
        assert res==list(map(lambda x:2*(x**2),data)) 
Example #6
Source File: backend_tests_mpi_model_mpi.py    From abcpy with BSD 3-Clause Clear License 5 votes vote down vote up
def test_function_pickle(self):

        def square_mpi(x, npc=None):
            local_res = numpy.array([2*(x**2)], 'i')
            #global_res = numpy.array([0], 'i')
            #model_comm.Reduce([local_res,MPI.INT], [global_res,MPI.INT], op=MPI.SUM, root=0)
            return local_res[0]

        class staticfunctest_mpi:
            @staticmethod 
            def square_mpi(x, npc=None):
                local_res = numpy.array([2*(x**2)], 'i')
                #global_res = numpy.array([0], 'i')
                #model_comm.Reduce([local_res,MPI.INT], [global_res,MPI.INT], op=MPI.SUM, root=0)
                return local_res[0]

        class nonstaticfunctest_mpi:
            def square_mpi(self, x, npc=None):
                local_res = numpy.array([2*(x**2)], 'i')
                #global_res = numpy.array([0], 'i')
                #model_comm.Reduce([local_res,MPI.INT], [global_res,MPI.INT], op=MPI.SUM, root=0)
                return local_res[0]

        data = [1,2,3,4,5]
        expected_result = [2,8,18,32,50]

        pds = backend_mpi.parallelize(data)
        pds_map1 = backend_mpi.map(square_mpi,pds)
        pds_res1 = backend_mpi.collect(pds_map1)
        
        self.assertTrue(pds_res1==expected_result,"Failed pickle test for general function")

        pds_map3 = backend_mpi.map(staticfunctest_mpi.square_mpi,pds)
        pds_res3 = backend_mpi.collect(pds_map3)
        self.assertTrue(pds_res3==expected_result,"Failed pickle test for static function")

        obj = nonstaticfunctest_mpi()
        pds_map4 = backend_mpi.map(obj.square_mpi ,pds)
        pds_res4 = backend_mpi.collect(pds_map4)
        self.assertTrue(pds_res4==expected_result,"Failed pickle test for non-static function") 
Example #7
Source File: plearn.py    From tribeflow with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def paired_update(comm, previous_encounters_s, Count_sz_local, Count_sz_pair, \
        Count_sz_others, P_local, P_pair):
    
    rank = comm.rank
    comm.isend(rank, dest=MASTER, tag=Msg.PAIRME.value)
    pair_id = comm.recv(source=MASTER, tag=Msg.PAIRED.value)
    
    if pair_id == rank: #Paired with self, do nothing
        return False
    
    elif pair_id < rank:
        comm.Recv([Count_sz_pair, MPI.INT], source=pair_id)
        comm.Recv([P_pair, MPI.DOUBLE], source=pair_id)
        
        comm.Send([Count_sz_local, MPI.INT], dest=pair_id)
        comm.Send([P_local, MPI.DOUBLE], dest=pair_id)
    else:
        comm.Send([Count_sz_local, MPI.INT], dest=pair_id)
        comm.Send([P_local, MPI.DOUBLE], dest=pair_id)
        
        comm.Recv([Count_sz_pair, MPI.INT], source=pair_id)
        comm.Recv([P_pair, MPI.DOUBLE], source=pair_id)

    #Update Counts
    #[:] is to avoid copies of arrays. Make sure we dont lose anything
    N_til_s = previous_encounters_s[pair_id]
    Count_sz_others[:] = Count_sz_others + Count_sz_pair - N_til_s

    N_til_s[:] = Count_sz_pair
    P_local[:] = (P_local + P_pair) / 2.0
    
    return True 
Example #8
Source File: plearn.py    From tribeflow with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def receive_workload(comm):
    sizes = np.zeros(6, dtype='i')
    comm.Recv([sizes, MPI.INT], source=MASTER)

    num_lines = sizes[0]
    nz = sizes[1]
    nh = sizes[2]
    ns = sizes[3]
    n_residency_priors = sizes[4]
    mem_size = sizes[5]

    Count_zh = np.zeros(shape=(nz, nh), dtype='i4') 
    Count_sz = np.zeros(shape=(ns, nz), dtype='i4')
    count_h = np.zeros(shape=(nh, ), dtype='i4')
    count_z = np.zeros(shape=(nz, ), dtype='i4')
    
    Dts = np.zeros(shape=(num_lines, mem_size), dtype='f8')
    Trace = np.zeros(shape=(num_lines, 2 + (mem_size + 1)), dtype='i4')

    comm.Recv([Dts, MPI.DOUBLE], source=MASTER)
    comm.Recv([Trace, MPI.INT], source=MASTER)
    priors = np.zeros(2 + n_residency_priors, dtype='f8')
    comm.Recv([priors, MPI.DOUBLE], source=MASTER)
    
    alpha_zh = priors[0]
    beta_zs = priors[1]
    residency_priors = priors[2:]
    kernel_class = comm.recv(source=MASTER)
    P = np.zeros(shape=(nz, n_residency_priors), dtype='f8')
    comm.Recv([P, MPI.DOUBLE], source=MASTER)

    kernel = kernel_class()
    kernel.build(Trace.shape[0], Count_zh.shape[0], residency_priors)
    if n_residency_priors > 0:
        kernel.update_state(P)
    
    return Dts, Trace, Count_zh, Count_sz, \
            count_h, count_z, alpha_zh, beta_zs, kernel 
Example #9
Source File: plearn.py    From tribeflow with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def work():
    comm = MPI.COMM_WORLD
    rank = comm.rank
    
    #pr = cProfile.Profile()
    #pr.enable()

    while True:
        status = MPI.Status()
        msg = comm.recv(source=MASTER, tag=MPI.ANY_TAG, status=status)
        event = status.Get_tag()

        if event == Msg.LEARN.value:
            comm.isend(rank, dest=MASTER, tag=Msg.STARTED.value)

            num_iter = msg

            Dts, Trace, Count_zh, Count_sz, count_h, count_z, \
                    alpha_zh, beta_zs, kernel = receive_workload(comm)
            fast_populate(Trace, Count_zh, Count_sz, count_h, \
                    count_z)
            sample(Dts, Trace, Count_zh, Count_sz, count_h, \
                    count_z, alpha_zh, beta_zs, kernel, num_iter, \
                    comm)
            
            comm.isend(rank, dest=MASTER, tag=Msg.FINISHED.value)
        elif event == Msg.SENDRESULTS.value:
            comm.Send([np.array(Trace[:, -1], order='C'), MPI.INT], dest=MASTER)
            comm.Send([Count_zh, MPI.INT], dest=MASTER)
            comm.Send([Count_sz, MPI.INT], dest=MASTER)
            comm.Send([count_h, MPI.INT], dest=MASTER)
            comm.Send([count_z, MPI.INT], dest=MASTER)
            comm.Send([kernel.get_state(), MPI.DOUBLE], dest=MASTER)
        elif event == Msg.STOP.value:
            break
        else:
            print('Unknown message received', msg, event, Msg(event))

    #pr.disable()
    #pr.dump_stats('worker-%d.pstats' % rank) 
Example #10
Source File: plearn.py    From tribeflow with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def dispatch_jobs(Dts, Trace, Count_zh, Count_sz, count_h, \
        count_z, alpha_zh, beta_zs, kernel, residency_priors, \
        workloads, num_workers, comm):
    
    for worker_id in xrange(1, num_workers + 1):
        idx = workloads[worker_id - 1]
        
        sizes = np.zeros(6, dtype='i')
        sizes[0] = Trace[idx].shape[0] 
        sizes[1] = Count_zh.shape[0]
        sizes[2] = Count_zh.shape[1]
        sizes[3] = Count_sz.shape[0]
        sizes[4] = residency_priors.shape[0]
        sizes[5] = Dts.shape[1]

        comm.Send([sizes, MPI.INT], dest=worker_id)
        comm.Send([Dts[idx], MPI.INT], dest=worker_id)
        comm.Send([Trace[idx], MPI.INT], dest=worker_id)

        priors = np.zeros(2 + residency_priors.shape[0], dtype='f8')
        priors[0] = alpha_zh
        priors[1] = beta_zs
        priors[2:] = residency_priors

        comm.Send([priors, MPI.DOUBLE], dest=worker_id)
        comm.send(kernel.__class__, dest=worker_id)
        comm.Send([kernel.get_state(), MPI.DOUBLE], dest=worker_id) 
Example #11
Source File: plearn.py    From tribeflow with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def fetch_results(comm, num_workers, workloads, Dts, Trace, \
        previous_stamps, Count_zh, Count_sz, count_h, count_z, \
        alpha_zh, beta_zs, Theta_zh, Psi_sz, kernel):
    
    Count_zh[:] = 0
    Count_zh_buff = np.zeros_like(Count_zh)

    Count_sz[:] = 0
    Count_sz_buff = np.zeros_like(Count_sz)

    count_h[:] = 0
    count_h_buff = np.zeros_like(count_h)

    count_z[:] = 0
    count_z_buff = np.zeros_like(count_z)

    P = kernel.get_state()
    P[:] = 0
    P_buff = np.zeros_like(P)
        
    for worker_id in xrange(1, num_workers + 1):
        comm.isend(worker_id, dest=worker_id, tag=Msg.SENDRESULTS.value)
        
        idx = workloads[worker_id - 1]
        assign = np.zeros(Trace[idx].shape[0], dtype='i')
        comm.Recv([assign, MPI.INT], source=worker_id)
        Trace[:, -1][idx] = assign

        comm.Recv([Count_zh_buff, MPI.INT], source=worker_id)
        Count_zh += Count_zh_buff
        
        comm.Recv([Count_sz_buff, MPI.INT], source=worker_id)
        Count_sz += Count_sz_buff

        comm.Recv([count_h_buff, MPI.INT], source=worker_id)
        count_h += count_h_buff
        
        comm.Recv([count_z_buff, MPI.INT], source=worker_id)
        count_z += count_z_buff
        
        comm.Recv([P_buff, MPI.DOUBLE], source=worker_id)
        P += P_buff
    
    P[:] = P / num_workers
    kernel.update_state(P)
    Theta_zh[:] = 0
    Psi_sz[:] = 0

    _aggregate(Count_zh, Count_sz, count_h, count_z, \
        alpha_zh, beta_zs, Theta_zh, Psi_sz)
    
    Theta_zh[:] = Theta_zh / Theta_zh.sum(axis=0)
    Psi_sz[:] = Psi_sz / Psi_sz.sum(axis=0)

    for z in xrange(Count_zh.shape[0]):
        previous_stamps._clear_one(z)
        #dts_assigned = Dts[Trace[:, -1] == z].ravel().copy()
        #np.sort(dts_assigned)
        previous_stamps._extend(z, Dts[Trace[:, -1] == z][:, -1])