Skip to content
Snippets Groups Projects
Commit ef1812b4 authored by David Hendriks's avatar David Hendriks
Browse files

added verbosity options and reformatted the code

parent bc2e9379
No related branches found
No related tags found
No related merge requests found
......@@ -8,7 +8,7 @@ import random
import uuid
def autogen_C_logging_code(logging_dict):
def autogen_C_logging_code(logging_dict, verbose=0):
"""
Function that autogenerates PRINTF statements for binaryc. intput is a dictionary where the key is the header of that logging line and items which are lists of parameters\
that will be put in that logging line
......@@ -33,6 +33,12 @@ def autogen_C_logging_code(logging_dict):
code = ""
# Loop over dict keys
for key in logging_dict:
if verbose > 0:
print(
"Generating Print statement for custom logging code with {} as a header".format(
key
)
)
logging_dict_entry = logging_dict[key]
# Check if item is of correct type:
......@@ -54,16 +60,20 @@ def autogen_C_logging_code(logging_dict):
"Error: please use a list for the list of parameters that you want to have logged"
)
code = code.strip()
# print("MADE AUTO CODE\n\n{}\n\n{}\n\n{}\n".format('*'*60, repr(code), '*'*60))
return code
####################################################################################
def binary_c_log_code(code):
def binary_c_log_code(code, verbose):
"""
Function to construct the code to construct the custom logging function
"""
if verbose > 0:
print("Creating the code for the shared library for the custom logging")
# Create code
custom_logging_function_string = """\
#pragma push_macro(\"MAX\")
#pragma push_macro(\"MIN\")
......@@ -91,7 +101,7 @@ void binary_c_API_function custom_output_function(struct stardata_t * stardata)
return textwrap.dedent(custom_logging_function_string)
def binary_c_write_log_code(code, filename):
def binary_c_write_log_code(code, filename, verbose=0):
"""
Function to write the generated logging code to a file
"""
......@@ -105,6 +115,8 @@ def binary_c_write_log_code(code, filename):
except:
print("Error while deleting file {}".format(filePath))
if verbose > 0:
print("Writing the custom logging code to {}".format(filePath))
with open(filePath, "w") as f:
f.write(code)
......@@ -125,7 +137,7 @@ def from_binary_c_config(config_file, flag):
return res
def return_compilation_dict(verbose=False):
def return_compilation_dict(verbose=0):
"""
Function to build the compile command for the shared library
......@@ -139,6 +151,10 @@ def return_compilation_dict(verbose=False):
- string containing the command to build the shared library
"""
if verbose > 0:
print(
"Calling the binary_c config code to get the info to build the shared library"
)
# use binary_c-config to get necessary flags
BINARY_C_DIR = os.getenv("BINARY_C")
if BINARY_C_DIR:
......@@ -209,12 +225,7 @@ def return_compilation_dict(verbose=False):
]
libs = "{} {}".format(" ".join(library_paths), " ".join(non_library_paths))
if verbose:
print(
"Building shared library for custom logging with (binary_c.h) at {} on {}\n".format(
BINARY_C_SRC_DIR, socket.gethostname()
)
)
if verbose > 0:
print(
"With options:\n\tcc = {cc}\n\tccflags = {ccflags}\n\tld = {ld}\n\tlibs = {libs}\n\tinc = {inc}\n\n".format(
cc=cc, ccflags=ccflags, ld=ld, libs=libs, inc=inc
......@@ -224,13 +235,13 @@ def return_compilation_dict(verbose=False):
return {"cc": cc, "ld": ld, "ccflags": ccflags, "libs": libs, "inc": inc}
def compile_shared_lib(code, sourcefile_name, outfile_name, verbose=False):
def compile_shared_lib(code, sourcefile_name, outfile_name, verbose=0):
"""
Function to write the custom logging code to a file and then compile it.
"""
# Write code to file
binary_c_write_log_code(code, sourcefile_name)
binary_c_write_log_code(code, sourcefile_name, verbose)
# Remove the library if present:
if os.path.exists(outfile_name):
......@@ -241,7 +252,7 @@ def compile_shared_lib(code, sourcefile_name, outfile_name, verbose=False):
print("Error while deleting file {}".format(outfile_name))
# create compilation command
compilation_dict = return_compilation_dict()
compilation_dict = return_compilation_dict(verbose)
# Construct full command
command = "{cc} {ccflags} {libs} -o {outfile_name} {sourcefile_name} {inc}".format(
......@@ -257,11 +268,22 @@ def compile_shared_lib(code, sourcefile_name, outfile_name, verbose=False):
command = " ".join(command.split())
# Execute compilation and create the library
if verbose:
print("Executing following command:\n{command}".format(command=command))
if verbose > 0:
BINARY_C_DIR = os.getenv("BINARY_C")
BINARY_C_SRC_DIR = os.path.join(BINARY_C_DIR, "src")
print(
"Building shared library for custom logging with (binary_c.h) at {} on {}\n".format(
BINARY_C_SRC_DIR, socket.gethostname()
)
)
print(
"Executing following command to compile the shared library:\n{command}".format(
command=command
)
)
res = subprocess.check_output("{command}".format(command=command), shell=True)
if verbose:
if verbose > 0:
if res:
print("Output of compilation command:\n{}".format(res))
......@@ -282,7 +304,7 @@ def temp_dir():
return path
def create_and_load_logging_function(custom_logging_code):
def create_and_load_logging_function(custom_logging_code, verbose):
"""
Function to automatically compile the shared library with the given custom logging code and load it with ctypes
......@@ -300,9 +322,12 @@ def create_and_load_logging_function(custom_logging_code):
custom_logging_code,
sourcefile_name=os.path.join(temp_dir(), "custom_logging.c"),
outfile_name=library_name,
# verbose=True
verbose=verbose,
)
if verbose > 0:
print("loading shared library for custom logging")
# Loading library
dll1 = ctypes.CDLL("libgslcblas.so", mode=ctypes.RTLD_GLOBAL)
dll2 = ctypes.CDLL("libgsl.so", mode=ctypes.RTLD_GLOBAL)
......@@ -316,4 +341,11 @@ def create_and_load_logging_function(custom_logging_code):
libcustom_logging.custom_output_function, ctypes.c_void_p
).value
if verbose > 0:
print(
"loaded shared library for custom logging. custom_output_function is loaded in memory at {}".format(
func_memaddr
)
)
return func_memaddr
......@@ -112,9 +112,10 @@ def create_hdf5(data_dir):
content_data_dir = os.listdir(data_dir)
# Settings
settings_file = os.path.join(data_dir, [
file for file in content_data_dir if file.endswith("_settings.json")
][0])
settings_file = os.path.join(
data_dir,
[file for file in content_data_dir if file.endswith("_settings.json")][0],
)
with open(settings_file, "r") as f:
settings_json = json.load(f)
......
This diff is collapsed.
......@@ -28,10 +28,9 @@ grid_options_defaults_dict = {
"binary_c_dir": os.environ["BINARYC_DIR"],
"tmp_dir": temp_dir(),
# Probability:
"weight": 1.0, # weighting for the probability
"repeat": 1.0, # number of times to repeat each system (probability is adjusted to be 1/repeat)
"weight": 1.0, # weighting for the probability
"repeat": 1.0, # number of times to repeat each system (probability is adjusted to be 1/repeat)
##
# return_array_refs=>1, # quicker data parsing mode
# sort_args=>1,
# save_args=>1,
......@@ -89,7 +88,6 @@ grid_options_defaults_dict = {
# merge_datafiles=>'',
# merge_datafiles_filelist=>'',
# # parameter space options
# binary=>0, # set to 0 for single stars, 1 for binaries
# # if use_full_resolution is 1, then run a dummy grid to
# # calculate the resolution. this could be slow...
......
......@@ -8,9 +8,11 @@ import re
import sys
# TODO: make this clean
GSL_DIR = os.getenv('GSL_DIR', None)
GSL_DIR = os.getenv("GSL_DIR", None)
if not GSL_DIR:
print("Warning: GSL_DIR is not set, this might lead to errors along the installation if there is no other version of GSL in the include dirs")
print(
"Warning: GSL_DIR is not set, this might lead to errors along the installation if there is no other version of GSL in the include dirs"
)
# TODO: write code to know exact parent directory of this file.
......@@ -63,27 +65,30 @@ for x in defines:
API_h = os.environ["BINARY_C"] + "/src/API/binary_c_API.h"
binary_c_define_macros.extend([("BINARY_C_API_H", API_h)])
#
include_dirs = [
os.environ["BINARY_C"] + "/src",
os.environ["BINARY_C"] + "/src/API",
"include", ] + binary_c_incdirs + [os.path.join(GSL_DIR,'include')] if GSL_DIR else []
#
include_dirs = (
[os.environ["BINARY_C"] + "/src", os.environ["BINARY_C"] + "/src/API", "include",]
+ binary_c_incdirs
+ [os.path.join(GSL_DIR, "include")]
if GSL_DIR
else []
)
libraries = ["binary_c"] + binary_c_libs + ["binary_c_api"]
library_dirs = [
os.environ["BINARY_C"] + "/src",
"./",
os.path.join(CWD, "lib/"),
# os.path.join(CWD, "binarycpython/core/"),
] + binary_c_libdirs
os.environ["BINARY_C"] + "/src",
"./",
os.path.join(CWD, "lib/"),
# os.path.join(CWD, "binarycpython/core/"),
] + binary_c_libdirs
runtime_library_dirs = [
os.environ["BINARY_C"] + "/src",
"./",
os.path.join(CWD, "lib/"),
# os.path.join(CWD, "binarycpython/core/"),
] + binary_c_libdirs
os.environ["BINARY_C"] + "/src",
"./",
os.path.join(CWD, "lib/"),
# os.path.join(CWD, "binarycpython/core/"),
] + binary_c_libdirs
# TODO: move the creeation of all the include stuff to here so its easier to print everything
# print('\n')
......@@ -102,7 +107,7 @@ binary_c_python_api_module = Extension(
# name="binarycpython.core.binary_c",
name="binary_c_python_api",
sources=["src/binary_c_python.c"],
include_dirs= include_dirs,
include_dirs=include_dirs,
libraries=libraries,
library_dirs=library_dirs,
runtime_library_dirs=runtime_library_dirs,
......@@ -112,6 +117,7 @@ binary_c_python_api_module = Extension(
language="C",
)
def readme():
with open("README.md") as f:
return f.read()
......
......@@ -13,22 +13,17 @@ from binarycpython.utils.functions import get_help_all, get_help, create_hdf5
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"amt_systems",
help='the amount of systems',
"amt_systems", help="the amount of systems",
)
parser.add_argument(
"amt_nodes",
help='the amount of nodes that are used for the multiprocessing',
"amt_nodes", help="the amount of nodes that are used for the multiprocessing",
)
parser.add_argument(
"name_testcase",
help='The name of the testcase (e.g. laptop, cluster etc)',
"name_testcase", help="The name of the testcase (e.g. laptop, cluster etc)",
)
......@@ -46,13 +41,14 @@ def output_lines(output):
"""
return output.splitlines()
def parse_function(self, output):
# extract info from the population instance
# TODO: think about whether this is smart. Passing around this object might be an overkill
# Get some information from the
data_dir = self.custom_options['data_dir']
base_filename = self.custom_options['base_filename']
# Get some information from the
data_dir = self.custom_options["data_dir"]
base_filename = self.custom_options["base_filename"]
# Check directory, make if necessary
os.makedirs(data_dir, exist_ok=True)
......@@ -65,17 +61,19 @@ def parse_function(self, output):
headerline = el.split()[0]
# CHeck the header and act accordingly
if (headerline=='DAVID_SN'):
parameters = ['time', 'mass_1', 'prev_mass_1', 'zams_mass_1', 'SN_type']
if headerline == "DAVID_SN":
parameters = ["time", "mass_1", "prev_mass_1", "zams_mass_1", "SN_type"]
values = el.split()[1:]
seperator='\t'
seperator = "\t"
if not os.path.exists(outfilename):
with open(outfilename, 'w') as f:
f.write(seperator.join(parameters)+'\n')
with open(outfilename, "w") as f:
f.write(seperator.join(parameters) + "\n")
with open(outfilename, "a") as f:
f.write(seperator.join(values) + "\n")
with open(outfilename, 'a') as f:
f.write(seperator.join(values)+'\n')
## Set values
test_pop = Population()
test_pop.set(
......@@ -96,20 +94,29 @@ if(stardata->star[0].SN_type != SN_NONE)
/* Kill the simulation to save time */
stardata->model.max_evolution_time = stardata->model.time - stardata->model.dtm;
};
""")
test_pop.set(separation=1000000000,
orbital_period=400000000,
metallicity=0.002,
data_dir=os.path.join(os.environ['BINARYC_DATA_ROOT'], 'testing_python', 'multiprocessing2', name_testcase))
"""
)
res = test_pop.evolve_population_comparison(parse_function, amt=int(amt_systems), nodes=int(amt_nodes))
with open('comparison_result.dat', 'a') as f:
f.write(str(res)+'\n')
test_pop.set(
separation=1000000000,
orbital_period=400000000,
metallicity=0.002,
data_dir=os.path.join(
os.environ["BINARYC_DATA_ROOT"],
"testing_python",
"multiprocessing2",
name_testcase,
),
)
res = test_pop.evolve_population_comparison(
parse_function, amt=int(amt_systems), nodes=int(amt_nodes)
)
with open("comparison_result.dat", "a") as f:
f.write(str(res) + "\n")
mass_distribution = np.arange(1, 200)
# evolve_population_mp(parse_function, mass_distribution)
\ No newline at end of file
# evolve_population_mp(parse_function, mass_distribution)
......@@ -155,6 +155,7 @@ def test_run_system():
out = binary_c_python_api.run_system(argstring, -1, -1)
print(out)
####
if __name__ == "__main__":
# test_run_binary()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment