Example #1
0
def run_main_parallel_algorithm(comm, rank, n, p, my_chunk):
    # print "Run main parallel with rank=", rank, "n=", n, "p=", p, \
    #     "chunks=", [star.id for star in my_chunk]

    forces = calculate_forces(my_chunk)
    chunk_to_send = my_chunk

    recv_rank, send_rank = _calculate_send_recv_rank_index(rank, p)

    for _ in range(p - 1):
        if rank == 0:
            comm.send(chunk_to_send, dest=send_rank)
            recv_chunk = comm.recv(source=recv_rank)
        else:
            recv_chunk = comm.recv(source=recv_rank)
            comm.send(chunk_to_send, dest=send_rank)

        # print "Process rank=", rank, "received chunk=", \
        #     [star.id for star in recv_chunk]

        for star in my_chunk:
            star_current_force = forces[star.id]
            star_update_force = calculate_force(star, recv_chunk)
            forces[star.id] = (star_current_force[0] + star_update_force[0],
                               star_current_force[1] + star_update_force[1],
                               star_current_force[2] + star_update_force[2])
        chunk_to_send = recv_chunk

    # print "Process rank=", rank, "calculated force=", forces
    return forces
Example #2
0
    def test_calculate_force_case1(self):
        s1 = Star(1, 10, (0, 0, 0))
        s2 = Star(2, 10, (0, 1, 0))
        s3 = Star(3, 5, (0, 1, 0))

        actual_forces = calculate_force(s1, [s2, s3])

        expected_result = (0, 150*G, 0)
        self.assertEqual(actual_forces, expected_result)