Skip to content

Commit 467ed0b

Browse files
committed
Add MPI-IO timings
1 parent 0055d2b commit 467ed0b

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

source-code/mpi4py/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ C/C++ or Fortran implementations.
2727
1. `run_pi_mpipool.sh`: Bash script to run `pi_mpipool.py`.
2828
1. `file_trafficker.py`: file write/read test application that can run
2929
serially, multi-threaded, multi-process and MPI.
30-
30+
1. `mpi_io.py`: timing of MPI-IO operations.
31+
1. `translate_bin.py`: translate binary to ASCII data.
3132

3233
## How to run?
3334

source-code/mpi4py/mpi_io.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
from mpi4py import MPI
5+
import numpy as np
6+
import re
7+
import time
8+
9+
10+
def compute_size(size_str):
11+
if (match := re.match(r'\s*(\d+)\s*([a-z]+)?', size_str, re.IGNORECASE)) is not None:
12+
size = int(match.group(1))
13+
if match.group(2):
14+
units = match.group(2).lower()
15+
if units == 'b':
16+
pass
17+
elif units == 'kb':
18+
size *= 1024
19+
elif units == 'mb':
20+
size *= 1024**2
21+
elif units == 'gb':
22+
size *= 1024**3
23+
elif units == 'tb':
24+
size *= 1024**4
25+
else:
26+
raise ValueError(f"invalid unit in size expression: '{size_str}'")
27+
return size
28+
else:
29+
raise ValueError(f"invalid size expression: '{size_str}'")
30+
31+
32+
if __name__ == '__main__':
33+
arg_parser = argparse.ArgumentParser(description='write a file using MPI-IO')
34+
arg_parser.add_argument('--file', required=True,
35+
help='file name to write')
36+
arg_parser.add_argument('--size', default='100mb',
37+
help='file size to write')
38+
options = arg_parser.parse_args()
39+
40+
comm = MPI.COMM_WORLD
41+
comm_rank = comm.Get_rank()
42+
comm_size = comm.Get_size()
43+
total_file_size = compute_size(options.size)//8
44+
data_size = total_file_size//comm_size
45+
offset = comm_rank*data_size*8
46+
47+
start_val = comm_rank*data_size
48+
end_val = (1 + comm_rank)*data_size
49+
if comm_rank == comm_size - 1:
50+
end_val += total_file_size % comm_size
51+
data = np.arange(start_val, end_val, 1.0)
52+
53+
mode = MPI.MODE_WRONLY | MPI.MODE_CREATE
54+
start_time = time.time()
55+
file = MPI.File.Open(comm, options.file, mode)
56+
file.Write_at_all(offset, data)
57+
file.Close()
58+
end_time = time.time()
59+
total_time = end_time - start_time
60+
print(f'{comm_rank}, {total_time} s, {compute_size(options.size)/(total_time*2**20):.3f} MB/s')

source-code/mpi4py/translate_bin.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import numpy as np
5+
6+
7+
if __name__ == '__main__':
8+
arg_parser = argparse.ArgumentParser(description="read and print MPI-IO data")
9+
arg_parser.add_argument('file', help='file to read')
10+
options = arg_parser.parse_args()
11+
data = np.fromfile(options.file)
12+
for value in data:
13+
print(value)

0 commit comments

Comments
 (0)