refactor(mpi): follow PEP8 function naming scheme

This commit is contained in:
Anthony Berg 2025-06-24 17:48:57 +02:00
parent d2544e7c55
commit 7475d590eb
7 changed files with 49 additions and 50 deletions

View File

@ -41,43 +41,43 @@ class MPIGrid(object):
assert ndims == 2, "Unsupported number of dimensions. Must be two at the moment"
assert comm.size >= 1, "Must have at least one node"
self.grid = MPIGrid.getGrid(comm.size, ndims)
self.grid = MPIGrid.get_grid(comm.size, ndims)
self.comm = comm
self.logger.debug("Created MPI grid: {:}. Rank {:d} has coordinate {:}".format(
self.grid, self.comm.rank, self.getCoordinate()))
def getCoordinate(self, rank=None):
if (rank is None):
def get_coordinate(self, rank=None):
if rank is None:
rank = self.comm.rank
i = (rank % self.grid[0])
j = (rank // self.grid[0])
return i, j
def getRank(self, i, j):
def get_rank(self, i, j):
return j*self.grid[0] + i
def getEast(self):
i, j = self.getCoordinate(self.comm.rank)
def get_east(self):
i, j = self.get_coordinate(self.comm.rank)
i = (i+1) % self.grid[0]
return self.getRank(i, j)
return self.get_rank(i, j)
def getWest(self):
i, j = self.getCoordinate(self.comm.rank)
def get_west(self):
i, j = self.get_coordinate(self.comm.rank)
i = (i+self.grid[0]-1) % self.grid[0]
return self.getRank(i, j)
return self.get_rank(i, j)
def getNorth(self):
i, j = self.getCoordinate(self.comm.rank)
def get_north(self):
i, j = self.get_coordinate(self.comm.rank)
j = (j+1) % self.grid[1]
return self.getRank(i, j)
return self.get_rank(i, j)
def getSouth(self):
i, j = self.getCoordinate(self.comm.rank)
def get_south(self):
i, j = self.get_coordinate(self.comm.rank)
j = (j+self.grid[1]-1) % self.grid[1]
return self.getRank(i, j)
return self.get_rank(i, j)
def getGrid(num_nodes, num_dims):
def get_grid(num_nodes, num_dims):
assert(isinstance(num_nodes, int))
assert(isinstance(num_dims, int))
@ -150,7 +150,7 @@ class MPIGrid(object):
self.comm.Gather(data, out_data, root)
return out_data
def getLocalRank(self):
def get_local_rank(self):
"""
Returns the local rank on this node for this MPI process
"""
@ -236,10 +236,10 @@ class MPISimulator(Simulator.BaseSimulator):
self.grid = grid
#Get neighbor node ids
self.east = grid.getEast()
self.west = grid.getWest()
self.north = grid.getNorth()
self.south = grid.getSouth()
self.east = grid.get_east()
self.west = grid.get_west()
self.north = grid.get_north()
self.south = grid.get_south()
#Get coordinate of this node
#and handle global boundary conditions
@ -249,7 +249,7 @@ class MPISimulator(Simulator.BaseSimulator):
'east': Simulator.BoundaryCondition.Type.Dirichlet,
'west': Simulator.BoundaryCondition.Type.Dirichlet
})
gi, gj = grid.getCoordinate()
gi, gj = grid.get_coordinate()
#print("gi: " + str(gi) + ", gj: " + str(gj))
if (gi == 0 and boundary_conditions.west != Simulator.BoundaryCondition.Type.Periodic):
self.west = None
@ -360,7 +360,7 @@ class MPISimulator(Simulator.BaseSimulator):
width = self.sim.nx*self.sim.dx
height = self.sim.ny*self.sim.dy
i, j = self.grid.getCoordinate()
i, j = self.grid.get_coordinate()
x0 = i * width
y0 = j * height
x1 = x0 + width
@ -440,7 +440,7 @@ class MPISimulator(Simulator.BaseSimulator):
self.profiling_data_mpi["end"]["t_mpi_halo_exchange_download"] += time.time()
#Send/receive to east/west neighbours
#Send/receive to east/west neighbors
self.profiling_data_mpi["start"]["t_mpi_halo_exchange_sendreceive"] += time.time()
comm_send = []

View File

@ -82,10 +82,10 @@ class SHMEMSimulator(Simulator.BaseSimulator):
for i, sim in enumerate(self.sims):
#Get neighbor subdomain ids
self.east[i] = grid.getEast(i)
self.west[i] = grid.getWest(i)
self.north[i] = grid.getNorth(i)
self.south[i] = grid.getSouth(i)
self.east[i] = grid.get_east(i)
self.west[i] = grid.get_west(i)
self.north[i] = grid.get_north(i)
self.south[i] = grid.get_south(i)
#Get coordinate of this subdomain
#and handle global boundary conditions
@ -95,7 +95,7 @@ class SHMEMSimulator(Simulator.BaseSimulator):
'east': Simulator.BoundaryCondition.Type.Dirichlet,
'west': Simulator.BoundaryCondition.Type.Dirichlet
})
gi, gj = grid.getCoordinate(i)
gi, gj = grid.get_coordinate(i)
if (gi == 0 and boundary_conditions.west != Simulator.BoundaryCondition.Type.Periodic):
self.west = None
new_boundary_conditions.west = boundary_conditions.west;
@ -186,7 +186,7 @@ class SHMEMSimulator(Simulator.BaseSimulator):
"""
width = self.sims[index].nx*self.sims[index].dx
height = self.sims[index].ny*self.sims[index].dy
i, j = self.grid.getCoordinate(index)
i, j = self.grid.get_coordinate(index)
x0 = i * width
y0 = j * height
x1 = x0 + width

View File

@ -217,10 +217,10 @@ class SHMEMSimulatorGroup(object):
for i, sim in enumerate(self.sims):
#Get neighbor subdomain ids
self.east[i] = grid.getEast(i)
self.west[i] = grid.getWest(i)
self.north[i] = grid.getNorth(i)
self.south[i] = grid.getSouth(i)
self.east[i] = grid.get_east(i)
self.west[i] = grid.get_west(i)
self.north[i] = grid.get_north(i)
self.south[i] = grid.get_south(i)
#Get coordinate of this subdomain
#and handle global boundary conditions
@ -230,7 +230,7 @@ class SHMEMSimulatorGroup(object):
'east': Simulator.BoundaryCondition.Type.Dirichlet,
'west': Simulator.BoundaryCondition.Type.Dirichlet
})
gi, gj = grid.getCoordinate(i)
gi, gj = grid.get_coordinate(i)
if (gi == 0 and boundary_conditions.west != Simulator.BoundaryCondition.Type.Periodic):
self.west = None
new_boundary_conditions.west = boundary_conditions.west;
@ -320,7 +320,7 @@ class SHMEMSimulatorGroup(object):
"""
width = self.sims[index].nx*self.sims[index].dx
height = self.sims[index].ny*self.sims[index].dy
i, j = self.grid.getCoordinate(index)
i, j = self.grid.get_coordinate(index)
x0 = i * width
y0 = j * height
x1 = x0 + width

