Skip to content
Snippets Groups Projects
Commit c509ac39 authored by Frederik Hennig's avatar Frederik Hennig
Browse files

First version of cmake integration

parent 602559e7
Branches
Tags
No related merge requests found
......@@ -3,4 +3,5 @@
**/bin
**/obj
**/out
**/generated_src
\ No newline at end of file
**/generated_src
**/build
\ No newline at end of file
Notes.md 0 → 100644
# Build System Integration
## Configurator Script
The configurator script should configure the code generator and provide global configuration to all codegen scripts.
In the CMake integration, it can be specified globally via the `PystencilsSfg_CONFIGURATOR_SCRIPT` cache variable.
To decide and implement:
- Use `runpy` and communicate via a global variable, or use `importlib.util.spec_from_file_location` and communicate via
a function call? In either case, there needs to be concensus about at least one name in the configurator script.
- Allow specifying a separate configurator file at `pystencilssfg_generate_target_sources`? Sound sensible... It's basically
for free with the potential to add lots of flexibility
## Generator flags
Two separate lists of flags may be passed to generator scripts: Some may be evaluated by the SFG, and the rest
will be passed on to the user script.
Arguments to the SFG include:
- Path of the configurator script
- Output directory
How to separate user from generator arguments?
set( PystencilsSfg_FOUND OFF CACHE BOOL "pystencils source file generator found" )
set( PystencilsSfg_CONFIGURATOR_SCRIPT "" CACHE STRING "Configurator script for the pystencils source file generator" )
mark_as_advanced( PystencilsSfg_FOUND )
find_package( Python COMPONENTS Interpreter REQUIRED )
# Try to find pystencils-sfg in the python environment
execute_process(COMMAND ${Python_EXECUTABLE} -c "import pystencilssfg; print(pystencilssfg.__version__, end='')"
RESULT_VARIABLE _PystencilsSfgFindResult OUTPUT_VARIABLE PystencilsSfg_VERSION )
if(${_PystencilsSfgFindResult} EQUAL 0)
set( PystencilsSfg_FOUND ON )
endif()
if(${PystencilsSfg_FIND_REQUIRED} AND (NOT ${PystencilsSfg_FOUND}))
message( FATAL_ERROR "Could not find pystencils-sfg in current Python environment." )
endif()
if(${PystencilsSfg_FOUND})
message( STATUS "Found pystencils Source File Generator (Version ${PystencilsSfg_VERSION})")
execute_process(COMMAND ${Python_EXECUTABLE} -c "from pystencilssfg.cmake import get_sfg_cmake_modulepath; print(get_sfg_cmake_modulepath(), end='')"
OUTPUT_VARIABLE _PystencilsSfg_CMAKE_MODULE_PATH)
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${_PystencilsSfg_CMAKE_MODULE_PATH})
include( PystencilsSfgFunctions )
endif()
......@@ -7,3 +7,5 @@ __all__ = [
SourceFileGenerator, SfgContext, SfgKernelNamespace, SfgKernelHandle,
PsType, SrcType
]
__version__ = "0.0.0"
set(PSSFG_GENERATED_SOURCES_DIR "${CMAKE_BINARY_DIR}/pystencils_generated_sources")
file(MAKE_DIRECTORY "${PSSFG_GENERATED_SOURCES_DIR}")
include_directories(${PSSFG_GENERATED_SOURCES_DIR})
function(pystencilssfg_generate_target_sources)
set(options)
set(oneValueArgs TARGET SCRIPT)
set(multiValueArgs DEPENDS)
cmake_parse_arguments(GENSRC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
set(generatedSourcesDir ${PSSFG_GENERATED_SOURCES_DIR}/gen/${GENSRC_TARGET})
get_filename_component(basename ${GENSRC_SCRIPT} NAME_WLE)
cmake_path(ABSOLUTE_PATH GENSRC_SCRIPT OUTPUT_VARIABLE pythonFile)
set(generatedSourceFiles ${basename}.h ${basename}.cpp)
set(generatedWithAbsolutePath)
foreach (filename ${generatedSourceFiles})
list(APPEND generatedWithAbsolutePath ${generatedSourcesDir}/${filename})
endforeach ()
file(MAKE_DIRECTORY "${generatedSourcesDir}")
# TODO: Get generator arguments via PYSTENCILS_GENERATOR_FLAGS, source file and target properties
add_custom_command(OUTPUT ${generatedWithAbsolutePath}
DEPENDS ${pythonFile} ${GENSRC_DEPENDS}
COMMAND ${Python_EXECUTABLE} ${pythonFile}
WORKING_DIRECTORY "${generatedSourcesDir}")
target_sources(${GENSRC_TARGET} PRIVATE ${generatedWithAbsolutePath})
endfunction()
from os.path import dirname, realpath
def get_sfg_cmake_modulepath():
return dirname(realpath(__file__))
......@@ -36,22 +36,22 @@ class SourceFileGenerator:
parser = ArgumentParser(
"pystencilssfg",
description="pystencils Source File Generator")
description="pystencils Source File Generator",
allow_abbrev=False)
parser.add_argument("script_args", nargs='*')
parser.add_argument("-d", "--output-dir", type=str, default='.', dest='output_directory')
parser.add_argument("-d", "--sfg-output-dir", type=str, default='.', dest='output_directory')
args = parser.parse_args(sys.argv)
generator_args, script_args = parser.parse_known_args(sys.argv)
import __main__
scriptpath = __main__.__file__
scriptname = path.split(scriptpath)[1]
basename = path.splitext(scriptname)[0]
self._context = SfgContext(args.script_args, namespace, codestyle)
self._context = SfgContext(script_args, namespace, codestyle)
from .emitters.cpu.basic_cpu import BasicCpuEmitter
self._emitter = BasicCpuEmitter(self._context, basename, args.output_directory)
self._emitter = BasicCpuEmitter(self._context, basename, generator_args.output_directory)
def clean_files(self):
for file in self._emitter.output_files:
......
cmake_minimum_required( VERSION 3.24 )
project( pssfg_cmake_integration_test )
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${pssfg_cmake_integration_test_SOURCE_DIR}/../../cmake )
find_package( PystencilsSfg REQUIRED )
add_library( genlib )
pystencilssfg_generate_target_sources( TARGET genlib SCRIPT kernels.py )
import sympy as sp
import numpy as np
from pystencils.session import *
from pystencilssfg import SourceFileGenerator
from pystencilssfg.source_concepts.cpp import std_mdspan
with SourceFileGenerator("poisson") as sfg:
src, dst = ps.fields("src, dst(1) : double[2D]")
h = sp.Symbol('h')
@ps.kernel
def poisson_jacobi():
dst[0,0] @= (src[1, 0] + src[-1, 0] + src[0, 1] + src[0, -1]) / 4
poisson_kernel = sfg.kernels.create(poisson_jacobi)
sfg.function("jacobi_smooth")(
sfg.call(poisson_kernel)
)
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment