-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement permute_dims #12
Merged
fschlimb
merged 15 commits into
IntelPython:main
from
AllanZyne:review/yang/permute_dims
Nov 4, 2024
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e4895f0
implement permute_dims
AllanZyne b9f831f
udpate imex
AllanZyne 8622fa0
clean code
AllanZyne 59422b2
fix comments
AllanZyne ff59b26
Merge branch 'main' into review/yang/permute_dims
AllanZyne 126c69e
update imex version
AllanZyne c55707a
update imex version
AllanZyne c208ce6
Update imex_version.txt
AllanZyne 5af4fc4
add an example: transpose.py
AllanZyne d886e84
update transpose.py
AllanZyne ec87a55
update transpose.py
AllanZyne 24da4fd
clean code
AllanZyne 8865c10
remove arange
AllanZyne afdc514
update np transopose
AllanZyne 809c0ff
move sync outside
AllanZyne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,184 @@ | ||
""" | ||
Transpose benchmark | ||
|
||
Matrix transpose benchmark for sharpy and numpy backends. | ||
|
||
Examples: | ||
|
||
# Run 1000 iterations of 1000*1000 matrix on sharpy backend | ||
python transpose.py -r 10 -c 1000 -b sharpy -i 1000 | ||
|
||
# MPI parallel run | ||
mpiexec -n 3 python transpose.py -r 1000 -c 1000 -b sharpy -i 1000 | ||
|
||
""" | ||
|
||
import argparse | ||
import time as time_mod | ||
|
||
import numpy | ||
|
||
import sharpy | ||
|
||
try: | ||
import mpi4py | ||
|
||
mpi4py.rc.finalize = False | ||
from mpi4py import MPI | ||
|
||
comm_rank = MPI.COMM_WORLD.Get_rank() | ||
comm = MPI.COMM_WORLD | ||
except ImportError: | ||
comm_rank = 0 | ||
comm = None | ||
|
||
|
||
def info(s): | ||
if comm_rank == 0: | ||
print(s) | ||
|
||
|
||
def sp_transpose(arr): | ||
brr = sharpy.permute_dims(arr, [1, 0]) | ||
sharpy.sync() | ||
return brr | ||
|
||
|
||
def np_transpose(arr): | ||
brr = arr.transpose() | ||
return brr.copy() | ||
|
||
|
||
def initialize(np, row, col, dtype): | ||
arr = np.arange(0, row * col, 1, dtype=dtype) | ||
return np.reshape(arr, (row, col)) | ||
|
||
|
||
def run(row, col, backend, iterations, datatype): | ||
if backend == "sharpy": | ||
import sharpy as np | ||
from sharpy import fini, init, sync | ||
|
||
transpose = sp_transpose | ||
|
||
init(False) | ||
elif backend == "numpy": | ||
import numpy as np | ||
|
||
if comm is not None: | ||
assert ( | ||
comm.Get_size() == 1 | ||
), "Numpy backend only supports serial execution." | ||
|
||
fini = sync = lambda x=None: None | ||
transpose = np_transpose | ||
else: | ||
raise ValueError(f'Unknown backend: "{backend}"') | ||
|
||
dtype = { | ||
"f32": np.float32, | ||
"f64": np.float64, | ||
}[datatype] | ||
|
||
info(f"Using backend: {backend}") | ||
info(f"Number of row: {row}") | ||
info(f"Number of column: {col}") | ||
info(f"Datatype: {datatype}") | ||
|
||
arr = initialize(np, row, col, dtype) | ||
sync() | ||
|
||
# verify | ||
if backend == "sharpy": | ||
brr = sp_transpose(arr) | ||
crr = np_transpose(sharpy.to_numpy(arr)) | ||
assert numpy.allclose(sharpy.to_numpy(brr), crr) | ||
|
||
def eval(): | ||
tic = time_mod.perf_counter() | ||
transpose(arr) | ||
toc = time_mod.perf_counter() | ||
return toc - tic | ||
|
||
# warm-up run | ||
t_warm = eval() | ||
|
||
# evaluate | ||
info(f"Running {iterations} iterations") | ||
time_list = [] | ||
for i in range(iterations): | ||
time_list.append(eval()) | ||
|
||
# get max time over mpi ranks | ||
if comm is not None: | ||
t_warm = comm.allreduce(t_warm, MPI.MAX) | ||
time_list = comm.allreduce(time_list, MPI.MAX) | ||
|
||
t_min = numpy.min(time_list) | ||
t_max = numpy.max(time_list) | ||
t_med = numpy.median(time_list) | ||
init_overhead = t_warm - t_med | ||
if backend == "sharpy": | ||
info(f"Estimated initialization overhead: {init_overhead:.5f} s") | ||
info(f"Min. duration: {t_min:.5f} s") | ||
info(f"Max. duration: {t_max:.5f} s") | ||
info(f"Median duration: {t_med:.5f} s") | ||
|
||
fini() | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description="Run transpose benchmark", | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
) | ||
|
||
parser.add_argument( | ||
"-r", | ||
"--row", | ||
type=int, | ||
default=10000, | ||
help="Number of row.", | ||
) | ||
parser.add_argument( | ||
"-c", | ||
"--column", | ||
type=int, | ||
default=10000, | ||
help="Number of column.", | ||
) | ||
|
||
parser.add_argument( | ||
"-b", | ||
"--backend", | ||
type=str, | ||
default="sharpy", | ||
choices=["sharpy", "numpy"], | ||
help="Backend to use.", | ||
) | ||
|
||
parser.add_argument( | ||
"-i", | ||
"--iterations", | ||
type=int, | ||
default=10, | ||
help="Number of iterations to run.", | ||
) | ||
|
||
parser.add_argument( | ||
"-d", | ||
"--datatype", | ||
type=str, | ||
default="f64", | ||
choices=["f32", "f64"], | ||
help="Datatype for model state variables", | ||
) | ||
|
||
args = parser.parse_args() | ||
run( | ||
args.row, | ||
args.column, | ||
args.backend, | ||
args.iterations, | ||
args.datatype, | ||
) |
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 |
---|---|---|
@@ -1 +1 @@ | ||
5a7bb80ede5fe4fa8d56ee0dd77c4e5c1327fe09 | ||
8ae485bbfb1303a414b375e25130fcaa4c02127a |
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need a sync here?