diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 651acd6fe1a58edc0b6f2c446e48e4bc4e4a8750..3efb308fa0f78dce35973ccb47d1303d7c8634af 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,9 +10,12 @@ stages: - build # Unit test stage - test + # Code coverage + - coverage include: - local: '/.gitlab/ci/_global.gitlab-ci.yml' - local: '/.gitlab/ci/static_analysis.gitlab-ci.yml' - local: '/.gitlab/ci/build.gitlab-ci.yml' - local: '/.gitlab/ci/test.gitlab-ci.yml' + - local: '/.gitlab/ci/coverage.gitlab-ci.yml' diff --git a/.gitlab/ci/_global.gitlab-ci.yml b/.gitlab/ci/_global.gitlab-ci.yml index 6f34fe701df035e68ce49825fde0ff88449a9637..aab5d745367d22052f82c6e3ef144680a822cd45 100644 --- a/.gitlab/ci/_global.gitlab-ci.yml +++ b/.gitlab/ci/_global.gitlab-ci.yml @@ -9,5 +9,8 @@ variables: GIT_SSL_NO_VERIFY: 1 DEBIAN_FRONTEND: noninteractive - -image: n2d2-ci/ubuntu20.04/cpu:latest \ No newline at end of file +default: + image: nvidia/cuda:12.2.0-devel-ubuntu22.04 + before_script: + - apt update + - apt install -y cmake cppcheck python-is-python3 pip git gcovr diff --git a/.gitlab/ci/build.gitlab-ci.yml b/.gitlab/ci/build.gitlab-ci.yml index 0c004a9f2dc9f4425a8e962d0c9d0e9fec83146e..da0d23c9de978ebcdbb370a6f4a92262829e05b9 100644 --- a/.gitlab/ci/build.gitlab-ci.yml +++ b/.gitlab/ci/build.gitlab-ci.yml @@ -1,14 +1,14 @@ build:ubuntu_cpp: stage: build + needs: [] tags: - docker - image: n2d2-ci/ubuntu20.04/cpu:latest script: - mkdir -p build_cpp - mkdir -p install_cpp - cd build_cpp - - cmake -DCMAKE_INSTALL_PREFIX:PATH=../install_cpp -DCMAKE_BUILD_TYPE=Debug -DWERROR=ON .. + - cmake -DCMAKE_INSTALL_PREFIX:PATH=../install_cpp -DCMAKE_BUILD_TYPE=Debug -DWERROR=ON -DCOVERAGE=ON .. - make -j4 all install artifacts: @@ -18,9 +18,9 @@ build:ubuntu_cpp: build:ubuntu_python: stage: build + needs: [] tags: - docker - image: n2d2-ci/ubuntu20.04/cpu:latest script: - python3 -m pip install virtualenv @@ -30,4 +30,33 @@ build:ubuntu_python: - python3 -m pip install . artifacts: paths: - - venv/ \ No newline at end of file + - venv/ + +build:windows_cpp: + stage: build + needs: [] + tags: + - windows + + image: buildtools + before_script: + # Install Chocolatey + - Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) + # Install dependencies + - choco install cmake.install --installargs '"ADD_CMAKE_TO_PATH=System"' -Y + - choco install git -Y + - choco install python -Y + # Update PATH + - $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") + script: + - mkdir -p build_cpp + - mkdir -p install_cpp + - cd build_cpp + - cmake -DCMAKE_INSTALL_PREFIX:PATH=../install_cpp -DCMAKE_BUILD_TYPE=Debug .. + - cmake --build . -j2 + - cmake --install . --config Debug + + artifacts: + paths: + - build_cpp/ + - install_cpp/ diff --git a/.gitlab/ci/coverage.gitlab-ci.yml b/.gitlab/ci/coverage.gitlab-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..027f3078180bb32b36ca4666f171dda90ef7f7be --- /dev/null +++ b/.gitlab/ci/coverage.gitlab-ci.yml @@ -0,0 +1,36 @@ +coverage:ubuntu_cpp: + stage: coverage + needs: ["build:ubuntu_cpp"] + tags: + - docker + script: + - cd build_cpp + - ctest --output-on-failure + - gcovr --xml-pretty --exclude-unreachable-branches --print-summary -o coverage.xml --root ${CI_PROJECT_DIR} --filter '\.\./include/' --filter '\.\./src/' + coverage: /^\s*lines:\s*\d+.\d+\%/ + artifacts: + name: ${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHA} + expire_in: 2 days + reports: + coverage_report: + coverage_format: cobertura + path: build_cpp/coverage.xml + +coverage:ubuntu_python: + stage: coverage + needs: ["build:ubuntu_python"] + tags: + - docker + script: + - source venv/bin/activate + - python3 -m pip install numpy coverage + - cd aidge_core + - python3 -m coverage run --source=. -m unittest discover -s unit_tests/ -v -b + - python3 -m coverage report + - python3 -m coverage xml + coverage: '/(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/' + artifacts: + reports: + coverage_report: + coverage_format: cobertura + path: aidge_core/coverage.xml diff --git a/.gitlab/ci/test.gitlab-ci.yml b/.gitlab/ci/test.gitlab-ci.yml index c85683f889fbc3b5d6af366642d0932d5e2ce9a1..1e67ce273abc7d6b02f9e3148264ff3f9ea1cf07 100644 --- a/.gitlab/ci/test.gitlab-ci.yml +++ b/.gitlab/ci/test.gitlab-ci.yml @@ -3,21 +3,45 @@ test:ubuntu_cpp: needs: ["build:ubuntu_cpp"] tags: - docker - image: n2d2-ci/ubuntu20.04/cpu:latest script: - cd build_cpp - - ctest --output-on-failure + - ctest --output-junit ctest-results.xml --output-on-failure + artifacts: + reports: + junit: build_cpp/ctest-results.xml test:ubuntu_python: stage: test needs: ["build:ubuntu_python"] tags: - docker - image: n2d2-ci/ubuntu20.04/cpu:latest script: - source venv/bin/activate - cd aidge_core + - python3 -m pip install unittest-xml-reporting - python3 -m pip list - # Run on discovery all tests located in core/unit_tests/python and discard the stdout - # only to show the errors/warnings and the results of the tests - - python3 -m unittest discover -s unit_tests/ -v -b 1> /dev/null + # Run on discovery all tests located in core/unit_tests/python + - python3 -m xmlrunner discover -s unit_tests/ -v -b --output-file xmlrunner-results.xml + artifacts: + reports: + junit: aidge_core/xmlrunner-results.xml + +test:windows_cpp: + stage: test + needs: ["build:windows_cpp"] + tags: + - windows + image: buildtools + before_script: + # Install Chocolatey + - Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) + # Install dependencies + - choco install cmake.install --installargs '"ADD_CMAKE_TO_PATH=System"' -Y + # Update PATH + - $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") + script: + - cd build_cpp + - ctest --output-junit ctest-results.xml --output-on-failure + artifacts: + reports: + junit: build_cpp/ctest-results.xml diff --git a/CMakeLists.txt b/CMakeLists.txt index 1368373abb3cf53a8c1427b552c8ad3b7a230df0..67ad9304bc3e682a9436fb52306b3ca8120c1c4b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,20 +12,27 @@ set(module_name _${project}) # target name project(${project}) -############################################## -# Import utils CMakeLists -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") -include(PybindModuleCreation) - ############################################## # Define options option(PYBIND "python binding" ON) option(WERROR "Warning as error" OFF) option(TEST "Enable tests" ON) +option(COVERAGE "Enable coverage" OFF) + +############################################## +# Import utils CMakeLists +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") +include(PybindModuleCreation) + +if(CMAKE_COMPILER_IS_GNUCXX AND COVERAGE) + Include(CodeCoverage) +endif() ############################################## # Find system dependencies + + ############################################## # Create target and set properties @@ -76,6 +83,10 @@ else() /W4>) endif() +if(CMAKE_COMPILER_IS_GNUCXX AND COVERAGE) + append_coverage_compiler_flags() +endif() + ############################################## # Installation instructions diff --git a/README.md b/README.md index e2726a4422d55e31b0a54a0ce2c8e35e270ab27c..992344a796a4634a25d2127fc49b57adeae45863 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +   + # Aidge Core library You can find here the C++ code of the Core library of Aidge. diff --git a/cmake/CodeCoverage.cmake b/cmake/CodeCoverage.cmake new file mode 100644 index 0000000000000000000000000000000000000000..d4a039fd0e511238df1c0e0502c7588409099289 --- /dev/null +++ b/cmake/CodeCoverage.cmake @@ -0,0 +1,742 @@ +# Copyright (c) 2012 - 2017, Lars Bilke +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors +# may be used to endorse or promote products derived from this software without +# specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# CHANGES: +# +# 2012-01-31, Lars Bilke +# - Enable Code Coverage +# +# 2013-09-17, Joakim Söderberg +# - Added support for Clang. +# - Some additional usage instructions. +# +# 2016-02-03, Lars Bilke +# - Refactored functions to use named parameters +# +# 2017-06-02, Lars Bilke +# - Merged with modified version from github.com/ufz/ogs +# +# 2019-05-06, Anatolii Kurotych +# - Remove unnecessary --coverage flag +# +# 2019-12-13, FeRD (Frank Dana) +# - Deprecate COVERAGE_LCOVR_EXCLUDES and COVERAGE_GCOVR_EXCLUDES lists in favor +# of tool-agnostic COVERAGE_EXCLUDES variable, or EXCLUDE setup arguments. +# - CMake 3.4+: All excludes can be specified relative to BASE_DIRECTORY +# - All setup functions: accept BASE_DIRECTORY, EXCLUDE list +# - Set lcov basedir with -b argument +# - Add automatic --demangle-cpp in lcovr, if 'c++filt' is available (can be +# overridden with NO_DEMANGLE option in setup_target_for_coverage_lcovr().) +# - Delete output dir, .info file on 'make clean' +# - Remove Python detection, since version mismatches will break gcovr +# - Minor cleanup (lowercase function names, update examples...) +# +# 2019-12-19, FeRD (Frank Dana) +# - Rename Lcov outputs, make filtered file canonical, fix cleanup for targets +# +# 2020-01-19, Bob Apthorpe +# - Added gfortran support +# +# 2020-02-17, FeRD (Frank Dana) +# - Make all add_custom_target()s VERBATIM to auto-escape wildcard characters +# in EXCLUDEs, and remove manual escaping from gcovr targets +# +# 2021-01-19, Robin Mueller +# - Add CODE_COVERAGE_VERBOSE option which will allow to print out commands which are run +# - Added the option for users to set the GCOVR_ADDITIONAL_ARGS variable to supply additional +# flags to the gcovr command +# +# 2020-05-04, Mihchael Davis +# - Add -fprofile-abs-path to make gcno files contain absolute paths +# - Fix BASE_DIRECTORY not working when defined +# - Change BYPRODUCT from folder to index.html to stop ninja from complaining about double defines +# +# 2021-05-10, Martin Stump +# - Check if the generator is multi-config before warning about non-Debug builds +# +# 2022-02-22, Marko Wehle +# - Change gcovr output from -o <filename> for --xml <filename> and --html <filename> output respectively. +# This will allow for Multiple Output Formats at the same time by making use of GCOVR_ADDITIONAL_ARGS, e.g. GCOVR_ADDITIONAL_ARGS "--txt". +# +# 2022-09-28, Sebastian Mueller +# - fix append_coverage_compiler_flags_to_target to correctly add flags +# - replace "-fprofile-arcs -ftest-coverage" with "--coverage" (equivalent) +# +# USAGE: +# +# 1. Copy this file into your cmake modules path. +# +# 2. Add the following line to your CMakeLists.txt (best inside an if-condition +# using a CMake option() to enable it just optionally): +# include(CodeCoverage) +# +# 3. Append necessary compiler flags for all supported source files: +# append_coverage_compiler_flags() +# Or for specific target: +# append_coverage_compiler_flags_to_target(YOUR_TARGET_NAME) +# +# 3.a (OPTIONAL) Set appropriate optimization flags, e.g. -O0, -O1 or -Og +# +# 4. If you need to exclude additional directories from the report, specify them +# using full paths in the COVERAGE_EXCLUDES variable before calling +# setup_target_for_coverage_*(). +# Example: +# set(COVERAGE_EXCLUDES +# '${PROJECT_SOURCE_DIR}/src/dir1/*' +# '/path/to/my/src/dir2/*') +# Or, use the EXCLUDE argument to setup_target_for_coverage_*(). +# Example: +# setup_target_for_coverage_lcov( +# NAME coverage +# EXECUTABLE testrunner +# EXCLUDE "${PROJECT_SOURCE_DIR}/src/dir1/*" "/path/to/my/src/dir2/*") +# +# 4.a NOTE: With CMake 3.4+, COVERAGE_EXCLUDES or EXCLUDE can also be set +# relative to the BASE_DIRECTORY (default: PROJECT_SOURCE_DIR) +# Example: +# set(COVERAGE_EXCLUDES "dir1/*") +# setup_target_for_coverage_gcovr_html( +# NAME coverage +# EXECUTABLE testrunner +# BASE_DIRECTORY "${PROJECT_SOURCE_DIR}/src" +# EXCLUDE "dir2/*") +# +# 5. Use the functions described below to create a custom make target which +# runs your test executable and produces a code coverage report. +# +# 6. Build a Debug build: +# cmake -DCMAKE_BUILD_TYPE=Debug .. +# make +# make my_coverage_target +# + +include(CMakeParseArguments) + +option(CODE_COVERAGE_VERBOSE "Verbose information" FALSE) + +# Check prereqs +find_program( GCOV_PATH gcov ) +find_program( LCOV_PATH NAMES lcov lcov.bat lcov.exe lcov.perl) +find_program( FASTCOV_PATH NAMES fastcov fastcov.py ) +find_program( GENHTML_PATH NAMES genhtml genhtml.perl genhtml.bat ) +find_program( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/scripts/test) +find_program( CPPFILT_PATH NAMES c++filt ) + +if(NOT GCOV_PATH) + message(FATAL_ERROR "gcov not found! Aborting...") +endif() # NOT GCOV_PATH + +# Check supported compiler (Clang, GNU and Flang) +get_property(LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES) +foreach(LANG ${LANGUAGES}) + if("${CMAKE_${LANG}_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang") + if("${CMAKE_${LANG}_COMPILER_VERSION}" VERSION_LESS 3) + message(FATAL_ERROR "Clang version must be 3.0.0 or greater! Aborting...") + endif() + elseif(NOT "${CMAKE_${LANG}_COMPILER_ID}" MATCHES "GNU" + AND NOT "${CMAKE_${LANG}_COMPILER_ID}" MATCHES "(LLVM)?[Ff]lang") + message(FATAL_ERROR "Compiler is not GNU or Flang! Aborting...") + endif() +endforeach() + +set(COVERAGE_COMPILER_FLAGS "-g --coverage" + CACHE INTERNAL "") +if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)") + include(CheckCXXCompilerFlag) + check_cxx_compiler_flag(-fprofile-abs-path HAVE_fprofile_abs_path) + if(HAVE_fprofile_abs_path) + set(COVERAGE_COMPILER_FLAGS "${COVERAGE_COMPILER_FLAGS} -fprofile-abs-path") + endif() +endif() + +set(CMAKE_Fortran_FLAGS_COVERAGE + ${COVERAGE_COMPILER_FLAGS} + CACHE STRING "Flags used by the Fortran compiler during coverage builds." + FORCE ) +set(CMAKE_CXX_FLAGS_COVERAGE + ${COVERAGE_COMPILER_FLAGS} + CACHE STRING "Flags used by the C++ compiler during coverage builds." + FORCE ) +set(CMAKE_C_FLAGS_COVERAGE + ${COVERAGE_COMPILER_FLAGS} + CACHE STRING "Flags used by the C compiler during coverage builds." + FORCE ) +set(CMAKE_EXE_LINKER_FLAGS_COVERAGE + "" + CACHE STRING "Flags used for linking binaries during coverage builds." + FORCE ) +set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE + "" + CACHE STRING "Flags used by the shared libraries linker during coverage builds." + FORCE ) +mark_as_advanced( + CMAKE_Fortran_FLAGS_COVERAGE + CMAKE_CXX_FLAGS_COVERAGE + CMAKE_C_FLAGS_COVERAGE + CMAKE_EXE_LINKER_FLAGS_COVERAGE + CMAKE_SHARED_LINKER_FLAGS_COVERAGE ) + +get_property(GENERATOR_IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) +if(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR GENERATOR_IS_MULTI_CONFIG)) + message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading") +endif() # NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR GENERATOR_IS_MULTI_CONFIG) + +if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") + link_libraries(gcov) +endif() + +# Defines a target for running and collection code coverage information +# Builds dependencies, runs the given executable and outputs reports. +# NOTE! The executable should always have a ZERO as exit code otherwise +# the coverage generation will not complete. +# +# setup_target_for_coverage_lcov( +# NAME testrunner_coverage # New target name +# EXECUTABLE testrunner -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR +# DEPENDENCIES testrunner # Dependencies to build first +# BASE_DIRECTORY "../" # Base directory for report +# # (defaults to PROJECT_SOURCE_DIR) +# EXCLUDE "src/dir1/*" "src/dir2/*" # Patterns to exclude (can be relative +# # to BASE_DIRECTORY, with CMake 3.4+) +# NO_DEMANGLE # Don't demangle C++ symbols +# # even if c++filt is found +# ) +function(setup_target_for_coverage_lcov) + + set(options NO_DEMANGLE SONARQUBE) + set(oneValueArgs BASE_DIRECTORY NAME) + set(multiValueArgs EXCLUDE EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES LCOV_ARGS GENHTML_ARGS) + cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + if(NOT LCOV_PATH) + message(FATAL_ERROR "lcov not found! Aborting...") + endif() # NOT LCOV_PATH + + if(NOT GENHTML_PATH) + message(FATAL_ERROR "genhtml not found! Aborting...") + endif() # NOT GENHTML_PATH + + # Set base directory (as absolute path), or default to PROJECT_SOURCE_DIR + if(DEFINED Coverage_BASE_DIRECTORY) + get_filename_component(BASEDIR ${Coverage_BASE_DIRECTORY} ABSOLUTE) + else() + set(BASEDIR ${PROJECT_SOURCE_DIR}) + endif() + + # Collect excludes (CMake 3.4+: Also compute absolute paths) + set(LCOV_EXCLUDES "") + foreach(EXCLUDE ${Coverage_EXCLUDE} ${COVERAGE_EXCLUDES} ${COVERAGE_LCOV_EXCLUDES}) + if(CMAKE_VERSION VERSION_GREATER 3.4) + get_filename_component(EXCLUDE ${EXCLUDE} ABSOLUTE BASE_DIR ${BASEDIR}) + endif() + list(APPEND LCOV_EXCLUDES "${EXCLUDE}") + endforeach() + list(REMOVE_DUPLICATES LCOV_EXCLUDES) + + # Conditional arguments + if(CPPFILT_PATH AND NOT ${Coverage_NO_DEMANGLE}) + set(GENHTML_EXTRA_ARGS "--demangle-cpp") + endif() + + # Setting up commands which will be run to generate coverage data. + # Cleanup lcov + set(LCOV_CLEAN_CMD + ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} -directory . + -b ${BASEDIR} --zerocounters + ) + # Create baseline to make sure untouched files show up in the report + set(LCOV_BASELINE_CMD + ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} -c -i -d . -b + ${BASEDIR} -o ${Coverage_NAME}.base + ) + # Run tests + set(LCOV_EXEC_TESTS_CMD + ${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS} + ) + # Capturing lcov counters and generating report + set(LCOV_CAPTURE_CMD + ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} --directory . -b + ${BASEDIR} --capture --output-file ${Coverage_NAME}.capture + ) + # add baseline counters + set(LCOV_BASELINE_COUNT_CMD + ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} -a ${Coverage_NAME}.base + -a ${Coverage_NAME}.capture --output-file ${Coverage_NAME}.total + ) + # filter collected data to final coverage report + set(LCOV_FILTER_CMD + ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} --remove + ${Coverage_NAME}.total ${LCOV_EXCLUDES} --output-file ${Coverage_NAME}.info + ) + # Generate HTML output + set(LCOV_GEN_HTML_CMD + ${GENHTML_PATH} ${GENHTML_EXTRA_ARGS} ${Coverage_GENHTML_ARGS} -o + ${Coverage_NAME} ${Coverage_NAME}.info + ) + if(${Coverage_SONARQUBE}) + # Generate SonarQube output + set(GCOVR_XML_CMD + ${GCOVR_PATH} --sonarqube ${Coverage_NAME}_sonarqube.xml -r ${BASEDIR} ${GCOVR_ADDITIONAL_ARGS} + ${GCOVR_EXCLUDE_ARGS} --object-directory=${PROJECT_BINARY_DIR} + ) + set(GCOVR_XML_CMD_COMMAND + COMMAND ${GCOVR_XML_CMD} + ) + set(GCOVR_XML_CMD_BYPRODUCTS ${Coverage_NAME}_sonarqube.xml) + set(GCOVR_XML_CMD_COMMENT COMMENT "SonarQube code coverage info report saved in ${Coverage_NAME}_sonarqube.xml.") + endif() + + + if(CODE_COVERAGE_VERBOSE) + message(STATUS "Executed command report") + message(STATUS "Command to clean up lcov: ") + string(REPLACE ";" " " LCOV_CLEAN_CMD_SPACED "${LCOV_CLEAN_CMD}") + message(STATUS "${LCOV_CLEAN_CMD_SPACED}") + + message(STATUS "Command to create baseline: ") + string(REPLACE ";" " " LCOV_BASELINE_CMD_SPACED "${LCOV_BASELINE_CMD}") + message(STATUS "${LCOV_BASELINE_CMD_SPACED}") + + message(STATUS "Command to run the tests: ") + string(REPLACE ";" " " LCOV_EXEC_TESTS_CMD_SPACED "${LCOV_EXEC_TESTS_CMD}") + message(STATUS "${LCOV_EXEC_TESTS_CMD_SPACED}") + + message(STATUS "Command to capture counters and generate report: ") + string(REPLACE ";" " " LCOV_CAPTURE_CMD_SPACED "${LCOV_CAPTURE_CMD}") + message(STATUS "${LCOV_CAPTURE_CMD_SPACED}") + + message(STATUS "Command to add baseline counters: ") + string(REPLACE ";" " " LCOV_BASELINE_COUNT_CMD_SPACED "${LCOV_BASELINE_COUNT_CMD}") + message(STATUS "${LCOV_BASELINE_COUNT_CMD_SPACED}") + + message(STATUS "Command to filter collected data: ") + string(REPLACE ";" " " LCOV_FILTER_CMD_SPACED "${LCOV_FILTER_CMD}") + message(STATUS "${LCOV_FILTER_CMD_SPACED}") + + message(STATUS "Command to generate lcov HTML output: ") + string(REPLACE ";" " " LCOV_GEN_HTML_CMD_SPACED "${LCOV_GEN_HTML_CMD}") + message(STATUS "${LCOV_GEN_HTML_CMD_SPACED}") + + if(${Coverage_SONARQUBE}) + message(STATUS "Command to generate SonarQube XML output: ") + string(REPLACE ";" " " GCOVR_XML_CMD_SPACED "${GCOVR_XML_CMD}") + message(STATUS "${GCOVR_XML_CMD_SPACED}") + endif() + endif() + + # Setup target + add_custom_target(${Coverage_NAME} + COMMAND ${LCOV_CLEAN_CMD} + COMMAND ${LCOV_BASELINE_CMD} + COMMAND ${LCOV_EXEC_TESTS_CMD} + COMMAND ${LCOV_CAPTURE_CMD} + COMMAND ${LCOV_BASELINE_COUNT_CMD} + COMMAND ${LCOV_FILTER_CMD} + COMMAND ${LCOV_GEN_HTML_CMD} + ${GCOVR_XML_CMD_COMMAND} + + # Set output files as GENERATED (will be removed on 'make clean') + BYPRODUCTS + ${Coverage_NAME}.base + ${Coverage_NAME}.capture + ${Coverage_NAME}.total + ${Coverage_NAME}.info + ${GCOVR_XML_CMD_BYPRODUCTS} + ${Coverage_NAME}/index.html + WORKING_DIRECTORY ${PROJECT_BINARY_DIR} + DEPENDS ${Coverage_DEPENDENCIES} + VERBATIM # Protect arguments to commands + COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report." + ) + + # Show where to find the lcov info report + add_custom_command(TARGET ${Coverage_NAME} POST_BUILD + COMMAND ; + COMMENT "Lcov code coverage info report saved in ${Coverage_NAME}.info." + ${GCOVR_XML_CMD_COMMENT} + ) + + # Show info where to find the report + add_custom_command(TARGET ${Coverage_NAME} POST_BUILD + COMMAND ; + COMMENT "Open ./${Coverage_NAME}/index.html in your browser to view the coverage report." + ) + +endfunction() # setup_target_for_coverage_lcov + +# Defines a target for running and collection code coverage information +# Builds dependencies, runs the given executable and outputs reports. +# NOTE! The executable should always have a ZERO as exit code otherwise +# the coverage generation will not complete. +# +# setup_target_for_coverage_gcovr_xml( +# NAME ctest_coverage # New target name +# EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR +# DEPENDENCIES executable_target # Dependencies to build first +# BASE_DIRECTORY "../" # Base directory for report +# # (defaults to PROJECT_SOURCE_DIR) +# EXCLUDE "src/dir1/*" "src/dir2/*" # Patterns to exclude (can be relative +# # to BASE_DIRECTORY, with CMake 3.4+) +# ) +# The user can set the variable GCOVR_ADDITIONAL_ARGS to supply additional flags to the +# GCVOR command. +function(setup_target_for_coverage_gcovr_xml) + + set(options NONE) + set(oneValueArgs BASE_DIRECTORY NAME) + set(multiValueArgs EXCLUDE EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES) + cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + if(NOT GCOVR_PATH) + message(FATAL_ERROR "gcovr not found! Aborting...") + endif() # NOT GCOVR_PATH + + # Set base directory (as absolute path), or default to PROJECT_SOURCE_DIR + if(DEFINED Coverage_BASE_DIRECTORY) + get_filename_component(BASEDIR ${Coverage_BASE_DIRECTORY} ABSOLUTE) + else() + set(BASEDIR ${PROJECT_SOURCE_DIR}) + endif() + + # Collect excludes (CMake 3.4+: Also compute absolute paths) + set(GCOVR_EXCLUDES "") + foreach(EXCLUDE ${Coverage_EXCLUDE} ${COVERAGE_EXCLUDES} ${COVERAGE_GCOVR_EXCLUDES}) + if(CMAKE_VERSION VERSION_GREATER 3.4) + get_filename_component(EXCLUDE ${EXCLUDE} ABSOLUTE BASE_DIR ${BASEDIR}) + endif() + list(APPEND GCOVR_EXCLUDES "${EXCLUDE}") + endforeach() + list(REMOVE_DUPLICATES GCOVR_EXCLUDES) + + # Combine excludes to several -e arguments + set(GCOVR_EXCLUDE_ARGS "") + foreach(EXCLUDE ${GCOVR_EXCLUDES}) + list(APPEND GCOVR_EXCLUDE_ARGS "-e") + list(APPEND GCOVR_EXCLUDE_ARGS "${EXCLUDE}") + endforeach() + + # Set up commands which will be run to generate coverage data + # Run tests + set(GCOVR_XML_EXEC_TESTS_CMD + ${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS} + ) + # Running gcovr + set(GCOVR_XML_CMD + ${GCOVR_PATH} --xml ${Coverage_NAME}.xml -r ${BASEDIR} ${GCOVR_ADDITIONAL_ARGS} + ${GCOVR_EXCLUDE_ARGS} --object-directory=${PROJECT_BINARY_DIR} + ) + + if(CODE_COVERAGE_VERBOSE) + message(STATUS "Executed command report") + + message(STATUS "Command to run tests: ") + string(REPLACE ";" " " GCOVR_XML_EXEC_TESTS_CMD_SPACED "${GCOVR_XML_EXEC_TESTS_CMD}") + message(STATUS "${GCOVR_XML_EXEC_TESTS_CMD_SPACED}") + + message(STATUS "Command to generate gcovr XML coverage data: ") + string(REPLACE ";" " " GCOVR_XML_CMD_SPACED "${GCOVR_XML_CMD}") + message(STATUS "${GCOVR_XML_CMD_SPACED}") + endif() + + add_custom_target(${Coverage_NAME} + COMMAND ${GCOVR_XML_EXEC_TESTS_CMD} + COMMAND ${GCOVR_XML_CMD} + + BYPRODUCTS ${Coverage_NAME}.xml + WORKING_DIRECTORY ${PROJECT_BINARY_DIR} + DEPENDS ${Coverage_DEPENDENCIES} + VERBATIM # Protect arguments to commands + COMMENT "Running gcovr to produce Cobertura code coverage report." + ) + + # Show info where to find the report + add_custom_command(TARGET ${Coverage_NAME} POST_BUILD + COMMAND ; + COMMENT "Cobertura code coverage report saved in ${Coverage_NAME}.xml." + ) +endfunction() # setup_target_for_coverage_gcovr_xml + +# Defines a target for running and collection code coverage information +# Builds dependencies, runs the given executable and outputs reports. +# NOTE! The executable should always have a ZERO as exit code otherwise +# the coverage generation will not complete. +# +# setup_target_for_coverage_gcovr_html( +# NAME ctest_coverage # New target name +# EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR +# DEPENDENCIES executable_target # Dependencies to build first +# BASE_DIRECTORY "../" # Base directory for report +# # (defaults to PROJECT_SOURCE_DIR) +# EXCLUDE "src/dir1/*" "src/dir2/*" # Patterns to exclude (can be relative +# # to BASE_DIRECTORY, with CMake 3.4+) +# ) +# The user can set the variable GCOVR_ADDITIONAL_ARGS to supply additional flags to the +# GCVOR command. +function(setup_target_for_coverage_gcovr_html) + + set(options NONE) + set(oneValueArgs BASE_DIRECTORY NAME) + set(multiValueArgs EXCLUDE EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES) + cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + if(NOT GCOVR_PATH) + message(FATAL_ERROR "gcovr not found! Aborting...") + endif() # NOT GCOVR_PATH + + # Set base directory (as absolute path), or default to PROJECT_SOURCE_DIR + if(DEFINED Coverage_BASE_DIRECTORY) + get_filename_component(BASEDIR ${Coverage_BASE_DIRECTORY} ABSOLUTE) + else() + set(BASEDIR ${PROJECT_SOURCE_DIR}) + endif() + + # Collect excludes (CMake 3.4+: Also compute absolute paths) + set(GCOVR_EXCLUDES "") + foreach(EXCLUDE ${Coverage_EXCLUDE} ${COVERAGE_EXCLUDES} ${COVERAGE_GCOVR_EXCLUDES}) + if(CMAKE_VERSION VERSION_GREATER 3.4) + get_filename_component(EXCLUDE ${EXCLUDE} ABSOLUTE BASE_DIR ${BASEDIR}) + endif() + list(APPEND GCOVR_EXCLUDES "${EXCLUDE}") + endforeach() + list(REMOVE_DUPLICATES GCOVR_EXCLUDES) + + # Combine excludes to several -e arguments + set(GCOVR_EXCLUDE_ARGS "") + foreach(EXCLUDE ${GCOVR_EXCLUDES}) + list(APPEND GCOVR_EXCLUDE_ARGS "-e") + list(APPEND GCOVR_EXCLUDE_ARGS "${EXCLUDE}") + endforeach() + + # Set up commands which will be run to generate coverage data + # Run tests + set(GCOVR_HTML_EXEC_TESTS_CMD + ${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS} + ) + # Create folder + set(GCOVR_HTML_FOLDER_CMD + ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/${Coverage_NAME} + ) + # Running gcovr + set(GCOVR_HTML_CMD + ${GCOVR_PATH} --html ${Coverage_NAME}/index.html --html-details -r ${BASEDIR} ${GCOVR_ADDITIONAL_ARGS} + ${GCOVR_EXCLUDE_ARGS} --object-directory=${PROJECT_BINARY_DIR} + ) + + if(CODE_COVERAGE_VERBOSE) + message(STATUS "Executed command report") + + message(STATUS "Command to run tests: ") + string(REPLACE ";" " " GCOVR_HTML_EXEC_TESTS_CMD_SPACED "${GCOVR_HTML_EXEC_TESTS_CMD}") + message(STATUS "${GCOVR_HTML_EXEC_TESTS_CMD_SPACED}") + + message(STATUS "Command to create a folder: ") + string(REPLACE ";" " " GCOVR_HTML_FOLDER_CMD_SPACED "${GCOVR_HTML_FOLDER_CMD}") + message(STATUS "${GCOVR_HTML_FOLDER_CMD_SPACED}") + + message(STATUS "Command to generate gcovr HTML coverage data: ") + string(REPLACE ";" " " GCOVR_HTML_CMD_SPACED "${GCOVR_HTML_CMD}") + message(STATUS "${GCOVR_HTML_CMD_SPACED}") + endif() + + add_custom_target(${Coverage_NAME} + COMMAND ${GCOVR_HTML_EXEC_TESTS_CMD} + COMMAND ${GCOVR_HTML_FOLDER_CMD} + COMMAND ${GCOVR_HTML_CMD} + + BYPRODUCTS ${PROJECT_BINARY_DIR}/${Coverage_NAME}/index.html # report directory + WORKING_DIRECTORY ${PROJECT_BINARY_DIR} + DEPENDS ${Coverage_DEPENDENCIES} + VERBATIM # Protect arguments to commands + COMMENT "Running gcovr to produce HTML code coverage report." + ) + + # Show info where to find the report + add_custom_command(TARGET ${Coverage_NAME} POST_BUILD + COMMAND ; + COMMENT "Open ./${Coverage_NAME}/index.html in your browser to view the coverage report." + ) + +endfunction() # setup_target_for_coverage_gcovr_html + +# Defines a target for running and collection code coverage information +# Builds dependencies, runs the given executable and outputs reports. +# NOTE! The executable should always have a ZERO as exit code otherwise +# the coverage generation will not complete. +# +# setup_target_for_coverage_fastcov( +# NAME testrunner_coverage # New target name +# EXECUTABLE testrunner -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR +# DEPENDENCIES testrunner # Dependencies to build first +# BASE_DIRECTORY "../" # Base directory for report +# # (defaults to PROJECT_SOURCE_DIR) +# EXCLUDE "src/dir1/" "src/dir2/" # Patterns to exclude. +# NO_DEMANGLE # Don't demangle C++ symbols +# # even if c++filt is found +# SKIP_HTML # Don't create html report +# POST_CMD perl -i -pe s!${PROJECT_SOURCE_DIR}/!!g ctest_coverage.json # E.g. for stripping source dir from file paths +# ) +function(setup_target_for_coverage_fastcov) + + set(options NO_DEMANGLE SKIP_HTML) + set(oneValueArgs BASE_DIRECTORY NAME) + set(multiValueArgs EXCLUDE EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES FASTCOV_ARGS GENHTML_ARGS POST_CMD) + cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + if(NOT FASTCOV_PATH) + message(FATAL_ERROR "fastcov not found! Aborting...") + endif() + + if(NOT Coverage_SKIP_HTML AND NOT GENHTML_PATH) + message(FATAL_ERROR "genhtml not found! Aborting...") + endif() + + # Set base directory (as absolute path), or default to PROJECT_SOURCE_DIR + if(Coverage_BASE_DIRECTORY) + get_filename_component(BASEDIR ${Coverage_BASE_DIRECTORY} ABSOLUTE) + else() + set(BASEDIR ${PROJECT_SOURCE_DIR}) + endif() + + # Collect excludes (Patterns, not paths, for fastcov) + set(FASTCOV_EXCLUDES "") + foreach(EXCLUDE ${Coverage_EXCLUDE} ${COVERAGE_EXCLUDES} ${COVERAGE_FASTCOV_EXCLUDES}) + list(APPEND FASTCOV_EXCLUDES "${EXCLUDE}") + endforeach() + list(REMOVE_DUPLICATES FASTCOV_EXCLUDES) + + # Conditional arguments + if(CPPFILT_PATH AND NOT ${Coverage_NO_DEMANGLE}) + set(GENHTML_EXTRA_ARGS "--demangle-cpp") + endif() + + # Set up commands which will be run to generate coverage data + set(FASTCOV_EXEC_TESTS_CMD ${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS}) + + set(FASTCOV_CAPTURE_CMD ${FASTCOV_PATH} ${Coverage_FASTCOV_ARGS} --gcov ${GCOV_PATH} + --search-directory ${BASEDIR} + --process-gcno + --output ${Coverage_NAME}.json + --exclude ${FASTCOV_EXCLUDES} + ) + + set(FASTCOV_CONVERT_CMD ${FASTCOV_PATH} + -C ${Coverage_NAME}.json --lcov --output ${Coverage_NAME}.info + ) + + if(Coverage_SKIP_HTML) + set(FASTCOV_HTML_CMD ";") + else() + set(FASTCOV_HTML_CMD ${GENHTML_PATH} ${GENHTML_EXTRA_ARGS} ${Coverage_GENHTML_ARGS} + -o ${Coverage_NAME} ${Coverage_NAME}.info + ) + endif() + + set(FASTCOV_POST_CMD ";") + if(Coverage_POST_CMD) + set(FASTCOV_POST_CMD ${Coverage_POST_CMD}) + endif() + + if(CODE_COVERAGE_VERBOSE) + message(STATUS "Code coverage commands for target ${Coverage_NAME} (fastcov):") + + message(" Running tests:") + string(REPLACE ";" " " FASTCOV_EXEC_TESTS_CMD_SPACED "${FASTCOV_EXEC_TESTS_CMD}") + message(" ${FASTCOV_EXEC_TESTS_CMD_SPACED}") + + message(" Capturing fastcov counters and generating report:") + string(REPLACE ";" " " FASTCOV_CAPTURE_CMD_SPACED "${FASTCOV_CAPTURE_CMD}") + message(" ${FASTCOV_CAPTURE_CMD_SPACED}") + + message(" Converting fastcov .json to lcov .info:") + string(REPLACE ";" " " FASTCOV_CONVERT_CMD_SPACED "${FASTCOV_CONVERT_CMD}") + message(" ${FASTCOV_CONVERT_CMD_SPACED}") + + if(NOT Coverage_SKIP_HTML) + message(" Generating HTML report: ") + string(REPLACE ";" " " FASTCOV_HTML_CMD_SPACED "${FASTCOV_HTML_CMD}") + message(" ${FASTCOV_HTML_CMD_SPACED}") + endif() + if(Coverage_POST_CMD) + message(" Running post command: ") + string(REPLACE ";" " " FASTCOV_POST_CMD_SPACED "${FASTCOV_POST_CMD}") + message(" ${FASTCOV_POST_CMD_SPACED}") + endif() + endif() + + # Setup target + add_custom_target(${Coverage_NAME} + + # Cleanup fastcov + COMMAND ${FASTCOV_PATH} ${Coverage_FASTCOV_ARGS} --gcov ${GCOV_PATH} + --search-directory ${BASEDIR} + --zerocounters + + COMMAND ${FASTCOV_EXEC_TESTS_CMD} + COMMAND ${FASTCOV_CAPTURE_CMD} + COMMAND ${FASTCOV_CONVERT_CMD} + COMMAND ${FASTCOV_HTML_CMD} + COMMAND ${FASTCOV_POST_CMD} + + # Set output files as GENERATED (will be removed on 'make clean') + BYPRODUCTS + ${Coverage_NAME}.info + ${Coverage_NAME}.json + ${Coverage_NAME}/index.html # report directory + + WORKING_DIRECTORY ${PROJECT_BINARY_DIR} + DEPENDS ${Coverage_DEPENDENCIES} + VERBATIM # Protect arguments to commands + COMMENT "Resetting code coverage counters to zero. Processing code coverage counters and generating report." + ) + + set(INFO_MSG "fastcov code coverage info report saved in ${Coverage_NAME}.info and ${Coverage_NAME}.json.") + if(NOT Coverage_SKIP_HTML) + string(APPEND INFO_MSG " Open ${PROJECT_BINARY_DIR}/${Coverage_NAME}/index.html in your browser to view the coverage report.") + endif() + # Show where to find the fastcov info report + add_custom_command(TARGET ${Coverage_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E echo ${INFO_MSG} + ) + +endfunction() # setup_target_for_coverage_fastcov + +function(append_coverage_compiler_flags) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE) + set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE) + message(STATUS "Appending code coverage compiler flags: ${COVERAGE_COMPILER_FLAGS}") +endfunction() # append_coverage_compiler_flags + +# Setup coverage for specific library +function(append_coverage_compiler_flags_to_target name) + separate_arguments(_flag_list NATIVE_COMMAND "${COVERAGE_COMPILER_FLAGS}") + target_compile_options(${name} PRIVATE ${_flag_list}) + if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") + target_link_libraries(${name} PRIVATE gcov) + endif() +endfunction() diff --git a/include/aidge/operator/Add.hpp b/include/aidge/operator/Add.hpp index de9afab203e78f83b1edfa294af1f9b3e90f209a..c96b2c571f412124ccdfb83dde685e111448a222 100644 --- a/include/aidge/operator/Add.hpp +++ b/include/aidge/operator/Add.hpp @@ -93,14 +93,15 @@ public: assert(static_cast<std::size_t>(inputIdx) < NUM && "wrong inputIdx for Add operator."); return *(mInputs[inputIdx].get()); } - inline Tensor& output(__attribute__((unused)) const IOIndex_t outputIdx) const override final { return *(mOutput.get()); } + inline Tensor& output(const IOIndex_t /*outputIdx*/) const override final { return *(mOutput.get()); } inline std::shared_ptr<Tensor> getInput(const IOIndex_t inputIdx) const override final { assert(static_cast<std::size_t>(inputIdx) < NUM && "wrong inputIdx for Add operator."); return mInputs[inputIdx]; } - inline std::shared_ptr<Tensor> getOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final { assert(outputIdx == 0 && "Add Operators has only 1 outputs"); + (void) outputIdx; // avoid unused warning return mOutput; } @@ -108,8 +109,9 @@ public: assert(static_cast<std::size_t>(inputIdx) < NUM && "wrong inputIdx for Add operator."); return std::static_pointer_cast<Data>(mInputs[inputIdx]); } - std::shared_ptr<Data> getRawOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + std::shared_ptr<Data> getRawOutput(const IOIndex_t outputIdx) const override final { assert(outputIdx == 0 && "operator supports only 1 output"); + (void) outputIdx; // avoid unused warning return std::static_pointer_cast<Data>(mOutput); } diff --git a/include/aidge/operator/AvgPooling.hpp b/include/aidge/operator/AvgPooling.hpp index 0dbd91e059656369cdf12a4dfab2a981806011d1..7bf8740877e635cc2e59418bee1c444c7f3884e8 100644 --- a/include/aidge/operator/AvgPooling.hpp +++ b/include/aidge/operator/AvgPooling.hpp @@ -63,8 +63,9 @@ public: setDatatype(DataType::Float32); } - constexpr void associateInput(__attribute__((unused)) const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { + constexpr void associateInput(const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { assert(inputIdx < 1 && "operators supports only 3 inputs"); + (void) inputIdx; // avoid unused warning assert(strcmp(data->type(), Tensor::Type) == 0 && "input data must be of Tensor type"); mInput = std::dynamic_pointer_cast<Tensor>(data); @@ -91,29 +92,34 @@ public: bool outputDimsForwarded() const override final { return !(mOutput->empty()); } - inline Tensor& input(__attribute__((unused)) const IOIndex_t inputIdx) const override final { + inline Tensor& input(const IOIndex_t inputIdx) const override final { assert(inputIdx == 0 && "operators supports only 1 inputs"); + (void) inputIdx; // avoid unused warning return *(mInput.get()); } - inline Tensor& output(__attribute__((unused)) const IOIndex_t outputIdx) const override final { return *(mOutput.get()); } + inline Tensor& output(const IOIndex_t /*outputIdx*/) const override final { return *(mOutput.get()); } - inline std::shared_ptr<Tensor> getInput(__attribute__((unused)) const IOIndex_t inputIdx) const override final { + inline std::shared_ptr<Tensor> getInput(const IOIndex_t inputIdx) const override final { assert(inputIdx == 0 && "AvgPooling Operators supports only 1 inputs"); + (void) inputIdx; // avoid unused warning return mInput; } - inline std::shared_ptr<Tensor> getOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final { assert(outputIdx == 0 && "AvgPooling Operators has only 1 outputs"); + (void) outputIdx; // avoid unused warning return mOutput; } - std::shared_ptr<Data> getRawInput(__attribute__((unused)) const IOIndex_t inputIdx) const override final { + std::shared_ptr<Data> getRawInput(const IOIndex_t inputIdx) const override final { assert(inputIdx == 0 && "operators supports only 1 inputs"); + (void) inputIdx; // avoid unused warning return std::static_pointer_cast<Data>(mInput); } - std::shared_ptr<Data> getRawOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + std::shared_ptr<Data> getRawOutput(const IOIndex_t outputIdx) const override final { assert(outputIdx == 0 && "operator supports only 1 output"); + (void) outputIdx; // avoid unused warning return std::static_pointer_cast<Data>(mOutput); } diff --git a/include/aidge/operator/BatchNorm.hpp b/include/aidge/operator/BatchNorm.hpp index 01a6ccf4f7cf30843cac3dc819a686d3ad11530d..07af5fa8416cf726e209cd9e690af345b321fb0e 100644 --- a/include/aidge/operator/BatchNorm.hpp +++ b/include/aidge/operator/BatchNorm.hpp @@ -65,7 +65,7 @@ public: // return *in; // } - constexpr void associateInput(__attribute__((unused)) const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { + constexpr void associateInput(const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { assert(inputIdx < 5 && "operators supports only 5 inputs"); assert(strcmp(data->type(), Tensor::Type) == 0 && "input data must be of Tensor type"); @@ -90,15 +90,16 @@ public: assert(inputIdx < 5 && "operators supports only 5 inputs"); return *(mInputs[inputIdx].get()); } - inline Tensor& output(__attribute__((unused)) const IOIndex_t outputIdx) const override final { return *(mOutput.get()); } + inline Tensor& output(const IOIndex_t /*outputIdx*/) const override final { return *(mOutput.get()); } inline std::shared_ptr<Tensor> getInput(const IOIndex_t inputIdx) const override final { assert(inputIdx < 5 && "BatchNorm Operators supports only 5 inputs"); return mInputs[inputIdx]; } - inline std::shared_ptr<Tensor> getOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final { assert((outputIdx == 0) && "BatchNorm Operator has only 1 output"); + (void) outputIdx; // avoid unused warning return mOutput; } @@ -107,8 +108,9 @@ public: assert(inputIdx < 5 && "operators supports only 5 inputs"); return std::static_pointer_cast<Data>(mInputs[inputIdx]); } - std::shared_ptr<Data> getRawOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + std::shared_ptr<Data> getRawOutput(const IOIndex_t outputIdx) const override final { assert(outputIdx == 0 && "operator supports only 1 output"); + (void) outputIdx; // avoid unused warning return std::static_pointer_cast<Data>(mOutput); } diff --git a/include/aidge/operator/Conv.hpp b/include/aidge/operator/Conv.hpp index 1fcea7a4b5d654d3ec1a8661c03912b6074918c3..d6efba2cec6908ad58b9feea5e53807c7227cc88 100644 --- a/include/aidge/operator/Conv.hpp +++ b/include/aidge/operator/Conv.hpp @@ -79,7 +79,7 @@ public: // } - constexpr void associateInput(__attribute__((unused)) const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { + constexpr void associateInput(const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { assert(inputIdx < 3 && "operators supports only 3 inputs"); assert(strcmp(data->type(), Tensor::Type) == 0 && "input data must be of Tensor type"); @@ -114,15 +114,16 @@ public: inline Tensor& input(const IOIndex_t inputIdx) const override final { assert(inputIdx < 3 && "operators supports only 3 inputs"); return *(mInputs[inputIdx].get()); } - inline Tensor& output(__attribute__((unused)) const IOIndex_t outputIdx) const override final { return *(mOutput.get()); } + inline Tensor& output(const IOIndex_t /*outputIdx*/) const override final { return *(mOutput.get()); } inline std::shared_ptr<Tensor> getInput(const IOIndex_t inputIdx) const override final { assert(inputIdx < 3 && "Conv Operators supports only 3 inputs"); return mInputs[inputIdx]; } - inline std::shared_ptr<Tensor> getOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final { assert((outputIdx == 0) && "Conv Operator has only 1 output"); + (void) outputIdx; // avoid unused warning return mOutput; } @@ -131,8 +132,9 @@ public: assert(inputIdx < 3 && "operators supports only 3 inputs"); return std::static_pointer_cast<Data>(mInputs[inputIdx]); } - std::shared_ptr<Data> getRawOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + std::shared_ptr<Data> getRawOutput(const IOIndex_t outputIdx) const override final { assert(outputIdx == 0 && "operator supports only 1 output"); + (void) outputIdx; // avoid unused warning return std::static_pointer_cast<Data>(mOutput); } diff --git a/include/aidge/operator/ConvDepthWise.hpp b/include/aidge/operator/ConvDepthWise.hpp index 4a4af179b454f231ae2ce8dbafeb319795a76861..a3b7fbf3b21a5b3fd9e532e0cc19cebd46e5d022 100644 --- a/include/aidge/operator/ConvDepthWise.hpp +++ b/include/aidge/operator/ConvDepthWise.hpp @@ -71,7 +71,7 @@ class ConvDepthWise_Op : public Operator, setDatatype(DataType::Float32); } - constexpr void associateInput(__attribute__((unused)) const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { + constexpr void associateInput(const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { assert(inputIdx < 3 && "operators supports only 3 inputs"); assert(strcmp(data->type(), Tensor::Type) == 0 && "input data must be of Tensor type"); @@ -114,15 +114,16 @@ class ConvDepthWise_Op : public Operator, assert(inputIdx < 3 && "operators supports only 3 inputs"); return *(mInputs[inputIdx].get()); } - inline Tensor& output(__attribute__((unused)) const IOIndex_t outputIdx) const override final { return *(mOutput.get()); } + inline Tensor& output(const IOIndex_t /*outputIdx*/) const override final { return *(mOutput.get()); } inline std::shared_ptr<Tensor> getInput(const IOIndex_t inputIdx) const override final { assert(inputIdx < 3 && "ConvDepthWise Operators supports only 3 inputs"); return mInputs[inputIdx]; } - inline std::shared_ptr<Tensor> getOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final { assert((outputIdx == 0) && "ConvDepthWise Operator has only 1 output"); + (void) outputIdx; // avoid unused warning return mOutput; } @@ -131,8 +132,9 @@ class ConvDepthWise_Op : public Operator, assert(inputIdx < 3 && "operators supports only 3 inputs"); return std::static_pointer_cast<Data>(mInputs[inputIdx]); } - std::shared_ptr<Data> getRawOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + std::shared_ptr<Data> getRawOutput(const IOIndex_t outputIdx) const override final { assert(outputIdx == 0 && "operator supports only 1 output"); + (void) outputIdx; // avoid unused warning return std::static_pointer_cast<Data>(mOutput); } diff --git a/include/aidge/operator/FC.hpp b/include/aidge/operator/FC.hpp index 74362044349a087896cbc231f86818c89187c568..6e4c54a030c108c29c08a8f5dfdc24d084ccc91c 100644 --- a/include/aidge/operator/FC.hpp +++ b/include/aidge/operator/FC.hpp @@ -57,7 +57,7 @@ public: setDatatype(DataType::Float32); } - void associateInput(__attribute__((unused)) const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { + void associateInput(const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { assert(inputIdx < 3 && "operators supports only 3 inputs"); assert(strcmp(data->type(), Tensor::Type)==0 && "input data must be of Tensor type"); if (inputIdx == 2) { @@ -89,15 +89,16 @@ public: inline Tensor& input(const IOIndex_t inputIdx) const override final { assert(inputIdx < 3 && "operators supports only 3 inputs"); return *(mInputs[inputIdx].get()); } - inline Tensor& output(__attribute__((unused)) const IOIndex_t inputIdx) const override final { return *(mOutput.get()); } + inline Tensor& output(const IOIndex_t /*inputIdx*/) const override final { return *(mOutput.get()); } inline std::shared_ptr<Tensor> getInput(const IOIndex_t inputIdx) const override final { assert(inputIdx < 3 && "FC Operators supports only 3 inputs"); return mInputs[inputIdx]; } - inline std::shared_ptr<Tensor> getOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final { assert((outputIdx == 0) && "FC Operator has only 1 output"); + (void) outputIdx; // avoid unused warning return mOutput; } @@ -106,8 +107,9 @@ public: assert(inputIdx < 3 && "operators supports only 3 inputs"); return std::static_pointer_cast<Data>(mInputs[inputIdx]); } - std::shared_ptr<Data> getRawOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + std::shared_ptr<Data> getRawOutput(const IOIndex_t outputIdx) const override final { assert(outputIdx == 0 && "operator supports only 1 output"); + (void) outputIdx; // avoid unused warning return std::static_pointer_cast<Data>(mOutput); } diff --git a/include/aidge/operator/GenericOperator.hpp b/include/aidge/operator/GenericOperator.hpp index 7efbe62a410ed21704d2a34d46a295202c43c31e..94cdc6727de7078ca4fc3bb0940a01731feb92cc 100644 --- a/include/aidge/operator/GenericOperator.hpp +++ b/include/aidge/operator/GenericOperator.hpp @@ -62,7 +62,12 @@ class GenericOperator_Op * @return template<class T> The parameter. */ template <class T> - T getParameter(std::string const &key) const { + const T& getParameter(std::string const &key) const { + return mParams.Get<const T>(key); + } + + template <class T> + T& getParameter(std::string const &key) { return mParams.Get<T>(key); } @@ -75,8 +80,8 @@ class GenericOperator_Op /// internal buffer in a new location (previous value is still in memory at /// its previous location) template <class T> - void addParameter(std::string const &key, T const &value) { - mParams.Add<T>(key, value); + void addParameter(std::string const &key, T&& value) { + mParams.Add<T>(key, std::forward<T>(value)); } @@ -85,7 +90,7 @@ class GenericOperator_Op std::vector<std::string> getParametersName() { return mParams.getParametersName(); } // Override Virtual Opertor methods - void associateInput(__attribute__((unused)) const IOIndex_t inputIdx, __attribute__((unused)) std::shared_ptr<Data> data) override final { + void associateInput(const IOIndex_t /*inputIdx*/, std::shared_ptr<Data> /*data*/) override final { printf("Info: using associateInput() on a GenericOperator.\n"); } diff --git a/include/aidge/operator/LeakyReLU.hpp b/include/aidge/operator/LeakyReLU.hpp index 058f41fd85516db97a66de448d0f4b1b61254021..64587d51de784082da455eb64aa5bbe175773b5d 100644 --- a/include/aidge/operator/LeakyReLU.hpp +++ b/include/aidge/operator/LeakyReLU.hpp @@ -53,8 +53,9 @@ public: setDatatype(DataType::Float32); } - void associateInput(__attribute__((unused)) const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { + void associateInput(const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { assert(inputIdx == 0 && "operator supports only 1 input"); + (void) inputIdx; // avoid unused warning assert(strcmp(data->type(), Tensor::Type)==0 && "input data must be of Tensor type"); mInput = std::dynamic_pointer_cast<Tensor>(data); } @@ -69,26 +70,30 @@ public: } - inline Tensor& input(__attribute__((unused)) const IOIndex_t inputIdx) const override final { return *(mInput.get()); } - inline Tensor& output(__attribute__((unused)) const IOIndex_t outputIdx) const override final { return *(mOutput.get()); } + inline Tensor& input(const IOIndex_t /*inputIdx*/) const override final { return *(mInput.get()); } + inline Tensor& output(const IOIndex_t /*outputIdx*/) const override final { return *(mOutput.get()); } - inline std::shared_ptr<Tensor> getInput(__attribute__((unused)) const IOIndex_t inputIdx) const override final { + inline std::shared_ptr<Tensor> getInput(const IOIndex_t inputIdx) const override final { assert((inputIdx == 0) && "LeakyReLU Operator has only 1 input"); + (void) inputIdx; // avoid unused warning return mInput; } - inline std::shared_ptr<Tensor> getOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final { assert((outputIdx == 0) && "LeakyReLU Operator has only 1 output"); + (void) outputIdx; // avoid unused warning return mOutput; } - std::shared_ptr<Data> getRawInput(__attribute__((unused)) const IOIndex_t inputIdx) const override final { + std::shared_ptr<Data> getRawInput(const IOIndex_t inputIdx) const override final { assert(inputIdx == 0 && "operator supports only 1 input"); + (void) inputIdx; // avoid unused warning return std::static_pointer_cast<Data>(mInput); } - std::shared_ptr<Data> getRawOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + std::shared_ptr<Data> getRawOutput(const IOIndex_t outputIdx) const override final { assert(outputIdx == 0 && "operator supports only 1 output"); + (void) outputIdx; // avoid unused warning return mOutput; } diff --git a/include/aidge/operator/Matmul.hpp b/include/aidge/operator/Matmul.hpp index b6181989cf8fa583bf87ac45b2af8d0491e387be..b44e8a9b9540e287ff35af1c9642c8202fd096d0 100644 --- a/include/aidge/operator/Matmul.hpp +++ b/include/aidge/operator/Matmul.hpp @@ -55,7 +55,7 @@ public: setDatatype(DataType::Float32); } - void associateInput(__attribute__((unused)) const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { + void associateInput(const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { assert(inputIdx < 2 && "operators supports only 2 inputs"); assert(strcmp(data->type(), Tensor::Type)==0 && "input data must be of Tensor type"); mInputs[inputIdx] = std::dynamic_pointer_cast<Tensor>(data); @@ -81,15 +81,16 @@ public: inline Tensor& input(const IOIndex_t inputIdx) const override final { assert(inputIdx < 2 && "operators supports only 2 inputs"); return *(mInputs[inputIdx].get()); } - inline Tensor& output(__attribute__((unused)) const IOIndex_t outputIdx) const override final { return *(mOutput.get()); } + inline Tensor& output(const IOIndex_t /*outputIdx*/) const override final { return *(mOutput.get()); } inline std::shared_ptr<Tensor> getInput(const IOIndex_t inputIdx) const override final { assert(inputIdx < 2 && "MatMul Operators has 2 inputs"); return mInputs[inputIdx]; } - inline std::shared_ptr<Tensor> getOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final { assert((outputIdx == 0) && "MatMul Operators has 1 output"); + (void) outputIdx; // avoid unused warning return mOutput; } @@ -98,8 +99,9 @@ public: assert(inputIdx < 2 && "operators supports only 2 inputs"); return std::static_pointer_cast<Data>(mInputs[inputIdx]); } - std::shared_ptr<Data> getRawOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + std::shared_ptr<Data> getRawOutput(const IOIndex_t outputIdx) const override final { assert(outputIdx == 0 && "operator supports only 1 output"); + (void) outputIdx; // avoid unused warning return std::static_pointer_cast<Data>(mOutput); } diff --git a/include/aidge/operator/Producer.hpp b/include/aidge/operator/Producer.hpp index 4268b46b2f0facdaf0d167f16ae01ba8cba2c01d..1f77400ce8a8ef727ea9e0a7d12477c6519ea2df 100644 --- a/include/aidge/operator/Producer.hpp +++ b/include/aidge/operator/Producer.hpp @@ -51,39 +51,41 @@ public: setDatatype(tensor->dataType()); } - void associateInput(__attribute__((unused)) const IOIndex_t inputIdx, __attribute__((unused)) std::shared_ptr<Data> data) override final { + void associateInput(const IOIndex_t /*inputIdx*/, std::shared_ptr<Data> /*data*/) override final { assert(false && "Producer operator takes no input"); } - constexpr void computeOutputDims() override final {} + void computeOutputDims() override final {} - constexpr bool outputDimsForwarded() const override final {return true;} + bool outputDimsForwarded() const override final {return true;} - [[noreturn]] inline Tensor& input(__attribute__((unused)) const IOIndex_t inputIdx) const override final { + [[noreturn]] inline Tensor& input(const IOIndex_t /*inputIdx*/) const override final { assert(false); exit(-1); } - inline Tensor& output(__attribute__((unused)) const IOIndex_t outputIdx) const override final { return *(mOutput.get()); } + inline Tensor& output(const IOIndex_t /*outputIdx*/) const override final { return *(mOutput.get()); } - inline std::shared_ptr<Tensor> getInput(__attribute__((unused)) const IOIndex_t inputIdx) const override final { + inline std::shared_ptr<Tensor> getInput(const IOIndex_t /*inputIdx*/) const override final { assert(false && "Producer Operator has no input"); return nullptr; } - inline std::shared_ptr<Tensor> getOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final { assert((outputIdx == 0) && "Producer Operator has only 1 output"); + (void) outputIdx; // avoid unused warning return mOutput; } - std::shared_ptr<Data> getRawInput(__attribute__((unused)) const IOIndex_t inputIdx) const override final { + std::shared_ptr<Data> getRawInput(const IOIndex_t /*inputIdx*/) const override final { assert(false && "Producer operator takes no input"); return nullptr; } - std::shared_ptr<Data> getRawOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + std::shared_ptr<Data> getRawOutput(const IOIndex_t outputIdx) const override final { assert(outputIdx == 0 && "operator supports only 1 output"); + (void) outputIdx; // avoid unused warning return std::static_pointer_cast<Data>(mOutput); } diff --git a/include/aidge/operator/ReLU.hpp b/include/aidge/operator/ReLU.hpp index b969a76bc24a6fa41288b009934a3c060289b597..3ea90462cf2b083a1a61ae39be06471093ec9f9f 100644 --- a/include/aidge/operator/ReLU.hpp +++ b/include/aidge/operator/ReLU.hpp @@ -42,8 +42,9 @@ public: setDatatype(DataType::Float32); } - void associateInput(__attribute__((unused)) const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { + void associateInput(const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { assert(inputIdx == 0 && "operator supports only 1 input"); + (void) inputIdx; // avoid unused warning assert(strcmp(data->type(), Tensor::Type)==0 && "input data must be of Tensor type"); mInput = std::dynamic_pointer_cast<Tensor>(data); } @@ -58,26 +59,30 @@ public: } - inline Tensor& input(__attribute__((unused)) const IOIndex_t inputIdx) const override final { return *(mInput.get()); } - inline Tensor& output(__attribute__((unused)) const IOIndex_t outputIdx) const override final { return *(mOutput.get()); } + inline Tensor& input(const IOIndex_t /*inputIdx*/) const override final { return *(mInput.get()); } + inline Tensor& output(const IOIndex_t /*outputIdx*/) const override final { return *(mOutput.get()); } - inline std::shared_ptr<Tensor> getInput(__attribute__((unused)) const IOIndex_t inputIdx) const override final { + inline std::shared_ptr<Tensor> getInput(const IOIndex_t inputIdx) const override final { assert((inputIdx == 0) && "ReLU Operator has only 1 input"); + (void) inputIdx; // avoid unused warning return mInput; } - inline std::shared_ptr<Tensor> getOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final { assert((outputIdx == 0) && "ReLU Operator has only 1 output"); + (void) outputIdx; // avoid unused warning return mOutput; } - std::shared_ptr<Data> getRawInput(__attribute__((unused)) const IOIndex_t inputIdx) const override final { + std::shared_ptr<Data> getRawInput(const IOIndex_t inputIdx) const override final { assert(inputIdx == 0 && "operator supports only 1 input"); + (void) inputIdx; // avoid unused warning return std::static_pointer_cast<Data>(mInput); } - std::shared_ptr<Data> getRawOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + std::shared_ptr<Data> getRawOutput(const IOIndex_t outputIdx) const override final { assert(outputIdx == 0 && "operator supports only 1 output"); + (void) outputIdx; // avoid unused warning return std::static_pointer_cast<Data>(mOutput); } diff --git a/include/aidge/operator/Softmax.hpp b/include/aidge/operator/Softmax.hpp index 3d3cb8204e4b133b944521cf84117b5d16ac83df..93eb262f703ca7eb385641c77df7ae7e79c00b96 100644 --- a/include/aidge/operator/Softmax.hpp +++ b/include/aidge/operator/Softmax.hpp @@ -42,8 +42,9 @@ public: setDatatype(DataType::Float32); } - void associateInput(__attribute__((unused)) const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { + void associateInput(const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { assert(inputIdx == 0 && "operator supports only 1 input"); + (void) inputIdx; // avoid unused warning assert(strcmp(data->type(), Tensor::Type)==0 && "input data must be of Tensor type"); mInput = std::dynamic_pointer_cast<Tensor>(data); } @@ -58,26 +59,30 @@ public: } - inline Tensor& input(__attribute__((unused)) const IOIndex_t inputIdx) const override final { return *(mInput.get()); } - inline Tensor& output(__attribute__((unused)) const IOIndex_t outputIdx) const override final { return *(mOutput.get()); } + inline Tensor& input(const IOIndex_t /*inputIdx*/) const override final { return *(mInput.get()); } + inline Tensor& output(const IOIndex_t /*outputIdx*/) const override final { return *(mOutput.get()); } - inline std::shared_ptr<Tensor> getInput(__attribute__((unused)) const IOIndex_t inputIdx) const override final { + inline std::shared_ptr<Tensor> getInput(const IOIndex_t inputIdx) const override final { assert((inputIdx == 0) && "Softmax Operator has only 1 input"); + (void) inputIdx; // avoid unused warning return mInput; } - inline std::shared_ptr<Tensor> getOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final { assert((outputIdx == 0) && "Softmax Operator has only 1 output"); + (void) outputIdx; // avoid unused warning return mOutput; } - std::shared_ptr<Data> getRawInput(__attribute__((unused)) const IOIndex_t inputIdx) const override final { + std::shared_ptr<Data> getRawInput(const IOIndex_t inputIdx) const override final { assert(inputIdx == 0 && "operator supports only 1 input"); + (void) inputIdx; // avoid unused warning return std::static_pointer_cast<Data>(mInput); } - std::shared_ptr<Data> getRawOutput(__attribute__((unused)) const IOIndex_t outputIdx) const override final { + std::shared_ptr<Data> getRawOutput(const IOIndex_t outputIdx) const override final { assert(outputIdx == 0 && "operator supports only 1 output"); + (void) outputIdx; // avoid unused warning return std::static_pointer_cast<Data>(mOutput); } diff --git a/include/aidge/utils/Any.hpp b/include/aidge/utils/Any.hpp new file mode 100644 index 0000000000000000000000000000000000000000..d7ff22de23763e399d23fd7abb9e28a7a85aff5c --- /dev/null +++ b/include/aidge/utils/Any.hpp @@ -0,0 +1,154 @@ + +/******************************************************************************** + * Copyright (c) 2023 CEA-List + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + ********************************************************************************/ + +#ifndef AIDGE_ANY_H_ +#define AIDGE_ANY_H_ + + +#include <typeinfo> // typeid +#include <type_traits> // std::enable_if_t, std::decay_t, std::is_same, std::is_copy_constructible, std::remove_cv, std::remove_reference +#include <assert.h> +#include <new> + +class _any { +private: + /// @brief Operation to perform on the object. + enum _Op { _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy }; + + union _Arg { + const std::type_info* _M_typeinfo; + _any* _M_any; + }; + + /// @brief Stored data without type information. + void* _M_data; + + /// @brief Member function to perform type-related computations on stored data. + void (*_M_manager)(_Op, const _any*, _Arg*); + +public: + /// @brief Class to centralize functions and type information in a memory efficient way. + /// @tparam Tp Decayed stored type. + template <typename Tp> + struct Manager { + static void manage(_Op which, const _any* __any, _Arg* __arg) { + auto ptr = static_cast<const Tp*>(__any->_M_data); + switch (which) + { + case _Op_get_type_info: + __arg->_M_typeinfo = &typeid(Tp); + break; + case _Op_clone: + __arg->_M_any->_M_data = new Tp(*ptr); + __arg->_M_any->_M_manager = __any->_M_manager; + break; + case _Op_destroy: + delete ptr; + break; + } + + } + static Tp* access(const _any* __any) { + return static_cast<Tp*>(__any->_M_data); + } + + // template <typename Up> + // static void create(void* data, Up&& value) { + // data = new Tp(std::forward<Up>(value)); + // } + }; + +private: + template<typename _Tp, typename _VTp = std::decay_t<_Tp>> + using _Decay_if_not_any = std::enable_if_t<!std::is_same<_VTp, _any>::value, _VTp>; + +public: + /// @brief Default constructor + _any() noexcept : _M_manager(nullptr) { } + + /// @brief Copy constructor + /// @param __other + _any(const _any& __other) + { + if (!__other._M_manager) + _M_manager = nullptr; + else + { + _Arg __arg; + __arg._M_any = this; + __other._M_manager(_Op_clone, &__other, &__arg); + } + } + + /// @brief Move constructor + /// @param __other + _any(_any&& __other) + { + if (!__other._M_manager) + _M_manager = nullptr; + else + { + _M_data = __other._M_data; + _M_manager = __other._M_manager; + const_cast<_any*>(&__other)->_M_manager = nullptr; + } + } + + /// @brief By-value constructor. + /// @tparam T Data type. + /// @tparam VT Decayed data type. + /// @param value + template<typename T, typename VT = _Decay_if_not_any<T>, std::enable_if_t<std::is_copy_constructible<VT>::value, bool> = true> + explicit _any(T&& value) + : _M_manager(&Manager<VT>::manage), + _M_data(new VT{std::forward<T>(value)}) + {} + + ~_any() + { + if(_M_manager) { + _M_manager(_Op_destroy, this, nullptr); + _M_manager = nullptr; + } + } + + /// @brief Access type id of the value currently stored + /// @return + const std::type_info& type() const + { + if (!_M_manager) + return typeid(void); + _Arg __arg; + _M_manager(_Op_get_type_info, this, &__arg); + return *__arg._M_typeinfo; + } +}; + +/// @brief Access value stored in the object converted in the template type if possible. +/// @tparam _ValueType +/// @param __any +/// @return Stored value. +template<typename _ValueType> +inline _ValueType any_cast(const _any& __any) +{ + using _Up = std::remove_cv_t<std::remove_reference_t<_ValueType>>; + assert((std::__or_<std::is_reference<_ValueType>, std::is_copy_constructible<_ValueType>>::value && "Template argument must be a reference or CopyConstructible type")); + assert((std::is_constructible<_ValueType, const _Up&>::value && "Template argument must be constructible from a const value.")); + assert(std::is_object<_Up>::value); + assert(__any.type() == typeid(_Up)); + auto __p = static_cast<_Up*>(__any._M_data); + if (__p) + return static_cast<_ValueType>(*__p); + throw std::bad_cast(); +} + +#endif /* AIDGE_ANY_H_ */ \ No newline at end of file diff --git a/include/aidge/utils/CParameter.hpp b/include/aidge/utils/CParameter.hpp index dd36e7b1528f00119612ff95529cc02b0fbad5fe..9231a6f33ea599d9b416ec1774c4fe06e910eeee 100644 --- a/include/aidge/utils/CParameter.hpp +++ b/include/aidge/utils/CParameter.hpp @@ -12,24 +12,30 @@ #ifndef AIDGE_CPARAMETER_H_ #define AIDGE_CPARAMETER_H_ -#include <assert.h> #include <map> #include <vector> -#include <any> +#include "aidge/utils/Any.hpp" + namespace Aidge { ///\todo store also a fix-sized code that indicates the type ///\todo managing complex types or excluding non-trivial, non-aggregate types -class CParameter -{ +class CParameter { private: - template <typename T> - struct is_vector : std::false_type {}; - - template <typename T, typename Alloc> - struct is_vector<std::vector<T, Alloc>> : std::true_type {}; - + template<typename _ValueType> + inline _ValueType& any_cast_ref(const _any& __any) + { + using _Up = std::remove_cv_t<std::remove_reference_t<_ValueType>>; + assert((std::__or_<std::is_reference<_ValueType>, std::is_copy_constructible<_ValueType>>::value && "Template argument must be a reference or CopyConstructible type")); + assert((std::is_constructible<_ValueType, const _Up&>::value && "Template argument must be constructible from a const value.")); + assert(std::is_object<_Up>::value); + assert(__any.type() == typeid(_Up)); + if (_any::Manager<_Up>::access(&__any)) { // assess if _any object is empty + return *static_cast<_ValueType*>(_any::Manager<_Up>::access(&__any)); + } + throw std::bad_cast(); + } public: // not copyable, not movable CParameter(CParameter const &) = delete; @@ -49,12 +55,16 @@ public: * param buffer that will get invalid after the CParam death. * \note at() throws if the parameter does not exist, using find to test for parameter existance */ - template<class T> T Get(std::string const i_ParamName) const + template<class T> T& Get(const std::string i_ParamName) { - assert(m_Params.find(i_ParamName) != m_Params.end()); - return std::any_cast<T>(m_Buffer[m_Params.at(i_ParamName)]); + return any_cast_ref<T>(m_Buffer[m_Params.at(i_ParamName)]); } + // template<class T> const T& Get(const std::string i_ParamName) const + // { + // return any_cast<T>(m_Buffer[m_Params.at(i_ParamName)]); + // } + ///\brief Add a parameter value, identified by its name ///\tparam T expected parameter type ///\param i_ParamName Parameter name @@ -62,10 +72,10 @@ public: ///\todo Pass i_Value by ref if large or not trivial ///\bug If parameter already exists, its value is changed but written in the /// internal buffer in a new location (previous value is still in memory at its previous location) - template<class T> void Add(std::string const &i_ParamName, T const &i_Value) + template<class T> void Add(const std::string &i_ParamName, T&& i_Value) { m_Params[i_ParamName] = m_Buffer.size(); // Copy pointer offset - m_Buffer.push_back(std::any(i_Value)); // Black-magic used to add anytype into the vector + m_Buffer.push_back(_any(std::forward<T>(i_Value))); } @@ -83,8 +93,16 @@ public: private: std::map<std::string, std::size_t> m_Params; // { Param name : offset } - ///\brief All parameters values concatenated in raw binary form. - std::vector<std::any> m_Buffer = {}; + ///\brief Map to check type error + /* Note : i tried this : `std::map<std::string, std::type_info const *> mTypes;` + but looks like the type_ingo object was destroyed. + I am not a hugde fan of storing a string and making string comparison. + Maybe we can use a custom enum type (or is there a standard solution ?) + */ + std::map<std::string, std::string> m_Types; + + ///\brief All raw pointers to parameters values concatenated. Use custom any class compatible with C++14. + std::vector<_any> m_Buffer = {}; }; } diff --git a/python_binding/data/pybind_Tensor.cpp b/python_binding/data/pybind_Tensor.cpp index 3f741946da59a118b023f0204da4f42231c1416d..d6442723ecc79527e8eaa7d3e03a466c085dfa58 100644 --- a/python_binding/data/pybind_Tensor.cpp +++ b/python_binding/data/pybind_Tensor.cpp @@ -98,9 +98,9 @@ void init_Tensor(py::module& m){ .def_buffer([](Tensor& b) -> py::buffer_info { const std::unique_ptr<TensorImpl>& tensorImpl = b.getImpl(); - std::vector<ssize_t> dims; - std::vector<ssize_t> strides; - ssize_t stride = tensorImpl->scalarSize(); + std::vector<size_t> dims; + std::vector<size_t> strides; + size_t stride = tensorImpl->scalarSize(); for (unsigned int dim = b.nbDims(); dim > 0; dim--) { dims.push_back(b.dims()[dim-1]); diff --git a/src/graph/GraphView.cpp b/src/graph/GraphView.cpp index afbc68c79c04f0cb783f55f043f991f6bd71a709..ad412f5b86d9cf0dee0823736548baeb7c7320a7 100644 --- a/src/graph/GraphView.cpp +++ b/src/graph/GraphView.cpp @@ -28,13 +28,15 @@ Aidge::Connector Aidge::GraphView::operator()( assert((inputNodes().size() == 1U) && "Too many input Nodes for the GraphView, undefined behaviour"); std::shared_ptr<Node> inNode = *inputNodes().begin(); assert((ctors.size() == static_cast<std::size_t>(inNode->nbDataInputs())) && "Wrong number of arguments.\n"); - for (__attribute__((unused)) std::pair<std::shared_ptr<Node>, IOIndex_t> &input : inNode->inputs()) { + for (std::pair<std::shared_ptr<Node>, IOIndex_t> &input : inNode->inputs()) { assert((gk_IODefaultIndex == input.second) && "At least one input connection is not free.\n"); + (void)input; // avoid unused warning } - for (__attribute__((unused)) const Connector &ctor : ctors) { + for (const Connector &ctor : ctors) { assert((ctor.node() != nullptr) && "Input Connector must be associated with a node"); + (void)ctors; // avoid unused warning } IOIndex_t inID = 0; for (const Connector &ctor : ctors) { @@ -117,7 +119,7 @@ Aidge::IOIndex_t Aidge::GraphView::getNbDataInputs() const { Aidge::IOIndex_t Aidge::GraphView::getNbFreeDataInputs() const { IOIndex_t nbIn = 0; - for (const std::shared_ptr<Node> inputNode : mInputNodes) { + for (const std::shared_ptr<Node>& inputNode : mInputNodes) { nbIn += inputNode->getNbFreeDataInputs(); } return nbIn; @@ -127,13 +129,13 @@ Aidge::IOIndex_t Aidge::GraphView::getNbFreeDataInputs() const { std::vector<std::pair<std::shared_ptr<Aidge::Node>, Aidge::IOIndex_t>> Aidge::GraphView::dataInputs() const { IOIndex_t nbDataIn = 0U; - for (const std::shared_ptr<Node> inputNode : mInputNodes) { + for (const std::shared_ptr<Node>& inputNode : mInputNodes) { nbDataIn += inputNode->nbDataInputs(); } std::vector<std::pair<std::shared_ptr<Node>, IOIndex_t>> res = std::vector<std::pair<std::shared_ptr<Node>, IOIndex_t>>(nbDataIn); nbDataIn = 0U; - for (const std::shared_ptr<Node> inputNode : mInputNodes) { + for (const std::shared_ptr<Node>& inputNode : mInputNodes) { std::vector<std::pair<std::shared_ptr<Node>, IOIndex_t>> inputNodeinputs = inputNode->dataInputs(); std::move(inputNodeinputs.begin(), inputNodeinputs.end(), @@ -149,13 +151,13 @@ Aidge::GraphView::dataInputs() const { std::vector<std::pair<std::shared_ptr<Aidge::Node>, Aidge::IOIndex_t>> Aidge::GraphView::inputs() const { std::size_t nbIn = 0U; - for (const std::shared_ptr<Node> inputNode : mInputNodes) { + for (const std::shared_ptr<Node>& inputNode : mInputNodes) { nbIn += inputNode->nbInputs(); } std::vector<std::pair<std::shared_ptr<Node>, IOIndex_t>> res = std::vector<std::pair<std::shared_ptr<Node>, IOIndex_t>>(nbIn); nbIn = 0U; - for (const std::shared_ptr<Node> inputNode : mInputNodes) { + for (const std::shared_ptr<Node>& inputNode : mInputNodes) { std::vector<std::pair<std::shared_ptr<Node>, IOIndex_t>> inputNodeinputs = inputNode->inputs(); std::move(inputNodeinputs.begin(), inputNodeinputs.end(), @@ -242,13 +244,13 @@ void Aidge::GraphView::setDatatype(const DataType &datatype) { void Aidge::GraphView::updateOutputNodes() { mOutputNodes.clear(); - for (const std::shared_ptr<Node> go_it : mNodes) { + for (const std::shared_ptr<Node>& go_it : mNodes) { if (go_it->nbOutputs() != go_it->nbValidOutputs()) { // an output linked to nothing mOutputNodes.insert(go_it); continue; } - for (const std::shared_ptr<Node> ch_ptr : go_it->getChildren()) { + for (const std::shared_ptr<Node>& ch_ptr : go_it->getChildren()) { if (mNodes.find(ch_ptr) == mNodes.end()) { // Child not in the graph mOutputNodes.insert(go_it); break; @@ -277,7 +279,7 @@ void Aidge::GraphView::updateOutputNodes(std::shared_ptr<Node> node) { mOutputNodes.end())) { // it's a match! Must check if the outputNode // found is still an outputNode bool remove = (pa_ptr->nbOutputs() == pa_ptr->nbValidOutputs()); - for (const std::shared_ptr<Node> ch_ptr : pa_ptr->getChildren()) { + for (const std::shared_ptr<Node>& ch_ptr : pa_ptr->getChildren()) { if (mNodes.find(ch_ptr) == mNodes.end()) { // Child not in the graph remove = false; break; @@ -295,7 +297,7 @@ std::vector< Aidge::GraphView::outputs() const { std::vector<std::vector<std::pair<std::shared_ptr<Node>, Aidge::IOIndex_t>>> outputTensors; - for (const std::shared_ptr<Node> outputNode : mOutputNodes) { + for (const std::shared_ptr<Node>& outputNode : mOutputNodes) { std::vector<std::vector<std::pair<std::shared_ptr<Node>, Aidge::IOIndex_t>>> tmpOutputs = (outputNode->outputs()); outputTensors.insert(outputTensors.end(), tmpOutputs.begin(), @@ -404,7 +406,7 @@ void Aidge::GraphView::addChild( std::set<std::shared_ptr<Aidge::Node>> Aidge::GraphView::getParents() const { // TODO: choose if we return a set or a vector std::set<std::shared_ptr<Node>> parents; - for (const std::shared_ptr<Node> inputNode : mInputNodes) { + for (const std::shared_ptr<Node>& inputNode : mInputNodes) { parents.insert(inputNode->getParents().begin(), inputNode->getParents().end()); } @@ -423,7 +425,7 @@ std::vector<std::shared_ptr<Aidge::Node>> Aidge::GraphView::getParents(const std std::vector<std::vector<std::shared_ptr<Aidge::Node>>> Aidge::GraphView::getOrderedParents() const { std::vector<std::vector<std::shared_ptr<Node>>> parents; - for (const std::shared_ptr<Node> inputNode : mInputNodes) { + for (const std::shared_ptr<Node>& inputNode : mInputNodes) { parents.push_back(inputNode->getParents()); } return parents; @@ -431,7 +433,7 @@ Aidge::GraphView::getOrderedParents() const { std::set<std::shared_ptr<Aidge::Node>> Aidge::GraphView::getChildren() const { std::set<std::shared_ptr<Node>> children; - for (const std::shared_ptr<Node> outputNode : mOutputNodes) { + for (const std::shared_ptr<Node>& outputNode : mOutputNodes) { children.insert((outputNode->getChildren()).begin(), (outputNode->getChildren()).end()); } @@ -597,8 +599,8 @@ bool Aidge::GraphView::replaceWith(std::set<std::shared_ptr<Node>> newNodes) { void Aidge::GraphView::updateInputNodes() { mInputNodes.clear(); - for (const std::shared_ptr<Node> go_ptr : mNodes) { - for (const std::shared_ptr<Node> pa_ptr : go_ptr->getParents()) { + for (const std::shared_ptr<Node>& go_ptr : mNodes) { + for (const std::shared_ptr<Node>& pa_ptr : go_ptr->getParents()) { if ((pa_ptr == nullptr) || (mNodes.find(pa_ptr) == mNodes.end())) { // Parent doesn't exist || Parent not in the graph @@ -613,7 +615,7 @@ void Aidge::GraphView::updateInputNodes(std::shared_ptr<Node> node) { // add node_ptr to inputNode if it can std::size_t filledWithKnownInputs = 0U; bool wasAdded = mInputNodes.find(node) != mInputNodes.end(); - for (const std::shared_ptr<Node> pa_ptr : node->getParents()) { + for (const std::shared_ptr<Node>& pa_ptr : node->getParents()) { if ((pa_ptr == nullptr) || (mNodes.find(pa_ptr) == mNodes.end())) { // Parent doesn't exist || Parent not in the graph @@ -627,14 +629,14 @@ void Aidge::GraphView::updateInputNodes(std::shared_ptr<Node> node) { mInputNodes.erase(node); } // update other inputNodes - for (const std::shared_ptr<Node> ch_ptr : + for (const std::shared_ptr<Node>& ch_ptr : node->getChildren()) { // check if any child is in InputNodes too if (mInputNodes.find(ch_ptr) != mInputNodes.end()) { // it's a match! Must check if the inputNode found // is still an inputNode // change here bool remove = true; - for (const std::shared_ptr<Node> pa_ptr : ch_ptr->getParents()) { + for (const std::shared_ptr<Node>& pa_ptr : ch_ptr->getParents()) { if (pa_ptr == nullptr || mNodes.find(pa_ptr) == mNodes diff --git a/src/graph/Node.cpp b/src/graph/Node.cpp index 286ed7136a369e63f567b35135f89afcc266e0e1..b3db5befbdc8299114514d8d554d439bffc5eae2 100644 --- a/src/graph/Node.cpp +++ b/src/graph/Node.cpp @@ -35,8 +35,9 @@ Aidge::Node::Node(std::shared_ptr<Operator> op, const char *name) Aidge::Connector Aidge::Node::operator()(const std::vector<Connector> &ctors) { assert((ctors.size() == nbDataInputs()) && "Wrong number of arguments.\n"); - for (__attribute__((unused)) std::pair<std::shared_ptr<Node>, IOIndex_t> &input : inputs()) { + for (std::pair<std::shared_ptr<Node>, IOIndex_t> &input : inputs()) { assert((gk_IODefaultIndex == input.second) && "At least one input connection is not free.\n"); + (void) input; // avoid unused warning } IOIndex_t i = 0; for (const Connector &ctor : ctors) { diff --git a/src/graph/OpArgs.cpp b/src/graph/OpArgs.cpp index 3994a111d0881268d8768b2cb5843df65f7b4d17..f5f33fb049dec440f3bae412348c83e3427f06ce 100644 --- a/src/graph/OpArgs.cpp +++ b/src/graph/OpArgs.cpp @@ -62,7 +62,7 @@ std::shared_ptr<Aidge::GraphView> Aidge::Residual(std::initializer_list<OpArgs> std::shared_ptr<Node> lastNode = *gv->outputNodes().begin(); assert(gv->inputNodes().size() == 2U && "Zero or more than one input Node for the GraphView, don't know which one to choose from for the residual connection"); std::shared_ptr<Node> firstNode = nullptr; - for (const std::shared_ptr<Node> node_ptr : gv->inputNodes()) { + for (const std::shared_ptr<Node>& node_ptr : gv->inputNodes()) { if (node_ptr != lastNode) { firstNode = node_ptr; } diff --git a/unit_tests/operator/Test_GenericOperator.cpp b/unit_tests/operator/Test_GenericOperator.cpp index 886326214a4a285fb32e5909da5114d74782ee46..2208399897f586becca798eb469344af01dbab64 100644 --- a/unit_tests/operator/Test_GenericOperator.cpp +++ b/unit_tests/operator/Test_GenericOperator.cpp @@ -20,10 +20,10 @@ using namespace Aidge; TEST_CASE("[core/operators] GenericOp(add & get parameters)", "[Operator]") { SECTION("INT") { GenericOperator_Op Testop("TestOp", 1, 1, 1); - int value = 5; const char* key = "intParam"; - Testop.addParameter(key, value); - REQUIRE(Testop.getParameter<int>(key) == value); + Testop.addParameter(key, int(5)); + int registeredVal = Testop.getParameter<int>(key); + REQUIRE(registeredVal == 5); } SECTION("LONG") { GenericOperator_Op Testop("TestOp", 1, 1, 1);