SINGA-21 Code review 5

Move the implementation of template functions back to driver.h from driver.cc.
Otherwise there would be link errors when users register their own classes.
Because the registration functions are instantiated during compiling the
user code. diriver.h must contain the declarations and the
implementations of these template functions to instantiate them for users 
classes.


Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/0c6e5c69
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/0c6e5c69
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/0c6e5c69

Branch: refs/heads/master
Commit: 0c6e5c692bb07b481baca004b07b08b2a0601c6e
Parents: 366e6a8
Author: Wei Wang <[email protected]>
Authored: Wed Sep 23 23:02:01 2015 +0800
Committer: Wei Wang <[email protected]>
Committed: Wed Sep 23 23:02:01 2015 +0800

----------------------------------------------------------------------
 include/driver.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++--
 src/driver.cc    | 48 ++---------------------------------------
 2 files changed, 59 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/0c6e5c69/include/driver.h
----------------------------------------------------------------------
diff --git a/include/driver.h b/include/driver.h
index 563be77..b33c7cc 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -7,9 +7,9 @@
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
-* 
+*
 *   http://www.apache.org/licenses/LICENSE-2.0
-* 
+*
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -24,6 +24,12 @@
 
 #include "proto/job.pb.h"
 #include "proto/singa.pb.h"
+#include "utils/factory.h"
+#include "utils/param.h"
+#include "utils/singleton.h"
+#include "utils/updater.h"
+#include "neuralnet/layer.h"
+#include "trainer/worker.h"
 
 namespace singa {
 
@@ -120,6 +126,55 @@ class Driver {
   SingaProto singa_conf_;
 };
 
+/************* Implementation of template functions*************************
+* Must put the implementation in driver.h file instead of driver.cc.
+* Otherwise there would be linking error caused by unknown registration
+* functions, becuase these function cannot be generated merely based on its
+* declearation in driver.h.
+*/
+
+template<typename Subclass, typename Type>
+int Driver::RegisterLayer(const Type& type) {
+  auto factory = Singleton<Factory<singa::Layer>>::Instance();
+  factory->Register(type, CreateInstance(Subclass, Layer));
+  return 1;
+}
+
+template<typename Subclass, typename Type>
+int Driver::RegisterParam(const Type& type) {
+  auto factory = Singleton<Factory<singa::Param>>::Instance();
+  factory->Register(type, CreateInstance(Subclass, Param));
+  return 1;
+}
+
+template<typename Subclass, typename Type>
+int Driver::RegisterParamGenerator(const Type& type) {
+  auto factory = Singleton<Factory<singa::ParamGenerator>>::Instance();
+  factory->Register(type, CreateInstance(Subclass, ParamGenerator));
+  return 1;
+}
+
+template<typename Subclass, typename Type>
+int Driver::RegisterUpdater(const Type& type) {
+  auto factory = Singleton<Factory<singa::Updater>>::Instance();
+  factory->Register(type, CreateInstance(Subclass, Updater));
+  return 1;
+}
+
+template<typename Subclass, typename Type>
+int Driver::RegisterLRGenerator(const Type& type) {
+  auto factory = Singleton<Factory<singa::LRGenerator>>::Instance();
+  factory->Register(type, CreateInstance(Subclass, LRGenerator));
+  return 1;
+}
+
+template<typename Subclass, typename Type>
+int Driver::RegisterWorker(const Type& type) {
+  auto factory = Singleton<Factory<singa::Worker>>::Instance();
+  factory->Register(type, CreateInstance(Subclass, Worker));
+  return 1;
+}
+
 }  // namespace singa
 
 #endif  // SINGA_DRIVER_H_

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/0c6e5c69/src/driver.cc
----------------------------------------------------------------------
diff --git a/src/driver.cc b/src/driver.cc
index 42a1330..41b2342 100644
--- a/src/driver.cc
+++ b/src/driver.cc
@@ -7,9 +7,9 @@
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
-* 
+*
 *   http://www.apache.org/licenses/LICENSE-2.0
-* 
+*
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -24,12 +24,9 @@
 #include <cblas.h>
 #include <glog/logging.h>
 #include <string>
-#include "neuralnet/neuralnet.h"
 #include "neuralnet/layer.h"
 #include "trainer/trainer.h"
 #include "utils/common.h"
-#include "utils/factory.h"
-#include "utils/singleton.h"
 #include "utils/tinydir.h"
 
 namespace singa {
@@ -110,47 +107,6 @@ void Driver::Init(int argc, char **argv) {
   RegisterParamGenerator<UniformSqrtFanInOutGen>(kUniformSqrtFanInOut);
 }
 
-template<typename Subclass, typename Type>
-int Driver::RegisterLayer(const Type& type) {
-  auto factory = Singleton<Factory<singa::Layer>>::Instance();
-  factory->Register(type, CreateInstance(Subclass, Layer));
-  return 1;
-}
-
-template<typename Subclass, typename Type>
-int Driver::RegisterParam(const Type& type) {
-  auto factory = Singleton<Factory<singa::Param>>::Instance();
-  factory->Register(type, CreateInstance(Subclass, Param));
-  return 1;
-}
-
-template<typename Subclass, typename Type>
-int Driver::RegisterParamGenerator(const Type& type) {
-  auto factory = Singleton<Factory<singa::ParamGenerator>>::Instance();
-  factory->Register(type, CreateInstance(Subclass, ParamGenerator));
-  return 1;
-}
-
-template<typename Subclass, typename Type>
-int Driver::RegisterUpdater(const Type& type) {
-  auto factory = Singleton<Factory<singa::Updater>>::Instance();
-  factory->Register(type, CreateInstance(Subclass, Updater));
-  return 1;
-}
-
-template<typename Subclass, typename Type>
-int Driver::RegisterLRGenerator(const Type& type) {
-  auto factory = Singleton<Factory<singa::LRGenerator>>::Instance();
-  factory->Register(type, CreateInstance(Subclass, LRGenerator));
-  return 1;
-}
-
-template<typename Subclass, typename Type>
-int Driver::RegisterWorker(const Type& type) {
-  auto factory = Singleton<Factory<singa::Worker>>::Instance();
-  factory->Register(type, CreateInstance(Subclass, Worker));
-  return 1;
-}
 
 void Driver::Submit(bool resume, const JobProto& jobConf) {
   if (singa_conf_.has_log_dir())

Reply via email to