5454from typing import Type , TypeVar
5555import logging
5656import itertools
57+ import warnings
5758
5859T = TypeVar ("T" )
5960
@@ -494,6 +495,7 @@ def toVTR(self, baseName, varNames, idxFormat="{:06d}"):
494495 # MPI-parallel implementation
495496 # -------------------------------------------------------------------------
496497 comm : MPI .Intracomm = None
498+ balanced_distribution = None
497499
498500 @classmethod
499501 def setupMPI (cls , comm : MPI .Intracomm , iLoc , nLoc ):
@@ -514,6 +516,11 @@ def setupMPI(cls, comm: MPI.Intracomm, iLoc, nLoc):
514516 cls .iLoc = iLoc
515517 cls .nLoc = nLoc
516518 cls .mpiFile = None
519+ cls .balanced_distribution = all (cls .nLoc == me for me in comm .allgather (nLoc ))
520+ if not cls .balanced_distribution :
521+ warnings .warn (
522+ f'You requested an unbalanced block decomposition, in { cls .__name__ !r} which will result in far slower IO! Please consider changing the resolution or number of tasks.'
523+ )
517524
518525 @property
519526 def MPI_ON (self ):
@@ -553,7 +560,10 @@ def MPI_WRITE_AT(self, offset, data: np.ndarray):
553560 data : np.ndarray
554561 Data to be written in the binary file.
555562 """
556- self .mpiFile .Write_at_all (offset , data )
563+ if self .balanced_distribution :
564+ self .mpiFile .Write_at_all (offset , data )
565+ else :
566+ self .mpiFile .Write_at (offset , data )
557567
558568 def MPI_READ_AT (self , offset , data ):
559569 """
@@ -567,7 +577,10 @@ def MPI_READ_AT(self, offset, data):
567577 data : np.ndarray
568578 Array on which to read the data from the binary file.
569579 """
570- self .mpiFile .Read_at_all (offset , data )
580+ if self .balanced_distribution :
581+ self .mpiFile .Read_at_all (offset , data )
582+ else :
583+ self .mpiFile .Read_at (offset , data )
571584
572585 def MPI_FILE_CLOSE (self ):
573586 """Close the binary file in MPI mode"""
0 commit comments