diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index 15c2f6b23e2d81249c4c6f735cb7bd28ea7dafb5..5f6935e77d304861b62c736e01e129dde0459fe2 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -554,246 +554,11 @@ class Population(object): pass # TODO: add call to function that cleans up the temp customlogging dir, and unloads the loaded libraries. - - def evolve_population_comparison( - self, parse_function, amt, nodes, custom_arg_file=None - ): - """ - The function that will evolve the population. This function contains many steps - """ - - ### Custom logging code: - self.set_custom_logging() - - ### Load store - self.grid_options["store_memaddr"] = binary_c_python_api.return_store("") - - # Execute. - - ### Part to test running this with and without multiprocessing. - import time - import multiprocessing as mp - from pathos.multiprocessing import ProcessingPool as Pool - import random - - start_no_mp = time.time() - self.set(base_filename="no_mp_{}.dat".format(amt)) - # amt = 1000 - masses = range(1, amt + 1) - for i in masses: - mass = random.randint(1, 500) - # print(mass) - self.set_bse_option("M_1", mass) - out = binary_c_python_api.run_population( - self.return_argline(), - self.grid_options["custom_logging_func_memaddr"], - self.grid_options["store_memaddr"], - ) - # parse_function(self, out) - stop_no_mp = time.time() - - print("without mp: {} systems took {}s".format(amt, stop_no_mp - start_no_mp)) - - ######################################################### - - start_mp = time.time() - - self.set(base_filename="mp_{}.dat".format(amt)) - - def evolve_mp(mass): - # print(mass) - self.set_bse_option("M_1", mass) - # self.set(M_1=mass) - out = binary_c_python_api.run_population( - self.return_argline(), - self.grid_options["custom_logging_func_memaddr"], - self.grid_options["store_memaddr"], - ) - # parse_function(self, out) - - p = Pool(nodes=nodes) - - def g(amt): - # amt = 1000 - masses = range(1, amt + 1) - for i in masses: - mass = random.randint(1, 500) - yield mass - print("generator done") - - r = list(p.imap(evolve_mp, g(amt))) - - stop_mp = time.time() - print("with mp: {} systems took {}s".format(amt, stop_mp - start_mp)) - - ######################################################### - duration_no_mp = stop_no_mp - start_no_mp - duration_mp = stop_mp - start_mp - ratio = duration_no_mp / duration_mp - print("Running mp versus no mp is {} times faster!".format(ratio)) - - return (nodes, amt, duration_no_mp, duration_mp, ratio) - - def evolve_population_mp(self, parse_function, mass_distribution): - """ - The function that will evolve the population. This function contains many steps - """ - - ### Custom logging code: - self.set_custom_logging() - - ### Load store - self.grid_options["store_memaddr"] = binary_c_python_api.return_store("") - - # evolve with mp - start_mp = time.time() - - def evolve_mp(mass): - self.set_bse_option("M_1", mass) - out = binary_c_python_api.run_population( - self.return_argline(), - self.grid_options["custom_logging_func_memaddr"], - self.grid_options["store_memaddr"], - ) - parse_function(self, out) - - p = Pool(nodes=self.grid_options["amt_cores"]) - - def g(mass_distribution): - masses = mass_distribution - for mass in masses: - yield mass - print("generator done") - - r = list(p.imap(evolve_mp, g(mass_distribution))) - stop_mp = time.time() - - print( - "with mp: {} systems took {}s using {} cores".format( - len(mass_distribution), - stop_mp - start_mp, - self.grid_options["amt_cores"], - ) - ) - - # TODO: add functionality to unload all the stores etc - - ######################################################### - # print("Running mp versus no mp is {} times faster!".format((start_no_mp-stop_no_mp)/(start_mp-stop_mp))) - # return (nodes, amt, stop_no_mp-start_no_mp, stop_mp-start_mp) - + ################################################### # Testing functions ################################################### - def test_evolve_population(self): - """ - The to test running a population - """ - - import time - import multiprocessing as mp - from pathos.multiprocessing import ProcessingPool as Pool - import random - - ####################### - ### Custom logging code: - self.set_custom_logging() - - ### Load store - self.grid_options["store_memaddr"] = binary_c_python_api.return_store("") - - - ####################### - # Dry run and getting starcount - self.grid_options['probtot'] = 0 - - self.generate_grid_code(dry_run=True) - - self.load_grid_function() - - self.dry_run() - - total_starcount_run = self.grid_options['total_starcount'] - print("Total starcount for this run will be: {}".format(total_starcount_run)) - - ####################### - # Linear run - start_lin = time.time() - - self.grid_options['probtot'] = 0 # To make sure that the values are reset. TODO: fix this in a cleaner way - - self.generate_grid_code(dry_run=False) - - self.load_grid_function() - - for i, system in enumerate(self.grid_options["system_generator"](self)): - full_system_dict = self.bse_options.copy() - full_system_dict.update(system) - - binary_cmdline_string = self.return_argline(full_system_dict) - out = binary_c_python_api.run_population( - binary_cmdline_string, - self.grid_options["custom_logging_func_memaddr"], - self.grid_options["store_memaddr"], - ) - print("{}/{}".format(i+1, total_starcount_run), binary_cmdline_string) - - stop_lin = time.time() - - ####################### - # MP run - self.grid_options['probtot'] = 0 # To make sure that the values are reset. TODO: fix this in a cleaner way - - start_mp = time.time() - - self.generate_grid_code(dry_run=False) - - self.load_grid_function() - - def evolve_system(binary_cmdline_string): - # print(binary_cmdline_string) - # pass - # print('next') - # self.set_bse_option("M_1", mass) - out = binary_c_python_api.run_population( - binary_cmdline_string, - self.grid_options["custom_logging_func_memaddr"], - self.grid_options["store_memaddr"], - ) - # # parse_function(self, out) - - def yield_system(): - for i, system in enumerate(self.grid_options["system_generator"](self)): - full_system_dict = self.bse_options.copy() - full_system_dict.update(system) - - binary_cmdline_string = self.return_argline(full_system_dict) - print("{}/{}".format(i+1, total_starcount_run), binary_cmdline_string) - - yield binary_cmdline_string - print("generator done") - - # Create pool - p = Pool(nodes=self.grid_options["amt_cores"]) - - # Execute - r = list(p.imap(evolve_system, yield_system())) - - stop_mp = time.time() - - # Give feedback - - print( - "Without mp: {} systems took {}s".format(total_starcount_run, stop_lin-start_lin)) - print( - "with mp: {} systems took {}s using {} cores".format( - self.grid_options['total_starcount'], - stop_mp - start_mp, - self.grid_options["amt_cores"], - ) - ) - def test_evolve_population_lin(self): """ Test function to evolve a population in a linear way.