Repository navigation
Expand file tree
/
Copy pathmpi.py
More file actions
53 lines (44 loc) · 1.36 KB
/
Copy pathmpi.py
File metadata and controls
53 lines (44 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from pathlib import Path
import math
from .utils import get_cpu_count
from .base import get_simsize
from .config import read_config
def get_mpi_count(path: Path) -> int:
""" get appropriate MPI image count for problem shape
Parameters
----------
path: pathlib.Path
simsize file
Returns
-------
count: int
detect number of physical CPU
"""
path = Path(path).expanduser()
max_cpu = get_cpu_count()
# %% config.nml file or directory or simsize.h5?
if path.is_dir():
size = get_simsize(path)
elif path.is_file():
if path.suffix in (".h5", ".nc", ".dat"):
size = get_simsize(path)
elif path.suffix in (".ini", ".nml"):
params = read_config(path)
# OK to use indat_size because we're going to run a sim on this machine
size = get_simsize(params["indat_size"])
else:
raise FileNotFoundError(f"{path} is not a file or directory")
mpi_count = 1
if size[2] == 1:
# 2D sim
for i in range(max_cpu, 2, -1):
mpi_count = max(math.gcd(size[1], i), mpi_count)
if i < mpi_count:
break
else:
# 3D sim
for i in range(max_cpu, 2, -1):
mpi_count = max(math.gcd(size[2], i), mpi_count)
if i < mpi_count:
break
return mpi_count