Commit c69609fd authored by David Hendriks's avatar David Hendriks
Browse files

updated the grid to handle single systems, also by loading the custom logging....

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
parent 62d46bfc
Loading
Loading
Loading
Loading
+22 −6
Original line number Diff line number Diff line
@@ -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
+48 −10
Original line number Diff line number Diff line
@@ -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: