示例#1
0
 def generate_stencil(self, grid):
     import lfa_lab as lfa
     from evostencils.evaluation.convergence import lfa_sparse_stencil_to_constant_stencil
     lfa_grid = lfa.Grid(grid.dimension, grid.step_size)
     lfa_restriction = lfa.gallery.fw_restriction_stencil(
         lfa_grid, lfa_grid.coarse(self.coarsening_factor))
     return lfa_sparse_stencil_to_constant_stencil(lfa_restriction)
示例#2
0
    def test_fw_restriction_2(self):
        grid = lfa_lab.Grid(2)
        coarse_grid = grid.coarse((2, 2))
        s = fw_restriction_stencil(grid, coarse_grid)

        expect = [
            0.0625, 0.125, 0.0625, 0.125, 0.25, 0.125, 0.0625, 0.125, 0.0625
        ]

        for (o, v), v_expect in zip(s, expect):
            self.assertAlmostEqual(v, v_expect)
示例#3
0
    def test_ml_interpolation_2(self):
        grid = lfa_lab.Grid(2)
        coarse_grid = grid.coarse((2, 2))
        s = ml_interpolation_stencil(grid, coarse_grid)

        expect = [
            1 / 4, 2 / 4, 1 / 4, 2 / 4, 4 / 4, 2 / 4, 1 / 4, 2 / 4, 1 / 4
        ]

        for (o, v), v_expect in zip(s, expect):
            self.assertAlmostEqual(v, v_expect)
示例#4
0
    def test_ml_interpolation_3(self):
        grid = lfa_lab.Grid(3)
        coarse_grid = grid.coarse((2, 2, 2))
        s = ml_interpolation_stencil(grid, coarse_grid)

        expect = [
            1.0 / 8, 1.0 / 4, 1.0 / 8, 1.0 / 4, 1.0 / 2, 1.0 / 4, 1.0 / 8,
            1.0 / 4, 1.0 / 8, 1.0 / 4, 1.0 / 2, 1.0 / 4, 1.0 / 2, 1.0, 1.0 / 2,
            1.0 / 4, 1.0 / 2, 1.0 / 4, 1.0 / 8, 1.0 / 4, 1.0 / 8, 1.0 / 4,
            1.0 / 2, 1.0 / 4, 1.0 / 8, 1.0 / 4, 1.0 / 8
        ]

        for (o, v), v_expect in zip(s, expect):
            self.assertAlmostEqual(v, v_expect)
示例#5
0
    def test_fw_restriction_3(self):
        grid = lfa_lab.Grid(3)
        coarse_grid = grid.coarse((2, 2, 2))
        s = fw_restriction_stencil(grid, coarse_grid)

        expect = [
            1.0 / 64, 2.0 / 64, 1.0 / 64, 2.0 / 64, 4.0 / 64, 2.0 / 64,
            1.0 / 64, 2.0 / 64, 1.0 / 64, 2.0 / 64, 4.0 / 64, 2.0 / 64,
            4.0 / 64, 8.0 / 64, 4.0 / 64, 2.0 / 64, 4.0 / 64, 2.0 / 64,
            1.0 / 64, 2.0 / 64, 1.0 / 64, 2.0 / 64, 4.0 / 64, 2.0 / 64,
            1.0 / 64, 2.0 / 64, 1.0 / 64
        ]

        for (o, v), v_expect in zip(s, expect):
            self.assertAlmostEqual(v, v_expect)
