-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing dot_product.py exercise file.
- 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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |