Skip to content

Commit

Permalink
Add missing dot_product.py exercise file.
Browse files Browse the repository at this point in the history
  • Loading branch information
McGill HPC Software Manager committed Oct 6, 2016
1 parent 6c7486a commit abce5b2
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions python/dot_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import sys
import numpy as np
from mpi4py import MPI

n = 8400

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

if n % size > 0:
if rank == 0:
sys.stderr.write("N must be a multiple of size\n")
comm.Abort(999)

if rank == 0:
a = np.arange(1, n+1)
b = n + 1 - a
else:
a = None
b = None

nloc = n // size
aloc = np.empty(nloc, int)
bloc = np.empty(nloc, int)

comm.Scatter(..., ..., root=0)
comm.Scatter(..., ..., root=0)

prodloc = np.dot(aloc, bloc)
prod = comm.reduce(..., op=MPI.SUM, root=...)

if rank == 0:
print("Inner product = %d"%prod)
print("Reference value = %d"%(n * (n + 1) * (n + 2) // 6))

0 comments on commit abce5b2

Please sign in to comment.