示例#6
0
def main():

    # TODO adapt to actual path to exastencils project

    cwd = os.getcwd()
    compiler_path = f'{cwd}/../exastencils/Compiler/Compiler.jar'
    base_path = f'{cwd}/../exastencils/Examples'

    # 2D Finite difference discretized Poisson
    settings_path = f'Poisson/2D_FD_Poisson_fromL2.settings'
    knowledge_path = f'Poisson/2D_FD_Poisson_fromL2.knowledge'

    # 3D Finite difference discretized Poisson
    # settings_path = f'Poisson/3D_FD_Poisson_fromL2.settings'
    # knowledge_path = f'Poisson/3D_FD_Poisson_fromL2.knowledge'

    # 2D Finite volume discretized Poisson
    # settings_path = f'Poisson/2D_FV_Poisson_fromL2.settings'
    # knowledge_path = f'Poisson/2D_FV_Poisson_fromL2.knowledge'

    # 3D Finite volume discretized Poisson
    # settings_path = f'Poisson/3D_FV_Poisson_fromL2.settings'
    # knowledge_path = f'Poisson/3D_FV_Poisson_fromL2.knowledge'

    # 2D Finite difference discretized Bi-Harmonic Equation
    # settings_path = f'BiHarmonic/2D_FD_BiHarmonic_fromL2.settings'
    # knowledge_path = f'BiHarmonic/2D_FD_BiHarmonic_fromL2.knowledge'

    # 2D Finite volume discretized Stokes
    # settings_path = f'Stokes/2D_FD_Stokes_fromL2.settings'
    # knowledge_path = f'Stokes/2D_FD_Stokes_fromL2.knowledge'

    # 2D Finite difference discretized linear elasticity
    # settings_path = f'LinearElasticity/2D_FD_LinearElasticity_fromL2.settings'
    # knowledge_path = f'LinearElasticity/2D_FD_LinearElasticity_fromL2.knowledge'

    # settings_path = f'Helmholtz/2D_FD_Helmholtz_fromL2.settings'
    # knowledge_path = f'Helmholtz/2D_FD_Helmholtz_fromL2.knowledge'

    comm = MPI.COMM_WORLD
    nprocs = comm.Get_size()
    mpi_rank = comm.Get_rank()
    if nprocs > 1:
        tmp = "processes"
    else:
        tmp = "process"
    if mpi_rank == 0:
        print(f"Running {nprocs} MPI {tmp}")
    program_generator = ProgramGenerator(compiler_path, base_path, settings_path, knowledge_path, mpi_rank)

    # Evaluate baseline program
    # program_generator.run_exastencils_compiler()
    # program_generator.run_c_compiler()
    # time, convergence_factor = program_generator.evaluate()
    # print(f'Time: {time}, Convergence factor: {convergence_factor}')

    # Obtain extracted information from program generator
    dimension = program_generator.dimension
    finest_grid = program_generator.finest_grid
    coarsening_factors = program_generator.coarsening_factor
    min_level = program_generator.min_level
    max_level = program_generator.max_level
    equations = program_generator.equations
    operators = program_generator.operators
    fields = program_generator.fields

    lfa_grids = [lfa_lab.Grid(dimension, g.step_size) for g in finest_grid]
    convergence_evaluator = ConvergenceEvaluator(dimension, coarsening_factors, lfa_grids)
    bytes_per_word = 8
    # Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz
    peak_performance = 26633.33 * 1e6
    peak_bandwidth = 26570.26 * 1e6
    # Measured on the target platform
    runtime_coarse_grid_solver = 2.833324499999999 * 1e-3
    performance_evaluator = PerformanceEvaluator(peak_performance, peak_bandwidth, bytes_per_word,
                                                 runtime_coarse_grid_solver=runtime_coarse_grid_solver)
    infinity = 1e300
    epsilon = 1e-12
    problem_name = program_generator.problem_name

    if not os.path.exists(f'{cwd}/{problem_name}'):
        os.makedirs(f'{cwd}/{problem_name}')
    checkpoint_directory_path = f'{cwd}/{problem_name}/checkpoints_{mpi_rank}'
    optimizer = Optimizer(dimension, finest_grid, coarsening_factors, min_level, max_level, equations, operators, fields,
                          mpi_comm=comm, mpi_rank=mpi_rank, number_of_mpi_processes=nprocs,
                          convergence_evaluator=convergence_evaluator,
                          performance_evaluator=performance_evaluator, program_generator=program_generator,
                          epsilon=epsilon, infinity=infinity, checkpoint_directory_path=checkpoint_directory_path)

    # restart_from_checkpoint = True
    restart_from_checkpoint = False
    levels_per_run = max_level - min_level
    required_convergence = 0.9
    maximum_block_size = 8
    optimization_method = optimizer.NSGAIII
    if len(sys.argv) > 1:
        if sys.argv[1].upper() == "NSGAIII":
            optimization_method = optimizer.NSGAIII
        elif sys.argv[1].upper() == "NSGAII":
            optimization_method = optimizer.NSGAII
        elif sys.argv[1].upper() == "SOGP":
            optimization_method = optimizer.SOGP
        elif sys.argv[1].upper() == "RANDOM":
            optimization_method = optimizer.multi_objective_random_search

    crossover_probability = 2.0/3.0
    mutation_probability = 1.0 - crossover_probability
    minimum_solver_iterations = 8
    maximum_solver_iterations = 1024
    krylov_subspace_methods = ('ConjugateGradient', 'BiCGStab', 'MinRes', 'ConjugateResidual')
    program, pops, stats = optimizer.evolutionary_optimization(optimization_method=optimization_method,
                                                               levels_per_run=levels_per_run,
                                                               gp_mu=64, gp_lambda=64,
                                                               gp_crossover_probability=crossover_probability,
                                                               gp_mutation_probability=mutation_probability,
                                                               gp_generations=100, es_generations=150,
                                                               maximum_block_size=maximum_block_size,
                                                               required_convergence=required_convergence,
                                                               restart_from_checkpoint=restart_from_checkpoint,
                                                               krylov_subspace_methods=krylov_subspace_methods,
                                                               minimum_solver_iterations=minimum_solver_iterations,
                                                               maximum_solver_iterations=maximum_solver_iterations)
    
    if mpi_rank == 0:
        print(f'ExaSlang representation:\n{program}\n', flush=True)
    log_dir_name = f'{problem_name}/data_{mpi_rank}'
    if not os.path.exists(log_dir_name):
        os.makedirs(log_dir_name)
    for i, log in enumerate(stats):
        optimizer.dump_data_structure(log, f"{log_dir_name}/log_{i}.p")
    for i, pop in enumerate(pops):
        optimizer.dump_data_structure(pop, f"{log_dir_name}/pop_{i}.p")
