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

removed old scaling stuff and putting it in 1 file

parent 554b7984
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
binary_c --M_1 10 --M2 5 --orbital_period 1000000
binary_c --M_1 10 --M2 10 --orbital_period 1000000
+0 −15
Original line number Diff line number Diff line
import os
import json
import time
import pickle
import sys

import matplotlib.pyplot as plt


from binarycpython.utils.grid import Population
from binarycpython.utils.functions import get_help_all, get_help

test_pop = Population()

test_pop.parse_cmdline()
+38 −0
Original line number Diff line number Diff line
import os
import json
import time
import pickle
import sys
import numpy as np

from binarycpython.utils.grid import Population
from binarycpython.utils.functions import get_help_all, get_help, create_hdf5


import argparse


parser = argparse.ArgumentParser()

parser.add_argument(
    "resolution_M_1", help="Resolution in M_1",
)

parser.add_argument(
    "resolution_per", help="Resolution in period",
)

parser.add_argument(
    "amt_cores", 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)",
)


args = parser.parse_args()
res_m_1 = int(args.resolution_M_1)
res_per = int(args.resolution_per)
AMT_CORES = int(args.amt_cores)
name_testcase = args.name_testcase
 No newline at end of file
+0 −164
Original line number Diff line number Diff line
import os
import json
import time
import pickle
import sys
import numpy as np

from binarycpython.utils.grid import Population
from binarycpython.utils.functions import get_help_all, get_help, create_hdf5


import argparse


parser = argparse.ArgumentParser()

parser.add_argument(
    "resolution_M_1", help="Resolution in M_1",
)

parser.add_argument(
    "resolution_per", help="Resolution in period",
)

parser.add_argument(
    "amt_cores", 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)",
)


args = parser.parse_args()

res_m_1 = int(args.resolution_M_1)
res_per = int(args.resolution_per)
AMT_CORES = int(args.amt_cores)
name_testcase = args.name_testcase

##############################################################################
## Quick script to get some output about which stars go supernova when.
def output_lines(output):
    """
    Function that outputs the lines that were recieved from the binary_c run. 
    """
    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"]

    # Check directory, make if necessary
    os.makedirs(data_dir, exist_ok=True)

    # Create filename
    outfilename = os.path.join(data_dir, base_filename)

    # Go over the output.
    for el in output_lines(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"]
            values = el.split()[1:]
            seperator = "\t"

            if not os.path.exists(outfilename):
                with open(outfilename, "w") as f:
                    f.write(seperator.join(parameters) + "\n")

            with open(outfilename, "a") as f:
                f.write(seperator.join(values) + "\n")


resolution = {"M_1": res_m_1, "per": res_per}
total_systems = np.prod([el for el in resolution.values()])
result_dir = "scaling_results"
# AMT_CORES = int(amt_cores)


test_pop = Population()

test_pop.set(
    verbose=1, amt_cores=AMT_CORES, binary=1,
)

test_pop.add_grid_variable(
    name="M_1",
    longname="log primary mass",
    valuerange=[1, 100],
    resolution="{}".format(resolution["M_1"]),
    spacingfunc="const(1, 100, {})".format(resolution["M_1"]),
    precode="",
    # precode="M_1=math.exp(lnm1)",
    probdist="Kroupa2001(M_1)",
    # probdist='self.custom_options["extra_prob_function"](M_1)',
    dphasevol="dlnm1",
    parameter_name="M_1",
    condition="",
)

### Grid generating test.
test_pop.add_grid_variable(
    name="period",
    longname="period",
    valuerange=["M_1", 20],
    resolution="{}".format(resolution["per"]),
    spacingfunc="np.linspace(1, 10, {})".format(resolution["per"]),
    precode="orbital_period = period**2",
    probdist="flat(orbital_period)",
    parameter_name="orbital_period",
    dphasevol="dper",
    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()
# evolve_mp_time = test_pop.test_evolve_population_mp_chunks_better()

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()

evolve_lin_time = test_pop.test_evolve_population_lin()

total_lin_stop = time.time()

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
    )
)

####
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")
+0 −41
Original line number Diff line number Diff line
# Script to generate the test script :P
import socket, psutil
import numpy as np

amount_of_cores = psutil.cpu_count(logical=False)
amount_of_cpus = psutil.cpu_count()
hostname = socket.gethostname()

if amount_of_cpus <= 4:
    stepsize = 1
elif 4 < amount_of_cpus <= 24:
    stepsize = 2
elif 24 < amount_of_cpus <= 48:
    stepsize = 4


# Generate the lines to run
with open("run_tests_{}.sh".format(hostname), "w") as f:
    command = ""
    for cpu_count in np.arange(stepsize, amount_of_cpus + stepsize, stepsize):

        command += "python3 evolve_population_comparing_with_multiprocessing.py 10 10 {} {}\n".format(
            cpu_count, hostname
        )
        command += "python3 evolve_population_comparing_with_multiprocessing.py 20 20 {} {}\n".format(
            cpu_count, hostname
        )
        command += "python3 evolve_population_comparing_with_multiprocessing.py 25 25 {} {}\n".format(
            cpu_count, hostname
        )
        command += "python3 evolve_population_comparing_with_multiprocessing.py 35 35 {} {}\n".format(
            cpu_count, hostname
        )
        command += "python3 evolve_population_comparing_with_multiprocessing.py 50 50 {} {}\n".format(
            cpu_count, hostname
        )

        command += "\n"

        f.write(command)
        command = ""
Loading