[mlpack] 173/207: Use extern templates to compile Load() overloads, so that spirit doesn't get included.

Barak A. Pearlmutter barak+git at pearlmutter.net
Thu Mar 23 17:53:51 UTC 2017


This is an automated email from the git hooks/post-receive script.

bap pushed a commit to branch master
in repository mlpack.

commit 40fda2378ddfdab9feec289dfc30fef00e070b99
Author: Ryan Curtin <ryan at ratml.org>
Date:   Tue Feb 14 14:02:44 2017 -0500

    Use extern templates to compile Load() overloads, so that spirit doesn't get included.
---
 src/mlpack/core/data/CMakeLists.txt                |  3 +-
 src/mlpack/core/data/load.cpp                      | 14 +++-
 src/mlpack/core/data/load.hpp                      | 17 +++-
 src/mlpack/core/data/load_impl.hpp                 | 91 ----------------------
 .../methods/preprocess/preprocess_imputer_main.cpp |  2 +
 src/mlpack/tests/imputation_test.cpp               |  1 +
 src/mlpack/tests/load_save_test.cpp                |  1 +
 7 files changed, 31 insertions(+), 98 deletions(-)

diff --git a/src/mlpack/core/data/CMakeLists.txt b/src/mlpack/core/data/CMakeLists.txt
index bc27227..87a5d9c 100644
--- a/src/mlpack/core/data/CMakeLists.txt
+++ b/src/mlpack/core/data/CMakeLists.txt
@@ -7,7 +7,8 @@ set(SOURCES
   format.hpp
   load_csv.hpp
   load_csv.cpp
-  load.hpp  
+  load.hpp
+  load_model_impl.hpp
   load_impl.hpp
   load.cpp
   load_arff.hpp
diff --git a/src/mlpack/core/data/load.cpp b/src/mlpack/core/data/load.cpp
index 3170f4c..f316766 100644
--- a/src/mlpack/core/data/load.cpp
+++ b/src/mlpack/core/data/load.cpp
@@ -1,9 +1,16 @@
 #include "load.hpp"
+#include "load_impl.hpp"
 
 namespace mlpack {
 namespace data /** Functions to load and save matrices and models. */ {
 
-/*template bool Load<int, IncrementPolicy>(std::string const&, arma::Mat<int>&, DatasetMapper<IncrementPolicy>&,
+template bool Load<int>(const std::string&, arma::Mat<int>&, const bool, const bool);
+template bool Load<size_t>(const std::string&, arma::Mat<size_t>&, const bool, const bool);
+template bool Load<float>(const std::string&, arma::Mat<float>&, const bool, const bool);
+template bool Load<double>(const std::string&, arma::Mat<double>&, const bool, const bool);
+template bool Load<unsigned long long>(const std::string&, arma::Mat<unsigned long long>&, const bool, const bool);
+
+template bool Load<int, IncrementPolicy>(std::string const&, arma::Mat<int>&, DatasetMapper<IncrementPolicy>&,
 const bool fatal, const bool transpose);
 
 template bool Load<size_t, IncrementPolicy>(std::string const&, arma::Mat<size_t>&, DatasetMapper<IncrementPolicy>&,
@@ -13,6 +20,9 @@ template bool Load<float, IncrementPolicy>(std::string const&, arma::Mat<float>&
 const bool fatal, const bool transpose);
 
 template bool Load<double, IncrementPolicy>(std::string const&, arma::Mat<double>&, DatasetMapper<IncrementPolicy>&,
-const bool fatal, const bool transpose);//*/
+const bool fatal, const bool transpose);
+
+template bool Load<unsigned long long, IncrementPolicy>(std::string const&, arma::Mat<unsigned long long>&, DatasetMapper<IncrementPolicy>&,
+const bool fatal, const bool transpose);
 
 }}
