Skip to content

Instantly share code, notes, and snippets.

@pedrovanzella
Created October 13, 2014 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrovanzella/29fa2274c0ece35e74e7 to your computer and use it in GitHub Desktop.
Save pedrovanzella/29fa2274c0ece35e74e7 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import numpy
from mpi4py import MPI
from bubblesort import bubblesort
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
status = MPI.Status()
ARRAYSIZE = 40
DELTA = 10
UNSORTED_TAG = 1
SORTED_TAG = 2
# se sou o zero, não tenho pai
if rank == 0:
# Gerar vetor aleatorio
data = numpy.random.random_integer(0, ARRAYSIZE,
ARRAYSIZE).astype(numpy.int32)
# divide e envia pra dois filhos
comm.Send([data[:ARRAYSIZE/2], ARRAYSIZE / 2, MPI.INT], dest=1,
tag=UNSORTED_TAG)
comm.Send([data[ARRAYSIZE/2:], ARRAYSIZE / 2, MPI.INT], dest=2,
tag=UNSORTED_TAG)
recvd_parts = 0
numparts = (rank + 1) * 2
while recvd_parts < numparts:
# Tem algo pra mim?
comm.Probe(source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=status)
source = status.Get_source()
size = status.Get_count()
tag = status.Get_tag()
if tag == UNSORTED_TAG:
# Tenho que dividir ou conquistar?
if size <= DELTA:
# Conquista
pass
else:
# Divide e manda
pass
elif tag == SORTED_TAG:
# inicio do subvetor
start = ARRAYSIZE / numparts * (source - 1)
# fim do subvetor
end = ARRAYSIZE / numparts * source
comm.Recv([data[start:end], ARRAYSIZE / numparts, MPI.INT],
source=source, tag=MPI.ANY_TAG)
recvd_parts += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment