From c69609fdb32af4a137fbe2800d2549582dd09643 Mon Sep 17 00:00:00 2001 From: David Hendriks <davidhendriks93@gmail.com> Date: Fri, 17 Jan 2020 14:34:17 +0000 Subject: [PATCH] updated the grid to handle single systems, also by loading the custom logging. then i stumbled on the following: if the .so is loaded once, and then we change and rebuild the library and load it again, it doesnt have effect. the solution to this is to give the newly built library name a random hash in its name. TODO: need to remove the contents of the custom_logging_dir each time the single or population function is done --- .../utils/custom_logging_functions.py | 28 +++++++-- binarycpython/utils/grid.py | 58 +++++++++++++++---- 2 files changed, 70 insertions(+), 16 deletions(-) diff --git a/binarycpython/utils/custom_logging_functions.py b/binarycpython/utils/custom_logging_functions.py index 9f58b705a..54bd02b65 100644 --- a/binarycpython/utils/custom_logging_functions.py +++ b/binarycpython/utils/custom_logging_functions.py @@ -4,7 +4,8 @@ import subprocess import socket import tempfile import ctypes - +import random +import uuid def autogen_C_logging_code(logging_dict): """ @@ -230,9 +231,18 @@ def compile_shared_lib(code, sourcefile_name, outfile_name, verbose=False): # Write code to file binary_c_write_log_code(code, sourcefile_name) + # Remove the library if present: + if os.path.exists(outfile_name): + try: + print('removing' ,outfile_name) + os.remove(outfile_name) + except: + print("Error while deleting file {}".format(outfile_name)) + # create compilation command compilation_dict = return_compilation_dict() + # Construct full command command = "{cc} {ccflags} {libs} -o {outfile_name} {sourcefile_name} {inc}".format( cc=compilation_dict["cc"], @@ -246,7 +256,7 @@ def compile_shared_lib(code, sourcefile_name, outfile_name, verbose=False): # remove extra whitespaces: command = " ".join(command.split()) - # Execute compilation + # Execute compilation and create the library if verbose: print("Executing following command:\n{command}".format(command=command)) res = subprocess.check_output("{command}".format(command=command), shell=True) @@ -281,22 +291,28 @@ def create_and_load_logging_function(custom_logging_code): """ # + + + library_name = os.path.join(temp_custom_logging_dir(), "libcustom_logging_{}.so".format(uuid.uuid4().hex)) + compile_shared_lib( custom_logging_code, sourcefile_name=os.path.join(temp_custom_logging_dir(), "custom_logging.c"), - outfile_name=os.path.join(temp_custom_logging_dir(), "libcustom_logging.so"), + # outfile_name=os.path.join(temp_custom_logging_dir(), "libcustom_logging.so"), + # outfile_name=os.path.join(temp_custom_logging_dir(), "libcustom_logging_{}.so".format(random.randint(1, 100))), + outfile_name=library_name, + # verbose=True ) # Loading library dll1 = ctypes.CDLL("libgslcblas.so", mode=ctypes.RTLD_GLOBAL) dll2 = ctypes.CDLL("libgsl.so", mode=ctypes.RTLD_GLOBAL) dll3 = ctypes.CDLL("libbinary_c.so", mode=ctypes.RTLD_GLOBAL) - libmean = ctypes.CDLL( - os.path.join(temp_custom_logging_dir(), "libcustom_logging.so"), + libcustom_logging = ctypes.CDLL(library_name, mode=ctypes.RTLD_GLOBAL, ) # loads the shared library # Get memory adress of function. mimicking a pointer - func_memaddr = ctypes.cast(libmean.custom_output_function, ctypes.c_void_p).value + func_memaddr = ctypes.cast(libcustom_logging.custom_output_function, ctypes.c_void_p).value return func_memaddr diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index c8b66521e..da789a5e8 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -87,11 +87,15 @@ class Population(object): # Filter out keys for the grid_options elif key in self.grid_options.keys(): + print("adding: {}={} to grid_options".format(key, kwargs[key])) + self.grid_options[key] = kwargs[key] # The of the keys go into a custom_options dict else: + print("!! Key doesnt match previously known parameter: adding: {}={} to custom_options".format(key, kwargs[key])) self.custom_options[key] = kwargs[key] + def return_argline(self, parameter_dict=None): """ Function to create the string for the arg line from a parameter dict @@ -250,6 +254,34 @@ class Population(object): with open(outfile, "w") as f: f.write(json.dumps(all_info, indent=4)) + def set_custom_logging(self): + """ + Function/routine to set all the custom logging so that the function memory pointer is known to the grid. + """ + + # C_logging_code gets priority of C_autogen_code + if self.grid_options["C_auto_logging"]: + # Generate real logging code + logging_line = autogen_C_logging_code(self.grid_options["C_auto_logging"]) + + # Generate entire shared lib code around logging lines + custom_logging_code = binary_c_log_code(logging_line) + + # Load memory adress + self.grid_options[ + "custom_logging_func_memaddr" + ] = create_and_load_logging_function(custom_logging_code) + # + if self.grid_options["C_logging_code"]: + # Generate entire shared lib code around logging lines + custom_logging_code = binary_c_log_code(self.grid_options["C_logging_code"]) + + # Load memory adress + self.grid_options[ + "custom_logging_func_memaddr" + ] = create_and_load_logging_function(custom_logging_code) + + ################################################### # Evolution functions ################################################### @@ -259,9 +291,11 @@ class Population(object): Function to run a single system """ + ### Custom logging code: + self.set_custom_logging() + argline = self.return_argline(self.bse_options) - out = binary_c_python_api.run_binary(argline) - # out = binary_c_python_api.run_binary('binary_c M_1 15 M_2 14 separation 0 orbital_period 4530 eccentricity 0 metallicity 0.02 max_evolution_time 15000') + out = binary_c_python_api.run_system(argline, self.grid_options['custom_logging_func_memaddr'], self.grid_options['store_memaddr']) # Todo: change this to run_binary again but then build in checks in binary return out def evolve_population(self, custom_arg_file=None): @@ -293,15 +327,17 @@ class Population(object): ] = create_and_load_logging_function(custom_logging_code) ### Load store - store = binary_c_python_api.return_store("") + self.grid_options['store_memaddr'] = binary_c_python_api.return_store("") - print("ho") - - print(self.return_argline()) + # Execute. + out = binary_c_python_api.run_population( + self.return_argline(), + self.grid_options['custom_logging_func_memaddr'], + self.grid_options['store_memaddr'] + ) - out = binary_c_python_api.run_population(self.return_argline(), 12, store) print(out) - quit() + ### Arguments # If user inputs a file containing arg lines then use that @@ -321,10 +357,12 @@ class Population(object): else: # generate population from options - print("ho") - pass + + quit() + + ####### # Do stuff for line in population_arglines: -- GitLab