Skip to content
Snippets Groups Projects

Add CUDA support

Merged Markus Holzer requested to merge CUDA into master
3 files
+ 126
147
Compare changes
  • Side-by-side
  • Inline
Files
3
from typing import Union, List
from typing import Union, List
from collections import namedtuple
from collections import namedtuple
from pathlib import Path
from pathlib import Path
from jinja2 import Environment, PackageLoader, StrictUndefined
import numpy as np
import numpy as np
from pystencils.backends.cbackend import generate_c, get_headers
from pystencils.astnodes import KernelFunction, PragmaBlock
from pystencils.astnodes import KernelFunction, PragmaBlock
from pystencils.enums import Backend
from pystencils.enums import Backend
from pystencils.typing import get_base_type
from pystencils.typing import get_base_type
from pystencils.sympyextensions import prod
from pystencils.sympyextensions import prod
from pystencils.integer_functions import modulo_ceil
from pystencils.integer_functions import modulo_ceil
 
from pystencils_benchmark.common import (_env,
 
_kernel_source,
 
_kernel_header,
 
compiler_toolchain,
 
copy_static_files,
 
setup_directories)
from pystencils_benchmark.enums import Compiler
from pystencils_benchmark.enums import Compiler
_env = Environment(loader=PackageLoader('pystencils_benchmark'), undefined=StrictUndefined, keep_trailing_newline=True,
trim_blocks=True, lstrip_blocks=True)
def generate_benchmark(kernel_asts: Union[KernelFunction, List[KernelFunction]],
def generate_benchmark(kernel_asts: Union[KernelFunction, List[KernelFunction]],
path: Path = None,
path: Path = None,
*,
*,
@@ -26,14 +26,8 @@ def generate_benchmark(kernel_asts: Union[KernelFunction, List[KernelFunction]],
@@ -26,14 +26,8 @@ def generate_benchmark(kernel_asts: Union[KernelFunction, List[KernelFunction]],
timing: bool = True,
timing: bool = True,
likwid: bool = False
likwid: bool = False
) -> None:
) -> None:
if path is None:
path = Path('.')
src_path, include_path = setup_directories(path)
else:
path.mkdir(parents=True, exist_ok=True)
src_path = path / 'src'
src_path.mkdir(parents=True, exist_ok=True)
include_path = path / 'include'
include_path.mkdir(parents=True, exist_ok=True)
if isinstance(kernel_asts, KernelFunction):
if isinstance(kernel_asts, KernelFunction):
kernel_asts = [kernel_asts]
kernel_asts = [kernel_asts]
@@ -56,39 +50,6 @@ def generate_benchmark(kernel_asts: Union[KernelFunction, List[KernelFunction]],
@@ -56,39 +50,6 @@ def generate_benchmark(kernel_asts: Union[KernelFunction, List[KernelFunction]],
compiler_toolchain(path, compiler, likwid)
compiler_toolchain(path, compiler, likwid)
def compiler_toolchain(path: Path, compiler: Compiler, likwid: bool) -> None:
name = compiler.name
jinja_context = {
'compiler': name,
'likwid': likwid,
}
files = ['Makefile', f'{name}.mk']
for file_name in files:
with open(path / file_name, 'w+') as f:
template = _env.get_template(file_name).render(**jinja_context)
f.write(template)
def copy_static_files(path: Path) -> None:
src_path = path / 'src'
src_path.mkdir(parents=True, exist_ok=True)
include_path = path / 'include'
include_path.mkdir(parents=True, exist_ok=True)
files = ['timing.h', 'timing.c']
for file_name in files:
template = _env.get_template(file_name).render()
if file_name[-1] == 'h':
target_path = include_path / file_name
elif file_name[-1] == 'c':
target_path = src_path / file_name
else:
target_path = path / file_name
with open(target_path, 'w+') as f:
f.write(template)
def kernel_main(kernels_ast: List[KernelFunction], *,
def kernel_main(kernels_ast: List[KernelFunction], *,
timing: bool = True, likwid: bool = False) -> str:
timing: bool = True, likwid: bool = False) -> str:
"""
"""
@@ -164,29 +125,8 @@ def kernel_main(kernels_ast: List[KernelFunction], *,
@@ -164,29 +125,8 @@ def kernel_main(kernels_ast: List[KernelFunction], *,
def kernel_header(kernel_ast: KernelFunction, dialect: Backend = Backend.C) -> str:
def kernel_header(kernel_ast: KernelFunction, dialect: Backend = Backend.C) -> str:
function_signature = generate_c(kernel_ast, dialect=dialect, signature_only=True)
return _kernel_header(kernel_ast, dialect=dialect, template_file='cpu/kernel.h')
header_guard = f'_{kernel_ast.function_name.upper()}_H'
jinja_context = {
'header_guard': header_guard,
'function_signature': function_signature,
}
header = _env.get_template('cpu/kernel.h').render(**jinja_context)
return header
def kernel_source(kernel_ast: KernelFunction, dialect: Backend = Backend.C) -> str:
def kernel_source(kernel_ast: KernelFunction, dialect: Backend = Backend.C) -> str:
kernel_name = kernel_ast.function_name
return _kernel_source(kernel_ast, dialect=dialect, template_file='cpu/kernel.c')
function_source = generate_c(kernel_ast, dialect=dialect)
headers = {f'"{kernel_name}.h"', '<math.h>', '<stdint.h>'}
headers.update(get_headers(kernel_ast))
jinja_context = {
'function_source': function_source,
'headers': sorted(headers),
'timing': True,
}
source = _env.get_template('cpu/kernel.c').render(**jinja_context)
return source
Loading