diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index e11b31a751ca294b4918a7de885ff351793b8d29..136b89112610a7f336e97aa43d48046909ca5145 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -433,7 +433,7 @@ class Population(object): # Get argument line argline = self.return_argline(self.bse_options) - + print('Running {}'.format(argline)) # Run system out = binary_c_python_api.run_system( argline, @@ -552,16 +552,17 @@ class Population(object): 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) + #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) + #binary_cmdline_string = self.return_argline(full_system_dict) + pass + # 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() @@ -617,11 +618,12 @@ class Population(object): # 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"], - ) + # out = binary_c_python_api.run_population( + # binary_cmdline_string, + # self.grid_options["custom_logging_func_memaddr"], + # self.grid_options["store_memaddr"], + # ) + pass # # parse_function(self, out) def yield_system(): @@ -630,9 +632,9 @@ class Population(object): full_system_dict.update(system) binary_cmdline_string = self.return_argline(full_system_dict) - print("{}/{}".format(i+1, total_starcount_run), binary_cmdline_string) - + # print("{}/{}".format(i+1, total_starcount_run), binary_cmdline_string) yield binary_cmdline_string + # yield i print("generator done") # Create pool @@ -654,6 +656,95 @@ class Population(object): return stop_mp - start_mp + def test_evolve_population_mp_chunks(self): + """ + Test function to evolve a population in a parallel way. + + returns total time spent on the actual interfacing with binaryc + """ + + import time + import multiprocessing as mp + # from pathos.multiprocessing import ProcessingPool as Pool + from pathos.pools import _ProcessPool as Pool + ####################### + ### 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)) + + ####################### + # 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"], + # ) + pass + # # 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 + # yield i + print("generator done") + + # Create pool + p = Pool(processes=self.grid_options["amt_cores"]) + + + # Execute + r = list(p.imap_unordered(evolve_system, yield_system(), chunksize=100)) + + stop_mp = time.time() + + # Give feedback + print( + "with mp: {} systems took {}s using {} cores".format( + self.grid_options['total_starcount'], + stop_mp - start_mp, + self.grid_options["amt_cores"], + ) + ) + + return stop_mp - start_mp + + + + + def test_evolve_single(self): """ Function to test the evolution of a system. Calls the api binding directly. @@ -953,7 +1044,7 @@ class Population(object): code_string += indent * (depth + 1) + "# Setting probabilities\n" code_string += ( indent * (depth + 1) - + "d{} = {}".format(grid_variable["name"], grid_variable["probdist"]) + + "d{} = phasevol_{} * {}".format(grid_variable["name"], grid_variable["name"], grid_variable["probdist"]) + "\n" ) diff --git a/tests/population/grid_tests.py b/tests/population/grid_tests.py index a6ae23c596b4111ade8203e9f6d3e3e4837dd8cb..a8947b5213df15ac02cd8cfe02efbfed495f21b6 100644 --- a/tests/population/grid_tests.py +++ b/tests/population/grid_tests.py @@ -304,7 +304,7 @@ test_pop.add_grid_variable( # precode="M_1=math.exp(lnm1)", probdist="flat(M_1)", # probdist='self.custom_options["extra_prob_function"](M_1)', - dphasevol="dlnm1", + dphasevol="dM_1", parameter_name="M_1", condition="", ) @@ -329,7 +329,7 @@ test_pop.set(verbose=1, ) -test_pop.test_evolve_population() +test_pop.test_evolve_population_mp() # print(test_pop.grid_options["probtot"]) # print(test_pop.grid_options["count"]) \ No newline at end of file diff --git a/tests/population/scaling/evolve_population_comparing_with_multiprocessing.py b/tests/population/scaling/evolve_population_comparing_with_multiprocessing.py index c880e820e9f3b9f39c6de03631183856d1c7db27..62e9da962f806e356eaa6f03ee8096d9204baa10 100644 --- a/tests/population/scaling/evolve_population_comparing_with_multiprocessing.py +++ b/tests/population/scaling/evolve_population_comparing_with_multiprocessing.py @@ -121,6 +121,18 @@ test_pop.add_grid_variable( condition='self.grid_options["binary"]==1', ) + +# MP +total_mp_start = time.time() + +# evolve_mp_time = test_pop.test_evolve_population_mp() +evolve_mp_time = test_pop.test_evolve_population_mp_chunks() + +total_mp_stop = time.time() + +total_mp = total_mp_stop - total_mp_start +print("MP ({} nodes) run with {} systems: {} of which {} spent on evolving the systems".format(AMT_CORES, total_systems, total_mp, evolve_mp_time)) + # Lin total_lin_start = time.time() @@ -132,22 +144,13 @@ total_lin = total_lin_stop - total_lin_start print("linear run with {} systems: {} of which {} spent on evolving the systems".format(total_systems, total_lin, evolve_lin_time)) -# MP -total_mp_start = time.time() - -evolve_mp_time = test_pop.test_evolve_population_mp() - -total_mp_stop = time.time() - -total_mp = total_mp_stop - total_mp_start -print("MP ({} nodes) run with {} systems: {} of which {} spent on evolving the systems".format(AMT_CORES, total_systems, total_mp, evolve_mp_time)) - +#### speed_up = total_lin/total_mp print("The speed up by using MP is: {}".format(total_lin/total_mp)) # Write to file: # amt_cores, amt_systems, total_time_lin, total_time_mp, speedup -with open(os.path.join(result_dir, "comparison_result_{}.dat".format(name_testcase)), "a") as f: - res = (AMT_CORES, total_systems, total_lin, total_mp, speed_up) - f.write(str(res) + "\n") \ No newline at end of file +#with open(os.path.join(result_dir, "comparison_result_{}.dat".format(name_testcase)), "a") as f: +# res = (AMT_CORES, total_systems, total_lin, total_mp, speed_up) +# f.write(str(res) + "\n")