From 3bb6ca17126ce654c59f8d0ca3ad6872ef52e9ba Mon Sep 17 00:00:00 2001 From: jpekkila Date: Wed, 18 Sep 2019 17:28:29 +0300 Subject: [PATCH 1/5] 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) From 24f46324e0dc2f97c9f9e03f4ae8b3319ed4b49c Mon Sep 17 00:00:00 2001 From: jpekkila Date: Wed, 18 Sep 2019 19:22:15 +0300 Subject: [PATCH 2/5] Deprecated the old style of selecting a config file (was a compile-time parameter). The config file is now a runtime parameter and can be changed without recompilation. Usage: ./ac_run -s . config/astaroth.conf is selected by default if the custom path is not supplied to ac_run. --- CMakeLists.txt | 1 - src/standalone/CMakeLists.txt | 9 +-------- src/standalone/autotest.cc | 4 ++-- src/standalone/benchmark.cc | 4 ++-- src/standalone/config_loader.cc | 6 ++++-- src/standalone/config_loader.h | 2 +- src/standalone/main.cc | 18 +++++++++++++----- src/standalone/renderer.cc | 6 +++--- src/standalone/run.h | 8 ++++---- src/standalone/simulation.cc | 4 ++-- 10 files changed, 32 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e46e06..6954dad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,6 @@ option(BUILD_C_API_TEST "Builds a C program to test whether the API is c option(BUILD_MPI_TEST "Builds a C program to test whether MPI works" OFF) option(DOUBLE_PRECISION "Generates double precision code" OFF) 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) diff --git a/src/standalone/CMakeLists.txt b/src/standalone/CMakeLists.txt index 0a61ede..b49ba98 100644 --- a/src/standalone/CMakeLists.txt +++ b/src/standalone/CMakeLists.txt @@ -30,11 +30,4 @@ target_link_libraries(astaroth_standalone PRIVATE astaroth_core "${OpenMP_CXX_FL add_executable(ac_run main.cc) target_link_libraries(ac_run PRIVATE astaroth_standalone) - -# Define the config directory -if (ALTER_CONF) - # ASTAROTH_CONF_PATH supplied by ac_mkbuilddir.sh - target_compile_definitions(astaroth_standalone PRIVATE CONFIG_PATH="${ASTAROTH_CONF_PATH}/") -else() - target_compile_definitions(astaroth_standalone PRIVATE CONFIG_PATH="${CMAKE_SOURCE_DIR}/config/") -endif() +add_definitions(-DAC_DEFAULT_CONFIG="${CMAKE_SOURCE_DIR}/config/astaroth.conf") diff --git a/src/standalone/autotest.cc b/src/standalone/autotest.cc index f484b30..76f5a63 100644 --- a/src/standalone/autotest.cc +++ b/src/standalone/autotest.cc @@ -456,7 +456,7 @@ check_rk3(const AcMeshInfo& mesh_info) } int -run_autotest(void) +run_autotest(const char* config_path) { #if GEN_TEST_RESULT == 1 char testresult_path[256]; @@ -471,7 +471,7 @@ run_autotest(void) /* Parse configs */ AcMeshInfo config; - load_config(&config); + load_config(config_path, &config); if (STENCIL_ORDER > 6) printf("WARNING!!! If the stencil order is larger than the computational domain some " diff --git a/src/standalone/benchmark.cc b/src/standalone/benchmark.cc index 1a6a919..39a1e4d 100644 --- a/src/standalone/benchmark.cc +++ b/src/standalone/benchmark.cc @@ -41,13 +41,13 @@ #include int -run_benchmark(void) +run_benchmark(const char* config_path) { const int nn = 256; const int num_iters = 100; AcMeshInfo mesh_info; - load_config(&mesh_info); + load_config(config_path, &mesh_info); mesh_info.int_params[AC_nx] = nn; mesh_info.int_params[AC_ny] = mesh_info.int_params[AC_nx]; mesh_info.int_params[AC_nz] = mesh_info.int_params[AC_nx]; diff --git a/src/standalone/config_loader.cc b/src/standalone/config_loader.cc index 98098ff..e06e2e3 100644 --- a/src/standalone/config_loader.cc +++ b/src/standalone/config_loader.cc @@ -159,14 +159,16 @@ update_config(AcMeshInfo* config) \return 0 on success, -1 if there are potentially uninitialized values. */ int -load_config(AcMeshInfo* config) +load_config(const char* config_path, AcMeshInfo* config) { int retval = 0; + ERRCHK(config_path); + // memset reads the second parameter as a byte even though it says int in // the function declaration memset(config, (uint8_t)0xFF, sizeof(*config)); - parse_config(CONFIG_PATH "astaroth.conf", config); + parse_config(config_path, config); update_config(config); // sizeof(config) must be a multiple of 4 bytes for this to work diff --git a/src/standalone/config_loader.h b/src/standalone/config_loader.h index c1db21a..5753197 100644 --- a/src/standalone/config_loader.h +++ b/src/standalone/config_loader.h @@ -26,7 +26,7 @@ #include "astaroth.h" /** Loads data from the config file */ -int load_config(AcMeshInfo* config); +int load_config(const char* config_path, AcMeshInfo* config); /** Recalculates the portion of int parameters which get their values from nx, * ny and nz. Must be called after modifying the config struct or otherwise diff --git a/src/standalone/main.cc b/src/standalone/main.cc index 48d5fc6..24f3575 100644 --- a/src/standalone/main.cc +++ b/src/standalone/main.cc @@ -73,16 +73,24 @@ main(int argc, char* argv[]) for (int i = 0; i < argc; ++i) printf("%d: %s\n", i, argv[i]); + const size_t buf_size = 256; + char config_path[buf_size]; + (argc == 3) ? strncpy(config_path, argv[2], buf_size) + : strncpy(config_path, AC_DEFAULT_CONFIG, buf_size); + + printf("Config path: %s\n", config_path); if (argc == 1) { - return run_renderer(); + return run_renderer(config_path); } - else if (argc == 2) { + else if (argc == 2 || argc == 3) { if (strcmp(argv[1], "-t") == 0) - return run_autotest(); + return run_autotest(config_path); else if (strcmp(argv[1], "-b") == 0) - return run_benchmark(); + return run_benchmark(config_path); else if (strcmp(argv[1], "-s") == 0) - return run_simulation(); + return run_simulation(config_path); + else if (strcmp(argv[1], "-r") == 0) + return run_renderer(config_path); else WARNING("Unrecognized option"); } diff --git a/src/standalone/renderer.cc b/src/standalone/renderer.cc index be36a37..8edefd8 100644 --- a/src/standalone/renderer.cc +++ b/src/standalone/renderer.cc @@ -354,11 +354,11 @@ check_input(const float& dt) } int -run_renderer(void) +run_renderer(const char* config_path) { /* Parse configs */ AcMeshInfo mesh_info; - load_config(&mesh_info); + load_config(config_path, &mesh_info); renderer_init(mesh_info.int_params[AC_mx], mesh_info.int_params[AC_my]); AcMesh* mesh = acmesh_create(mesh_info); @@ -455,7 +455,7 @@ run_renderer(void) #else // BUILD_RT_VISUALIZATION == 0 #include "src/core/errchk.h" int -run_renderer(void) +run_renderer(const char* /*config_path*/) { WARNING("Real-time visualization module not built. Set BUILD_RT_VISUALIZATION=ON with cmake."); return 1; diff --git a/src/standalone/run.h b/src/standalone/run.h index 805ee2c..6f1b325 100644 --- a/src/standalone/run.h +++ b/src/standalone/run.h @@ -26,10 +26,10 @@ */ #pragma once -int run_autotest(void); +int run_autotest(const char* config_path); -int run_simulation(void); +int run_simulation(const char* config_path); -int run_benchmark(void); +int run_benchmark(const char* config_path); -int run_renderer(void); +int run_renderer(const char* config_path); diff --git a/src/standalone/simulation.cc b/src/standalone/simulation.cc index bdf0b18..11a991f 100644 --- a/src/standalone/simulation.cc +++ b/src/standalone/simulation.cc @@ -223,11 +223,11 @@ print_diagnostics(const int step, const AcReal dt, const AcReal t_step, FILE* di */ int -run_simulation(void) +run_simulation(const char* config_path) { /* Parse configs */ AcMeshInfo mesh_info; - load_config(&mesh_info); + load_config(config_path, &mesh_info); AcMesh* mesh = acmesh_create(mesh_info); // TODO: This need to be possible to define in astaroth.conf From 1c14ec6e160123435b3daf498f959d1a0179d982 Mon Sep 17 00:00:00 2001 From: jpekkila Date: Thu, 19 Sep 2019 07:41:48 +0000 Subject: [PATCH 3/5] Updated Bitbucket pipelines --- bitbucket-pipelines.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 20f274c..7d99799 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -18,11 +18,9 @@ pipelines: scheduled: - step: script: # Modify the commands below to build your repository. - - source ./sourceme.sh - mkdir -p build && cd build - apt-get update - apt-get install -y cmake flex bison - - ../scripts/compile_acc.sh - - cmake .. + - cmake -DDSL_MODULE_DIR="acc/mhd_solver" .. - make -j # - ./ac_run -t From 5cc2d613beade3ebaeb305000597f09a1cbe1161 Mon Sep 17 00:00:00 2001 From: Miikka Vaisala Date: Mon, 23 Sep 2019 15:35:09 +0800 Subject: [PATCH 4/5] Changed realpath to readlink -f for better combatibility in different systems. --- scripts/compile_acc_module.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/compile_acc_module.sh b/scripts/compile_acc_module.sh index 49829be..7513e4a 100755 --- a/scripts/compile_acc_module.sh +++ b/scripts/compile_acc_module.sh @@ -1,6 +1,6 @@ #!/bin/bash -ACC_DIR=$(realpath $(dirname $0)/../acc) -MODULE_DIR=$(realpath $1) +ACC_DIR=$(readlink -f $(dirname $0)/../acc) +MODULE_DIR=$(readlink -f $1) echo "-- Compiling project in "${MODULE_DIR} for source in ${MODULE_DIR}/*.sas ${MODULE_DIR}/*.sps ${MODULE_DIR}/*.sdh From 4dfd4a101207614700bb605957ae56cdf9819cc4 Mon Sep 17 00:00:00 2001 From: jpekkila Date: Mon, 23 Sep 2019 17:25:03 +0300 Subject: [PATCH 5/5] Changed from strncmp to strdup to work around warnings --- src/standalone/main.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/standalone/main.cc b/src/standalone/main.cc index 24f3575..b29db31 100644 --- a/src/standalone/main.cc +++ b/src/standalone/main.cc @@ -73,12 +73,13 @@ main(int argc, char* argv[]) for (int i = 0; i < argc; ++i) printf("%d: %s\n", i, argv[i]); - const size_t buf_size = 256; - char config_path[buf_size]; - (argc == 3) ? strncpy(config_path, argv[2], buf_size) - : strncpy(config_path, AC_DEFAULT_CONFIG, buf_size); + char* config_path; + (argc == 3) ? config_path = strdup(argv[2]) + : config_path = strdup(AC_DEFAULT_CONFIG); printf("Config path: %s\n", config_path); + ERRCHK(config_path); + if (argc == 1) { return run_renderer(config_path); } @@ -98,5 +99,6 @@ main(int argc, char* argv[]) WARNING("Too many options given"); } + free(config_path); return EXIT_FAILURE; }