diff --git a/src/mlpack/core/data/load.hpp b/src/mlpack/core/data/load.hpp
index 11aad88..b8a37f0 100644
--- a/src/mlpack/core/data/load.hpp
+++ b/src/mlpack/core/data/load.hpp
@@ -65,6 +65,12 @@ bool Load(const std::string& filename,
           const bool fatal = false,
           const bool transpose = true);
 
+extern template bool Load<int>(const std::string&, arma::Mat<int>&, const bool, const bool);
+extern template bool Load<size_t>(const std::string&, arma::Mat<size_t>&, const bool, const bool);
+extern template bool Load<float>(const std::string&, arma::Mat<float>&, const bool, const bool);
+extern template bool Load<double>(const std::string&, arma::Mat<double>&, const bool, const bool);
+extern template bool Load<unsigned long long>(const std::string&, arma::Mat<unsigned long long>&, const bool, const bool);
+
 /**
  * Load a column vector from a file, guessing the filetype from the extension.
  *
@@ -168,7 +174,7 @@ bool Load(const std::string& filename,
           const bool fatal = false,
           const bool transpose = true);
 
-/*extern template bool Load<int, IncrementPolicy>(std::string const&, arma::Mat<int>&, DatasetMapper<IncrementPolicy>&,
+extern template bool Load<int, IncrementPolicy>(std::string const&, arma::Mat<int>&, DatasetMapper<IncrementPolicy>&,
 const bool fatal, const bool transpose);
 
 extern template bool Load<size_t, IncrementPolicy>(std::string const&, arma::Mat<size_t>&, DatasetMapper<IncrementPolicy>&,
@@ -178,7 +184,10 @@ extern template bool Load<float, IncrementPolicy>(std::string const&, arma::Mat<
 const bool fatal, const bool transpose);
 
 extern template bool Load<double, IncrementPolicy>(std::string const&, arma::Mat<double>&, DatasetMapper<IncrementPolicy>&,
-const bool fatal, const bool transpose);//*/
+const bool fatal, const bool transpose);
+
+extern template bool Load<unsigned long long, IncrementPolicy>(std::string const&, arma::Mat<unsigned long long>&, DatasetMapper<IncrementPolicy>&,
+const bool fatal, const bool transpose);
 
 /**
  * Load a model from a file, guessing the filetype from the extension, or,
@@ -215,7 +224,7 @@ bool Load(const std::string& filename,
 } // namespace data
 } // namespace mlpack
 
-// Include implementation.
-#include "load_impl.hpp"
+// Include implementation of model-loading Load() overload.
+#include "load_model_impl.hpp"
 
 #endif
diff --git a/src/mlpack/core/data/load_impl.hpp b/src/mlpack/core/data/load_impl.hpp
index 98b1598..123f12f 100644
--- a/src/mlpack/core/data/load_impl.hpp
+++ b/src/mlpack/core/data/load_impl.hpp
@@ -25,16 +25,10 @@
 #include "load.hpp"
 #include "extension.hpp"
 
-#include <boost/serialization/serialization.hpp>
 #include <boost/algorithm/string/trim.hpp>
-#include <boost/archive/xml_iarchive.hpp>
-#include <boost/archive/text_iarchive.hpp>
-#include <boost/archive/binary_iarchive.hpp>
 #include <boost/tokenizer.hpp>
 #include <boost/algorithm/string.hpp>
 
-#include "serialization_shim.hpp"
-
 #include "load_arff.hpp"
 
 namespace mlpack {
@@ -422,91 +416,6 @@ bool Load(const std::string& filename,
   return true;
 }
 
-// Load a model from file.
-template<typename T>
-bool Load(const std::string& filename,
-          const std::string& name,
-          T& t,
-          const bool fatal,
-          format f)
-{
-  if (f == format::autodetect)
-  {
-    std::string extension = Extension(filename);
-
-    if (extension == "xml")
-      f = format::xml;
-    else if (extension == "bin")
-      f = format::binary;
-    else if (extension == "txt")
-      f = format::text;
-    else
-    {
-      if (fatal)
-        Log::Fatal << "Unable to detect type of '" << filename << "'; incorrect"
-            << " extension?" << std::endl;
-      else
-        Log::Warn << "Unable to detect type of '" << filename << "'; load "
-            << "failed.  Incorrect extension?" << std::endl;
-
-      return false;
-    }
-  }
-
-  // Now load the given format.
-  std::ifstream ifs;
-#ifdef _WIN32 // Open non-text in binary mode on Windows.
-  if (f == format::binary)
-    ifs.open(filename, std::ifstream::in | std::ifstream::binary);
-  else
-    ifs.open(filename, std::ifstream::in);
-#else
-  ifs.open(filename, std::ifstream::in);
-#endif
-
-  if (!ifs.is_open())
-  {
-    if (fatal)
-      Log::Fatal << "Unable to open file '" << filename << "' to load object '"
-          << name << "'." << std::endl;
-    else
-      Log::Warn << "Unable to open file '" << filename << "' to load object '"
-          << name << "'." << std::endl;
-
-    return false;
-  }
-
-  try
-  {
-    if (f == format::xml)
-    {
-      boost::archive::xml_iarchive ar(ifs);
-      ar >> CreateNVP(t, name);
-    }
-    else if (f == format::text)
-    {
-      boost::archive::text_iarchive ar(ifs);
-      ar >> CreateNVP(t, name);
-    }
-    else if (f == format::binary)
-    {
-      boost::archive::binary_iarchive ar(ifs);
-      ar >> CreateNVP(t, name);
-    }
-
-    return true;
-  }
-  catch (boost::archive::archive_exception& e)
-  {
-    if (fatal)
-      Log::Fatal << e.what() << std::endl;
-    else
-      Log::Warn << e.what() << std::endl;
-
-    return false;
-  }
-}
-
 } // namespace data
 } // namespace mlpack
 
diff --git a/src/mlpack/methods/preprocess/preprocess_imputer_main.cpp b/src/mlpack/methods/preprocess/preprocess_imputer_main.cpp
index 54a6a07..19ba467 100644
--- a/src/mlpack/methods/preprocess/preprocess_imputer_main.cpp
+++ b/src/mlpack/methods/preprocess/preprocess_imputer_main.cpp
@@ -14,6 +14,8 @@
 #include <mlpack/core/data/load.hpp>
 #include <mlpack/core/data/save.hpp>
 #include <mlpack/core/util/param.hpp>
+#include <mlpack/core/data/load_impl.hpp>
+#include <mlpack/core/util/cli.hpp>
 #include <mlpack/core/data/imputer.hpp>
 #include <mlpack/core/data/dataset_mapper.hpp>
 #include <mlpack/core/data/map_policies/increment_policy.hpp>
diff --git a/src/mlpack/tests/imputation_test.cpp b/src/mlpack/tests/imputation_test.cpp
index 2e9f8cf..9fcd26a 100644
--- a/src/mlpack/tests/imputation_test.cpp
+++ b/src/mlpack/tests/imputation_test.cpp
@@ -12,6 +12,7 @@
 #include <sstream>
 
 #include <mlpack/core.hpp>
+#include <mlpack/core/data/load_impl.hpp>
 #include <mlpack/core/data/dataset_mapper.hpp>
 #include <mlpack/core/data/map_policies/increment_policy.hpp>
 #include <mlpack/core/data/map_policies/missing_policy.hpp>
diff --git a/src/mlpack/tests/load_save_test.cpp b/src/mlpack/tests/load_save_test.cpp
index 621f002..474a6cc 100644
--- a/src/mlpack/tests/load_save_test.cpp
+++ b/src/mlpack/tests/load_save_test.cpp
@@ -12,6 +12,7 @@
 #include <sstream>
 
 #include <mlpack/core.hpp>
+#include <mlpack/core/data/load_arff.hpp>
 
 #include <boost/test/unit_test.hpp>
 #include "test_tools.hpp"

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git



More information about the debian-science-commits mailing list