workers.py 3.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
# -*- coding: utf-8 -*-
# Copyright (C) 2013 Centre de données Astrophysiques de Marseille
# Copyright (C) 2013-2014 Institute of Astronomy
# Copyright (C) 2014 Yannick Roehlly <yannick@iaora.eu>
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Yannick Roehlly & Médéric Boquien

import time

import numpy as np

12
from ...warehouse import SedWarehouse
13

14
from ..utils import OUT_DIR
15

Médéric Boquien's avatar
Médéric Boquien committed
16

17
def init_fluxes(params, filters, save_sed, fluxes, info, t_begin, n_computed):
18 19 20 21 22 23 24 25
    """Initializer of the pool of processes. It is mostly used to convert
    RawArrays into numpy arrays. The latter are defined as global variables to
    be accessible from the workers.

    Parameters
    ----------
    params: ParametersHandler
        Handles the parameters from a 1D index.
26 27
    filters: List
        Contains the names of the filters to compute the fluxes.
28 29
    save_sed: boolean
        Indicates whether the SED should be saved.
30 31 32 33 34 35 36 37 38
    fluxes: RawArray and tuple containing the shape
        Fluxes of individual models. Shared among workers.
    n_computed: Value
        Number of computed models. Shared among workers.
    t_begin: float
        Time of the beginning of the computation.

    """
    global gbl_model_fluxes, gbl_model_info, gbl_n_computed, gbl_t_begin
39 40
    global gbl_params, gbl_previous_idx, gbl_filters, gbl_save_sed
    global gbl_warehouse
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

    gbl_model_fluxes = np.ctypeslib.as_array(fluxes[0])
    gbl_model_fluxes = gbl_model_fluxes.reshape(fluxes[1])

    gbl_model_info = np.ctypeslib.as_array(info[0])
    gbl_model_info = gbl_model_info.reshape(info[1])

    gbl_n_computed = n_computed
    gbl_t_begin = t_begin

    gbl_params = params

    gbl_previous_idx = -1

    gbl_filters = filters

57 58
    gbl_save_sed = save_sed

59
    gbl_warehouse = SedWarehouse()
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81


def fluxes(idx):
    """Worker process to retrieve a SED and affect the relevant data to shared
    RawArrays.

    Parameters
    ----------
    idx: int
        Index of the model to retrieve its parameters from the parameters
        handler.

    """
    global gbl_previous_idx
    if gbl_previous_idx > -1:
        gbl_warehouse.partial_clear_cache(
            gbl_params.index_module_changed(gbl_previous_idx, idx))
    gbl_previous_idx = idx

    sed = gbl_warehouse.get_sed(gbl_params.modules,
                                gbl_params.from_index(idx))

Médéric Boquien's avatar
Médéric Boquien committed
82
    if gbl_save_sed is True:
83 84
        sed.to_votable(OUT_DIR + "{}_best_model.xml".format(idx))

85
    if 'sfh.age' in sed.info and sed.info['sfh.age'] > sed.info['universe.age']:
86 87
        model_fluxes = -99. * np.ones(len(gbl_filters))
    else:
88 89
        model_fluxes = np.array([sed.compute_fnu(filter_) for filter_ in
                                 gbl_filters])
90 91 92 93 94 95 96 97 98 99 100 101 102

    gbl_model_fluxes[idx, :] = model_fluxes
    gbl_model_info[idx, :] = list(sed.info.values())

    with gbl_n_computed.get_lock():
        gbl_n_computed.value += 1
        n_computed = gbl_n_computed.value
    if n_computed % 100 == 0 or n_computed == gbl_params.size:
        t_elapsed = time.time() - gbl_t_begin
        print("{}/{} models computed in {} seconds ({} models/s)".
              format(n_computed, gbl_params.size,
                     np.around(t_elapsed, decimals=1),
                     np.around(n_computed/t_elapsed, decimals=1)),
103
              end="\n" if n_computed == gbl_params.size else "\r")