def run_simulations(self,i_iteration): self.rank_directory = self.RANK_DIR_FORMAT.format( self.mpi_rank) # if the directory exists delete it if os.path.isdir(self.rank_directory): shutil.rmtree(self.rank_directory) os.makedirs(self.rank_directory) # change execution context for this rank # this provides a directory for each worker directory so that the # disk IO writes don't conflict os.chdir(self.rank_directory) _config_filename = os.path.join( self.root_directory, self.pyposmat_configuration_filename) _results_filename = os.path.join( self.root_directory, self.rank_directory, 'pyposmat.results.out') # determine number of sims for this rank _n_sims = 1000 * self.mpi_size +2 _n_sims_per_rank = int(_n_sims/self.mpi_size) if _n_sims%self.mpi_size > self.mpi_rank: _n_sims_per_rank += 1 # set random seed np.random.seed(self.rv_seeds[self.mpi_rank,i_iteration]) self.pyposmat_mc_sampler = PyposmatMonteCarloSampler( filename_in = _config_filename, filename_out = _results_filename) self.pyposmat_mc_sampler.create_base_directories() self.pyposmat_mc_sampler.read_configuration_file() self.pyposmat_mc_sampler.configure_qoi_manager() self.pyposmat_mc_sampler.configure_task_manager() self.pyposmat_mc_sampler.configure_pyposmat_datafile_out() #pyposmat_datafile_out = PyposmatDataFile(filename_out) if self.mpi_rank == 0: self.pyposmat_mc_sampler.print_structure_database() self.pyposmat_mc_sampler.print_sampling_configuration() self.pyposmat_mc_sampler.print_initial_parameter_distribution() for i in range(self.pyposmat_mc_sampler.n_iterations): self.pyposmat_mc_sampler.run_simulations(i_iteration=i,n_samples=1000) os.chdir(self.root_directory)
MgO_structures['structure_directory'] = 'test__PyposmatMonteCarloSampler' MgO_structures['structures'] = OrderedDict() MgO_structures['structures']['MgO_NaCl'] = 'MgO_NaCl_unit.gga.relax.vasp' MgO_configuration = PyposmatConfigurationFile() MgO_configuration.qois = MgO_qoi_db.qois MgO_configuration.potential = MgO_potential MgO_configuration.structures = MgO_structures assert isinstance(MgO_configuration.configuration,OrderedDict) MgO_configuration.write(filename='pypospack.config.in') MgO_configuration.read(filename='pypospack.config.in') # <---------------- end make configuration file filename_in='pypospack.config.in' filename_out='pypospack.results.out' engine = PyposmatMonteCarloSampler( filename_in=filename_in, filename_out=filename_out) # <---------------- printout for debugging purposes print('base_directory:{}'.format(engine.base_directory)) print('input_filename:{}'.format(engine.pyposmat_filename_in)) print('output_filename:{}'.format(engine.pyposmat_filename_out)) # <---------------- the steps of engine.configure() tested individually # this is the step which configures the object from the # configuration file # engine.configure() engine.create_base_directories() engine.read_configuration_file() engine.configure_qoi_manager() engine.configure_task_manager()
#------------------------------------------------------------------------------ # WRITE CONFIGURATION FILE #------------------------------------------------------------------------------ Ni_eam_configuration = PyposmatConfigurationFile() Ni_eam_configuration.qois = Ni_eam.Ni_qoi_db.qois Ni_eam_configuration.potential = Ni_eam.Ni_eam_potential_formalism Ni_eam_configuration.structures = Ni_eam.Ni_structure_db Ni_eam_configuration.sampling_type = Ni_eam.Ni_eam_sampling Ni_eam_configuration.sampling_distribution = Ni_eam.Ni_eam_parameter_distribution Ni_eam_configuration.write(filename='pypospack.config.in') Ni_eam_configuration.read(filename='pypospack.config.in') # <---------------- end make configuration file filename_in = 'pypospack.config.in' filename_out = 'pypospack.results.out' engine = PyposmatMonteCarloSampler(filename_in=filename_in, filename_out=filename_out) # <---------------- printout for debugging purposes print('base_directory:{}'.format(engine.base_directory)) print('input_filename:{}'.format(engine.pyposmat_filename_in)) print('output_filename:{}'.format(engine.pyposmat_filename_out)) # <---------------- the steps of engine.configure() tested individually # this is the step which configures the object from the # configuration file # engine.configure() engine.create_base_directories() engine.read_configuration_file() engine.configure_qoi_manager() engine.configure_task_manager() engine.configure_pyposmat_datafile_out() #pyposmat_datafile_out = PyposmatDataFile(filename_out)
# WRITE CONFIGURATION FILE #------------------------------------------------------------------------------ Si_sw_configuration = PyposmatConfigurationFile() Si_sw_configuration.qois = Si_sw_qoi_db.qois Si_sw_configuration.potential = Si_sw_potential Si_sw_configuration.structures = Si_sw_structures Si_sw_configuration.sampling_type = sampling Si_sw_configuration.sampling_distribution = Si_sw_param_dist Si_sw_configuration.write(filename='pypospack.config.in') Si_sw_configuration.read(filename='pypospack.config.in') # <---------------- end make configuration file filename_in='pypospack.config.in' filename_out='pypospack.results.out' engine = PyposmatMonteCarloSampler( filename_in=filename_in, filename_out=filename_out) # <---------------- printout for debugging purposes print('base_directory:{}'.format(engine.base_directory)) print('input_filename:{}'.format(engine.pyposmat_filename_in)) print('output_filename:{}'.format(engine.pyposmat_filename_out)) # <---------------- the steps of engine.configure() tested individually # this is the step which configures the object from the # configuration file engine.create_base_directories() engine.read_configuration_file() engine.configure_qoi_manager() engine.configure_task_manager() engine.configure_pyposmat_datafile_out() #pyposmat_datafile_out = PyposmatDataFile(filename_out)
class PyposmatIterativeSampler(object): def __init__(self, configuration_filename): self.RANK_DIR_FORMAT = 'rank_{}' self.mpi_comm = None self.mpi_rank = None self.mpi_nprocs = None self.n_iterations = None self.rv_seed = None self.rv_seeds = None self.pyposmat_configuration_filename = configuration_filename self.pyposmat_mc_sampler = None self.root_directory = os.getcwd() def run_simulations(self,i_iteration): self.rank_directory = self.RANK_DIR_FORMAT.format( self.mpi_rank) # if the directory exists delete it if os.path.isdir(self.rank_directory): shutil.rmtree(self.rank_directory) os.makedirs(self.rank_directory) # change execution context for this rank # this provides a directory for each worker directory so that the # disk IO writes don't conflict os.chdir(self.rank_directory) _config_filename = os.path.join( self.root_directory, self.pyposmat_configuration_filename) _results_filename = os.path.join( self.root_directory, self.rank_directory, 'pyposmat.results.out') # determine number of sims for this rank _n_sims = 1000 * self.mpi_size +2 _n_sims_per_rank = int(_n_sims/self.mpi_size) if _n_sims%self.mpi_size > self.mpi_rank: _n_sims_per_rank += 1 # set random seed np.random.seed(self.rv_seeds[self.mpi_rank,i_iteration]) self.pyposmat_mc_sampler = PyposmatMonteCarloSampler( filename_in = _config_filename, filename_out = _results_filename) self.pyposmat_mc_sampler.create_base_directories() self.pyposmat_mc_sampler.read_configuration_file() self.pyposmat_mc_sampler.configure_qoi_manager() self.pyposmat_mc_sampler.configure_task_manager() self.pyposmat_mc_sampler.configure_pyposmat_datafile_out() #pyposmat_datafile_out = PyposmatDataFile(filename_out) if self.mpi_rank == 0: self.pyposmat_mc_sampler.print_structure_database() self.pyposmat_mc_sampler.print_sampling_configuration() self.pyposmat_mc_sampler.print_initial_parameter_distribution() for i in range(self.pyposmat_mc_sampler.n_iterations): self.pyposmat_mc_sampler.run_simulations(i_iteration=i,n_samples=1000) os.chdir(self.root_directory) def setup_mpi_environment(self): self.mpi_comm = MPI.COMM_WORLD self.mpi_rank = self.mpi_comm.Get_rank() self.mpi_size = self.mpi_comm.Get_size() self.mpi_procname = MPI.Get_processor_name() if self.mpi_rank == 0: self.print_mpi_environment() def print_mpi_environment(self): print(80*'-') print('{:^80}'.format('MPI COMMUNICATION INFORMATION')) print(80*'-') print('mpi_size={}'.format(self.mpi_size)) def determine_rv_seeds(self): _randint_low = 0 _randint_high = 2147483647 # set original seed if self.rv_seed is None: self.rv_seed = np.random.randint( low=_randint_low, high=_randint_high) np.random.seed(self.rv_seed) # determine rank seed self.rv_seeds = np.random.randint( low=0, high=2147483647, size=(int(self.mpi_size),self.n_iterations) ) if self.mpi_rank == 0: self.print_random_seeds() def run_all(self): self.setup_mpi_environment() self.determine_rv_seeds() for i in range(self.n_iterations): self.run_simulations(i) MPI.COMM_WORLD.Barrier() self.analyze_results(i) MPI.COMM_WORLD.Barrier() def analyze_results(self,i_iterations): # merge datafiles if self.mpi_rank == 0: _filename_out = 'pyposmat.results.{}.out'.format( i_iterations) print('merging results into {}'.format(_filename_out)) for i_rank in range(self.mpi_size): _filename_in = os.path.join( self.RANK_DIR_FORMAT.format(i_rank)) print('\t merging...{}'.format(_filename_in)) def print_random_seeds(self): print(80*'-') print('{:^80}'.format('GENERATED RANDOM SEEDS')) print(80*'-') print() print('rv_seed={}'.format(self.rv_seed)) print() print('{:^8} {:^8} {:^10}'.format('rank','iter','seed')) print('{} {} {}'.format(8*'-',8*'-',10*'-')) for i_rank in range(self.mpi_size): for i_iter in range(self.n_iterations): print('{:^8} {:^8} {:>10}'.format( i_rank, i_iter, self.rv_seeds[i_rank,i_iter]))