diff --git a/CMakeLists.txt b/CMakeLists.txt index f4998e69f3..75ee5781cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,13 +104,11 @@ option(BUILD_MARKDOWN_BINDINGS "Build Markdown bindings for website documentatio option(MATHJAX "Use MathJax for HTML Doxygen output (disabled by default)." OFF) -option(FORCE_CXX11 - "Don't check that the compiler supports C++11, just assume it. Make sure to specify any necessary flag to enable C++11 as part of CXXFLAGS." OFF) option(USE_OPENMP "If available, use OpenMP for parallelization." ON) enable_testing() -# Set required standard to C++11. -set(CMAKE_CXX_STANDARD 11) +# Set required standard to C++14. +set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Ensure that GCC is new enough, if the compiler is GCC. @@ -375,26 +373,16 @@ endif() set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} ${CEREAL_INCLUDE_DIR}) # Detect OpenMP support in a compiler. If the compiler supports OpenMP, flags -# to compile with OpenMP are returned and added and the HAS_OPENMP definition -# is added for compilation. -# -# This way we can skip calls to functions defined in omp.h with code like: -# #ifdef HAS_OPENMP -# { -# ... openMP code here ... -# } -# #endif +# to compile with OpenMP are returned and added. Note that MSVC does not +# support a new-enough version of OpenMP to be useful. if (USE_OPENMP) find_package(OpenMP) endif () -if (OPENMP_FOUND) - add_definitions(-DHAS_OPENMP) +if (OpenMP_FOUND AND OpenMP_CXX_VERSION VERSION_GREATER_EQUAL 3.0.0) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") - if (OpenMP_CXX_FOUND) - set(MLPACK_LIBRARIES ${MLPACK_LIBRARIES} ${OpenMP_CXX_LIBRARIES}) - endif () + set(MLPACK_LIBRARIES ${MLPACK_LIBRARIES} ${OpenMP_CXX_LIBRARIES}) else () # Disable warnings for all the unknown OpenMP pragmas. if (NOT MSVC) diff --git a/HISTORY.md b/HISTORY.md index 605aaec568..3fe3b827c8 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,7 @@ ### mlpack ?.?.? ###### ????-??-?? + * Bump C++ standard requirement to C++14 (#3233). + * Fix `Perceptron` to work with cross-validation framework (#3190). * Migrate from boost tests to Catch2 framework (#2523), (#2584). diff --git a/doc/guide/build.hpp b/doc/guide/build.hpp index 79bf51df64..d992096dab 100644 --- a/doc/guide/build.hpp +++ b/doc/guide/build.hpp @@ -199,9 +199,6 @@ The full list of options mlpack allows: (default ON) - MATHJAX=(ON/OFF): use MathJax for generated Doxygen documentation (default OFF) - - FORCE_CXX11=(ON/OFF): assume that the compiler supports C++11 instead of - checking; be sure to specify any necessary flag to enable C++11 as part - of CXXFLAGS (default OFF) - USE_OPENMP=(ON/OFF): if ON, then use OpenMP if the compiler supports it; if OFF, OpenMP support is manually disabled (default ON) diff --git a/src/mlpack/CMakeLists.txt b/src/mlpack/CMakeLists.txt index 83979dfd20..25aa3856b9 100644 --- a/src/mlpack/CMakeLists.txt +++ b/src/mlpack/CMakeLists.txt @@ -21,24 +21,20 @@ endforeach() # are set in the root CMakeLists.txt. add_library(mlpack ${MLPACK_SRCS}) -# If we are not forcing C++11 support, check that the compiler supports C++11 -# and enable it. -if (NOT FORCE_CXX11) - target_compile_features(mlpack PUBLIC - cxx_decltype - cxx_alias_templates - cxx_auto_type - cxx_lambdas - cxx_constexpr - cxx_rvalue_references - cxx_static_assert - cxx_template_template_parameters - cxx_delegating_constructors - cxx_variadic_templates - cxx_nullptr - cxx_noexcept - ) -endif () +target_compile_features(mlpack PUBLIC + cxx_decltype + cxx_alias_templates + cxx_auto_type + cxx_lambdas + cxx_constexpr + cxx_rvalue_references + cxx_static_assert + cxx_template_template_parameters + cxx_delegating_constructors + cxx_variadic_templates + cxx_nullptr + cxx_noexcept +) # Generate export symbols for Windows, instead of adding __declspec(dllimport) # and __declspec(dllexport) everywhere. However, those modifiers are still diff --git a/src/mlpack/base.hpp b/src/mlpack/base.hpp new file mode 100644 index 0000000000..8700ea2d37 --- /dev/null +++ b/src/mlpack/base.hpp @@ -0,0 +1,111 @@ +/** + * @file base.hpp + * + * The most basic core includes that mlpack expects; standard C++ includes and + * Armadillo only + * + * mlpack is free software; you may redistribute it and/or modify it under the + * terms of the 3-clause BSD license. You should have received a copy of the + * 3-clause BSD license along with mlpack. If not, see + * http://www.opensource.org/licenses/BSD-3-Clause for more information. + */ +#ifndef MLPACK_BASE_HPP +#define MLPACK_BASE_HPP + +// First, check if Armadillo was included before, warning if so. +#ifdef ARMA_INCLUDES +#pragma message "Armadillo was included before mlpack; this can sometimes cause\ + problems. It should only be necessary to include and not \ +." +#endif + +// Defining _USE_MATH_DEFINES should set M_PI. +#define _USE_MATH_DEFINES +#include + +// Next, standard includes. +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// But if it's not defined, we'll do it. +#ifndef M_PI + #define M_PI 3.141592653589793238462643383279 +#endif + +// MLPACK_COUT_STREAM is used to change the default stream for printing +// purpose. +#if !defined(MLPACK_COUT_STREAM) + #define MLPACK_COUT_STREAM std::cout +#endif + +// MLPACK_CERR_STREAM is used to change the stream for printing warnings +// and errors. +#if !defined(MLPACK_CERR_STREAM) + #define MLPACK_CERR_STREAM std::cerr +#endif + +// Give ourselves a nice way to force functions to be inline if we need. +#undef mlpack_force_inline +#define mlpack_force_inline +#if defined(__GNUG__) && !defined(DEBUG) + #undef mlpack_force_inline + #define mlpack_force_inline __attribute__((always_inline)) +#elif defined(_MSC_VER) && !defined(DEBUG) + #undef mlpack_force_inline + #define mlpack_force_inline __forceinline +#endif + +// Backport std::any from C+17 to C++11 to replace boost::any. +// Use mnmlstc backport implementation only if compiler does not +// support C++17. +#if __cplusplus < 201703L + #include + #include + #define MLPACK_ANY core::v2::any + #define MLPACK_ANY_CAST core::v2::any_cast + #define MLPACK_STRING_VIEW core::v2::string_view +#else + #include + #include + #define MLPACK_ANY std::any + #define MLPACK_ANY_CAST std::any_cast + #define MLPACK_STRING_VIEW std::string_view +#endif + +// Now include Armadillo through the special mlpack extensions. +#include +#include + +// On Visual Studio, disable C4519 (default arguments for function templates) +// since it's by default an error, which doesn't even make any sense because +// it's part of the C++11 standard. +#ifdef _MSC_VER + #pragma warning(disable : 4519) +#endif + +// Ensure that the user isn't doing something stupid with their Armadillo +// defines. +#include + +// This can be removed when Visual Studio supports an OpenMP version with +// unsigned loop variables. +#if (defined(_OPENMP) && (_OPENMP >= 201107)) + #undef MLPACK_USE_OPENMP + #define MLPACK_USE_OPENMP +#endif + +// We need to be able to mark functions deprecated. +#include + +#endif diff --git a/src/mlpack/bindings/CMakeLists.txt b/src/mlpack/bindings/CMakeLists.txt index c9c96328e5..ad759d5594 100644 --- a/src/mlpack/bindings/CMakeLists.txt +++ b/src/mlpack/bindings/CMakeLists.txt @@ -15,6 +15,7 @@ endforeach() set(MARKDOWN_CATEGORIES ${MARKDOWN_CATEGORIES} PARENT_SCOPE) set(MLPACK_SRCS ${MLPACK_SRCS} PARENT_SCOPE) +set(MLPACK_TEST_SRCS ${MLPACK_TEST_SRCS} PARENT_SCOPE) set(MLPACK_PYXS ${MLPACK_PYXS} PARENT_SCOPE) set(DISABLE_CFLAGS ${DISABLE_CFLAGS} PARENT_SCOPE) set(BUILDING_PYTHON_BINDINGS ${BUILDING_PYTHON_BINDINGS} PARENT_SCOPE) diff --git a/src/mlpack/bindings/R/CMakeLists.txt b/src/mlpack/bindings/R/CMakeLists.txt index bd70f206fb..fa7cfaaa1e 100644 --- a/src/mlpack/bindings/R/CMakeLists.txt +++ b/src/mlpack/bindings/R/CMakeLists.txt @@ -414,6 +414,7 @@ macro (post_r_setup) # Then copy each of the header and source files over to that directory. set(MLPACK_SOURCES "${CMAKE_BINARY_DIR}/src/mlpack/mlpack_export.hpp" + "${CMAKE_CURRENT_SOURCE_DIR}/base.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/prereqs.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/core.hpp" ) diff --git a/src/mlpack/bindings/R/R_option.hpp b/src/mlpack/bindings/R/R_option.hpp index 5982494c50..f03626800a 100644 --- a/src/mlpack/bindings/R/R_option.hpp +++ b/src/mlpack/bindings/R/R_option.hpp @@ -73,7 +73,7 @@ class ROption data.cppType = cppName; // Every parameter we'll get from R will have the correct type. - data.value = ANY(defaultValue); + data.value = defaultValue; // Set the function pointers that we'll need. All of these function // pointers will be used by both the program that generates the R, and diff --git a/src/mlpack/bindings/R/default_param_impl.hpp b/src/mlpack/bindings/R/default_param_impl.hpp index 64eea0d1e2..fd7e994200 100644 --- a/src/mlpack/bindings/R/default_param_impl.hpp +++ b/src/mlpack/bindings/R/default_param_impl.hpp @@ -36,7 +36,7 @@ std::string DefaultParamImpl( if (std::is_same::value) oss << "FALSE"; else - oss << ANY_CAST(data.value); + oss << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -51,7 +51,7 @@ std::string DefaultParamImpl( { // Print each element in an array delimited by square brackets. std::ostringstream oss; - const T& vector = ANY_CAST(data.value); + const T& vector = MLPACK_ANY_CAST(data.value); oss << "c("; if (std::is_same>::value) { @@ -92,7 +92,7 @@ std::string DefaultParamImpl( util::ParamData& data, const typename std::enable_if::value>::type*) { - const std::string& s = *ANY_CAST(&data.value); + const std::string& s = *MLPACK_ANY_CAST(&data.value); return "\"" + s + "\""; } diff --git a/src/mlpack/bindings/R/get_param.hpp b/src/mlpack/bindings/R/get_param.hpp index b725698c9e..72f6d3b311 100644 --- a/src/mlpack/bindings/R/get_param.hpp +++ b/src/mlpack/bindings/R/get_param.hpp @@ -27,7 +27,7 @@ void GetParam(util::ParamData& d, const void* /* input */, void* output) { - *((T**) output) = const_cast(ANY_CAST(&d.value)); + *((T**) output) = const_cast(MLPACK_ANY_CAST(&d.value)); } } // namespace r diff --git a/src/mlpack/bindings/R/get_printable_param.hpp b/src/mlpack/bindings/R/get_printable_param.hpp index 8f8fefaaaf..657dd18334 100644 --- a/src/mlpack/bindings/R/get_printable_param.hpp +++ b/src/mlpack/bindings/R/get_printable_param.hpp @@ -32,7 +32,7 @@ std::string GetPrintableParam( std::tuple>::value>::type* = 0) { std::ostringstream oss; - oss << ANY_CAST(data.value); + oss << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -44,7 +44,7 @@ std::string GetPrintableParam( util::ParamData& data, const typename std::enable_if::value>::type* = 0) { - const T& t = ANY_CAST(data.value); + const T& t = MLPACK_ANY_CAST(data.value); std::ostringstream oss; for (size_t i = 0; i < t.size(); ++i) @@ -61,7 +61,7 @@ std::string GetPrintableParam( const typename std::enable_if::value>::type* = 0) { // Get the matrix. - const T& matrix = ANY_CAST(data.value); + const T& matrix = MLPACK_ANY_CAST(data.value); std::ostringstream oss; oss << matrix.n_rows << "x" << matrix.n_cols << " matrix"; @@ -78,7 +78,7 @@ std::string GetPrintableParam( const typename std::enable_if::value>::type* = 0) { std::ostringstream oss; - oss << data.cppType << " model at " << ANY_CAST(data.value); + oss << data.cppType << " model at " << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -92,7 +92,7 @@ std::string GetPrintableParam( std::tuple>::value>::type* = 0) { // Get the matrix. - const T& tuple = ANY_CAST(data.value); + const T& tuple = MLPACK_ANY_CAST(data.value); const arma::mat& matrix = std::get<1>(tuple); std::ostringstream oss; diff --git a/src/mlpack/bindings/R/mlpack/DESCRIPTION.in b/src/mlpack/bindings/R/mlpack/DESCRIPTION.in index 9ccc0e0e03..4745c42a17 100644 --- a/src/mlpack/bindings/R/mlpack/DESCRIPTION.in +++ b/src/mlpack/bindings/R/mlpack/DESCRIPTION.in @@ -7,8 +7,7 @@ Description: A fast, flexible machine learning library, written in C++, that aims to provide fast, extensible implementations of cutting-edge machine learning algorithms. See also Curtin et al. (2018) . -SystemRequirements: A C++11 compiler. Versions 4.8.*, 4.9.* or later of GCC - will be fine. +SystemRequirements: A C++14 compiler. Version 5 or later of GCC will be fine. License: BSD_3_clause + file LICENSE Depends: R (>= 4.0.0) Imports: Rcpp (>= 0.12.12) diff --git a/src/mlpack/bindings/R/mlpack/src/Makevars b/src/mlpack/bindings/R/mlpack/src/Makevars index bb6a88cc84..489fe04d78 100644 --- a/src/mlpack/bindings/R/mlpack/src/Makevars +++ b/src/mlpack/bindings/R/mlpack/src/Makevars @@ -1,3 +1,3 @@ PKG_CXXFLAGS = -DBOOST_MATH_PROMOTE_DOUBLE_POLICY=false -I. $(SHLIB_OPENMP_CXXFLAGS) PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS) $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) -CXX_STD = CXX11 +CXX_STD = CXX14 diff --git a/src/mlpack/bindings/R/mlpack/src/Makevars.win b/src/mlpack/bindings/R/mlpack/src/Makevars.win index 12f71348e6..cb4f589642 100644 --- a/src/mlpack/bindings/R/mlpack/src/Makevars.win +++ b/src/mlpack/bindings/R/mlpack/src/Makevars.win @@ -1,3 +1,3 @@ PKG_CXXFLAGS = -DBOOST_MATH_PROMOTE_DOUBLE_POLICY=false -I. $(SHLIB_OPENMP_CXXFLAGS) -ftrack-macro-expansion=0 -pipe --param ggc-min-expand=10 --param ggc-min-heapsize=8192 PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS) $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) -CXX_STD = CXX11 +CXX_STD = CXX14 diff --git a/src/mlpack/bindings/R/print_doc.hpp b/src/mlpack/bindings/R/print_doc.hpp index 2fa9a216bb..0c08a129a9 100644 --- a/src/mlpack/bindings/R/print_doc.hpp +++ b/src/mlpack/bindings/R/print_doc.hpp @@ -55,19 +55,19 @@ void PrintDoc(util::ParamData& d, oss << ". Default value \""; if (d.cppType == "std::string") { - oss << ANY_CAST(d.value); + oss << MLPACK_ANY_CAST(d.value); } else if (d.cppType == "double") { - oss << ANY_CAST(d.value); + oss << MLPACK_ANY_CAST(d.value); } else if (d.cppType == "int") { - oss << ANY_CAST(d.value); + oss << MLPACK_ANY_CAST(d.value); } else if (d.cppType == "bool") { - oss << (ANY_CAST(d.value) ? "TRUE" : "FALSE"); + oss << (MLPACK_ANY_CAST(d.value) ? "TRUE" : "FALSE"); } oss << "\""; } diff --git a/src/mlpack/bindings/cli/add_to_cli11.hpp b/src/mlpack/bindings/cli/add_to_cli11.hpp index 1e4e89f366..39de491b2e 100644 --- a/src/mlpack/bindings/cli/add_to_cli11.hpp +++ b/src/mlpack/bindings/cli/add_to_cli11.hpp @@ -47,8 +47,8 @@ void AddToCLI11(const std::string& cliName, [¶m](const std::string& value) { using TupleType = std::tuple::type>; - TupleType& tuple = *ANY_CAST(¶m.value); - std::get<0>(std::get<1>(tuple)) = ANY_CAST(value); + TupleType& tuple = *MLPACK_ANY_CAST(¶m.value); + std::get<0>(std::get<1>(tuple)) = MLPACK_ANY_CAST(value); param.wasPassed = true; }, param.desc.c_str()); @@ -79,8 +79,8 @@ void AddToCLI11(const std::string& cliName, [¶m](const std::string& value) { using TupleType = std::tuple::type>; - TupleType& tuple = *ANY_CAST(¶m.value); - std::get<1>(tuple) = ANY_CAST(value); + TupleType& tuple = *MLPACK_ANY_CAST(¶m.value); + std::get<1>(tuple) = MLPACK_ANY_CAST(value); param.wasPassed = true; }, param.desc.c_str()); @@ -109,8 +109,8 @@ void AddToCLI11(const std::string& cliName, [¶m](const std::string& value) { using TupleType = std::tuple::type>; - TupleType& tuple = *ANY_CAST(¶m.value); - std::get<0>(std::get<1>(tuple)) = ANY_CAST(value); + TupleType& tuple = *MLPACK_ANY_CAST(¶m.value); + std::get<0>(std::get<1>(tuple)) = MLPACK_ANY_CAST(value); param.wasPassed = true; }, param.desc.c_str()); diff --git a/src/mlpack/bindings/cli/cli_option.hpp b/src/mlpack/bindings/cli/cli_option.hpp index d8ea8e6182..52c41474ce 100644 --- a/src/mlpack/bindings/cli/cli_option.hpp +++ b/src/mlpack/bindings/cli/cli_option.hpp @@ -95,12 +95,12 @@ class CLIOption typename ParameterType::type>::type>::value) { - data.value = ANY(defaultValue); + data.value = defaultValue; } else { typename ParameterType::type>::type tmp; - data.value = ANY(std::tuple(defaultValue, tmp)); + data.value = std::tuple(defaultValue, tmp); } const std::string tname = data.tname; diff --git a/src/mlpack/bindings/cli/default_param_impl.hpp b/src/mlpack/bindings/cli/default_param_impl.hpp index ea0ec7cd7c..cae9a691c3 100644 --- a/src/mlpack/bindings/cli/default_param_impl.hpp +++ b/src/mlpack/bindings/cli/default_param_impl.hpp @@ -34,7 +34,7 @@ std::string DefaultParamImpl( { std::ostringstream oss; if (!std::is_same::value) - oss << ANY_CAST(data.value); + oss << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -49,7 +49,7 @@ std::string DefaultParamImpl( { // Print each element in an array delimited by square brackets. std::ostringstream oss; - const T& vector = ANY_CAST(data.value); + const T& vector = MLPACK_ANY_CAST(data.value); oss << "["; if (std::is_same>::value) { @@ -91,7 +91,7 @@ std::string DefaultParamImpl( util::ParamData& data, const typename std::enable_if::value>::type*) { - const std::string& s = *ANY_CAST(&data.value); + const std::string& s = *MLPACK_ANY_CAST(&data.value); return "'" + s + "'"; } diff --git a/src/mlpack/bindings/cli/delete_allocated_memory.hpp b/src/mlpack/bindings/cli/delete_allocated_memory.hpp index 2ba1681301..2aec6fc712 100644 --- a/src/mlpack/bindings/cli/delete_allocated_memory.hpp +++ b/src/mlpack/bindings/cli/delete_allocated_memory.hpp @@ -43,7 +43,7 @@ void DeleteAllocatedMemoryImpl( { // Delete the allocated memory (hopefully we actually own it). typedef std::tuple TupleType; - delete std::get<0>(*ANY_CAST(&d.value)); + delete std::get<0>(*MLPACK_ANY_CAST(&d.value)); } template diff --git a/src/mlpack/bindings/cli/get_allocated_memory.hpp b/src/mlpack/bindings/cli/get_allocated_memory.hpp index 82e4bf3b76..bb9fb931b4 100644 --- a/src/mlpack/bindings/cli/get_allocated_memory.hpp +++ b/src/mlpack/bindings/cli/get_allocated_memory.hpp @@ -45,7 +45,7 @@ void* GetAllocatedMemory( // Here we have a model, which is a tuple, and we need the address of the // memory. typedef std::tuple TupleType; - return std::get<0>(*ANY_CAST(&d.value)); + return std::get<0>(*MLPACK_ANY_CAST(&d.value)); } template diff --git a/src/mlpack/bindings/cli/get_param.hpp b/src/mlpack/bindings/cli/get_param.hpp index a2fd746187..d32d6655a2 100644 --- a/src/mlpack/bindings/cli/get_param.hpp +++ b/src/mlpack/bindings/cli/get_param.hpp @@ -34,7 +34,7 @@ T& GetParam( std::tuple>::value>::type* = 0) { // No mapping is needed, so just cast it directly. - return *ANY_CAST(&d.value); + return *MLPACK_ANY_CAST(&d.value); } /** @@ -52,7 +52,7 @@ T& GetParam( // times, but I am not bothered by that---it shouldn't be something that // happens. typedef std::tuple::type> TupleType; - TupleType& tuple = *ANY_CAST(&d.value); + TupleType& tuple = *MLPACK_ANY_CAST(&d.value); const std::string& value = std::get<0>(std::get<1>(tuple)); T& matrix = std::get<0>(tuple); size_t& n_rows = std::get<1>(std::get<1>(tuple)); @@ -86,7 +86,7 @@ T& GetParam( // If this is an input parameter, we need to load both the matrix and the // dataset info. typedef std::tuple> TupleType; - TupleType* tuple = ANY_CAST(&d.value); + TupleType* tuple = MLPACK_ANY_CAST(&d.value); const std::string& value = std::get<0>(std::get<1>(*tuple)); T& t = std::get<0>(*tuple); size_t& n_rows = std::get<1>(std::get<1>(*tuple)); @@ -116,7 +116,7 @@ T*& GetParam( // If the model is an input model, we have to load it from file. 'value' // contains the filename. typedef std::tuple TupleType; - TupleType* tuple = ANY_CAST(&d.value); + TupleType* tuple = MLPACK_ANY_CAST(&d.value); const std::string& value = std::get<1>(*tuple); if (d.input && !d.loaded) { diff --git a/src/mlpack/bindings/cli/get_printable_param_impl.hpp b/src/mlpack/bindings/cli/get_printable_param_impl.hpp index f6f303fce8..0c9875490d 100644 --- a/src/mlpack/bindings/cli/get_printable_param_impl.hpp +++ b/src/mlpack/bindings/cli/get_printable_param_impl.hpp @@ -30,7 +30,7 @@ std::string GetPrintableParam( std::tuple>::value>::type* /* junk */) { std::ostringstream oss; - oss << ANY_CAST(data.value); + oss << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -41,7 +41,7 @@ std::string GetPrintableParam( const typename std::enable_if::value>::type* /* junk */) { - const T& t = ANY_CAST(data.value); + const T& t = MLPACK_ANY_CAST(data.value); std::ostringstream oss; for (size_t i = 0; i < t.size(); ++i) @@ -80,7 +80,7 @@ std::string GetPrintableParam( { // Extract the string from the tuple that's being held. typedef std::tuple::type> TupleType; - const TupleType* tuple = ANY_CAST(&data.value); + const TupleType* tuple = MLPACK_ANY_CAST(&data.value); std::ostringstream oss; oss << "'" << std::get<0>(std::get<1>(*tuple)) << "'"; @@ -108,7 +108,7 @@ std::string GetPrintableParam( { // Extract the string from the tuple that's being held. typedef std::tuple::type> TupleType; - const TupleType* tuple = ANY_CAST(&data.value); + const TupleType* tuple = MLPACK_ANY_CAST(&data.value); std::ostringstream oss; oss << std::get<1>(*tuple); diff --git a/src/mlpack/bindings/cli/get_raw_param.hpp b/src/mlpack/bindings/cli/get_raw_param.hpp index b9aacd7f16..ea3c43f865 100644 --- a/src/mlpack/bindings/cli/get_raw_param.hpp +++ b/src/mlpack/bindings/cli/get_raw_param.hpp @@ -33,7 +33,7 @@ T& GetRawParam( std::tuple>::value>::type* = 0) { // No mapping is needed, so just cast it directly. - return *ANY_CAST(&d.value); + return *MLPACK_ANY_CAST(&d.value); } /** @@ -49,7 +49,7 @@ T& GetRawParam( { // Don't load the matrix. typedef std::tuple> TupleType; - T& value = std::get<0>(*ANY_CAST(&d.value)); + T& value = std::get<0>(*MLPACK_ANY_CAST(&d.value)); return value; } @@ -64,7 +64,7 @@ T*& GetRawParam( { // Don't load the model. typedef std::tuple TupleType; - T*& value = std::get<0>(*ANY_CAST(&d.value)); + T*& value = std::get<0>(*MLPACK_ANY_CAST(&d.value)); return value; } diff --git a/src/mlpack/bindings/cli/in_place_copy.hpp b/src/mlpack/bindings/cli/in_place_copy.hpp index a787045eca..742058b1c5 100644 --- a/src/mlpack/bindings/cli/in_place_copy.hpp +++ b/src/mlpack/bindings/cli/in_place_copy.hpp @@ -58,10 +58,10 @@ void InPlaceCopyInternal( { // Make the output filename the same as the input filename. typedef std::tuple::type> TupleType; - TupleType& tuple = *ANY_CAST(&d.value); + TupleType& tuple = *MLPACK_ANY_CAST(&d.value); std::string& value = std::get<0>(std::get<1>(tuple)); - const TupleType& inputTuple = *ANY_CAST(&input.value); + const TupleType& inputTuple = *MLPACK_ANY_CAST(&input.value); value = std::get<0>(std::get<1>(inputTuple)); } @@ -81,10 +81,10 @@ void InPlaceCopyInternal( { // Make the output filename the same as the input filename. typedef std::tuple::type> TupleType; - TupleType& tuple = *ANY_CAST(&d.value); + TupleType& tuple = *MLPACK_ANY_CAST(&d.value); std::string& value = std::get<1>(tuple); - const TupleType& inputTuple = *ANY_CAST(&input.value); + const TupleType& inputTuple = *MLPACK_ANY_CAST(&input.value); value = std::get<1>(inputTuple); } diff --git a/src/mlpack/bindings/cli/mlpack_main.hpp b/src/mlpack/bindings/cli/mlpack_main.hpp index 5e4832e675..9be224fbab 100644 --- a/src/mlpack/bindings/cli/mlpack_main.hpp +++ b/src/mlpack/bindings/cli/mlpack_main.hpp @@ -80,7 +80,7 @@ using Option = mlpack::bindings::cli::CLIOption; } #include -#include +#include #include #include diff --git a/src/mlpack/bindings/cli/output_param_impl.hpp b/src/mlpack/bindings/cli/output_param_impl.hpp index e1cd1f411a..e9892643b1 100644 --- a/src/mlpack/bindings/cli/output_param_impl.hpp +++ b/src/mlpack/bindings/cli/output_param_impl.hpp @@ -30,7 +30,7 @@ void OutputParamImpl( const typename std::enable_if>::value>::type* /* junk */) { - std::cout << data.name << ": " << *ANY_CAST(&data.value) + std::cout << data.name << ": " << *MLPACK_ANY_CAST(&data.value) << std::endl; } @@ -41,7 +41,7 @@ void OutputParamImpl( const typename std::enable_if::value>::type* /* junk */) { std::cout << data.name << ": "; - const T& t = *ANY_CAST(&data.value); + const T& t = *MLPACK_ANY_CAST(&data.value); for (size_t i = 0; i < t.size(); ++i) std::cout << t[i] << " "; std::cout << std::endl; @@ -54,9 +54,9 @@ void OutputParamImpl( const typename std::enable_if::value>::type* /* junk */) { typedef std::tuple> TupleType; - const T& output = std::get<0>(*ANY_CAST(&data.value)); + const T& output = std::get<0>(*MLPACK_ANY_CAST(&data.value)); const std::string& filename = - std::get<0>(std::get<1>(*ANY_CAST(&data.value))); + std::get<0>(std::get<1>(*MLPACK_ANY_CAST(&data.value))); if (output.n_elem > 0 && filename != "") { @@ -78,10 +78,10 @@ void OutputParamImpl( // const. In this case we can assume it though, since we will be saving and // not loading. typedef std::tuple TupleType; - T*& output = const_cast(std::get<0>(*ANY_CAST( + T*& output = const_cast(std::get<0>(*MLPACK_ANY_CAST( &data.value))); const std::string& filename = - std::get<1>(*ANY_CAST(&data.value)); + std::get<1>(*MLPACK_ANY_CAST(&data.value)); if (filename != "") data::Save(filename, "model", *output); @@ -96,9 +96,9 @@ void OutputParamImpl( { // Output the matrix with the mappings. typedef std::tuple> TupleType; - const T& tuple = std::get<0>(*ANY_CAST(&data.value)); + const T& tuple = std::get<0>(*MLPACK_ANY_CAST(&data.value)); const std::string& filename = - std::get<0>(std::get<1>(*ANY_CAST(&data.value))); + std::get<0>(std::get<1>(*MLPACK_ANY_CAST(&data.value))); const arma::mat& matrix = std::get<1>(tuple); // The mapping isn't taken into account. We should write a data::Save() diff --git a/src/mlpack/bindings/cli/set_param.hpp b/src/mlpack/bindings/cli/set_param.hpp index cab459dd7c..5d80b07d36 100644 --- a/src/mlpack/bindings/cli/set_param.hpp +++ b/src/mlpack/bindings/cli/set_param.hpp @@ -26,7 +26,7 @@ namespace cli { template void SetParam( util::ParamData& d, - const ANY& value, + const MLPACK_ANY& value, const typename std::enable_if::value>::type* = 0, const typename std::enable_if::value>::type* = 0, const typename std::enable_if::value>::type* = 0) { // No mapping is needed. - d.value = value; + d.value = *MLPACK_ANY_CAST(&value); } /** @@ -43,7 +43,7 @@ void SetParam( template void SetParam( util::ParamData& d, - const ANY& /* value */, + const MLPACK_ANY& /* value */, const typename std::enable_if::value>::type* = 0) { // Force set to the value of whether or not this was passed. @@ -57,15 +57,15 @@ void SetParam( template void SetParam( util::ParamData& d, - const ANY& value, + const MLPACK_ANY& value, const typename std::enable_if::value || std::is_same>::value>::type* = 0) { // We're setting the string filename. typedef std::tuple::type> TupleType; - TupleType& tuple = *ANY_CAST(&d.value); - std::get<0>(std::get<1>(tuple)) = ANY_CAST(value); + TupleType& tuple = *MLPACK_ANY_CAST(&d.value); + std::get<0>(std::get<1>(tuple)) = MLPACK_ANY_CAST(value); } /** @@ -75,14 +75,14 @@ void SetParam( template void SetParam( util::ParamData& d, - const ANY& value, + const MLPACK_ANY& value, const typename std::enable_if::value>::type* = 0, const typename std::enable_if::value>::type* = 0) { // We're setting the string filename. typedef std::tuple::type> TupleType; - TupleType& tuple = *ANY_CAST(&d.value); - std::get<1>(tuple) = ANY_CAST(value); + TupleType& tuple = *MLPACK_ANY_CAST(&d.value); + std::get<1>(tuple) = MLPACK_ANY_CAST(value); } /** @@ -97,7 +97,7 @@ template void SetParam(util::ParamData& d, const void* input, void* /* output */) { SetParam::type>( - const_cast(d), *((ANY*) input)); + const_cast(d), *((MLPACK_ANY*) input)); } } // namespace cli diff --git a/src/mlpack/bindings/go/default_param_impl.hpp b/src/mlpack/bindings/go/default_param_impl.hpp index 0bae6ce306..1be5d696be 100644 --- a/src/mlpack/bindings/go/default_param_impl.hpp +++ b/src/mlpack/bindings/go/default_param_impl.hpp @@ -36,7 +36,7 @@ std::string DefaultParamImpl( if (std::is_same::value) oss << "false"; else - oss << ANY_CAST(data.value); + oss << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -51,7 +51,7 @@ std::string DefaultParamImpl( { // Print each element in an array delimited by square brackets. std::ostringstream oss; - const T& vector = ANY_CAST(data.value); + const T& vector = MLPACK_ANY_CAST(data.value); if (std::is_same>::value) { oss << "[]string{"; @@ -93,7 +93,7 @@ std::string DefaultParamImpl( util::ParamData& data, const typename std::enable_if::value>::type*) { - const std::string& s = *ANY_CAST(&data.value); + const std::string& s = *MLPACK_ANY_CAST(&data.value); return "\"" + s + "\""; } diff --git a/src/mlpack/bindings/go/get_param.hpp b/src/mlpack/bindings/go/get_param.hpp index 8ef97d31e1..9c02ab8e77 100644 --- a/src/mlpack/bindings/go/get_param.hpp +++ b/src/mlpack/bindings/go/get_param.hpp @@ -27,7 +27,7 @@ void GetParam(util::ParamData& d, const void* /* input */, void* output) { - *((T**) output) = const_cast(ANY_CAST(&d.value)); + *((T**) output) = const_cast(MLPACK_ANY_CAST(&d.value)); } } // namespace go diff --git a/src/mlpack/bindings/go/get_printable_param.hpp b/src/mlpack/bindings/go/get_printable_param.hpp index fb46da993d..76d3301192 100644 --- a/src/mlpack/bindings/go/get_printable_param.hpp +++ b/src/mlpack/bindings/go/get_printable_param.hpp @@ -32,7 +32,7 @@ std::string GetPrintableParam( std::tuple>::value>::type* = 0) { std::ostringstream oss; - oss << ANY_CAST(data.value); + oss << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -44,7 +44,7 @@ std::string GetPrintableParam( util::ParamData& data, const typename std::enable_if::value>::type* = 0) { - const T& t = ANY_CAST(data.value); + const T& t = MLPACK_ANY_CAST(data.value); std::ostringstream oss; for (size_t i = 0; i < t.size(); ++i) @@ -61,7 +61,7 @@ std::string GetPrintableParam( const typename std::enable_if::value>::type* = 0) { // Get the matrix. - const T& matrix = ANY_CAST(data.value); + const T& matrix = MLPACK_ANY_CAST(data.value); std::ostringstream oss; oss << matrix.n_rows << "x" << matrix.n_cols << " matrix"; @@ -78,7 +78,7 @@ std::string GetPrintableParam( const typename std::enable_if::value>::type* = 0) { std::ostringstream oss; - oss << data.cppType << " model at " << ANY_CAST(data.value); + oss << data.cppType << " model at " << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -92,7 +92,7 @@ std::string GetPrintableParam( std::tuple>::value>::type* = 0) { // Get the matrix. - const T& tuple = ANY_CAST(data.value); + const T& tuple = MLPACK_ANY_CAST(data.value); const arma::mat& matrix = std::get<1>(tuple); std::ostringstream oss; diff --git a/src/mlpack/bindings/go/go_option.hpp b/src/mlpack/bindings/go/go_option.hpp index 2554bc403e..000db2c9b0 100644 --- a/src/mlpack/bindings/go/go_option.hpp +++ b/src/mlpack/bindings/go/go_option.hpp @@ -78,7 +78,7 @@ class GoOption data.loaded = false; data.cppType = cppName; - data.value = ANY(defaultValue); + data.value = defaultValue; // Set the function pointers that we'll need. All of these function // pointers will be used by both the program that generates the .cpp, diff --git a/src/mlpack/bindings/go/print_doc.hpp b/src/mlpack/bindings/go/print_doc.hpp index d312f602f1..c12aa83a62 100644 --- a/src/mlpack/bindings/go/print_doc.hpp +++ b/src/mlpack/bindings/go/print_doc.hpp @@ -53,16 +53,16 @@ void PrintDoc(util::ParamData& d, { if (d.cppType == "std::string") { - oss << " Default value '" << ANY_CAST(d.value) + oss << " Default value '" << MLPACK_ANY_CAST(d.value) << "'."; } else if (d.cppType == "double") { - oss << " Default value " << ANY_CAST(d.value) << "."; + oss << " Default value " << MLPACK_ANY_CAST(d.value) << "."; } else if (d.cppType == "int") { - oss << " Default value " << ANY_CAST(d.value) << "."; + oss << " Default value " << MLPACK_ANY_CAST(d.value) << "."; } } diff --git a/src/mlpack/bindings/go/print_input_processing.hpp b/src/mlpack/bindings/go/print_input_processing.hpp index 880770c3b6..a337d8b577 100644 --- a/src/mlpack/bindings/go/print_input_processing.hpp +++ b/src/mlpack/bindings/go/print_input_processing.hpp @@ -67,22 +67,22 @@ void PrintInputProcessing( // Print out default value. if (d.cppType == "std::string") { - std::string value = ANY_CAST(d.value); + std::string value = MLPACK_ANY_CAST(d.value); std::cout << "\"" << value << "\""; } else if (d.cppType == "double") { - double value = ANY_CAST(d.value); + double value = MLPACK_ANY_CAST(d.value); std::cout << value; } else if (d.cppType == "int") { - int value = ANY_CAST(d.value); + int value = MLPACK_ANY_CAST(d.value); std::cout << value; } else if (d.cppType == "bool") { - bool value = ANY_CAST(d.value); + bool value = MLPACK_ANY_CAST(d.value); if (value == 0) std::cout << "false"; else diff --git a/src/mlpack/bindings/go/print_method_init.hpp b/src/mlpack/bindings/go/print_method_init.hpp index 0e2c055d5b..7c42bfdcab 100644 --- a/src/mlpack/bindings/go/print_method_init.hpp +++ b/src/mlpack/bindings/go/print_method_init.hpp @@ -54,23 +54,23 @@ void PrintMethodInit( { if (d.cppType == "std::string") { - std::string value = ANY_CAST(d.value); + std::string value = MLPACK_ANY_CAST(d.value); std::cout << prefix << goParamName << ": \"" << value << "\"," << std::endl; } else if (d.cppType == "double") { - double value = ANY_CAST(d.value); + double value = MLPACK_ANY_CAST(d.value); std::cout << prefix << goParamName << ": " << value << "," << std::endl; } else if (d.cppType == "int") { - int value = ANY_CAST(d.value); + int value = MLPACK_ANY_CAST(d.value); std::cout << prefix << goParamName << ": " << value << "," << std::endl; } else if (d.cppType == "bool") { - bool value = ANY_CAST(d.value); + bool value = MLPACK_ANY_CAST(d.value); if (value == 0) std::cout << prefix << goParamName << ": false," << std::endl; else diff --git a/src/mlpack/bindings/julia/default_param_impl.hpp b/src/mlpack/bindings/julia/default_param_impl.hpp index d36b146608..1a2b8ca954 100644 --- a/src/mlpack/bindings/julia/default_param_impl.hpp +++ b/src/mlpack/bindings/julia/default_param_impl.hpp @@ -36,7 +36,7 @@ std::string DefaultParamImpl( if (std::is_same::value) oss << "false"; else - oss << ANY_CAST(data.value); + oss << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -51,7 +51,7 @@ std::string DefaultParamImpl( { // Print each element in an array delimited by square brackets. std::ostringstream oss; - const T& vector = ANY_CAST(data.value); + const T& vector = MLPACK_ANY_CAST(data.value); oss << "["; if (std::is_same>::value) { @@ -92,7 +92,7 @@ std::string DefaultParamImpl( util::ParamData& data, const typename std::enable_if::value>::type*) { - const std::string& s = *ANY_CAST(&data.value); + const std::string& s = *MLPACK_ANY_CAST(&data.value); return "\"" + s + "\""; } diff --git a/src/mlpack/bindings/julia/get_param.hpp b/src/mlpack/bindings/julia/get_param.hpp index 53983b88b8..4d493f7113 100644 --- a/src/mlpack/bindings/julia/get_param.hpp +++ b/src/mlpack/bindings/julia/get_param.hpp @@ -27,7 +27,7 @@ void GetParam(util::ParamData& d, const void* /* input */, void* output) { - *((T**) output) = const_cast(ANY_CAST(&d.value)); + *((T**) output) = const_cast(MLPACK_ANY_CAST(&d.value)); } } // namespace julia diff --git a/src/mlpack/bindings/julia/get_printable_param.hpp b/src/mlpack/bindings/julia/get_printable_param.hpp index 0f204f02ea..4fbb0e6859 100644 --- a/src/mlpack/bindings/julia/get_printable_param.hpp +++ b/src/mlpack/bindings/julia/get_printable_param.hpp @@ -32,7 +32,7 @@ std::string GetPrintableParam( std::tuple>::value>::type* = 0) { std::ostringstream oss; - oss << ANY_CAST(data.value); + oss << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -44,7 +44,7 @@ std::string GetPrintableParam( util::ParamData& data, const typename std::enable_if::value>::type* = 0) { - const T& t = ANY_CAST(data.value); + const T& t = MLPACK_ANY_CAST(data.value); std::ostringstream oss; for (size_t i = 0; i < t.size(); ++i) @@ -61,7 +61,7 @@ std::string GetPrintableParam( const typename std::enable_if::value>::type* = 0) { // Get the matrix. - const T& matrix = ANY_CAST(data.value); + const T& matrix = MLPACK_ANY_CAST(data.value); std::ostringstream oss; oss << matrix.n_rows << "x" << matrix.n_cols << " matrix"; @@ -78,7 +78,7 @@ std::string GetPrintableParam( const typename std::enable_if::value>::type* = 0) { std::ostringstream oss; - oss << data.cppType << " model at " << ANY_CAST(data.value); + oss << data.cppType << " model at " << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -92,7 +92,7 @@ std::string GetPrintableParam( std::tuple>::value>::type* = 0) { // Get the matrix. - const T& tuple = ANY_CAST(data.value); + const T& tuple = MLPACK_ANY_CAST(data.value); const arma::mat& matrix = std::get<1>(tuple); std::ostringstream oss; diff --git a/src/mlpack/bindings/julia/julia_option.hpp b/src/mlpack/bindings/julia/julia_option.hpp index 1a1bcea4b2..b1bcaf66ec 100644 --- a/src/mlpack/bindings/julia/julia_option.hpp +++ b/src/mlpack/bindings/julia/julia_option.hpp @@ -64,7 +64,7 @@ class JuliaOption data.cppType = cppName; // Every parameter we'll get from Julia will have the correct type. - data.value = ANY(defaultValue); + data.value = defaultValue; // Set the function pointers that we'll need. All of these function // pointers will be used by both the program that generates the pyx, and diff --git a/src/mlpack/bindings/julia/print_doc.hpp b/src/mlpack/bindings/julia/print_doc.hpp index 30d887137c..c63a9d6872 100644 --- a/src/mlpack/bindings/julia/print_doc.hpp +++ b/src/mlpack/bindings/julia/print_doc.hpp @@ -39,19 +39,19 @@ void PrintDoc(util::ParamData& d, const void* /* input */, void* output) oss << " Default value `"; if (d.cppType == "std::string") { - oss << ANY_CAST(d.value); + oss << MLPACK_ANY_CAST(d.value); } else if (d.cppType == "double") { - oss << ANY_CAST(d.value); + oss << MLPACK_ANY_CAST(d.value); } else if (d.cppType == "int") { - oss << ANY_CAST(d.value); + oss << MLPACK_ANY_CAST(d.value); } else if (d.cppType == "bool") { - oss << (ANY_CAST(d.value) ? "true" : "false"); + oss << (MLPACK_ANY_CAST(d.value) ? "true" : "false"); } oss << "`." << std::endl; } diff --git a/src/mlpack/bindings/markdown/get_param.hpp b/src/mlpack/bindings/markdown/get_param.hpp index 748798355c..40176677c0 100644 --- a/src/mlpack/bindings/markdown/get_param.hpp +++ b/src/mlpack/bindings/markdown/get_param.hpp @@ -28,7 +28,7 @@ void GetParam(util::ParamData& d, void* output) { util::ParamData& dmod = const_cast(d); - *((T**) output) = ANY_CAST(&dmod.value); + *((T**) output) = MLPACK_ANY_CAST(&dmod.value); } } // namespace markdown diff --git a/src/mlpack/bindings/markdown/get_printable_param.hpp b/src/mlpack/bindings/markdown/get_printable_param.hpp index 068d4c8ce9..39e2c435d3 100644 --- a/src/mlpack/bindings/markdown/get_printable_param.hpp +++ b/src/mlpack/bindings/markdown/get_printable_param.hpp @@ -32,7 +32,7 @@ std::string GetPrintableParam( std::tuple>::value>::type* = 0) { std::ostringstream oss; - oss << ANY_CAST(data.value); + oss << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -44,7 +44,7 @@ std::string GetPrintableParam( util::ParamData& data, const typename std::enable_if::value>::type* = 0) { - const T& t = ANY_CAST(data.value); + const T& t = MLPACK_ANY_CAST(data.value); std::ostringstream oss; for (size_t i = 0; i < t.size(); ++i) @@ -61,7 +61,7 @@ std::string GetPrintableParam( const typename std::enable_if::value>::type* = 0) { // Get the matrix. - const T& matrix = ANY_CAST(data.value); + const T& matrix = MLPACK_ANY_CAST(data.value); std::ostringstream oss; oss << matrix.n_rows << "x" << matrix.n_cols << " matrix"; @@ -78,7 +78,7 @@ std::string GetPrintableParam( const typename std::enable_if::value>::type* = 0) { std::ostringstream oss; - oss << data.cppType << " model at " << ANY_CAST(data.value); + oss << data.cppType << " model at " << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -92,7 +92,7 @@ std::string GetPrintableParam( std::tuple>::value>::type* = 0) { // Get the matrix. - const T& tuple = ANY_CAST(data.value); + const T& tuple = MLPACK_ANY_CAST(data.value); const arma::mat& matrix = std::get<1>(tuple); std::ostringstream oss; diff --git a/src/mlpack/bindings/markdown/md_option.hpp b/src/mlpack/bindings/markdown/md_option.hpp index e8d4db8da2..1a502a64de 100644 --- a/src/mlpack/bindings/markdown/md_option.hpp +++ b/src/mlpack/bindings/markdown/md_option.hpp @@ -63,7 +63,7 @@ class MDOption data.cppType = cppName; // Every parameter we'll get from Markdown will have the correct type. - data.value = ANY(defaultValue); + data.value = defaultValue; // Set the function pointers that we'll need. Most of these simply delegate // to the current binding type's implementation. Any new language will need diff --git a/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp b/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp index e298d6088e..38c5037a06 100644 --- a/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp +++ b/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp @@ -263,7 +263,7 @@ inline std::string PrintTypeDocs() data.required = false; data.input = true; data.loaded = false; - data.value = ANY(int(0)); + data.value = MLPACK_ANY(int(0)); std::string type = GetPrintableType(data); oss << " - `" << type << "`{: #doc_" << BindingInfo::Language() << "_" @@ -271,7 +271,7 @@ inline std::string PrintTypeDocs() data.tname = std::string(typeid(double).name()); data.cppType = "double"; - data.value = ANY(double(0.0)); + data.value = MLPACK_ANY(double(0.0)); type = GetPrintableType(data); oss << " - `" << type << "`{: #doc_" << BindingInfo::Language() << "_" @@ -280,7 +280,7 @@ inline std::string PrintTypeDocs() data.tname = std::string(typeid(bool).name()); data.cppType = "double"; - data.value = ANY(bool(0.0)); + data.value = MLPACK_ANY(bool(0.0)); type = GetPrintableType(data); oss << " - `" << type << "`{: #doc_" << BindingInfo::Language() << "_" @@ -288,7 +288,7 @@ inline std::string PrintTypeDocs() data.tname = std::string(typeid(std::string).name()); data.cppType = "std::string"; - data.value = ANY(std::string("")); + data.value = MLPACK_ANY(std::string("")); type = GetPrintableType(data); oss << " - `" << type << "`{: #doc_" << BindingInfo::Language() << "_" @@ -297,7 +297,7 @@ inline std::string PrintTypeDocs() data.tname = std::string(typeid(std::vector).name()); data.cppType = "std::vector"; - data.value = ANY(std::vector()); + data.value = MLPACK_ANY(std::vector()); type = GetPrintableType>(data); oss << " - `" << type << "`{: #doc_" << BindingInfo::Language() << "_" @@ -306,7 +306,7 @@ inline std::string PrintTypeDocs() data.tname = std::string(typeid(std::vector).name()); data.cppType = "std::vector"; - data.value = ANY(std::vector()); + data.value = MLPACK_ANY(std::vector()); type = GetPrintableType>(data); oss << " - `" << type << "`{: " << "#doc_" << BindingInfo::Language() << "_" @@ -315,7 +315,7 @@ inline std::string PrintTypeDocs() data.tname = std::string(typeid(arma::mat).name()); data.cppType = "arma::mat"; - data.value = ANY(arma::mat()); + data.value = MLPACK_ANY(arma::mat()); type = GetPrintableType(data); oss << " - `" << type << "`{: #doc_" << BindingInfo::Language() << "_" @@ -324,7 +324,7 @@ inline std::string PrintTypeDocs() data.tname = std::string(typeid(arma::Mat).name()); data.cppType = "arma::Mat"; - data.value = ANY(arma::Mat()); + data.value = MLPACK_ANY(arma::Mat()); type = GetPrintableType>(data); oss << " - `" << type << "`{: #doc_" << BindingInfo::Language() << "_" @@ -333,7 +333,7 @@ inline std::string PrintTypeDocs() data.tname = std::string(typeid(arma::rowvec).name()); data.cppType = "arma::rowvec"; - data.value = ANY(arma::rowvec()); + data.value = MLPACK_ANY(arma::rowvec()); const std::string& rowType = GetPrintableType(data); oss << " - `" << rowType << "`{: #doc_" << BindingInfo::Language() << "_" @@ -342,7 +342,7 @@ inline std::string PrintTypeDocs() data.tname = std::string(typeid(arma::Row).name()); data.cppType = "arma::Row"; - data.value = ANY(arma::Row()); + data.value = MLPACK_ANY(arma::Row()); const std::string& urowType = GetPrintableType>(data); oss << " - `" << urowType << "`{: #doc_" << BindingInfo::Language() << "_" @@ -352,7 +352,7 @@ inline std::string PrintTypeDocs() data.tname = std::string(typeid(arma::vec).name()); data.cppType = "arma::vec"; - data.value = ANY(arma::vec()); + data.value = MLPACK_ANY(arma::vec()); const std::string& colType = GetPrintableType(data); // For some languages there is no distinction between column and row vectors. @@ -366,7 +366,7 @@ inline std::string PrintTypeDocs() data.tname = std::string(typeid(arma::Col).name()); data.cppType = "arma::Col"; - data.value = ANY(arma::Col()); + data.value = MLPACK_ANY(arma::Col()); const std::string& ucolType = GetPrintableType>(data); // For some languages there is no distinction between column and row vectors. @@ -381,7 +381,7 @@ inline std::string PrintTypeDocs() data.tname = std::string(typeid(std::tuple).name()); data.cppType = "std::tuple"; - data.value = ANY(std::tuple()); + data.value = MLPACK_ANY(std::tuple()); type = GetPrintableType>(data); oss << " - `" << type << "`{: #doc_" << BindingInfo::Language() << "_" @@ -391,14 +391,14 @@ inline std::string PrintTypeDocs() data.tname = std::string(typeid(priv::mlpackModel).name()); data.cppType = "mlpackModel"; - data.value = ANY(new priv::mlpackModel()); + data.value = MLPACK_ANY(new priv::mlpackModel()); type = GetPrintableType(data); oss << " - `" << type << "`{: #doc_" << BindingInfo::Language() << "_model }: " << PrintTypeDoc(data) << std::endl; // Clean up memory. - delete ANY_CAST(data.value); + delete MLPACK_ANY_CAST(data.value); oss << std::endl << "" << std::endl; diff --git a/src/mlpack/bindings/python/default_param_impl.hpp b/src/mlpack/bindings/python/default_param_impl.hpp index b370a97fda..d032f4fa52 100644 --- a/src/mlpack/bindings/python/default_param_impl.hpp +++ b/src/mlpack/bindings/python/default_param_impl.hpp @@ -36,7 +36,7 @@ std::string DefaultParamImpl( if (std::is_same::value) oss << "False"; else - oss << ANY_CAST(data.value); + oss << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -51,7 +51,7 @@ std::string DefaultParamImpl( { // Print each element in an array delimited by square brackets. std::ostringstream oss; - const T& vector = ANY_CAST(data.value); + const T& vector = MLPACK_ANY_CAST(data.value); oss << "["; if (std::is_same>::value) { @@ -92,7 +92,7 @@ std::string DefaultParamImpl( util::ParamData& data, const typename std::enable_if::value>::type*) { - const std::string& s = *ANY_CAST(&data.value); + const std::string& s = *MLPACK_ANY_CAST(&data.value); return "'" + s + "'"; } diff --git a/src/mlpack/bindings/python/get_param.hpp b/src/mlpack/bindings/python/get_param.hpp index 8f9657ea53..7cbfb947c4 100644 --- a/src/mlpack/bindings/python/get_param.hpp +++ b/src/mlpack/bindings/python/get_param.hpp @@ -27,7 +27,7 @@ void GetParam(util::ParamData& d, const void* /* input */, void* output) { - *((T**) output) = const_cast(ANY_CAST(&d.value)); + *((T**) output) = const_cast(MLPACK_ANY_CAST(&d.value)); } } // namespace python diff --git a/src/mlpack/bindings/python/get_printable_param.hpp b/src/mlpack/bindings/python/get_printable_param.hpp index b9c7993365..1a67510f2a 100644 --- a/src/mlpack/bindings/python/get_printable_param.hpp +++ b/src/mlpack/bindings/python/get_printable_param.hpp @@ -32,7 +32,7 @@ std::string GetPrintableParam( std::tuple>::value>::type* = 0) { std::ostringstream oss; - oss << ANY_CAST(data.value); + oss << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -44,7 +44,7 @@ std::string GetPrintableParam( util::ParamData& data, const typename std::enable_if::value>::type* = 0) { - const T& t = ANY_CAST(data.value); + const T& t = MLPACK_ANY_CAST(data.value); std::ostringstream oss; for (size_t i = 0; i < t.size(); ++i) @@ -61,7 +61,7 @@ std::string GetPrintableParam( const typename std::enable_if::value>::type* = 0) { // Get the matrix. - const T& matrix = ANY_CAST(data.value); + const T& matrix = MLPACK_ANY_CAST(data.value); std::ostringstream oss; oss << matrix.n_rows << "x" << matrix.n_cols << " matrix"; @@ -78,7 +78,7 @@ std::string GetPrintableParam( const typename std::enable_if::value>::type* = 0) { std::ostringstream oss; - oss << data.cppType << " model at " << ANY_CAST(data.value); + oss << data.cppType << " model at " << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -92,7 +92,7 @@ std::string GetPrintableParam( std::tuple>::value>::type* = 0) { // Get the matrix. - const T& tuple = ANY_CAST(data.value); + const T& tuple = MLPACK_ANY_CAST(data.value); const arma::mat& matrix = std::get<1>(tuple); std::ostringstream oss; diff --git a/src/mlpack/bindings/python/py_option.hpp b/src/mlpack/bindings/python/py_option.hpp index d529d85ea5..ac6badfc9f 100644 --- a/src/mlpack/bindings/python/py_option.hpp +++ b/src/mlpack/bindings/python/py_option.hpp @@ -64,7 +64,7 @@ class PyOption data.cppType = cppName; // Every parameter we'll get from Python will have the correct type. - data.value = ANY(defaultValue); + data.value = defaultValue; // Set the function pointers that we'll need. All of these function // pointers will be used by both the program that generates the pyx, and diff --git a/src/mlpack/bindings/python/setup.py.in b/src/mlpack/bindings/python/setup.py.in index 0a0efd8318..492a89183f 100644 --- a/src/mlpack/bindings/python/setup.py.in +++ b/src/mlpack/bindings/python/setup.py.in @@ -52,7 +52,7 @@ if os.getenv('NO_BUILD') == '1': else: cxx_flags = '${CMAKE_CXX_FLAGS}'.strip() cxx_flags = re.sub(' +', ' ', cxx_flags) - extra_args = ['-DBINDING_TYPE=BINDING_TYPE_PYX', '-std=c++11'] + extra_args = ['-DBINDING_TYPE=BINDING_TYPE_PYX', '-std=c++14'] if '${OpenMP_CXX_FLAGS}' != '': extra_args.append('${OpenMP_CXX_FLAGS}') if cxx_flags: diff --git a/src/mlpack/bindings/tests/CMakeLists.txt b/src/mlpack/bindings/tests/CMakeLists.txt index 7e87aa4bfc..6845830e49 100644 --- a/src/mlpack/bindings/tests/CMakeLists.txt +++ b/src/mlpack/bindings/tests/CMakeLists.txt @@ -1,6 +1,6 @@ -# Define the files we need to compile. -# Anything not in this list will not be compiled into mlpack. -set(SOURCES +# Define the files we need to compile into the test executable. +# Anything not in this list will not be compiled into mlpack_test. +set(BINDING_SOURCES clean_memory.hpp clean_memory.cpp test_option.hpp @@ -17,9 +17,9 @@ set(SOURCES # Add directory name to sources. set(DIR_SRCS) -foreach(file ${SOURCES}) +foreach(file ${BINDING_SOURCES}) set(DIR_SRCS ${DIR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/${file}) endforeach() # Append source (with directory name) to list of all mlpack sources (used at the # parent scope). -set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE) +set(MLPACK_TEST_SRCS ${MLPACK_TEST_SRCS} ${DIR_SRCS} PARENT_SCOPE) diff --git a/src/mlpack/bindings/tests/delete_allocated_memory.hpp b/src/mlpack/bindings/tests/delete_allocated_memory.hpp index 99d6ea89c4..cdc477b8df 100644 --- a/src/mlpack/bindings/tests/delete_allocated_memory.hpp +++ b/src/mlpack/bindings/tests/delete_allocated_memory.hpp @@ -42,7 +42,7 @@ void DeleteAllocatedMemoryImpl( const typename std::enable_if::value>::type* = 0) { // Delete the allocated memory (hopefully we actually own it). - delete *ANY_CAST(&d.value); + delete *MLPACK_ANY_CAST(&d.value); } template diff --git a/src/mlpack/bindings/tests/get_allocated_memory.hpp b/src/mlpack/bindings/tests/get_allocated_memory.hpp index 4776e9ddb6..f32d0a01f4 100644 --- a/src/mlpack/bindings/tests/get_allocated_memory.hpp +++ b/src/mlpack/bindings/tests/get_allocated_memory.hpp @@ -43,7 +43,7 @@ void* GetAllocatedMemory( const typename std::enable_if::value>::type* = 0) { // Here we have a model; return its memory location. - return *ANY_CAST(&d.value); + return *MLPACK_ANY_CAST(&d.value); } template diff --git a/src/mlpack/bindings/tests/get_param.hpp b/src/mlpack/bindings/tests/get_param.hpp index 6838b2bbc2..715dadc719 100644 --- a/src/mlpack/bindings/tests/get_param.hpp +++ b/src/mlpack/bindings/tests/get_param.hpp @@ -26,7 +26,7 @@ template T& GetParam(util::ParamData& d) { // No mapping is needed, so just cast it directly. - return *ANY_CAST(&d.value); + return *MLPACK_ANY_CAST(&d.value); } /** diff --git a/src/mlpack/bindings/tests/get_printable_param_impl.hpp b/src/mlpack/bindings/tests/get_printable_param_impl.hpp index 23d1820296..ff86de230c 100644 --- a/src/mlpack/bindings/tests/get_printable_param_impl.hpp +++ b/src/mlpack/bindings/tests/get_printable_param_impl.hpp @@ -29,7 +29,7 @@ std::string GetPrintableParam( std::tuple>::value>::type* /* junk */) { std::ostringstream oss; - oss << ANY_CAST(data.value); + oss << MLPACK_ANY_CAST(data.value); return oss.str(); } @@ -39,7 +39,7 @@ std::string GetPrintableParam( util::ParamData& data, const typename std::enable_if::value>::type* /* junk */) { - const T& t = ANY_CAST(data.value); + const T& t = MLPACK_ANY_CAST(data.value); std::ostringstream oss; for (size_t i = 0; i < t.size(); ++i) diff --git a/src/mlpack/bindings/tests/test_option.hpp b/src/mlpack/bindings/tests/test_option.hpp index 8f4bf87c23..28681c7fc3 100644 --- a/src/mlpack/bindings/tests/test_option.hpp +++ b/src/mlpack/bindings/tests/test_option.hpp @@ -83,7 +83,7 @@ class TestOption data.input = input; data.loaded = false; data.cppType = cppName; - data.value = ANY(defaultValue); + data.value = defaultValue; const std::string tname = data.tname; diff --git a/src/mlpack/core.hpp b/src/mlpack/core.hpp index a2275ad188..603ff7bcb9 100644 --- a/src/mlpack/core.hpp +++ b/src/mlpack/core.hpp @@ -111,8 +111,8 @@ #include #include -// Use OpenMP if compiled with -DHAS_OPENMP. -#ifdef HAS_OPENMP +// Use OpenMP if available. +#ifdef MLPACK_USE_OPENMP #include #endif diff --git a/src/mlpack/core/data/CMakeLists.txt b/src/mlpack/core/data/CMakeLists.txt index 0b83444c00..38a9b6ec61 100644 --- a/src/mlpack/core/data/CMakeLists.txt +++ b/src/mlpack/core/data/CMakeLists.txt @@ -1,6 +1,7 @@ # Define the files that we need to compile. # Anything not in this list will not be compiled into mlpack. set(SOURCES + check_categorical_param.hpp dataset_mapper.hpp dataset_mapper_impl.hpp detect_file_type.hpp diff --git a/src/mlpack/core/data/binarize.hpp b/src/mlpack/core/data/binarize.hpp index 6602e2d451..c0c87cf4e1 100644 --- a/src/mlpack/core/data/binarize.hpp +++ b/src/mlpack/core/data/binarize.hpp @@ -48,7 +48,7 @@ void Binarize(const arma::Mat& input, T *outPtr = output.memptr(); #pragma omp parallel for - for (omp_size_t i = 0; i < (omp_size_t) input.n_elem; ++i) + for (size_t i = 0; i < (size_t) input.n_elem; ++i) outPtr[i] = inPtr[i] > threshold; } @@ -82,7 +82,7 @@ void Binarize(const arma::Mat& input, output = input; #pragma omp parallel for - for (omp_size_t i = 0; i < (omp_size_t) input.n_cols; ++i) + for (size_t i = 0; i < (size_t) input.n_cols; ++i) output(dimension, i) = input(dimension, i) > threshold; } diff --git a/src/mlpack/core/data/check_categorical_param.hpp b/src/mlpack/core/data/check_categorical_param.hpp new file mode 100644 index 0000000000..128ed9dbed --- /dev/null +++ b/src/mlpack/core/data/check_categorical_param.hpp @@ -0,0 +1,36 @@ +/** + * @file core/data/check_categorical_param.hpp + * @author Ryan Curtin + * + * This file provides an implementation of a simple function to check the values + * of a categorical parameter. It cannot be defined in util/, since the + * DatasetMapper class is not fully defined when that is included.. + */ +#ifndef MLPACK_CORE_DATA_CHECK_CATEGORICAL_PARAM_HPP +#define MLPACK_CORE_DATA_CHECK_CATEGORICAL_PARAM_HPP + +#include + +namespace mlpack { +namespace data { + +inline void CheckCategoricalParam(util::Params& params, + const std::string& paramName) +{ + typedef typename std::tuple TupleType; + arma::mat& matrix = std::get<1>(params.Get(paramName)); + + // This comes from Params::CheckInputMatrix(). + const std::string errMsg1 = "The input '" + paramName + "' has NaN values."; + const std::string errMsg2 = "The input '" + paramName + "' has Inf values."; + + if (matrix.has_nan()) + Log::Fatal << errMsg1 << std::endl; + if (matrix.has_inf()) + Log::Fatal << errMsg2 << std::endl; +} + +} // namespace data +} // namespace mlpack + +#endif diff --git a/src/mlpack/core/data/dataset_mapper.hpp b/src/mlpack/core/data/dataset_mapper.hpp index ec959e70b5..893385e0cf 100644 --- a/src/mlpack/core/data/dataset_mapper.hpp +++ b/src/mlpack/core/data/dataset_mapper.hpp @@ -200,4 +200,7 @@ using DatasetInfo = DatasetMapper; #include "dataset_mapper_impl.hpp" +// Also include utility function. +#include "check_categorical_param.hpp" + #endif diff --git a/src/mlpack/core/data/load.hpp b/src/mlpack/core/data/load.hpp index 195838affe..c237c5cc00 100644 --- a/src/mlpack/core/data/load.hpp +++ b/src/mlpack/core/data/load.hpp @@ -15,7 +15,6 @@ #define MLPACK_CORE_DATA_LOAD_HPP #include -#include #include #include "format.hpp" diff --git a/src/mlpack/core/data/load_impl.hpp b/src/mlpack/core/data/load_impl.hpp index 03993716db..5e8b7a349d 100644 --- a/src/mlpack/core/data/load_impl.hpp +++ b/src/mlpack/core/data/load_impl.hpp @@ -18,7 +18,6 @@ #include #include -#include #include "extension.hpp" #include "detect_file_type.hpp" diff --git a/src/mlpack/core/data/string_encoding.hpp b/src/mlpack/core/data/string_encoding.hpp index eba8c87849..58e4d2d09d 100644 --- a/src/mlpack/core/data/string_encoding.hpp +++ b/src/mlpack/core/data/string_encoding.hpp @@ -74,7 +74,7 @@ class StringEncoding * @param tokenizer The tokenizer object. * * The tokenization algorithm has to be an object with two public methods: - * 1. operator() which accepts a reference to STRING_VIEW, extracts + * 1. operator() which accepts a reference to MLPACK_STRING_VIEW, extracts * the next token from the given view, removes the prefix containing * the extracted token and returns the token; * 2. IsTokenEmpty() that accepts a token and returns true if the given @@ -108,7 +108,7 @@ class StringEncoding * @param tokenizer The tokenizer object. * * The tokenization algorithm has to be an object with two public methods: - * 1. operator() which accepts a reference to STRING_VIEW, extracts + * 1. operator() which accepts a reference to MLPACK_STRING_VIEW, extracts * the next token from the given view, removes the prefix containing * the extracted token and returns the token; * 2. IsTokenEmpty() that accepts a token and returns true if the given @@ -158,7 +158,7 @@ class StringEncoding * @param policy The policy object. * * The tokenization algorithm has to be an object with two public methods: - * 1. operator() which accepts a reference to STRING_VIEW, extracts + * 1. operator() which accepts a reference to MLPACK_STRING_VIEW, extracts * the next token from the given view, removes the prefix containing * the extracted token and returns the token; * 2. IsTokenEmpty() that accepts a token and returns true if the given @@ -187,7 +187,7 @@ class StringEncoding * @param policy The policy object. * * The tokenization algorithm has to be an object with two public methods: - * 1. operator() which accepts a reference to STRING_VIEW, extracts + * 1. operator() which accepts a reference to MLPACK_STRING_VIEW, extracts * the next token from the given view, removes the prefix containing * the extracted token and returns the token; * 2. IsTokenEmpty() that accepts a token and returns true if the given diff --git a/src/mlpack/core/data/string_encoding_dictionary.hpp b/src/mlpack/core/data/string_encoding_dictionary.hpp index ea429afc01..54ad5f5703 100644 --- a/src/mlpack/core/data/string_encoding_dictionary.hpp +++ b/src/mlpack/core/data/string_encoding_dictionary.hpp @@ -106,20 +106,20 @@ class StringEncodingDictionary }; /* - * Specialization of the StringEncodingDictionary class for STRING_VIEW. + * Specialization of the StringEncodingDictionary class for MLPACK_STRING_VIEW. */ template<> -class StringEncodingDictionary +class StringEncodingDictionary { public: //! A convenient alias for the internal type of the map. using MapType = std::unordered_map< - STRING_VIEW, + MLPACK_STRING_VIEW, size_t, - std::hash>; + std::hash>; //! The type of the token that the dictionary stores. - using TokenType = STRING_VIEW; + using TokenType = MLPACK_STRING_VIEW; //! Construct the default class. StringEncodingDictionary() = default; @@ -156,7 +156,7 @@ class StringEncodingDictionary * * @param token The given token. */ - bool HasToken(const STRING_VIEW token) const + bool HasToken(const MLPACK_STRING_VIEW token) const { return mapping.find(token) != mapping.end(); } @@ -168,7 +168,7 @@ class StringEncodingDictionary * * @param token The given token. */ - size_t AddToken(const STRING_VIEW token) + size_t AddToken(const MLPACK_STRING_VIEW token) { tokens.emplace_back(token); @@ -185,7 +185,7 @@ class StringEncodingDictionary * * @param token The given token. */ - size_t Value(const STRING_VIEW token) const + size_t Value(const MLPACK_STRING_VIEW token) const { return mapping.at(token); } diff --git a/src/mlpack/core/data/string_encoding_impl.hpp b/src/mlpack/core/data/string_encoding_impl.hpp index 06558960e9..71efb0e550 100644 --- a/src/mlpack/core/data/string_encoding_impl.hpp +++ b/src/mlpack/core/data/string_encoding_impl.hpp @@ -66,7 +66,7 @@ void StringEncoding::CreateMap( const std::string& input, const TokenizerType& tokenizer) { - STRING_VIEW strView(input); + MLPACK_STRING_VIEW strView(input); auto token = tokenizer(strView); static_assert( @@ -112,7 +112,7 @@ EncodeHelper(const std::vector& input, // The first pass adds the extracted tokens to the dictionary. for (size_t i = 0; i < input.size(); ++i) { - STRING_VIEW strView(input[i]); + MLPACK_STRING_VIEW strView(input[i]); auto token = tokenizer(strView); static_assert( @@ -143,7 +143,7 @@ EncodeHelper(const std::vector& input, // The second pass writes the encoded values to the output. for (size_t i = 0; i < input.size(); ++i) { - STRING_VIEW strView(input[i]); + MLPACK_STRING_VIEW strView(input[i]); auto token = tokenizer(strView); size_t numTokens = 0; @@ -172,7 +172,7 @@ EncodeHelper(const std::vector& input, // at once. for (size_t i = 0; i < input.size(); ++i) { - STRING_VIEW strView(input[i]); + MLPACK_STRING_VIEW strView(input[i]); auto token = tokenizer(strView); static_assert( diff --git a/src/mlpack/core/data/tokenizers/char_extract.hpp b/src/mlpack/core/data/tokenizers/char_extract.hpp index 084e5e8631..c6912e099b 100644 --- a/src/mlpack/core/data/tokenizers/char_extract.hpp +++ b/src/mlpack/core/data/tokenizers/char_extract.hpp @@ -36,7 +36,7 @@ class CharExtract * * @param str String view to retrieve the next token from. */ - int operator()(STRING_VIEW& str) const + int operator()(MLPACK_STRING_VIEW& str) const { if (str.empty()) return EOF; diff --git a/src/mlpack/core/data/tokenizers/split_by_any_of.hpp b/src/mlpack/core/data/tokenizers/split_by_any_of.hpp index 9097c34f76..d1b8d5e2c9 100644 --- a/src/mlpack/core/data/tokenizers/split_by_any_of.hpp +++ b/src/mlpack/core/data/tokenizers/split_by_any_of.hpp @@ -27,7 +27,7 @@ class SplitByAnyOf { public: //! The type of the token which the tokenizer extracts. - using TokenType = STRING_VIEW; + using TokenType = MLPACK_STRING_VIEW; //! A convenient alias for the mask type. using MaskType = std::array; @@ -37,7 +37,7 @@ class SplitByAnyOf * * @param delimiters The given delimiters. */ - SplitByAnyOf(const STRING_VIEW delimiters) + SplitByAnyOf(const MLPACK_STRING_VIEW delimiters) { mask.fill(false); @@ -51,13 +51,13 @@ class SplitByAnyOf * * @param str String view to retrieve the token from. */ - STRING_VIEW operator()(STRING_VIEW& str) const + MLPACK_STRING_VIEW operator()(MLPACK_STRING_VIEW& str) const { - STRING_VIEW retval; + MLPACK_STRING_VIEW retval; // std::basic_string_view does not have empty function. // Therefore, we are assiging an empty string when reaching the last // delimiter. - STRING_VIEW empty_string{""}; + MLPACK_STRING_VIEW empty_string{""}; while (retval.empty()) { @@ -79,7 +79,7 @@ class SplitByAnyOf * * @param token The given token. */ - static bool IsTokenEmpty(const STRING_VIEW token) + static bool IsTokenEmpty(const MLPACK_STRING_VIEW token) { return token.empty(); } @@ -93,11 +93,11 @@ class SplitByAnyOf /** * The function finds the first character in the given string view equal to * any of the delimiters and returns the position of the character or - * STRING_VIEW::npos if no such character is found. + * MLPACK_STRING_VIEW::npos if no such character is found. * * @param str String where to find the character. */ - size_t FindFirstDelimiter(const STRING_VIEW str) const + size_t FindFirstDelimiter(const MLPACK_STRING_VIEW str) const { for (size_t pos = 0; pos < str.size(); pos++) { diff --git a/src/mlpack/core/util/CMakeLists.txt b/src/mlpack/core/util/CMakeLists.txt index 4ffe767d47..1cc1dfa9b9 100644 --- a/src/mlpack/core/util/CMakeLists.txt +++ b/src/mlpack/core/util/CMakeLists.txt @@ -5,15 +5,16 @@ set(SOURCES arma_config.hpp arma_config_check.hpp backtrace.hpp - backtrace.cpp + backtrace_impl.hpp binding_details.hpp + forward.hpp io.hpp - io.cpp + io_impl.hpp deprecated.hpp hyphenate_string.hpp is_std_vector.hpp log.hpp - log.cpp + log_impl.hpp mlpack_main.hpp nulloutstream.hpp param.hpp @@ -22,20 +23,17 @@ set(SOURCES param_data.hpp params.hpp params_impl.hpp - params.cpp prefixedoutstream.hpp - prefixedoutstream.cpp prefixedoutstream_impl.hpp program_doc.hpp - program_doc.cpp + program_doc_impl.hpp size_checks.hpp sfinae_utility.hpp - singletons.cpp timers.hpp - timers.cpp + timers_impl.hpp to_lower.hpp version.hpp - version.cpp + version_impl.hpp ) # add directory name to sources diff --git a/src/mlpack/core/util/backtrace.hpp b/src/mlpack/core/util/backtrace.hpp index 181240f8f2..ed58a839b7 100644 --- a/src/mlpack/core/util/backtrace.hpp +++ b/src/mlpack/core/util/backtrace.hpp @@ -15,6 +15,37 @@ #include #include +#ifdef HAS_BFD_DL + #include + #include + #include + #include + + // Some versions of libbfd require PACKAGE and PACKAGE_VERSION to be set in + // order for the include to not fail. For more information: + // https://github.com/mlpack/mlpack/issues/574 + #ifndef PACKAGE + #define PACKAGE + #ifndef PACKAGE_VERSION + #define PACKAGE_VERSION + #include + #undef PACKAGE_VERSION + #else + #include + #endif + #undef PACKAGE + #else + #ifndef PACKAGE_VERSION + #define PACKAGE_VERSION + #include + #undef PACKAGE_VERSION + #else + #include + #endif + #endif + #include +#endif + namespace mlpack { /** @@ -71,17 +102,17 @@ class Backtrace * * @param maxDepth Maximum depth of backtrace. Default 32 steps. */ - static void GetAddress(int maxDepth); + void GetAddress(int maxDepth); /** * Decodes file name, function & line number. * * @param address Address of traced frame. */ - static void DecodeAddress(long address); + void DecodeAddress(long address); //! Demangles function name. - static void DemangleFunction(); + void DemangleFunction(); //! Backtrace datastructure. struct Frames @@ -90,12 +121,22 @@ class Backtrace const char* function; const char* file; unsigned line; - } static frame; + }; - //! A vector for all the backtrace information. - static std::vector stack; + Frames frame; + std::vector stack; + +#ifdef HAS_BFD_DL + // Binary File Descriptor objects. + bfd* abfd; // Descriptor datastructure. + asymbol **syms; // Symbols datastructure. + asection *text; // Strings datastructure. +#endif }; }; // namespace mlpack +// Include implementation. +#include "backtrace_impl.hpp" + #endif diff --git a/src/mlpack/core/util/backtrace.cpp b/src/mlpack/core/util/backtrace_impl.hpp similarity index 66% rename from src/mlpack/core/util/backtrace.cpp rename to src/mlpack/core/util/backtrace_impl.hpp index 3bdf7c4f29..a3eea2bf7f 100644 --- a/src/mlpack/core/util/backtrace.cpp +++ b/src/mlpack/core/util/backtrace_impl.hpp @@ -1,5 +1,5 @@ /** - * @file core/util/backtrace.cpp + * @file core/util/backtrace_impl.hpp * @author Grzegorz Krajewski * * Implementation of the Backtrace class. @@ -11,74 +11,36 @@ */ #include -#ifdef HAS_BFD_DL - #include - #include - #include - #include - - // Some versions of libbfd require PACKAGE and PACKAGE_VERSION to be set in - // order for the include to not fail. For more information: - // https://github.com/mlpack/mlpack/issues/574 - #ifndef PACKAGE - #define PACKAGE - #ifndef PACKAGE_VERSION - #define PACKAGE_VERSION - #include - #undef PACKAGE_VERSION - #else - #include - #endif - #undef PACKAGE - #else - #ifndef PACKAGE_VERSION - #define PACKAGE_VERSION - #include - #undef PACKAGE_VERSION - #else - #include - #endif - #endif - #include -#endif - #include "backtrace.hpp" #include "log.hpp" -using namespace mlpack; - -// Initialize Backtrace static inctances. -Backtrace::Frames Backtrace::frame; -std::vector Backtrace::stack; +namespace mlpack { #ifdef HAS_BFD_DL -// Binary File Descriptor objects. -bfd* abfd = 0; // Descriptor datastructure. -asymbol **syms = 0; // Symbols datastructure. -asection *text = 0; // Strings datastructure. -#endif - -#ifdef HAS_BFD_DL -Backtrace::Backtrace(int maxDepth) +inline Backtrace::Backtrace(int maxDepth) { frame.address = NULL; frame.function = "0"; frame.file = "0"; frame.line = 0; + abfd = 0; + syms = 0; + text = 0; + stack.clear(); GetAddress(maxDepth); } #else -Backtrace::Backtrace() +inline Backtrace::Backtrace() { // Dummy constructor } #endif #ifdef HAS_BFD_DL -void Backtrace::GetAddress(int maxDepth) +inline void Backtrace::GetAddress(int maxDepth) { void* trace[maxDepth]; int stackDepth = backtrace(trace, maxDepth); @@ -100,7 +62,7 @@ void Backtrace::GetAddress(int maxDepth) } } -void Backtrace::DecodeAddress(long addr) +inline void Backtrace::DecodeAddress(long addr) { // Check to see if there is anything to descript. If it doesn't, we'll // dump running program. @@ -147,7 +109,7 @@ void Backtrace::DecodeAddress(long addr) } } -void Backtrace::DemangleFunction() +inline void Backtrace::DemangleFunction() { int status; char* tmp = abi::__cxa_demangle(frame.function, 0, 0, &status); @@ -160,12 +122,12 @@ void Backtrace::DemangleFunction() } } #else -void Backtrace::GetAddress(int /* maxDepth */) { } -void Backtrace::DecodeAddress(long /* address */) { } -void Backtrace::DemangleFunction() { } +inline void Backtrace::GetAddress(int /* maxDepth */) { } +inline void Backtrace::DecodeAddress(long /* address */) { } +inline void Backtrace::DemangleFunction() { } #endif -std::string Backtrace::ToString() +inline std::string Backtrace::ToString() { std::string stackStr; @@ -198,8 +160,10 @@ std::string Backtrace::ToString() it.str(""); } #else - stackStr = "[bt]: No backtrace for this OS. Work in progress."; + stackStr = "[bt]: No backtrace for this OS."; #endif return stackStr; } + +} // namespace mlpack diff --git a/src/mlpack/core/util/binding_details.hpp b/src/mlpack/core/util/binding_details.hpp index 2320aed8cb..f5d0bc30f1 100644 --- a/src/mlpack/core/util/binding_details.hpp +++ b/src/mlpack/core/util/binding_details.hpp @@ -12,8 +12,7 @@ #ifndef MLPACK_CORE_UTIL_BINDING_DETAILS_HPP #define MLPACK_CORE_UTIL_BINDING_DETAILS_HPP -#include -#include "program_doc.hpp" +#include namespace mlpack { namespace util { diff --git a/src/mlpack/core/util/forward.hpp b/src/mlpack/core/util/forward.hpp new file mode 100644 index 0000000000..f25e3fb8f1 --- /dev/null +++ b/src/mlpack/core/util/forward.hpp @@ -0,0 +1,45 @@ +/** + * @file core/util/forward.hpp + * @author Ryan Curtin + * + * Forward declaration of components from other subdirectories necessary for + * various util implementations. + */ +#ifndef MLPACK_CORE_UTIL_FORWARD_HPP +#define MLPACK_CORE_UTIL_FORWARD_HPP + +#include + +// Required forward declarations. +namespace mlpack { + +class IO; + +namespace util { + +class Timers; + +} // namespace util +} + +#include "params.hpp" + +namespace mlpack { +namespace data { + +class IncrementPolicy; + +template +class DatasetMapper; + +using DatasetInfo = DatasetMapper; + +// This is a forward declaration of a function that just calls std::get(); but, +// we cannot use std::get directly because we have only forward-declared +// DatasetInfo. +void CheckCategoricalParam(util::Params& p, const std::string& paramName); + +} // namespace data +} // namespace mlpack + +#endif diff --git a/src/mlpack/core/util/io.hpp b/src/mlpack/core/util/io.hpp index 0b45563b6c..f15d6f067a 100644 --- a/src/mlpack/core/util/io.hpp +++ b/src/mlpack/core/util/io.hpp @@ -22,11 +22,11 @@ #include "timers.hpp" #include "binding_details.hpp" -#include "program_doc.hpp" #include "version.hpp" #include "param_data.hpp" #include "params.hpp" +#include "params_impl.hpp" #include #include @@ -305,4 +305,13 @@ class IO } // namespace mlpack +// This file must be included after IO is declared and fully defined. +#include "program_doc.hpp" + +// Include the implementation. +#include "io_impl.hpp" + +// Now include the implementation of the timers. +#include "timers_impl.hpp" + #endif diff --git a/src/mlpack/core/util/io.cpp b/src/mlpack/core/util/io_impl.hpp similarity index 79% rename from src/mlpack/core/util/io.cpp rename to src/mlpack/core/util/io_impl.hpp index cb2c39625d..798f34d65b 100644 --- a/src/mlpack/core/util/io.cpp +++ b/src/mlpack/core/util/io_impl.hpp @@ -1,5 +1,5 @@ /** - * @file core/util/io.cpp + * @file core/util/io_impl.hpp * @author Matthew Amidon * * Implementation of the IO module for parsing parameters. @@ -9,31 +9,33 @@ * 3-clause BSD license along with mlpack. If not, see * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ +#ifndef MLPACK_CORE_UTIL_IO_IMPL_HPP +#define MLPACK_CORE_UTIL_IO_IMPL_HPP #include "io.hpp" #include "log.hpp" #include "hyphenate_string.hpp" -using namespace mlpack; -using namespace mlpack::util; +namespace mlpack { /* Constructors, Destructors, Copy */ /* Make the constructor private, to preclude unauthorized instances */ -IO::IO() +inline IO::IO() { return; } // Private copy constructor; don't want copies floating around. -IO::IO(const IO& /* other */) +inline IO::IO(const IO& /* other */) { return; } // Private copy operator; don't want copies floating around. -IO& IO::operator=(const IO& /* other */) { return *this; } +inline IO& IO::operator=(const IO& /* other */) { return *this; } -void IO::AddParameter(const std::string& bindingName, ParamData&& data) +inline void IO::AddParameter(const std::string& bindingName, + util::ParamData&& data) { // Temporarily define color code escape sequences. #ifndef _WIN32 @@ -93,9 +95,9 @@ void IO::AddParameter(const std::string& bindingName, ParamData&& data) * @param name Name of the function. * @param func Function to call. */ -void IO::AddFunction(const std::string& type, - const std::string& name, - void (*func)(util::ParamData&, const void*, void*)) +inline void IO::AddFunction(const std::string& type, + const std::string& name, + void (*func)(util::ParamData&, const void*, void*)) { std::lock_guard lock(GetSingleton().mapMutex); GetSingleton().functionMap[type][name] = func; @@ -107,7 +109,8 @@ void IO::AddFunction(const std::string& type, * @param bindingName Name of the binding to add the user-friendly name for. * @param name User-friendly name. */ -void IO::AddBindingName(const std::string& bindingName, const std::string& name) +inline void IO::AddBindingName(const std::string& bindingName, + const std::string& name) { std::lock_guard lock(GetSingleton().mapMutex); GetSingleton().docs[bindingName].name = name; @@ -119,8 +122,8 @@ void IO::AddBindingName(const std::string& bindingName, const std::string& name) * @param bindingName Name of the binding to add the description for. * @param shortDescription Description to use. */ -void IO::AddShortDescription(const std::string& bindingName, - const std::string& shortDescription) +inline void IO::AddShortDescription(const std::string& bindingName, + const std::string& shortDescription) { std::lock_guard lock(GetSingleton().docMutex); GetSingleton().docs[bindingName].shortDescription = shortDescription; @@ -132,7 +135,7 @@ void IO::AddShortDescription(const std::string& bindingName, * @param bindingName Name of the binding to add the description for. * @param longDescription Function that returns the long description. */ -void IO::AddLongDescription( +inline void IO::AddLongDescription( const std::string& bindingName, const std::function& longDescription) { @@ -146,8 +149,8 @@ void IO::AddLongDescription( * @param bindingName Name of the binding to add the example for. * @param example Function that returns the example. */ -void IO::AddExample(const std::string& bindingName, - const std::function& example) +inline void IO::AddExample(const std::string& bindingName, + const std::function& example) { std::lock_guard lock(GetSingleton().docMutex); GetSingleton().docs[bindingName].example.push_back(std::move(example)); @@ -160,9 +163,9 @@ void IO::AddExample(const std::string& bindingName, * @param description Description of the SeeAlso. * @param link Link of the SeeAlso. */ -void IO::AddSeeAlso(const std::string& bindingName, - const std::string& description, - const std::string& link) +inline void IO::AddSeeAlso(const std::string& bindingName, + const std::string& description, + const std::string& link) { std::lock_guard lock(GetSingleton().docMutex); GetSingleton().docs[bindingName].seeAlso.push_back( @@ -170,14 +173,14 @@ void IO::AddSeeAlso(const std::string& bindingName, } // Returns the sole instance of this class. -IO& IO::GetSingleton() +inline IO& IO::GetSingleton() { static IO singleton; return singleton; } // Returns the sole instance of the timers. -util::Timers& IO::GetTimers() +inline util::Timers& IO::GetTimers() { return GetSingleton().timer; } @@ -187,7 +190,7 @@ util::Timers& IO::GetTimers() * binding `bindingName`. This is intended to be called at the beginning of * the run of a binding. */ -util::Params IO::Parameters(const std::string& bindingName) +inline util::Params IO::Parameters(const std::string& bindingName) { // We don't need a mutex here, because we are only randomly accessing elements // of the maps. @@ -205,6 +208,10 @@ util::Params IO::Parameters(const std::string& bindingName) GetSingleton().parameters[""]; resultParams.insert(persistentParams.begin(), persistentParams.end()); - return Params(resultAliases, resultParams, GetSingleton().functionMap, + return util::Params(resultAliases, resultParams, GetSingleton().functionMap, bindingName, GetSingleton().docs[bindingName]); } + +} // namespace mlpack + +#endif diff --git a/src/mlpack/core/util/log.hpp b/src/mlpack/core/util/log.hpp index 351e362c88..78f4725bf6 100644 --- a/src/mlpack/core/util/log.hpp +++ b/src/mlpack/core/util/log.hpp @@ -53,46 +53,66 @@ namespace mlpack { * * @see PrefixedOutStream, NullOutStream, IO */ -class Log -{ - public: - /** - * Checks if the specified condition is true. - * If not, halts program execution and prints a custom error message. - * Does nothing in non-debug mode. - */ - static void Assert(bool condition, - const std::string& message = "Assert Failed."); +namespace Log { - /** - * MLPACK_EXPORT is required for global variables, so that they are properly - * exported by the Windows compiler. - */ +/** + * Checks if the specified condition is true. + * If not, halts program execution and prints a custom error message. + * Does nothing in non-debug mode. + */ +void Assert(bool condition, + const std::string& message = "Assert Failed."); - // We only use PrefixedOutStream if the program is compiled with debug - // symbols. -#ifdef DEBUG - //! Prints debug output with the appropriate tag: [DEBUG]. - static MLPACK_EXPORT util::PrefixedOutStream Debug; +/** + * MLPACK_EXPORT is required for global variables, so that they are properly + * exported by the Windows compiler. + */ + +// Color code escape sequences -- but not on Windows. +#ifndef _WIN32 + #define BASH_RED "\033[0;31m" + #define BASH_GREEN "\033[0;32m" + #define BASH_YELLOW "\033[0;33m" + #define BASH_CYAN "\033[0;36m" + #define BASH_CLEAR "\033[0m" #else - //! Dumps debug output into the bit nether regions. - static MLPACK_EXPORT util::NullOutStream Debug; + #define BASH_RED "" + #define BASH_GREEN "" + #define BASH_YELLOW "" + #define BASH_CYAN "" + #define BASH_CLEAR "" #endif - //! Prints informational messages if --verbose is specified, prefixed with - //! [INFO ]. - static MLPACK_EXPORT util::PrefixedOutStream Info; +#ifdef DEBUG +static util::PrefixedOutStream Debug = + util::PrefixedOutStream(MLPACK_COUT_STREAM, + BASH_CYAN "[DEBUG] " BASH_CLEAR); +#else +static util::NullOutStream Debug = util::NullOutStream(); +#endif - //! Prints warning messages prefixed with [WARN ]. - static MLPACK_EXPORT util::PrefixedOutStream Warn; +static util::PrefixedOutStream Info = + util::PrefixedOutStream(MLPACK_COUT_STREAM, + BASH_GREEN "[INFO ] " BASH_CLEAR, + true /* unless --verbose */, + false); - //! Prints fatal messages prefixed with [FATAL], then terminates the program. - static MLPACK_EXPORT util::PrefixedOutStream Fatal; +static util::PrefixedOutStream Warn = + util::PrefixedOutStream(MLPACK_COUT_STREAM, + BASH_YELLOW "[WARN ] " BASH_CLEAR, + false, + false); - //! Reference to cout, if necessary. - static std::ostream& cout; -}; +static util::PrefixedOutStream Fatal = + util::PrefixedOutStream(MLPACK_CERR_STREAM, + BASH_RED "[FATAL] " BASH_CLEAR, + false, + true /* fatal */); -}; // namespace mlpack +} // namespace Log +} // namespace mlpack + +// Include implementation. +#include "log_impl.hpp" #endif diff --git a/src/mlpack/core/util/log.cpp b/src/mlpack/core/util/log_impl.hpp similarity index 70% rename from src/mlpack/core/util/log.cpp rename to src/mlpack/core/util/log_impl.hpp index d8b2767025..e55e7f7d8c 100644 --- a/src/mlpack/core/util/log.cpp +++ b/src/mlpack/core/util/log_impl.hpp @@ -1,5 +1,5 @@ /** - * @file core/util/log.cpp + * @file core/util/log_impl.hpp * @author Matthew Amidon * * Implementation of the Log class. @@ -9,18 +9,20 @@ * 3-clause BSD license along with mlpack. If not, see * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ +#ifndef MLPACK_CORE_UTIL_LOG_IMPL_HPP +#define MLPACK_CORE_UTIL_LOG_IMPL_HPP + #include "log.hpp" #ifdef HAS_BFD_DL #include "backtrace.hpp" #endif -using namespace mlpack; -using namespace mlpack::util; +namespace mlpack { // Only do anything for Assert() if in debugging mode. #ifdef DEBUG -void Log::Assert(bool condition, const std::string& message) +inline void Log::Assert(bool condition, const std::string& message) { if (!condition) { @@ -35,6 +37,10 @@ void Log::Assert(bool condition, const std::string& message) } } #else -void Log::Assert(bool /* condition */, const std::string& /* message */) +inline void Log::Assert(bool /* condition */, const std::string& /* message */) { } #endif + +} // namespace mlpack + +#endif diff --git a/src/mlpack/core/util/param.hpp b/src/mlpack/core/util/param.hpp index c2dbe7e2aa..6ca2a8ed6c 100644 --- a/src/mlpack/core/util/param.hpp +++ b/src/mlpack/core/util/param.hpp @@ -15,19 +15,7 @@ #ifndef MLPACK_CORE_UTIL_PARAM_HPP #define MLPACK_CORE_UTIL_PARAM_HPP -// Required forward declarations. -namespace mlpack { -namespace data { - -class IncrementPolicy; - -template -class DatasetMapper; - -using DatasetInfo = DatasetMapper; - -} // namespace data -} // namespace mlpack +#include "forward.hpp" /** * @cond diff --git a/src/mlpack/core/util/param_data.hpp b/src/mlpack/core/util/param_data.hpp index d760f75c34..4477e84de9 100644 --- a/src/mlpack/core/util/param_data.hpp +++ b/src/mlpack/core/util/param_data.hpp @@ -13,26 +13,13 @@ #ifndef MLPACK_CORE_UTIL_PARAM_DATA_HPP #define MLPACK_CORE_UTIL_PARAM_DATA_HPP -#include +#include /** * The TYPENAME macro is used internally to convert a type into a string. */ #define TYPENAME(x) (std::string(typeid(x).name())) -namespace mlpack { -namespace data { - -class IncrementPolicy; - -template -class DatasetMapper; - -using DatasetInfo = DatasetMapper; - -} // namespace data -} // namespace mlpack - namespace mlpack { namespace util { @@ -75,7 +62,7 @@ struct ParamData bool loaded; //! The actual value that is held. If the user has passed a different type, //! this may be a tuple containing multiple values. - ANY value; + MLPACK_ANY value; //! The true name of the type, as it would be written in C++. std::string cppType; }; diff --git a/src/mlpack/core/util/params.cpp b/src/mlpack/core/util/params.cpp deleted file mode 100644 index cfd9b0fd11..0000000000 --- a/src/mlpack/core/util/params.cpp +++ /dev/null @@ -1,144 +0,0 @@ -/** - * @file params.cpp - * @author Ryan Curtin - * - * Implementation of functions in the Param class. - */ -#include "params.hpp" -#include - -namespace mlpack { -namespace util { - -Params::Params(const std::map& aliases, - const std::map& parameters, - Params::FunctionMapType& functionMap, - const std::string& bindingName, - const BindingDetails& doc) : - // Copy all the given inputs. - aliases(aliases), - parameters(parameters), - functionMap(functionMap), - bindingName(bindingName), - doc(doc) -{ - // Nothing to do. -} - -Params::Params() -{ - // Nothing to do. -} - -/** - * Return `true` if the specified parameter was given. - * - * @param identifier The name of the parameter in question. - */ -bool Params::Has(const std::string& key) const -{ - std::string usedKey = key; - - if (!parameters.count(key)) - { - // Check any aliases, but only after we are sure the actual option as given - // does not exist. - // TODO: can we isolate alias support inside of the CLI binding code? - if (key.length() == 1 && aliases.count(key[0])) - usedKey = aliases.at(key[0]); - - if (!parameters.count(usedKey)) - { - Log::Fatal << "Parameter '" << key << "' does not exist in this " - << "program." << std::endl; - } - } - const std::string& checkKey = usedKey; - - return (parameters.at(checkKey).wasPassed > 0); -} - -/** - * Given two (matrix) parameters, ensure that the first is an in-place copy of - * the second. This will generally do nothing (as the bindings already do - * this automatically), except for command-line bindings, where we need to - * ensure that the output filename is the same as the input filename. - * - * @param outputParamName Name of output (matrix) parameter. - * @param inputParamName Name of input (matrix) parameter. - */ -void Params::MakeInPlaceCopy(const std::string& outputParamName, - const std::string& inputParamName) -{ - if (!parameters.count(outputParamName)) - Log::Fatal << "Unknown parameter '" << outputParamName << "'!" << std::endl; - if (!parameters.count(inputParamName)) - Log::Fatal << "Unknown parameter '" << inputParamName << "'!" << std::endl; - - ParamData& output = parameters[outputParamName]; - ParamData& input = parameters[inputParamName]; - - if (output.cppType != input.cppType) - { - Log::Fatal << "Cannot call MakeInPlaceCopy() with different types (" - << output.cppType << " and " << input.cppType << ")!" << std::endl; - } - - // Is there a function to do this? - if (functionMap[output.tname].count("InPlaceCopy") != 0) - { - functionMap[output.tname]["InPlaceCopy"](output, (void*) &input, NULL); - } -} - -/** - * Set the particular parameter as passed. - * - * @param identifier The name of the parameter to set as passed. - */ -void Params::SetPassed(const std::string& name) -{ - if (parameters.count(name) == 0) - { - throw std::invalid_argument("Params::SetPassed(): parameter " + name + - " not known for binding " + bindingName + "!"); - } - - // Set passed to true. - parameters[name].wasPassed = true; -} - -/** - * Check all input matrices for NaN and inf values, and throw an exception if - * any are found. - */ -void Params::CheckInputMatrices() -{ - typedef typename std::tuple TupleType; - std::map::iterator itr; - - for (itr = parameters.begin(); itr != parameters.end(); ++itr) - { - std::string paramName = itr->first; - std::string paramType = itr->second.cppType; - if (paramType == "arma::mat") - { - CheckInputMatrix(Get(paramName), paramName); - } - else if (paramType == "arma::vec") - { - CheckInputMatrix(Get(paramName), paramName); - } - else if (paramType == "arma::rowvec") - { - CheckInputMatrix(Get(paramName), paramName); - } - else if (paramType == "std::tuple") - { - CheckInputMatrix(std::get<1>(Get(paramName)), paramName); - } - } -} - -} // namespace util -} // namespace mlpack diff --git a/src/mlpack/core/util/params.hpp b/src/mlpack/core/util/params.hpp index 00cd31a908..e01b09b611 100644 --- a/src/mlpack/core/util/params.hpp +++ b/src/mlpack/core/util/params.hpp @@ -7,8 +7,10 @@ #ifndef MLPACK_CORE_UTIL_PARAMS_HPP #define MLPACK_CORE_UTIL_PARAMS_HPP +//#include "forward.hpp" #include "param_data.hpp" #include "binding_details.hpp" +#include namespace mlpack { namespace util { @@ -144,7 +146,6 @@ class Params } // namespace util } // namespace mlpack -// Include implementation. -#include "params_impl.hpp" +// Implementation intentionally not included. #endif diff --git a/src/mlpack/core/util/params_impl.hpp b/src/mlpack/core/util/params_impl.hpp index 79d29de1c7..2914224c0c 100644 --- a/src/mlpack/core/util/params_impl.hpp +++ b/src/mlpack/core/util/params_impl.hpp @@ -9,11 +9,60 @@ #define MLPACK_CORE_UTIL_PARAMS_IMPL_HPP // Include definition, if needed. +#include "forward.hpp" #include "params.hpp" namespace mlpack { namespace util { +inline Params::Params(const std::map& aliases, + const std::map& parameters, + Params::FunctionMapType& functionMap, + const std::string& bindingName, + const BindingDetails& doc) : + // Copy all the given inputs. + aliases(aliases), + parameters(parameters), + functionMap(functionMap), + bindingName(bindingName), + doc(doc) +{ + // Nothing to do. +} + +inline Params::Params() +{ + // Nothing to do. +} + +/** + * Return `true` if the specified parameter was given. + * + * @param identifier The name of the parameter in question. + */ +inline bool Params::Has(const std::string& key) const +{ + std::string usedKey = key; + + if (!parameters.count(key)) + { + // Check any aliases, but only after we are sure the actual option as given + // does not exist. + // TODO: can we isolate alias support inside of the CLI binding code? + if (key.length() == 1 && aliases.count(key[0])) + usedKey = aliases.at(key[0]); + + if (!parameters.count(usedKey)) + { + Log::Fatal << "Parameter '" << key << "' does not exist in this " + << "program." << std::endl; + } + } + const std::string& checkKey = usedKey; + + return (parameters.at(checkKey).wasPassed > 0); +} + /** * Get the value of type T found for the parameter specified by `identifier`. * You can set the value using this reference safely. @@ -50,7 +99,7 @@ T& Params::Get(const std::string& identifier) } else { - return *ANY_CAST(&d.value); + return *MLPACK_ANY_CAST(&d.value); } } @@ -156,6 +205,89 @@ void Params::CheckInputMatrix(const T& matrix, const std::string& identifier) Log::Fatal << errMsg2 << std::endl; } +/** + * Given two (matrix) parameters, ensure that the first is an in-place copy of + * the second. This will generally do nothing (as the bindings already do + * this automatically), except for command-line bindings, where we need to + * ensure that the output filename is the same as the input filename. + * + * @param outputParamName Name of output (matrix) parameter. + * @param inputParamName Name of input (matrix) parameter. + */ +inline void Params::MakeInPlaceCopy(const std::string& outputParamName, + const std::string& inputParamName) +{ + if (!parameters.count(outputParamName)) + Log::Fatal << "Unknown parameter '" << outputParamName << "'!" << std::endl; + if (!parameters.count(inputParamName)) + Log::Fatal << "Unknown parameter '" << inputParamName << "'!" << std::endl; + + ParamData& output = parameters[outputParamName]; + ParamData& input = parameters[inputParamName]; + + if (output.cppType != input.cppType) + { + Log::Fatal << "Cannot call MakeInPlaceCopy() with different types (" + << output.cppType << " and " << input.cppType << ")!" << std::endl; + } + + // Is there a function to do this? + if (functionMap[output.tname].count("InPlaceCopy") != 0) + { + functionMap[output.tname]["InPlaceCopy"](output, (void*) &input, NULL); + } +} + +/** + * Set the particular parameter as passed. + * + * @param identifier The name of the parameter to set as passed. + */ +inline void Params::SetPassed(const std::string& name) +{ + if (parameters.count(name) == 0) + { + throw std::invalid_argument("Params::SetPassed(): parameter " + name + + " not known for binding " + bindingName + "!"); + } + + // Set passed to true. + parameters[name].wasPassed = true; +} + +/** + * Check all input matrices for NaN and inf values, and throw an exception if + * any are found. + */ +inline void Params::CheckInputMatrices() +{ + std::map::iterator itr; + + for (itr = parameters.begin(); itr != parameters.end(); ++itr) + { + std::string paramName = itr->first; + std::string paramType = itr->second.cppType; + if (paramType == "arma::mat") + { + CheckInputMatrix(Get(paramName), paramName); + } + else if (paramType == "arma::vec") + { + CheckInputMatrix(Get(paramName), paramName); + } + else if (paramType == "arma::rowvec") + { + CheckInputMatrix(Get(paramName), paramName); + } + else if (paramType == "std::tuple") + { + // Note that CheckCategoricalParam() is a utility function that must be + // defined after DatasetInfo is fully defined. + data::CheckCategoricalParam(*this, paramName); + } + } +} + } // namespace util } // namespace mlpack diff --git a/src/mlpack/core/util/prefixedoutstream.cpp b/src/mlpack/core/util/prefixedoutstream.cpp deleted file mode 100644 index 283cdcd10f..0000000000 --- a/src/mlpack/core/util/prefixedoutstream.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/** - * @file core/util/prefixedoutstream.cpp - * @author Ryan Curtin - * @author Matthew Amidon - * - * Implementation of PrefixedOutStream methods. - * - * mlpack is free software; you may redistribute it and/or modify it under the - * terms of the 3-clause BSD license. You should have received a copy of the - * 3-clause BSD license along with mlpack. If not, see - * http://www.opensource.org/licenses/BSD-3-Clause for more information. - */ -#include - -#include "prefixedoutstream.hpp" - -using namespace mlpack::util; - -/** - * These are all necessary because gcc's template mechanism does not seem smart - * enough to figure out what I want to pass into operator<< without these. That - * may not be the actual case, but it works when these is here. - */ - -PrefixedOutStream& PrefixedOutStream::operator<<(bool val) -{ - BaseLogic(val); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<(short val) -{ - BaseLogic(val); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<(unsigned short val) -{ - BaseLogic(val); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<(int val) -{ - BaseLogic(val); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<(unsigned int val) -{ - BaseLogic(val); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<(long val) -{ - BaseLogic(val); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<(unsigned long val) -{ - BaseLogic(val); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<(float val) -{ - BaseLogic(val); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<(double val) -{ - BaseLogic(val); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<(long double val) -{ - BaseLogic(val); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<(void* val) -{ - BaseLogic(val); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<(const char* str) -{ - BaseLogic(str); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<(std::string& str) -{ - BaseLogic(str); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<(std::streambuf* sb) -{ - BaseLogic(sb); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<( - std::ostream& (*pf)(std::ostream&)) -{ - BaseLogic(pf); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<(std::ios& (*pf)(std::ios&)) -{ - BaseLogic(pf); - return *this; -} - -PrefixedOutStream& PrefixedOutStream::operator<<( - std::ios_base& (*pf) (std::ios_base&)) -{ - BaseLogic(pf); - return *this; -} diff --git a/src/mlpack/core/util/prefixedoutstream.hpp b/src/mlpack/core/util/prefixedoutstream.hpp index 664c3b6203..fddf90439f 100644 --- a/src/mlpack/core/util/prefixedoutstream.hpp +++ b/src/mlpack/core/util/prefixedoutstream.hpp @@ -13,7 +13,7 @@ #ifndef MLPACK_CORE_UTIL_PREFIXEDOUTSTREAM_HPP #define MLPACK_CORE_UTIL_PREFIXEDOUTSTREAM_HPP -#include +#include namespace mlpack { namespace util { diff --git a/src/mlpack/core/util/prefixedoutstream_impl.hpp b/src/mlpack/core/util/prefixedoutstream_impl.hpp index 3cb9eea353..bce0b0f4cd 100644 --- a/src/mlpack/core/util/prefixedoutstream_impl.hpp +++ b/src/mlpack/core/util/prefixedoutstream_impl.hpp @@ -33,6 +33,116 @@ PrefixedOutStream& PrefixedOutStream::operator<<(const T& s) return *this; } +/** + * These are all necessary because gcc's template mechanism does not seem smart + * enough to figure out what I want to pass into operator<< without these. That + * may not be the actual case, but it works when these is here. + */ + +inline PrefixedOutStream& PrefixedOutStream::operator<<(bool val) +{ + BaseLogic(val); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<(short val) +{ + BaseLogic(val); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<(unsigned short val) +{ + BaseLogic(val); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<(int val) +{ + BaseLogic(val); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<(unsigned int val) +{ + BaseLogic(val); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<(long val) +{ + BaseLogic(val); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<(unsigned long val) +{ + BaseLogic(val); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<(float val) +{ + BaseLogic(val); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<(double val) +{ + BaseLogic(val); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<(long double val) +{ + BaseLogic(val); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<(void* val) +{ + BaseLogic(val); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<(const char* str) +{ + BaseLogic(str); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<(std::string& str) +{ + BaseLogic(str); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<(std::streambuf* sb) +{ + BaseLogic(sb); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<( + std::ostream& (*pf)(std::ostream&)) +{ + BaseLogic(pf); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<(std::ios& (*pf)(std::ios&)) +{ + BaseLogic(pf); + return *this; +} + +inline PrefixedOutStream& PrefixedOutStream::operator<<( + std::ios_base& (*pf) (std::ios_base&)) +{ + BaseLogic(pf); + return *this; +} + // For non-Armadillo types. template typename std::enable_if::value>::type diff --git a/src/mlpack/core/util/program_doc.hpp b/src/mlpack/core/util/program_doc.hpp index 7d6f64d7c4..5dc21826f5 100644 --- a/src/mlpack/core/util/program_doc.hpp +++ b/src/mlpack/core/util/program_doc.hpp @@ -97,4 +97,7 @@ class SeeAlso } // namespace util } // namespace mlpack +// Include implementation. +#include "program_doc_impl.hpp" + #endif diff --git a/src/mlpack/core/util/program_doc.cpp b/src/mlpack/core/util/program_doc_impl.hpp similarity index 75% rename from src/mlpack/core/util/program_doc.cpp rename to src/mlpack/core/util/program_doc_impl.hpp index f95f560c96..002477a592 100644 --- a/src/mlpack/core/util/program_doc.cpp +++ b/src/mlpack/core/util/program_doc_impl.hpp @@ -1,5 +1,5 @@ /** - * @file core/util/program_doc.cpp + * @file core/util/program_doc_impl.hpp * @author Yashwant Singh Parihar * @author Ryan Curtin * @@ -11,14 +11,14 @@ * 3-clause BSD license along with mlpack. If not, see * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ +#ifndef MLPACK_CORE_UTIL_PROGRAM_DOC_IMPL_HPP +#define MLPACK_CORE_UTIL_PROGRAM_DOC_IMPL_HPP + #include "io.hpp" #include "program_doc.hpp" -#include - -using namespace mlpack; -using namespace mlpack::util; -using namespace std; +namespace mlpack { +namespace util { /** * Construct a BindingName object. When constructed, it will register itself @@ -28,8 +28,8 @@ using namespace std; * @param bindingName Name of the binding. * @param name Name displayed to user of the binding. */ -BindingName::BindingName(const std::string& bindingName, - const std::string& name) +inline BindingName::BindingName(const std::string& bindingName, + const std::string& name) { // Register this with IO. IO::AddBindingName(bindingName, name); @@ -44,8 +44,8 @@ BindingName::BindingName(const std::string& bindingName, * @param shortDescription A short two-sentence description of the binding, * what it does, and what it is useful for. */ -ShortDescription::ShortDescription(const std::string& bindingName, - const std::string& shortDescription) +inline ShortDescription::ShortDescription(const std::string& bindingName, + const std::string& shortDescription) { // Register this with IO. IO::AddShortDescription(bindingName, shortDescription); @@ -61,7 +61,7 @@ ShortDescription::ShortDescription(const std::string& bindingName, * what it is. No newline characters are necessary; this is * taken care of by IO later. */ -LongDescription::LongDescription( +inline LongDescription::LongDescription( const std::string& bindingName, const std::function& longDescription) { @@ -76,8 +76,8 @@ LongDescription::LongDescription( * @param bindingName Name of the binding. * @param example Documentation on how to use the binding. */ -Example::Example(const std::string& bindingName, - const std::function& example) +inline Example::Example(const std::string& bindingName, + const std::function& example) { // Register this with IO. IO::AddExample(bindingName, example); @@ -91,10 +91,15 @@ Example::Example(const std::string& bindingName, * @param description Description of SeeAlso. * @param link Link of SeeAlso. */ -SeeAlso::SeeAlso(const std::string& bindingName, - const std::string& description, - const std::string& link) +inline SeeAlso::SeeAlso(const std::string& bindingName, + const std::string& description, + const std::string& link) { // Register this with IO. IO::AddSeeAlso(bindingName, description, link); } + +} // namespace util +} // namespace mlpack + +#endif diff --git a/src/mlpack/core/util/singletons.cpp b/src/mlpack/core/util/singletons.cpp deleted file mode 100644 index 2e653ef3e8..0000000000 --- a/src/mlpack/core/util/singletons.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/** - * @file core/util/singletons.cpp - * @author Ryan Curtin - * - * Declaration of singletons in libmlpack.so. - * - * mlpack is free software; you may redistribute it and/or modify it under the - * terms of the 3-clause BSD license. You should have received a copy of the - * 3-clause BSD license along with mlpack. If not, see - * http://www.opensource.org/licenses/BSD-3-Clause for more information. - */ -#include "io.hpp" -#include "log.hpp" -#include - -using namespace mlpack; -using namespace mlpack::util; - -// Color code escape sequences -- but not on Windows. -#ifndef _WIN32 - #define BASH_RED "\033[0;31m" - #define BASH_GREEN "\033[0;32m" - #define BASH_YELLOW "\033[0;33m" - #define BASH_CYAN "\033[0;36m" - #define BASH_CLEAR "\033[0m" -#else - #define BASH_RED "" - #define BASH_GREEN "" - #define BASH_YELLOW "" - #define BASH_CYAN "" - #define BASH_CLEAR "" -#endif - -#ifdef DEBUG -PrefixedOutStream Log::Debug = PrefixedOutStream(MLPACK_COUT_STREAM, - BASH_CYAN "[DEBUG] " BASH_CLEAR); -#else -NullOutStream Log::Debug = NullOutStream(); -#endif - -PrefixedOutStream Log::Info = PrefixedOutStream(MLPACK_COUT_STREAM, - BASH_GREEN "[INFO ] " BASH_CLEAR, true /* unless --verbose */, false); -PrefixedOutStream Log::Warn = PrefixedOutStream(MLPACK_COUT_STREAM, - BASH_YELLOW "[WARN ] " BASH_CLEAR, false, false); -PrefixedOutStream Log::Fatal = PrefixedOutStream(MLPACK_CERR_STREAM, - BASH_RED "[FATAL] " BASH_CLEAR, false, true /* fatal */); diff --git a/src/mlpack/core/util/timers.hpp b/src/mlpack/core/util/timers.hpp index 177850de52..86266fcef6 100644 --- a/src/mlpack/core/util/timers.hpp +++ b/src/mlpack/core/util/timers.hpp @@ -182,4 +182,7 @@ class Timers } // namespace util } // namespace mlpack +// Note that the implementation is not included, to avoid include ordering +// issues! + #endif // MLPACK_CORE_UTILITIES_TIMERS_HPP diff --git a/src/mlpack/core/util/timers.cpp b/src/mlpack/core/util/timers_impl.hpp similarity index 50% rename from src/mlpack/core/util/timers.cpp rename to src/mlpack/core/util/timers_impl.hpp index b93f5731fe..80d40e5df5 100644 --- a/src/mlpack/core/util/timers.cpp +++ b/src/mlpack/core/util/timers_impl.hpp @@ -1,5 +1,5 @@ /** - * @file core/util/timers.cpp + * @file core/util/timers_impl.hpp * @author Matthew Amidon * @author Marcus Edel * @author Ryan Curtin @@ -12,106 +12,112 @@ * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ #include "timers.hpp" + +#include "forward.hpp" #include "io.hpp" #include "log.hpp" #include #include -using namespace mlpack; -using namespace mlpack::util; -using namespace std; -using namespace chrono; +namespace mlpack { /** * Start the given timer. */ -void Timer::Start(const string& name) +inline void Timer::Start(const std::string& name) { - IO::GetSingleton().timer.Start(name, this_thread::get_id()); + IO::GetSingleton().timer.Start(name, std::this_thread::get_id()); } /** * Stop the given timer. */ -void Timer::Stop(const string& name) +inline void Timer::Stop(const std::string& name) { - IO::GetSingleton().timer.Stop(name, this_thread::get_id()); + IO::GetSingleton().timer.Stop(name, std::this_thread::get_id()); } /** * Get the given timer, summing over all threads. */ -microseconds Timer::Get(const string& name) +inline std::chrono::microseconds Timer::Get(const std::string& name) { return IO::GetSingleton().timer.Get(name); } // Enable timing. -void Timer::EnableTiming() +inline void Timer::EnableTiming() { IO::GetSingleton().timer.Enabled() = true; } // Disable timing. -void Timer::DisableTiming() +inline void Timer::DisableTiming() { IO::GetSingleton().timer.Enabled() = false; } // Reset all timers. Save state of enabled. -void Timer::ResetAll() +inline void Timer::ResetAll() { IO::GetSingleton().timer.Reset(); } -std::map Timer::GetAllTimers() +inline std::map Timer::GetAllTimers() { return IO::GetSingleton().timer.GetAllTimers(); } +namespace util { + // Reset a Timers object. -void Timers::Reset() +inline void Timers::Reset() { - lock_guard lock(timersMutex); + std::lock_guard lock(timersMutex); timers.clear(); timerStartTime.clear(); } -map Timers::GetAllTimers() +inline std::map Timers::GetAllTimers() { // Make a copy of the timer. - lock_guard lock(timersMutex); + std::lock_guard lock(timersMutex); return timers; } -microseconds Timers::Get(const string& timerName) +inline std::chrono::microseconds Timers::Get(const std::string& timerName) { if (!enabled) - return microseconds(0); + return std::chrono::microseconds(0); - lock_guard lock(timersMutex); + std::lock_guard lock(timersMutex); return timers[timerName]; } -std::string Timers::Print(const microseconds& totalDuration) +inline std::string Timers::Print(const std::chrono::microseconds& totalDuration) { // Convert microseconds to seconds. - seconds totalDurationSec = duration_cast(totalDuration); - microseconds totalDurationMicroSec = - duration_cast(totalDuration % seconds(1)); + std::chrono::seconds totalDurationSec = + std::chrono::duration_cast(totalDuration); + std::chrono::microseconds totalDurationMicroSec = + std::chrono::duration_cast( + totalDuration % std::chrono::seconds(1)); std::ostringstream oss; - oss << totalDurationSec.count() << "." << setw(6) - << setfill('0') << totalDurationMicroSec.count() << "s"; + oss << totalDurationSec.count() << "." << std::setw(6) + << std::setfill('0') << totalDurationMicroSec.count() << "s"; // Also output convenient day/hr/min/sec. // The following line is a custom duration for a day. - typedef duration> days; - days d = duration_cast(totalDuration); - hours h = duration_cast(totalDuration % days(1)); - minutes m = duration_cast(totalDuration % hours(1)); - seconds s = duration_cast(totalDuration % minutes(1)); + typedef std::chrono::duration> days; + days d = std::chrono::duration_cast(totalDuration); + std::chrono::hours h = std::chrono::duration_cast( + totalDuration % days(1)); + std::chrono::minutes m = std::chrono::duration_cast( + totalDuration % std::chrono::hours(1)); + std::chrono::seconds s = std::chrono::duration_cast( + totalDuration % std::chrono::minutes(1)); // No output if it didn't even take a minute. if (!(d.count() == 0 && h.count() == 0 && m.count() == 0)) { @@ -145,87 +151,99 @@ std::string Timers::Print(const microseconds& totalDuration) { if (output) oss << ", "; - oss << s.count() << "." << setw(1) + oss << s.count() << "." << std::setw(1) << (totalDurationMicroSec.count() / 100000) << " secs"; } oss << ")"; } - oss << endl; + oss << std::endl; return oss.str(); } -void Timers::StopAllTimers() +inline void Timers::StopAllTimers() { // Terminate the program timers. Don't use StopTimer() since that modifies // the map and would invalidate our iterators. - lock_guard lock(timersMutex); + std::lock_guard lock(timersMutex); - high_resolution_clock::time_point currTime = high_resolution_clock::now(); + std::chrono::high_resolution_clock::time_point currTime = + std::chrono::high_resolution_clock::now(); for (auto it : timerStartTime) + { for (auto it2 : it.second) - timers[it2.first] += duration_cast(currTime - it2.second); + { + timers[it2.first] += + std::chrono::duration_cast( + currTime - it2.second); + } + } // If all timers are stopped, we can clear the maps. timerStartTime.clear(); } -void Timers::Start(const string& timerName, - const thread::id& threadId) +inline void Timers::Start(const std::string& timerName, + const std::thread::id& threadId) { // Don't do anything if we aren't timing. if (!enabled) return; - lock_guard lock(timersMutex); + std::lock_guard lock(timersMutex); if ((timerStartTime.count(threadId) > 0) && (timerStartTime[threadId].count(timerName))) { - ostringstream error; + std::ostringstream error; error << "Timer::Start(): timer '" << timerName << "' has already been started"; - throw runtime_error(error.str()); + throw std::runtime_error(error.str()); } - high_resolution_clock::time_point currTime = high_resolution_clock::now(); + std::chrono::high_resolution_clock::time_point currTime = + std::chrono::high_resolution_clock::now(); // If the timer is added for the first time. if (timers.count(timerName) == 0) { - timers[timerName] = (microseconds) 0; + timers[timerName] = (std::chrono::microseconds) 0; } timerStartTime[threadId][timerName] = currTime; } -void Timers::Stop(const string& timerName, - const thread::id& threadId) +inline void Timers::Stop(const std::string& timerName, + const std::thread::id& threadId) { // Don't do anything if we aren't timing. if (!enabled) return; - lock_guard lock(timersMutex); + std::lock_guard lock(timersMutex); if ((timerStartTime.count(threadId) == 0) || (timerStartTime[threadId].count(timerName) == 0)) { - ostringstream error; + std::ostringstream error; error << "Timer::Stop(): no timer with name '" << timerName << "' currently running"; - throw runtime_error(error.str()); + throw std::runtime_error(error.str()); } - high_resolution_clock::time_point currTime = high_resolution_clock::now(); + std::chrono::high_resolution_clock::time_point currTime = + std::chrono::high_resolution_clock::now(); // Calculate the delta time. - timers[timerName] += duration_cast(currTime - - timerStartTime[threadId][timerName]); + timers[timerName] += std::chrono::duration_cast( + currTime - timerStartTime[threadId][timerName]); // Remove the entries. timerStartTime[threadId].erase(timerName); if (timerStartTime[threadId].empty()) timerStartTime.erase(threadId); } + +} // namespace util +} // namespace mlpack diff --git a/src/mlpack/core/util/version.hpp b/src/mlpack/core/util/version.hpp index 5292b64999..474c04225a 100644 --- a/src/mlpack/core/util/version.hpp +++ b/src/mlpack/core/util/version.hpp @@ -28,9 +28,12 @@ namespace util { * This will return either "mlpack x.y.z" or "mlpack master-XXXXXXX" depending on * whether or not this is a stable version of mlpack or a git repository. */ -std::string GetVersion(); +inline std::string GetVersion(); } // namespace util } // namespace mlpack +// Include implementation. +#include "version_impl.hpp" + #endif diff --git a/src/mlpack/core/util/version.cpp b/src/mlpack/core/util/version_impl.hpp similarity index 84% rename from src/mlpack/core/util/version.cpp rename to src/mlpack/core/util/version_impl.hpp index 2e0dfd1cf2..23f29554f3 100644 --- a/src/mlpack/core/util/version.cpp +++ b/src/mlpack/core/util/version_impl.hpp @@ -1,5 +1,5 @@ /** - * @file core/util/version.cpp + * @file core/util/version_impl.hpp * @author Ryan Curtin * * The implementation of GetVersion(). @@ -13,9 +13,12 @@ #include +namespace mlpack { +namespace util { + // If we are not a git revision, just use the macros to assemble the version // name. -std::string mlpack::util::GetVersion() +inline std::string GetVersion() { #ifndef MLPACK_GIT_VERSION std::stringstream o; @@ -28,3 +31,6 @@ std::string mlpack::util::GetVersion() #include "gitversion.hpp" #endif } + +} // namespace util +} // namespace mlpack diff --git a/src/mlpack/methods/bayesian_linear_regression/bayesian_linear_regression.hpp b/src/mlpack/methods/bayesian_linear_regression/bayesian_linear_regression.hpp index 7665c3e9a6..417e795bff 100644 --- a/src/mlpack/methods/bayesian_linear_regression/bayesian_linear_regression.hpp +++ b/src/mlpack/methods/bayesian_linear_regression/bayesian_linear_regression.hpp @@ -16,8 +16,6 @@ #define MLPACK_METHODS_BAYESIAN_LINEAR_REGRESSION_HPP #include -#include -#include namespace mlpack { namespace regression { diff --git a/src/mlpack/methods/bias_svd/bias_svd_function_impl.hpp b/src/mlpack/methods/bias_svd/bias_svd_function_impl.hpp index 004638b442..363f6a199c 100644 --- a/src/mlpack/methods/bias_svd/bias_svd_function_impl.hpp +++ b/src/mlpack/methods/bias_svd/bias_svd_function_impl.hpp @@ -288,7 +288,7 @@ inline double ParallelSGD::Optimize( overallObjective = 0; #pragma omp parallel for reduction(+:overallObjective) - for (omp_size_t j = 0; j < (omp_size_t) function.NumFunctions(); ++j) + for (size_t j = 0; j < (size_t) function.NumFunctions(); ++j) { overallObjective += function.Evaluate(iterate, j); } @@ -324,7 +324,7 @@ inline double ParallelSGD::Optimize( // Each processor gets a subset of the instances. // Each subset is of size threadShareSize. size_t threadId = 0; - #ifdef HAS_OPENMP + #ifdef MLPACK_USE_OPENMP threadId = omp_get_thread_num(); #endif diff --git a/src/mlpack/methods/det/dt_utils_impl.hpp b/src/mlpack/methods/det/dt_utils_impl.hpp index b5c50daa16..def663fdfd 100644 --- a/src/mlpack/methods/det/dt_utils_impl.hpp +++ b/src/mlpack/methods/det/dt_utils_impl.hpp @@ -181,12 +181,9 @@ DTree* Trainer(MatType& dataset, regularizationConstants.fill(0.0); timers.Start("cross_validation"); - // Go through each fold. On the Visual Studio compiler, we have to use - // intmax_t because size_t is not yet supported by their OpenMP - // implementation. omp_size_t is the appropriate type according to the - // platform. + // Go through each fold. #pragma omp parallel for shared(prunedSequence, regularizationConstants) - for (omp_size_t fold = 0; fold < (omp_size_t) folds; fold++) + for (size_t fold = 0; fold < (size_t) folds; fold++) { // Break up data into train and test sets. const size_t start = fold * testSize; diff --git a/src/mlpack/methods/emst/dtb_rules_impl.hpp b/src/mlpack/methods/emst/dtb_rules_impl.hpp index c7d7096d6b..88a6b59c97 100644 --- a/src/mlpack/methods/emst/dtb_rules_impl.hpp +++ b/src/mlpack/methods/emst/dtb_rules_impl.hpp @@ -37,7 +37,7 @@ DTBRules(const arma::mat& dataSet, } template -inline force_inline +inline mlpack_force_inline double DTBRules::BaseCase(const size_t queryIndex, const size_t referenceIndex) { diff --git a/src/mlpack/methods/fastmks/fastmks_rules_impl.hpp b/src/mlpack/methods/fastmks/fastmks_rules_impl.hpp index 898f8ba38b..567a348abc 100644 --- a/src/mlpack/methods/fastmks/fastmks_rules_impl.hpp +++ b/src/mlpack/methods/fastmks/fastmks_rules_impl.hpp @@ -82,7 +82,7 @@ void FastMKSRules::GetResults( } template -inline force_inline +inline mlpack_force_inline double FastMKSRules::BaseCase( const size_t queryIndex, const size_t referenceIndex) diff --git a/src/mlpack/methods/kde/kde_rules_impl.hpp b/src/mlpack/methods/kde/kde_rules_impl.hpp index 062e740efa..3c1faafa9e 100644 --- a/src/mlpack/methods/kde/kde_rules_impl.hpp +++ b/src/mlpack/methods/kde/kde_rules_impl.hpp @@ -66,7 +66,7 @@ KDERules::KDERules( //! The base case. template -inline force_inline +inline mlpack_force_inline double KDERules::BaseCase( const size_t queryIndex, const size_t referenceIndex) @@ -292,7 +292,7 @@ Score(const size_t queryIndex, TreeType& referenceNode) } template -inline force_inline double KDERules:: +inline mlpack_force_inline double KDERules:: Rescore(const size_t /* queryIndex */, TreeType& /* referenceNode */, const double oldScore) const @@ -515,7 +515,7 @@ Score(TreeType& queryNode, TreeType& referenceNode) //! Dual-tree rescore. template -inline force_inline double KDERules:: +inline mlpack_force_inline double KDERules:: Rescore(TreeType& /*queryNode*/, TreeType& /*referenceNode*/, const double oldScore) const @@ -525,7 +525,7 @@ Rescore(TreeType& /*queryNode*/, } template -inline force_inline double KDERules:: +inline mlpack_force_inline double KDERules:: EvaluateKernel(const size_t queryIndex, const size_t referenceIndex) const { @@ -534,14 +534,14 @@ EvaluateKernel(const size_t queryIndex, } template -inline force_inline double KDERules:: +inline mlpack_force_inline double KDERules:: EvaluateKernel(const arma::vec& query, const arma::vec& reference) const { return kernel.Evaluate(metric.Evaluate(query, reference)); } template -inline force_inline double KDERules:: +inline mlpack_force_inline double KDERules:: CalculateAlpha(TreeType* node) { KDEStat& stat = node->Stat(); @@ -571,7 +571,7 @@ CalculateAlpha(TreeType* node) //! Clean rules base case. template -inline force_inline +inline mlpack_force_inline double KDECleanRules::BaseCase(const size_t /* queryIndex */, const size_t /* refIndex */) { @@ -580,7 +580,7 @@ double KDECleanRules::BaseCase(const size_t /* queryIndex */, //! Clean rules single-tree score. template -inline force_inline +inline mlpack_force_inline double KDECleanRules::Score(const size_t /* queryIndex */, TreeType& referenceNode) { @@ -591,7 +591,7 @@ double KDECleanRules::Score(const size_t /* queryIndex */, //! Clean rules double-tree score. template -inline force_inline +inline mlpack_force_inline double KDECleanRules::Score(TreeType& queryNode, TreeType& referenceNode) { diff --git a/src/mlpack/methods/kmeans/allow_empty_clusters.hpp b/src/mlpack/methods/kmeans/allow_empty_clusters.hpp index ab0bdf34d0..3f65d06563 100644 --- a/src/mlpack/methods/kmeans/allow_empty_clusters.hpp +++ b/src/mlpack/methods/kmeans/allow_empty_clusters.hpp @@ -46,7 +46,7 @@ class AllowEmptyClusters * @return Number of points changed (0). */ template - static inline force_inline void EmptyCluster( + static inline mlpack_force_inline void EmptyCluster( const MatType& /* data */, const size_t emptyCluster, const arma::mat& oldCentroids, diff --git a/src/mlpack/methods/kmeans/dual_tree_kmeans_rules_impl.hpp b/src/mlpack/methods/kmeans/dual_tree_kmeans_rules_impl.hpp index 2d1a66fe12..ff60df7192 100644 --- a/src/mlpack/methods/kmeans/dual_tree_kmeans_rules_impl.hpp +++ b/src/mlpack/methods/kmeans/dual_tree_kmeans_rules_impl.hpp @@ -51,7 +51,8 @@ DualTreeKMeansRules::DualTreeKMeansRules( } template -inline force_inline double DualTreeKMeansRules::BaseCase( +inline mlpack_force_inline +double DualTreeKMeansRules::BaseCase( const size_t queryIndex, const size_t referenceIndex) { diff --git a/src/mlpack/methods/kmeans/kill_empty_clusters.hpp b/src/mlpack/methods/kmeans/kill_empty_clusters.hpp index fa3ededd2e..dd20fd5cf8 100644 --- a/src/mlpack/methods/kmeans/kill_empty_clusters.hpp +++ b/src/mlpack/methods/kmeans/kill_empty_clusters.hpp @@ -46,7 +46,7 @@ class KillEmptyClusters * @return Number of points changed (0). */ template - static inline force_inline void EmptyCluster( + static inline mlpack_force_inline void EmptyCluster( const MatType& /* data */, const size_t emptyCluster, const arma::mat& /* oldCentroids */, diff --git a/src/mlpack/methods/kmeans/kmeans_impl.hpp b/src/mlpack/methods/kmeans/kmeans_impl.hpp index e8f782e916..3deb5b2ec9 100644 --- a/src/mlpack/methods/kmeans/kmeans_impl.hpp +++ b/src/mlpack/methods/kmeans/kmeans_impl.hpp @@ -306,7 +306,7 @@ Cluster(const MatType& data, assignments.set_size(data.n_cols); #pragma omp parallel for - for (omp_size_t i = 0; i < (omp_size_t) data.n_cols; ++i) + for (size_t i = 0; i < (size_t) data.n_cols; ++i) { // Find the closest centroid to this point. double minDistance = std::numeric_limits::infinity(); diff --git a/src/mlpack/methods/kmeans/naive_kmeans_impl.hpp b/src/mlpack/methods/kmeans/naive_kmeans_impl.hpp index d829adfbbb..e81fca12c7 100644 --- a/src/mlpack/methods/kmeans/naive_kmeans_impl.hpp +++ b/src/mlpack/methods/kmeans/naive_kmeans_impl.hpp @@ -49,7 +49,7 @@ double NaiveKMeans::Iterate(const arma::mat& centroids, arma::Col localCounts(centroids.n_cols, arma::fill::zeros); #pragma omp for - for (omp_size_t i = 0; i < (omp_size_t) dataset.n_cols; ++i) + for (size_t i = 0; i < (size_t) dataset.n_cols; ++i) { // Find the closest centroid to this point. double minDistance = std::numeric_limits::infinity(); diff --git a/src/mlpack/methods/kmeans/pelleg_moore_kmeans_rules_impl.hpp b/src/mlpack/methods/kmeans/pelleg_moore_kmeans_rules_impl.hpp index 8e8f715636..617a71d763 100644 --- a/src/mlpack/methods/kmeans/pelleg_moore_kmeans_rules_impl.hpp +++ b/src/mlpack/methods/kmeans/pelleg_moore_kmeans_rules_impl.hpp @@ -38,7 +38,7 @@ PellegMooreKMeansRules::PellegMooreKMeansRules( } template -inline force_inline +inline mlpack_force_inline double PellegMooreKMeansRules::BaseCase( const size_t /* queryIndex */, const size_t /* referenceIndex */) diff --git a/src/mlpack/methods/lars/lars.hpp b/src/mlpack/methods/lars/lars.hpp index 37dd212507..d019fec900 100644 --- a/src/mlpack/methods/lars/lars.hpp +++ b/src/mlpack/methods/lars/lars.hpp @@ -25,8 +25,6 @@ #define MLPACK_METHODS_LARS_LARS_HPP #include -#include -#include namespace mlpack { namespace regression { diff --git a/src/mlpack/methods/linear_svm/linear_svm_main.cpp b/src/mlpack/methods/linear_svm/linear_svm_main.cpp index a037ec2271..6adc9a254a 100644 --- a/src/mlpack/methods/linear_svm/linear_svm_main.cpp +++ b/src/mlpack/methods/linear_svm/linear_svm_main.cpp @@ -357,7 +357,7 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) ens::ConstantStep decayPolicy(stepSize); - #ifdef HAS_OPENMP + #ifdef MLPACK_USE_OPENMP size_t threads = omp_get_max_threads(); #else size_t threads = 1; diff --git a/src/mlpack/methods/lsh/lsh_search_impl.hpp b/src/mlpack/methods/lsh/lsh_search_impl.hpp index db0bb12bdc..922c8de2dd 100644 --- a/src/mlpack/methods/lsh/lsh_search_impl.hpp +++ b/src/mlpack/methods/lsh/lsh_search_impl.hpp @@ -352,7 +352,7 @@ void LSHSearch::Train(MatType referenceSet, // Base case where the query set is the reference set. (So, we can't return // ourselves as the nearest neighbor.) template -inline force_inline +inline mlpack_force_inline void LSHSearch::BaseCase( const size_t queryIndex, const arma::uvec& referenceIndices, @@ -398,7 +398,7 @@ void LSHSearch::BaseCase( // Base case for bichromatic search. template -inline force_inline +inline mlpack_force_inline void LSHSearch::BaseCase( const size_t queryIndex, const arma::uvec& referenceIndices, @@ -440,7 +440,7 @@ void LSHSearch::BaseCase( } template -inline force_inline +inline mlpack_force_inline double LSHSearch::PerturbationScore( const std::vector& A, const arma::vec& scores) const @@ -453,7 +453,7 @@ double LSHSearch::PerturbationScore( } template -inline force_inline +inline mlpack_force_inline bool LSHSearch::PerturbationShift( std::vector& A) const { @@ -472,7 +472,7 @@ bool LSHSearch::PerturbationShift( } template -inline force_inline +inline mlpack_force_inline bool LSHSearch::PerturbationExpand( std::vector& A) const { @@ -491,7 +491,7 @@ bool LSHSearch::PerturbationExpand( } template -inline force_inline +inline mlpack_force_inline bool LSHSearch::PerturbationValid( const std::vector& A) const { @@ -908,7 +908,7 @@ void LSHSearch::Search( shared(resultingNeighbors, distances) \ schedule(dynamic)\ reduction(+:avgIndicesReturned) - for (omp_size_t i = 0; i < (omp_size_t) querySet.n_cols; ++i) + for (size_t i = 0; i < (size_t) querySet.n_cols; ++i) { // Go through every query point. // Hash every query into every hash table and eventually into the @@ -970,7 +970,7 @@ Search(const size_t k, shared(resultingNeighbors, distances) \ schedule(dynamic)\ reduction(+:avgIndicesReturned) - for (omp_size_t i = 0; i < (omp_size_t) referenceSet.n_cols; ++i) + for (size_t i = 0; i < (size_t) referenceSet.n_cols; ++i) { // Go through every query point. // Hash every query into every hash table and eventually into the diff --git a/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp b/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp index 6bf8055efd..cd24cea8eb 100644 --- a/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp +++ b/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp @@ -80,7 +80,7 @@ void NeighborSearchRules::GetResults( }; template -inline force_inline // Absolutely MUST be inline so optimizations can happen. +inline mlpack_force_inline // Must be inline so optimizations can happen. double NeighborSearchRules:: BaseCase(const size_t queryIndex, const size_t referenceIndex) { diff --git a/src/mlpack/methods/radical/radical.hpp b/src/mlpack/methods/radical/radical.hpp index 5d79b3daad..c482e9bf46 100644 --- a/src/mlpack/methods/radical/radical.hpp +++ b/src/mlpack/methods/radical/radical.hpp @@ -15,9 +15,6 @@ #define MLPACK_METHODS_RADICAL_RADICAL_HPP #include -#include -#include -#include namespace mlpack { namespace radical { diff --git a/src/mlpack/methods/random_forest/random_forest_impl.hpp b/src/mlpack/methods/random_forest/random_forest_impl.hpp index 93acb3b788..e40b5ee3bd 100644 --- a/src/mlpack/methods/random_forest/random_forest_impl.hpp +++ b/src/mlpack/methods/random_forest/random_forest_impl.hpp @@ -393,7 +393,7 @@ void RandomForest< predictions.set_size(data.n_cols); #pragma omp parallel for - for (omp_size_t i = 0; i < data.n_cols; ++i) + for (size_t i = 0; i < data.n_cols; ++i) { predictions[i] = Classify(data.col(i)); } @@ -430,7 +430,7 @@ void RandomForest< probabilities.set_size(trees[0].NumClasses(), data.n_cols); predictions.set_size(data.n_cols); #pragma omp parallel for - for (omp_size_t i = 0; i < data.n_cols; ++i) + for (size_t i = 0; i < data.n_cols; ++i) { arma::vec probs = probabilities.unsafe_col(i); Classify(data.col(i), predictions[i], probs); @@ -506,7 +506,7 @@ double RandomForest< // Train each tree individually. #pragma omp parallel for reduction( + : totalGain) - for (omp_size_t i = 0; i < numTrees; ++i) + for (size_t i = 0; i < numTrees; ++i) { MatType bootstrapDataset; arma::Row bootstrapLabels; diff --git a/src/mlpack/methods/range_search/range_search_rules_impl.hpp b/src/mlpack/methods/range_search/range_search_rules_impl.hpp index c9bbcfdbe2..098680f591 100644 --- a/src/mlpack/methods/range_search/range_search_rules_impl.hpp +++ b/src/mlpack/methods/range_search/range_search_rules_impl.hpp @@ -45,7 +45,7 @@ RangeSearchRules::RangeSearchRules( //! The base case. Evaluate the distance between the two points and add to the //! results if necessary. template -inline force_inline +inline mlpack_force_inline double RangeSearchRules::BaseCase( const size_t queryIndex, const size_t referenceIndex) diff --git a/src/mlpack/methods/rann/ra_search_rules_impl.hpp b/src/mlpack/methods/rann/ra_search_rules_impl.hpp index 8ed3ddb107..ff75d4be56 100644 --- a/src/mlpack/methods/rann/ra_search_rules_impl.hpp +++ b/src/mlpack/methods/rann/ra_search_rules_impl.hpp @@ -116,7 +116,7 @@ void RASearchRules::GetResults( }; template -inline force_inline +inline mlpack_force_inline double RASearchRules::BaseCase( const size_t queryIndex, const size_t referenceIndex) diff --git a/src/mlpack/methods/regularized_svd/regularized_svd_function_impl.hpp b/src/mlpack/methods/regularized_svd/regularized_svd_function_impl.hpp index 1a3ec638be..0d0205ed69 100644 --- a/src/mlpack/methods/regularized_svd/regularized_svd_function_impl.hpp +++ b/src/mlpack/methods/regularized_svd/regularized_svd_function_impl.hpp @@ -243,7 +243,7 @@ inline double ParallelSGD::Optimize( overallObjective = 0; #pragma omp parallel for reduction(+:overallObjective) - for (omp_size_t j = 0; j < (omp_size_t) function.NumFunctions(); ++j) + for (size_t j = 0; j < (size_t) function.NumFunctions(); ++j) { overallObjective += function.Evaluate(iterate, j); } @@ -279,7 +279,7 @@ inline double ParallelSGD::Optimize( // Each processor gets a subset of the instances. // Each subset is of size threadShareSize. size_t threadId = 0; - #ifdef HAS_OPENMP + #ifdef MLPACK_USE_OPENMP threadId = omp_get_thread_num(); #endif diff --git a/src/mlpack/methods/reinforcement_learning/async_learning_impl.hpp b/src/mlpack/methods/reinforcement_learning/async_learning_impl.hpp index ba2f5b6bf4..fe12a58e5c 100644 --- a/src/mlpack/methods/reinforcement_learning/async_learning_impl.hpp +++ b/src/mlpack/methods/reinforcement_learning/async_learning_impl.hpp @@ -97,11 +97,11 @@ void AsyncLearning< #pragma omp parallel for shared(stop, workers, tasks, learningNetwork, \ targetNetwork, totalSteps, policy) - for (omp_size_t i = 0; i < numThreads; ++i) + for (size_t i = 0; i < numThreads; ++i) { #pragma omp critical { - #ifdef HAS_OPENMP + #ifdef MLPACK_USE_OPENMP Log::Debug << "Thread " << omp_get_thread_num() << " started." << std::endl; #endif diff --git a/src/mlpack/methods/svdplusplus/svdplusplus_function_impl.hpp b/src/mlpack/methods/svdplusplus/svdplusplus_function_impl.hpp index d575848902..f991c03b33 100644 --- a/src/mlpack/methods/svdplusplus/svdplusplus_function_impl.hpp +++ b/src/mlpack/methods/svdplusplus/svdplusplus_function_impl.hpp @@ -425,7 +425,7 @@ inline double ParallelSGD::Optimize( overallObjective = 0; #pragma omp parallel for reduction(+:overallObjective) - for (omp_size_t j = 0; j < (omp_size_t) function.NumFunctions(); ++j) + for (size_t j = 0; j < (size_t) function.NumFunctions(); ++j) { overallObjective += function.Evaluate(iterate, j); } @@ -461,7 +461,7 @@ inline double ParallelSGD::Optimize( // Each processor gets a subset of the instances. // Each subset is of size threadShareSize. size_t threadId = 0; - #ifdef HAS_OPENMP + #ifdef MLPACK_USE_OPENMP threadId = omp_get_thread_num(); #endif diff --git a/src/mlpack/prereqs.hpp b/src/mlpack/prereqs.hpp index 6310364f55..202788013e 100644 --- a/src/mlpack/prereqs.hpp +++ b/src/mlpack/prereqs.hpp @@ -1,7 +1,8 @@ /** * @file prereqs.hpp * - * The core includes that mlpack expects; standard C++ includes and Armadillo. + * The core includes that mlpack expects; standard C++ includes, Armadillo, + * cereal, and a few basic mlpack utilities. * * mlpack is free software; you may redistribute it and/or modify it under the * terms of the 3-clause BSD license. You should have received a copy of the @@ -11,97 +12,7 @@ #ifndef MLPACK_PREREQS_HPP #define MLPACK_PREREQS_HPP -// First, check if Armadillo was included before, warning if so. -#ifdef ARMA_INCLUDES -#pragma message "Armadillo was included before mlpack; this can sometimes cause\ - problems. It should only be necessary to include and not \ -." -#endif - -// Defining _USE_MATH_DEFINES should set M_PI. -#define _USE_MATH_DEFINES -#include - -// Next, standard includes. -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// But if it's not defined, we'll do it. -#ifndef M_PI - #define M_PI 3.141592653589793238462643383279 -#endif - -// MLPACK_COUT_STREAM is used to change the default stream for printing -// purpose. -#if !defined(MLPACK_COUT_STREAM) - #define MLPACK_COUT_STREAM std::cout -#endif - -// MLPACK_CERR_STREAM is used to change the stream for printing warnings -// and errors. -#if !defined(MLPACK_CERR_STREAM) - #define MLPACK_CERR_STREAM std::cerr -#endif - -// Give ourselves a nice way to force functions to be inline if we need. -#define force_inline -#if defined(__GNUG__) && !defined(DEBUG) - #undef force_inline - #define force_inline __attribute__((always_inline)) -#elif defined(_MSC_VER) && !defined(DEBUG) - #undef force_inline - #define force_inline __forceinline -#endif - -// Backport this functionality from C++14, if it doesn't exist. -#if __cplusplus <= 201103L -#if !defined(_MSC_VER) || _MSC_VER <= 1800 -namespace std { - -template -using enable_if_t = typename enable_if::type; - -} -#endif -#endif - -// Backport std::any from C+17 to C++11 to replace boost::any. -// Use mnmlstc backport implementation only if compiler does not -// support C++17. -#if __cplusplus < 201703L - #include - #include - #define ANY core::v2::any - #define ANY_CAST core::v2::any_cast - #define STRING_VIEW core::v2::string_view -#else - #include - #include - #define ANY std::any - #define ANY_CAST std::any_cast - #define STRING_VIEW std::string_view -#endif - -// Increase the number of template arguments for the boost list class. -#undef BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS -#undef BOOST_MPL_LIMIT_LIST_SIZE -#define BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS -#define BOOST_MPL_LIMIT_LIST_SIZE 50 - -// Now include Armadillo through the special mlpack extensions. -#include -#include +#include "base.hpp" #include #include @@ -122,32 +33,9 @@ using enable_if_t = typename enable_if::type; #include #include -// On Visual Studio, disable C4519 (default arguments for function templates) -// since it's by default an error, which doesn't even make any sense because -// it's part of the C++11 standard. -#ifdef _MSC_VER - #pragma warning(disable : 4519) - #define ARMA_USE_CXX11 -#endif - -// Ensure that the user isn't doing something stupid with their Armadillo -// defines. -#include - // All code should have access to logging. #include -#include - -// This can be removed with Visual Studio supports an OpenMP version with -// unsigned loop variables. -#ifdef _WIN32 - #define omp_size_t intmax_t -#else - #define omp_size_t size_t -#endif - -// We need to be able to mark functions deprecated. -#include +#include // Include ready to use utility function to check sizes of datasets. #include diff --git a/src/mlpack/tests/ann/activation_functions_test.cpp b/src/mlpack/tests/ann/activation_functions_test.cpp index 37400b34b9..b05a7df561 100644 --- a/src/mlpack/tests/ann/activation_functions_test.cpp +++ b/src/mlpack/tests/ann/activation_functions_test.cpp @@ -325,7 +325,6 @@ TEST_CASE("SELUFunctionUnnormalizedTest", "[ActivationFunctionsTest]") /** * Simple SELU derivative test to check whether the derivatives * produced by the activation function are correct. - * */ TEST_CASE("SELUFunctionDerivativeTest", "[ActivationFunctionsTest]") {