示例#7
0
def main():

    # TODO adapt to actual path to exastencils project

    cwd = os.getcwd()
    compiler_path = f'{cwd}/../exastencils/Compiler/Compiler.jar'
    base_path = f'{cwd}/../exastencils/Examples'

    # 2D Finite difference discretized Poisson
    settings_path = f'Poisson/2D_FD_Poisson_fromL2.settings'
    knowledge_path = f'Poisson/2D_FD_Poisson_fromL2.knowledge'

    # 3D Finite difference discretized Poisson
    # settings_path = f'Poisson/3D_FD_Poisson_fromL2.settings'
    # knowledge_path = f'Poisson/3D_FD_Poisson_fromL2.knowledge'

    # 2D Finite volume discretized Poisson
    # settings_path = f'Poisson/2D_FV_Poisson_fromL2.settings'
    # knowledge_path = f'Poisson/2D_FV_Poisson_fromL2.knowledge'

    # 3D Finite volume discretized Poisson
    # settings_path = f'Poisson/3D_FV_Poisson_fromL2.settings'
    # knowledge_path = f'Poisson/3D_FV_Poisson_fromL2.knowledge'

    # 2D Finite difference discretized Bi-Harmonic Equation
    # settings_path = f'BiHarmonic/2D_FD_BiHarmonic_fromL2.settings'
    # knowledge_path = f'BiHarmonic/2D_FD_BiHarmonic_fromL2.knowledge'

    # 2D Finite volume discretized Stokes
    # settings_path = f'Stokes/2D_FV_Stokes_fromL2.settings'
    # knowledge_path = f'Stokes/2D_FV_Stokes_fromL2.knowledge'

    # 2D Finite difference discretized linear elasticity
    # settings_path = f'LinearElasticity/2D_FD_LinearElasticity_fromL2.settings'
    # knowledge_path = f'LinearElasticity/2D_FD_LinearElasticity_fromL2.knowledge'

    program_generator = ProgramGenerator(compiler_path, base_path,
                                         settings_path, knowledge_path)

    # Evaluate baseline program
    # program_generator.run_exastencils_compiler()
    # program_generator.run_c_compiler()
    # time, convergence_factor = program_generator.evaluate()
    # print(f'Time: {time}, Convergence factor: {convergence_factor}')

    # Obtain extracted information from program generator
    dimension = program_generator.dimension
    finest_grid = program_generator.finest_grid
    coarsening_factors = program_generator.coarsening_factor
    min_level = program_generator.min_level
    max_level = program_generator.max_level
    equations = program_generator.equations
    operators = program_generator.operators
    fields = program_generator.fields

    lfa_grids = [lfa_lab.Grid(dimension, g.step_size) for g in finest_grid]
    convergence_evaluator = ConvergenceEvaluator(dimension, coarsening_factors,
                                                 lfa_grids)
    bytes_per_word = 8
    peak_performance = 20344.07 * 1e6
    peak_bandwidth = 19255.70 * 1e6
    performance_evaluator = PerformanceEvaluator(peak_performance,
                                                 peak_bandwidth,
                                                 bytes_per_word)
    infinity = np.finfo(np.float64).max
    epsilon = 1e-10
    problem_name = program_generator.problem_name

    if not os.path.exists(problem_name):
        os.makedirs(problem_name)
    checkpoint_directory_path = f'{problem_name}/checkpoints'
    if not os.path.exists(checkpoint_directory_path):
        os.makedirs(checkpoint_directory_path)
    optimizer = Optimizer(dimension,
                          finest_grid,
                          coarsening_factors,
                          min_level,
                          max_level,
                          equations,
                          operators,
                          fields,
                          convergence_evaluator=convergence_evaluator,
                          performance_evaluator=performance_evaluator,
                          program_generator=program_generator,
                          epsilon=epsilon,
                          infinity=infinity,
                          checkpoint_directory_path=checkpoint_directory_path)
    base_path = ''
    hofs = []
    front = []
    for i in range(0, 3):
        file_name = f'{base_path}_NSGAII_{i}/data'
        pop = optimizer.load_data_structure(f'{file_name}/pop_0.p')
        hof = tools.ParetoFront(similar=lambda a, b: a.fitness == b.fitness)
        hof.update(pop)
        hofs.append(hof)
        front += [(*ind.fitness.values, 'NSGA-II', i + 1) for ind in hof]

    for i in range(0, 3):
        file_name = f'{base_path}_RANDOM_{i}/data'
        pop = optimizer.load_data_structure(f'{file_name}/pop_0.p')
        hof = tools.ParetoFront(similar=lambda a, b: a.fitness == b.fitness)
        hof.update(pop)
        hofs.append(hof)
        front += [(*ind.fitness.values, 'Random', i + 1) for ind in hof]

    columns = [
        'Estimated Number of Iterations',
        'Estimated Runtime per Iteration (ms)', 'Method', 'Experiment'
    ]
    df = pd.DataFrame(front, columns=columns)
    rc('text', usetex=True)
    sns.set(style="white")
    sns.set_context("paper")
    sns.despine()
    palette = sns.color_palette("colorblind", n_colors=3)
    sns.relplot(x=columns[0],
                y=columns[1],
                hue=columns[3],
                style=columns[2],
                data=df,
                kind='line',
                markers=['o', 'X', 's'],
                dashes=False,
                palette=palette)
    plt.xlim(0, 35)
    plt.tight_layout()
    plt.savefig("pareto-front.pdf", quality=100, dpi=300)
