From 6b1e73888b69df8460c34eec9f4dea792ee978c9 Mon Sep 17 00:00:00 2001 From: Viktor Gal Date: Wed, 11 Sep 2019 11:48:25 +0200 Subject: [PATCH] add cmake support for precompiled headers --- .ci/ci.yml | 2 +- cmake/FindCCache.cmake | 6 +- src/gpl | 2 +- src/shogun/CMakeLists.txt | 62 ++++++++++++ src/shogun/classifier/mkl/MKLMulticlass.cpp | 27 +++--- .../distributions/DiscreteDistribution.h | 2 +- src/shogun/io/fs/NullFileSystem.h | 24 ++--- src/shogun/io/stream/BufferedOutputStream.h | 97 ++++++++++--------- .../lib/external/falconn/ffht/fht_impl.h | 4 + .../multitask/MultitaskKernelTreeNormalizer.h | 54 +++++------ 10 files changed, 172 insertions(+), 108 deletions(-) diff --git a/.ci/ci.yml b/.ci/ci.yml index dd16805391e..4d3a7e36b37 100644 --- a/.ci/ci.yml +++ b/.ci/ci.yml @@ -128,7 +128,7 @@ jobs: ccacheDir: '$(build.binariesDirectory)/ccache' ccacheArtifactName: 'ccache-macos' ccacheArchive: '$(Build.ArtifactStagingDirectory)/ccache-$(Build.BuildId).tar' - commonMacOSCMakeFlags: '-DBUILD_EXAMPLES=OFF -DBUNDLE_JSON=OFF -DBUNDLE_NLOPT=OFF -DENABLE_TESTING=ON -DENABLE_COVERAGE=OFF -DBUILD_META_EXAMPLES=OFF' + commonMacOSCMakeFlags: '-DBUILD_EXAMPLES=OFF -DBUNDLE_NLOPT=OFF -DENABLE_TESTING=ON -DENABLE_COVERAGE=OFF -DBUILD_META_EXAMPLES=OFF' openMPMacOSCMakeFlags: '-DOpenMP_CXX_FLAGS="-Xpreprocessor -fopenmp" -DOpenMP_C_FLAGS="-Xpreprocessor -fopenmp" diff --git a/cmake/FindCCache.cmake b/cmake/FindCCache.cmake index 2fad2ba8005..e5e7f88af78 100644 --- a/cmake/FindCCache.cmake +++ b/cmake/FindCCache.cmake @@ -12,11 +12,7 @@ endif() # handle REQUIRED and QUIET options include(FindPackageHandleStandardArgs) -if (CMAKE_VERSION LESS 2.8.3) - find_package_handle_standard_args(CCache DEFAULT_MSG CCACHE CCACHE_VERSION) -else () - find_package_handle_standard_args(CCache REQUIRED_VARS CCACHE CCACHE_VERSION) -endif () +find_package_handle_standard_args(CCache REQUIRED_VARS CCACHE CCACHE_VERSION) if (CCACHE_FOUND) if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") diff --git a/src/gpl b/src/gpl index c967b2623a9..e958236680e 160000 --- a/src/gpl +++ b/src/gpl @@ -1 +1 @@ -Subproject commit c967b2623a9dc90ef0810304edd883a466eb87e4 +Subproject commit e958236680e90d6cc1b5badacacada27ce7a81a1 diff --git a/src/shogun/CMakeLists.txt b/src/shogun/CMakeLists.txt index 9a05757e789..790da35f3d9 100644 --- a/src/shogun/CMakeLists.txt +++ b/src/shogun/CMakeLists.txt @@ -178,6 +178,68 @@ IF(MSVC) target_link_libraries(shogun_deps INTERFACE winmm Shlwapi) ENDIF() +########################### PCH +if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.16.0) + if (CCACHE_FOUND) + if (DEFINED ENV{CCACHE_SLOPPINESS}) + if (NOT "$ENV{CCACHE_SLOPPINESS}" MATCHES "pch_defines" OR + NOT "$ENV{CCACHE_SLOPPINESS}" MATCHES "time_macros") + MESSAGE (WARNING + "ccache requires the environment variable CCACHE_SLOPPINESS to be set to \"pch_defines,time_macros\"." + ) + SET(CCACHE_PCH_READY 0) + else() + SET(CCACHE_PCH_READY 1) + endif() + else() + execute_process( + COMMAND "${COMPILER_CACHE_EXECUTABLE}" "-p" + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" + RESULT_VARIABLE _result + OUTPUT_VARIABLE _ccacheConfig OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET) + if (_result) + MESSAGE (WARNING "ccache configuration cannot be determined.") + SET(CCACHE_PCH_READY 0) + elseif (NOT _ccacheConfig MATCHES "sloppiness.*=.*time_macros" OR + NOT _ccacheConfig MATCHES "sloppiness.*=.*pch_defines") + MESSAGE (WARNING + "ccache requires configuration setting \"sloppiness\" to be set to \"pch_defines,time_macros\"." + ) + SET(CCACHE_PCH_READY 0) + else() + SET(CCACHE_PCH_READY 1) + endif() + endif() + + endif() + + if (NOT CCACHE_FOUND OR CCACHE_PCH_READY) + message(STATUS "Using precompiled headers") + # FIXME: make sure all the headers are listed that could be precompiled + target_precompile_headers(libshogun + PUBLIC "$<$:${LIBSHOGUN_HEADERS}>" + PRIVATE + $<$:> + $<$:> + $<$:> + $<$:> + $<$:> + $<$:> + $<$:> + $<$:> + $<$:> + $<$:> + $<$:> + $<$:> + $<$:> + $<$:> + $<$:> + $<$:>) + endif() +endif() + + ########################### compiler capabilities FIND_PACKAGE(Threads) IF (CMAKE_USE_PTHREADS_INIT) diff --git a/src/shogun/classifier/mkl/MKLMulticlass.cpp b/src/shogun/classifier/mkl/MKLMulticlass.cpp index 45e981054d5..56b811f682b 100644 --- a/src/shogun/classifier/mkl/MKLMulticlass.cpp +++ b/src/shogun/classifier/mkl/MKLMulticlass.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -137,7 +138,7 @@ bool MKLMulticlass::evaluatefinishcriterion(const int32_t wold=weightshistory[ weightshistory.size()-2 ]; wnew=weightshistory.back(); - float64_t delta=0; + float64_t _delta=0; ASSERT (wold.size()==wnew.size()) @@ -146,23 +147,23 @@ bool MKLMulticlass::evaluatefinishcriterion(const int32_t { //check dual gap part for mkl - delta=oldalphaterm-curalphaterm; + _delta=oldalphaterm-curalphaterm; int32_t maxind=0; float64_t maxval=normweightssquared[maxind]; for (size_t i=0;i< wnew.size();++i) { - delta+=-0.5*oldnormweightssquared[i]*wold[i]; + _delta+=-0.5*oldnormweightssquared[i]*wold[i]; if(normweightssquared[i]>maxval) { maxind=i; maxval=normweightssquared[i]; } } - delta+=0.5*normweightssquared[maxind]; - //delta=fabs(delta); - io::info("L1 Norm chosen, MKL part of duality gap {} ",delta); - if( (delta < mkl_eps) && (numberofsilpiterations>=1) ) + _delta+=0.5*normweightssquared[maxind]; + //_delta=fabs(_delta); + io::info("L1 Norm chosen, MKL part of duality gap {} ",_delta); + if( (_delta < mkl_eps) && (numberofsilpiterations>=1) ) { return true; } @@ -172,27 +173,27 @@ bool MKLMulticlass::evaluatefinishcriterion(const int32_t } else { - delta=0; + _delta=0; float64_t deltaold=oldalphaterm,deltanew=curalphaterm; for (size_t i=0;i< wnew.size();++i) { - delta+=(wold[i]-wnew[i])*(wold[i]-wnew[i]); + _delta+=(wold[i]-wnew[i])*(wold[i]-wnew[i]); deltaold+= -0.5*oldnormweightssquared[i]*wold[i]; deltanew+= -0.5*normweightssquared[i]*wnew[i]; } if(deltanew>0) { - delta=1-deltanew/deltaold; + _delta=1-deltanew/deltaold; } else { io::warn("MKLMulticlass::evaluatefinishcriterion(...): deltanew<=0.Switching back to weight norsm difference as criterion."); - delta=sqrt(delta); + _delta=sqrt(_delta); } - io::info("weight delta {} ",delta); + io::info("weight delta {} ",_delta); - if( (delta < mkl_eps) && (numberofsilpiterations>=1) ) + if( (_delta < mkl_eps) && (numberofsilpiterations>=1) ) { return true; } diff --git a/src/shogun/distributions/DiscreteDistribution.h b/src/shogun/distributions/DiscreteDistribution.h index daf7c0943b5..daabf4bb631 100644 --- a/src/shogun/distributions/DiscreteDistribution.h +++ b/src/shogun/distributions/DiscreteDistribution.h @@ -97,7 +97,7 @@ class DiscreteDistribution : public Distribution * * @param alpha_k "belongingness" values of various data points */ - virtual void update_params_em(const SGVector alpha_k)=0; + virtual float64_t update_params_em(const SGVector alpha_k)=0; }; } #endif /* _DISCRETEDISTRIBUTION_H__ */ \ No newline at end of file diff --git a/src/shogun/io/fs/NullFileSystem.h b/src/shogun/io/fs/NullFileSystem.h index 6cc08442d0a..19dabf132bf 100644 --- a/src/shogun/io/fs/NullFileSystem.h +++ b/src/shogun/io/fs/NullFileSystem.h @@ -15,50 +15,50 @@ namespace shogun ~NullFileSystem() override = default; - std::unique_ptr new_random_access_file( - const std::string& fname) override + std::error_condition new_random_access_file( + const std::string& fname, std::unique_ptr*) const override { throw ShogunNotImplementedException("new_random_access_file unimplemented"); } - std::unique_ptr new_writable_file( - const std::string& fname) override + std::error_condition new_writable_file( + const std::string& fname, std::unique_ptr*) const override { throw ShogunNotImplementedException("NewWritableFile new_writable_file"); } - std::unique_ptr new_appendable_file( - const std::string& fname) override + std::error_condition new_appendable_file( + const std::string& fname, std::unique_ptr*) const override { throw ShogunNotImplementedException("new_appendable_file unimplemented"); } - bool file_exists(const std::string& fname) override + std::error_condition file_exists(const std::string& fname) const override { throw ShogunNotImplementedException("file_exists unimplemented"); } - void delete_file(const std::string& fname) override + std::error_condition delete_file(const std::string& fname) const override { throw ShogunNotImplementedException("delete_file unimplemented"); } - void create_dir(const std::string& dirname) override + std::error_condition create_dir(const std::string& dirname) const override { throw ShogunNotImplementedException("create_dir unimplemented"); } - void delete_dir(const std::string& dirname) override + std::error_condition delete_dir(const std::string& dirname) const override { throw ShogunNotImplementedException("delete_dir unimplemented"); } - int64_t get_file_size(const std::string& fname) override + int64_t get_file_size(const std::string& fname) const override { throw ShogunNotImplementedException("get_file_size unimplemented"); } - void rename_file(const std::string& src, const std::string& target) override + std::error_condition rename_file(const std::string& src, const std::string& target) const override { throw ShogunNotImplementedException("rename_file unimplemented"); } diff --git a/src/shogun/io/stream/BufferedOutputStream.h b/src/shogun/io/stream/BufferedOutputStream.h index 23f3b691aa6..5e8104820ee 100644 --- a/src/shogun/io/stream/BufferedOutputStream.h +++ b/src/shogun/io/stream/BufferedOutputStream.h @@ -11,64 +11,67 @@ namespace shogun { - IGNORE_IN_CLASSLIST class BufferedOutputStream : public OutputStream + namespace io { - public: - /** - * Construct a buffered output stream - * - * @param os - * @param buffer_bytes - */ - BufferedOutputStream(std::shared_ptr os, index_t buffer_bytes = 4096): - OutputStream(), m_os(std::move(os)) + IGNORE_IN_CLASSLIST class BufferedOutputStream : public OutputStream { + public: + /** + * Construct a buffered output stream + * + * @param os + * @param buffer_bytes + */ + BufferedOutputStream(std::shared_ptr os, index_t buffer_bytes = 4096): + OutputStream(), m_os(std::move(os)) + { - } + } - BufferedOutputStream(BufferedOutputStream&& src): - OutputStream(), m_os(std::move(src.m_os)) - { - src.m_os = nullptr; - } + BufferedOutputStream(BufferedOutputStream&& src): + OutputStream(), m_os(std::move(src.m_os)) + { + src.m_os = nullptr; + } - BufferedOutputStream& operator=(BufferedOutputStream&& src) - { - m_os = std::move(src.m_os); - return *this; - } + BufferedOutputStream& operator=(BufferedOutputStream&& src) + { + m_os = std::move(src.m_os); + return *this; + } - ~BufferedOutputStream() override - { - m_os->flush(); - m_os->close(); - } + ~BufferedOutputStream() override + { + m_os->flush(); + m_os->close(); + } - std::error_condition write(void* buffer, int64_t size) override - { - m_os->write(buffer, size); - } + std::error_condition write(const void* buffer, int64_t size) override + { + m_os->write(buffer, size); + } - std::error_condition close() override - { - m_os->close(); - } + std::error_condition close() override + { + m_os->close(); + } - std::error_condition flush() override - { - m_os->flush(); - } + std::error_condition flush() override + { + m_os->flush(); + } - const char* get_name() const override - { - return "BufferedOutputStream"; - } + const char* get_name() const override + { + return "BufferedOutputStream"; + } - private: - std::shared_ptr m_os; + private: + std::shared_ptr m_os; - SG_DELETE_COPY_AND_ASSIGN(BufferedOutputStream); - }; -} + SG_DELETE_COPY_AND_ASSIGN(BufferedOutputStream); + }; + } // namespace io +} // namespace shogun #endif /* __BUFFERED_OUTPUT_STREAM_H__ */ diff --git a/src/shogun/lib/external/falconn/ffht/fht_impl.h b/src/shogun/lib/external/falconn/ffht/fht_impl.h index c33b434bc49..5e60c5b3e4e 100644 --- a/src/shogun/lib/external/falconn/ffht/fht_impl.h +++ b/src/shogun/lib/external/falconn/ffht/fht_impl.h @@ -1,3 +1,6 @@ +#ifndef _FHT_IMPL_H +#define _FHT_IMPL_H + #ifdef __AVX__ #include #endif @@ -544,3 +547,4 @@ void FHTFloatIterativeLongHelperAVX(float *buffer, int len, int logLen) { } #endif +#endif diff --git a/src/shogun/transfer/multitask/MultitaskKernelTreeNormalizer.h b/src/shogun/transfer/multitask/MultitaskKernelTreeNormalizer.h index 0651abb13d1..3f0647c8602 100644 --- a/src/shogun/transfer/multitask/MultitaskKernelTreeNormalizer.h +++ b/src/shogun/transfer/multitask/MultitaskKernelTreeNormalizer.h @@ -26,30 +26,30 @@ namespace shogun * structure between tasks. * */ -class Node: public SGObject +class TaxonomyNode: public SGObject { public: - using NodeSet = std::set>; + using NodeSet = std::set>; /** default constructor */ - Node() + TaxonomyNode() { parent = NULL; beta = 1.0; node_id = 0; } - virtual ~Node() + virtual ~TaxonomyNode() { } /** get a list of all ancestors of this node * @return set of Nodes */ - Node::NodeSet get_path_root() + TaxonomyNode::NodeSet get_path_root() { - Node::NodeSet nodes_on_path; - std::shared_ptr node = shared_from_this()->as(); + TaxonomyNode::NodeSet nodes_on_path; + std::shared_ptr node = shared_from_this()->as(); while (node != NULL) { nodes_on_path.insert(node); node = node->parent; @@ -64,8 +64,8 @@ class Node: public SGObject { std::vector task_ids; - std::deque> grey_nodes; - grey_nodes.push_back(shared_from_this()->as()); + std::deque> grey_nodes; + grey_nodes.push_back(shared_from_this()->as()); while(grey_nodes.size() > 0) { @@ -88,16 +88,16 @@ class Node: public SGObject /** add child to current node * @param node child node */ - void add_child(std::shared_ptrnode) + void add_child(std::shared_ptr node) { - node->parent = shared_from_this()->as(); + node->parent = shared_from_this()->as(); this->children.push_back(node); } /** @return object name */ virtual const char *get_name() const { - return "Node"; + return "TaxonomyNode"; } /** @return boolean indicating, whether this node is a leaf */ @@ -125,10 +125,10 @@ class Node: public SGObject protected: /** parent node **/ - std::shared_ptr parent; + std::shared_ptr parent; /** list of child nodes **/ - std::vector> children; + std::vector> children; /** identifier of node **/ int32_t node_id; @@ -149,7 +149,7 @@ class Taxonomy */ Taxonomy() { - root = std::make_shared(); + root = std::make_shared(); nodes.push_back(root); name2id = std::map(); @@ -167,7 +167,7 @@ class Taxonomy * @param task_id task identifier * @return node with id task_id */ - std::shared_ptr get_node(int32_t task_id) const { + std::shared_ptr get_node(int32_t task_id) const { return nodes[task_id]; } @@ -184,13 +184,13 @@ class Taxonomy * @param child_name name of child * @param beta weight of child */ - std::shared_ptr add_node(std::string parent_name, std::string child_name, float64_t beta) + std::shared_ptr add_node(std::string parent_name, std::string child_name, float64_t beta) { if (child_name=="") error("child_name empty"); if (parent_name=="") error("parent_name empty"); - auto child_node = std::make_shared(); + auto child_node = std::make_shared(); child_node->beta = beta; @@ -223,13 +223,13 @@ class Taxonomy * @param node_rhs node of right hand side * @return intersection of the two sets of ancestors */ - Node::NodeSet intersect_root_path(std::shared_ptr node_lhs, std::shared_ptr node_rhs) const + TaxonomyNode::NodeSet intersect_root_path(std::shared_ptr node_lhs, std::shared_ptr node_rhs) const { - Node::NodeSet root_path_lhs = node_lhs->get_path_root(); - Node::NodeSet root_path_rhs = node_rhs->get_path_root(); + TaxonomyNode::NodeSet root_path_lhs = node_lhs->get_path_root(); + TaxonomyNode::NodeSet root_path_rhs = node_rhs->get_path_root(); - Node::NodeSet intersection; + TaxonomyNode::NodeSet intersection; std::set_intersection(root_path_lhs.begin(), root_path_lhs.end(), root_path_rhs.begin(), root_path_rhs.end(), @@ -251,14 +251,12 @@ class Taxonomy auto node_rhs = get_node(task_rhs); // compute intersection of paths to root - Node::NodeSet intersection = intersect_root_path(node_lhs, node_rhs); + TaxonomyNode::NodeSet intersection = intersect_root_path(node_lhs, node_rhs); // sum up weights float64_t gamma = 0; - for (Node::NodeSet::const_iterator p = intersection.begin(); p != intersection.end(); ++p) { - + for (TaxonomyNode::NodeSet::const_iterator p = intersection.begin(); p != intersection.end(); ++p) gamma += (*p)->beta; - } return gamma; @@ -357,11 +355,11 @@ class Taxonomy protected: /** root */ - std::shared_ptr root; + std::shared_ptr root; /** name 2 id */ std::map name2id; /** nodes */ - std::vector> nodes; + std::vector> nodes; /** task histogram */ std::map task_histogram;