View File

@ -84,8 +84,7 @@ class BoundaryCondition(object):
raise (NotImplementedError("Neumann boundary condition not supported"))
def __str__(self):
return '[north={:s}, south={:s}, east={:s}, west={:s}]'.format(str(self.north), str(self.south), str(self.east),
str(self.west))
return f"[north={str(self.north)}, south={str(self.south)}, east={str(self.east)}, west={str(self.west)}]"
def as_coded_int(self):
"""
@ -153,7 +152,7 @@ class BaseSimulator(object):
peak_configuration = self.context.autotuner.get_peak_performance(self.__class__)
block_width = int(peak_configuration["block_width"])
block_height = int(peak_configuration["block_height"])
self.logger.debug("Used autotuning to get block size [%d x %d]", block_width, block_height)
self.logger.debug(f"Used autotuning to get block size [{block_width} x {block_height}]")
# Compute kernel launch parameters
self.block_size = (block_width, block_height, 1)
@ -171,7 +170,7 @@ class BaseSimulator(object):
self.nt = 0
def __str__(self):
return "{:s} [{:d}x{:d}]".format(self.__class__.__name__, self.nx, self.ny)
return f"{self.__class__.__name__} [{self.nx}x{self.ny}]"
def simulate(self, t, dt=None):
"""
@ -200,7 +199,7 @@ class BaseSimulator(object):
# Stop if end reached (should not happen)
if current_dt <= 0.0:
self.logger.warning("Timestep size {:d} is less than or equal to zero!".format(self.sim_steps()))
self.logger.warning(f"Timestep size {self.sim_steps()} is less than or equal to zero!")
break
# Step forward in time
@ -209,11 +208,11 @@ class BaseSimulator(object):
# Print info
print_string = printer.get_print_string(self.sim_time() - t_start)
if print_string:
self.logger.info("%s: %s", self, print_string)
self.logger.info(f"{self}: {print_string}")
try:
self.check()
except AssertionError as e:
e.args += ("Step={:d}, time={:f}".format(self.sim_steps(), self.sim_time()),)
e.args += f"Step={self.sim_steps()}, time={self.sim_time()}"
raise
def step(self, dt: int):
@ -246,7 +245,7 @@ class BaseSimulator(object):
return [0, 0, self.nx * self.dx, self.ny * self.dy]
def set_boundary_conditions(self, boundary_conditions):
self.logger.debug("Boundary conditions set to {:s}".format(str(boundary_conditions)))
self.logger.debug(f"Boundary conditions set to {str(boundary_conditions)}")
self.boundary_conditions = boundary_conditions.as_coded_int()
def get_boundary_conditions(self):

View File

@ -29,9 +29,9 @@ def getExtent(width, height, nx, ny, grid, index=None):
gx = grid.grid[0]
gy = grid.grid[1]
if index is not None:
i, j = grid.getCoordinate(index)
i, j = grid.get_coordinate(index)
else:
i, j = grid.getCoordinate()
i, j = grid.get_coordinate()
dx = (width / gx) / nx
dy = (height / gy) / ny

View File

@ -227,7 +227,7 @@
"importlib.reload(MPISimulator)\n",
"\n",
"grid = MPISimulator.MPIGrid(MPI.COMM_WORLD)\n",
"print(grid.getLocalRank())"
"print(grid.get_local_rank())"
]
},
{
@ -327,7 +327,7 @@
"\n",
"\n",
"grid = MPISimulator.MPIGrid(MPI.COMM_WORLD)\n",
"print(grid.getLocalRank())\n",
"print(grid.get_local_rank())\n",
"\n",
"#arguments = InitialConditions.genShockBubble(nx, ny, gamma, grid=grid)\n",
"arguments = InitialConditions.genKelvinHelmholtz(nx, ny, gamma, grid=grid)\n",

View File

@ -97,7 +97,7 @@ grid = MPISimulator.MPIGrid(MPI.COMM_WORLD)
####
cuda.init(flags=0)
logger.info("Initializing CUDA")
local_rank = grid.getLocalRank()
local_rank = grid.get_local_rank()
num_cuda_devices = cuda.Device.count()
cuda_device = local_rank % num_cuda_devices
logger.info("Process %s using CUDA device %s", str(local_rank), str(cuda_device))