コード例 #1
0
ファイル: representation.py プロジェクト: Nurtal/dpix_clean
def compute_grid_score_wrapper(corr_matrix, grid_matrix):
    """
	Split the grid matrix and use compute matrix score
	function on each splitted block using multiprocessing

	-> x 10 acceleration on 119*119 grid compared to linear
	approach

	-> return the grid score (actually sum of the splitted grid's score)
	"""

    ## importation
    import numpy as np
    from pathos.multiprocessing import ProcessingPool as Pool
    from operator import add
    import functools

    ## split the grid
    half_split = np.array_split(grid_matrix, 2)
    res = map(lambda x: np.array_split(x, 2, axis=1), half_split)

    ## python 2
    #splitted_grids = reduce(add, res)

    ## python 3
    splitted_grids = functools.reduce(add, res)

    ## Multiprocessing, call score function on each
    ## part of the splitted grid
    score = 0
    inputs = []

    ## create inputs
    for grid in splitted_grids:
        inputs.append((corr_matrix, grid))

    ## run process
    try:
        res = Pool().amap(compute_matrix_score, inputs)
        results = res.get()
    except:

        ## DEBUG : pool lib mihgt break memory on big grids
        ## trying to split the inputs to solve the problem
        print("[WARNINGS] : Grid dcore computint : BREAKING MEMORY")
        print(
            "[WARNINGS] : Attempt to reduce charge on CPU : splitting multiprocessing"
        )

        inputs_1 = inputs[0:int(len(inputs) / 2)]
        inputs_2 = inputs[int(len(inputs) / 2):]

        res_1 = Pool().amap(compute_matrix_score, inputs_1)
        results_1 = res.get()

        res_2 = Pool().amap(compute_matrix_score, inputs_2)
        results_2 = res.get()

        results = results_1 + results_2

    ## compute results
    for scalar in results:
        score += scalar

    return score