示例#8
0
# LFA Lab - Library to simplify local Fourier analysis.
# Copyright (C) 2018  Hannah Rittich
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import lfa_lab
import lfa_lab.gallery

grid = lfa_lab.Grid(2)
stencil = lfa_lab.gallery.poisson_2d(grid)
BJ = lfa_lab.rb_block_jacobi(stencil, (8, 8), 1.0)

sym = BJ.symbol((32, 32))
M = sym.col_norms_2d()
示例#9
0
 def setUp(self):
     self.fine = lfa_lab.Grid(2)
     self.coarse = self.fine.coarse((2, 2))
示例#10
0
 def test_zero(self):
     fine = lfa_lab.Grid(2)
     lfa_lab.operator.zero(fine)
示例#11
0
 def test_identity(self):
     fine = lfa_lab.Grid(2)
     lfa_lab.operator.identity(fine)
示例#12
0
 def reinitialize_lfa_grids(self, finest_grids: List[base.Grid]):
     self._lfa_grids = [
         lfa_lab.Grid(self.dimension, g.step_size) for g in finest_grids
     ]
示例#13
0
 def __init__(self, dimension, coarsening_factors, finest_grid):
     self._coarsening_factors = coarsening_factors
     self._dimension = dimension
     self._lfa_grids = [lfa_lab.Grid(dimension, g.spacing) for g in finest_grid]