Skip to content
Snippets Groups Projects
test_benchmark.py 2.13 KiB
import subprocess
import numpy as np
import sympy as sp
import tempfile

import pytest
import pystencils as ps
from pathlib import Path

from pystencils_benchmark import Compiler
import pystencils_benchmark as pb


compilers = (Compiler.GCC, Compiler.GCCdebug, Compiler.Clang)
config_kwargs = ({},
                 {'cpu_vectorize_info': {'instruction_set': 'best'}},
                 {'cpu_vectorize_info': {'instruction_set': 'best',
                                         'assume_aligned': True}})


@pytest.mark.parametrize('compiler', compilers)
@pytest.mark.parametrize('config_kwarg', config_kwargs)
def test_generate(compiler, config_kwarg):
    a, b, c = ps.fields(a=np.ones(4000000), b=np.ones(4000000), c=np.ones(4000000))
    alpha = sp.symbols('alpha')

    @ps.kernel_config(ps.CreateKernelConfig(**config_kwarg))
    def vadd():
        a[0] @= b[0] + c[0]
    kernel_vadd = ps.create_kernel(**vadd)

    @ps.kernel_config(ps.CreateKernelConfig(**config_kwarg))
    def daxpy():
        b[0] @= alpha * a[0] + b[0]
    kernel_daxpy = ps.create_kernel(**daxpy)

    with tempfile.TemporaryDirectory(dir=Path.cwd()) as temp_dir:
        temp_dir = Path(temp_dir)
        pb.cpu.generate_benchmark([kernel_vadd, kernel_daxpy], temp_dir, compiler=compiler)
        subprocess.run(['make', '-C', f'{temp_dir}'], check=True)
        subprocess.run([f'{temp_dir}/benchmark-{compiler.name}', '10'], check=True)


gpu_kwargs = ({}, {'launch_bounds': (256,)}, {'launch_bounds': (256, 2)})


@pytest.mark.parametrize('kwargs', gpu_kwargs)
def test_generate_gpu(kwargs):
    compiler = Compiler.NVCC
    a, b, c = ps.fields(a=np.ones(4000000), b=np.ones(4000000), c=np.ones(4000000))

    @ps.kernel_config(ps.CreateKernelConfig(target=ps.Target.GPU))
    def vadd():
        a[0] @= b[0] + c[0]
    kernel_vadd = ps.create_kernel(**vadd)

    with tempfile.TemporaryDirectory(dir=Path.cwd()) as temp_dir:
        temp_dir = Path(temp_dir)
        pb.gpu.generate_benchmark(kernel_vadd, temp_dir, compiler=compiler, **kwargs)
        # subprocess.run(['make', '-C', f'{temp_dir}'], check=True)
        # subprocess.run([f'{temp_dir}/benchmark-{compiler.name}', '10'], check=True)