From 3bb6ca17126ce654c59f8d0ca3ad6872ef52e9ba Mon Sep 17 00:00:00 2001 From: jpekkila Date: Wed, 18 Sep 2019 17:28:29 +0300 Subject: [PATCH] The Astaroth Code Compiler (acc) is now built with cmake. Additionally, make is now used to generate the CUDA headers from DSL sources. The headers are also properly regenerated whenever a DSL file has been changed. With this commit, the DSL is now seamlessly integrated to the library and we no longer need complicated scripts to figure out the correct files. The current workflow for using custom DSL sources is to pass the DSL module directory to cmake, f.ex. cmake -DDSL_MODULE_DIR=/acc/mhd_solver. Note that the path must be absolute or then given relative to the CMakeLists.txt directory. f.ex cd build && cmake -DDSL_MODULE_DIR=../acc/mhd_solver does not work. CMake then takes all DSL files in that directory and handles the rest. --- CMakeLists.txt | 29 +++++++++++++++++++++++++++++ acc/build_acc.sh | 8 ++++---- acc/compile.sh | 14 +++++++------- acc/preprocess.sh | 2 +- acc/src/code_generator.c | 5 ++++- scripts/compile_acc_module.sh | 9 +++++++++ src/core/CMakeLists.txt | 1 + 7 files changed, 55 insertions(+), 13 deletions(-) create mode 100755 scripts/compile_acc_module.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 6007a37..5e46e06 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,35 @@ option(DOUBLE_PRECISION "Generates double precision code" option(MULTIGPU_ENABLED "If enabled, uses all the available GPUs" ON) option(ALTER_CONF "If enabled, loads astaroth.conf from the build directory" OFF) +## Compile the Astaroth Code compiler +find_package(FLEX) +find_package(BISON) +execute_process ( + COMMAND ./build_acc.sh + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/acc +) +# NOTE: Manually defined DSL_MODULE_DIR must be set relative to the project root, not the actual +# build directory! +# NO! ../acc/mhd_solver +# YES! acc/mhd_solver + +if (NOT DEFINED DSL_MODULE_DIR) + set(DSL_MODULE_DIR ${CMAKE_SOURCE_DIR}/acc/mhd_solver) +endif () +get_filename_component(DSL_MODULE_DIR ${DSL_MODULE_DIR} REALPATH) +message(STATUS "DSL module dir: ${DSL_MODULE_DIR}") +set(DSL_SOURCES ${DSL_MODULE_DIR}/*) +set(DSL_HEADERS "${PROJECT_BINARY_DIR}/stencil_assembly.cuh" + "${PROJECT_BINARY_DIR}/stencil_process.cuh" + "${PROJECT_BINARY_DIR}/stencil_defines.h") +add_custom_command ( + COMMENT "Building AC objects ${DSL_MODULE_DIR}" + COMMAND ${CMAKE_SOURCE_DIR}/scripts/compile_acc_module.sh ${DSL_MODULE_DIR} + DEPENDS ${DSL_SOURCES} + OUTPUT ${DSL_HEADERS} +) +add_custom_target(dsl_headers ALL DEPENDS ${DSL_HEADERS}) + ## Build types # Available types (case-sensitive): # RELEASE (best performance) diff --git a/acc/build_acc.sh b/acc/build_acc.sh index ed275d5..6bcab28 100755 --- a/acc/build_acc.sh +++ b/acc/build_acc.sh @@ -6,14 +6,14 @@ COMPILER_NAME="acc" SRC_DIR=${PWD}/src BUILD_DIR=${PWD}/build -echo "Created" ${BUILD_DIR} +echo "-- Compiling acc:" ${BUILD_DIR} mkdir -p ${BUILD_DIR} cd ${BUILD_DIR} -echo ${BASE_DIR} -echo ${SRC_DIR} -echo ${BUILD_DIR} +#echo ${BASE_DIR} +#echo ${SRC_DIR} +#echo ${BUILD_DIR} # Generate Bison headers bison --verbose -d ${SRC_DIR}/${COMPILER_NAME}.y diff --git a/acc/compile.sh b/acc/compile.sh index 0fe6b4c..256ddba 100755 --- a/acc/compile.sh +++ b/acc/compile.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Usage ./compile +# Usage ./compile ACC_DIR=`dirname $0` @@ -10,19 +10,19 @@ EXTENSION="${FULL_NAME##*.}" if [ "${EXTENSION}" = "sas" ]; then COMPILE_FLAGS="-sas" # Generate stencil assembly stage CUH_FILENAME="stencil_assembly.cuh" - echo "Generating stencil assembly stage ${FILENAME}.sas -> ${CUH_FILENAME}" + echo "-- Generating stencil assembly stage: ${FILENAME}.sas -> ${CUH_FILENAME}" elif [ "${EXTENSION}" = "sps" ]; then COMPILE_FLAGS="-sps" # Generate stencil processing stage CUH_FILENAME="stencil_process.cuh" - echo "Generating stencil processing stage: ${FILENAME}.sps -> ${CUH_FILENAME}" + echo "-- Generating stencil processing stage: ${FILENAME}.sps -> ${CUH_FILENAME}" elif [ "${EXTENSION}" = "sdh" ]; then COMPILE_FLAGS="-sdh" # Generate stencil definition header CUH_FILENAME="stencil_defines.h" - echo "Generating stencil definition header: ${FILENAME}.sdh -> ${CUH_FILENAME}" + echo "-- Generating stencil definition header: ${FILENAME}.sdh -> ${CUH_FILENAME}" else - echo "Error: unknown extension" ${EXTENSION} "of file" ${FULL_NAME} - echo "Extension should be either .sas, .sps or .sdh" + echo "-- Error: unknown extension" ${EXTENSION} "of file" ${FULL_NAME} + echo "-- Extension should be either .sas, .sps or .sdh" exit fi -${ACC_DIR}/preprocess.sh $1 | ${ACC_DIR}/build/acc ${COMPILE_FLAGS} > ${CUH_FILENAME} +${ACC_DIR}/preprocess.sh $@ | ${ACC_DIR}/build/acc ${COMPILE_FLAGS} > ${CUH_FILENAME} diff --git a/acc/preprocess.sh b/acc/preprocess.sh index ededfdc..dad6b2b 100755 --- a/acc/preprocess.sh +++ b/acc/preprocess.sh @@ -1,4 +1,4 @@ #!/bin/bash # Preprocesses the give file using GCC. This script is usually automatically called in # ./compile.sh, but may be called also individually for debugging purposes. -cat ${@} | gcc -x c -E - | sed "s/#.*//g" +cat $1 | gcc ${@:2} -x c -E - | sed "s/#.*//g" diff --git a/acc/src/code_generator.c b/acc/src/code_generator.c index b961f77..98f3b4d 100644 --- a/acc/src/code_generator.c +++ b/acc/src/code_generator.c @@ -650,8 +650,10 @@ main(int argc, char** argv) compilation_type = STENCIL_PROCESS; else if (!strcmp(argv[1], "-sdh")) compilation_type = STENCIL_HEADER; - else + else { printf("Unknown flag %s. Generating stencil assembly.\n", argv[1]); + return EXIT_FAILURE; + } } else { printf("Usage: ./acc [flags]\n" @@ -685,4 +687,5 @@ main(int argc, char** argv) // Cleanup astnode_destroy(root); // printf("COMPILATION SUCCESS\n"); + return EXIT_SUCCESS; } diff --git a/scripts/compile_acc_module.sh b/scripts/compile_acc_module.sh new file mode 100755 index 0000000..49829be --- /dev/null +++ b/scripts/compile_acc_module.sh @@ -0,0 +1,9 @@ +#!/bin/bash +ACC_DIR=$(realpath $(dirname $0)/../acc) +MODULE_DIR=$(realpath $1) + +echo "-- Compiling project in "${MODULE_DIR} +for source in ${MODULE_DIR}/*.sas ${MODULE_DIR}/*.sps ${MODULE_DIR}/*.sdh +do + ${ACC_DIR}/compile.sh $source -I ${MODULE_DIR} +done diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 642c7f1..d47a211 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -37,3 +37,4 @@ endif () cuda_add_library(astaroth_core STATIC astaroth.cu device.cu node.cu) target_include_directories(astaroth_core PRIVATE .) target_link_libraries(astaroth_core m) +add_dependencies(astaroth_core dsl_headers)