SINGA-108 Add Python wrapper to singa Add a new python API -- InitLog(char* log) to initialize singa logs. The related code is also added in driver.h and driver.cc. Please test via the guidance in last commit.
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/9aaba610 Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/9aaba610 Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/9aaba610 Branch: refs/heads/master Commit: 9aaba610df0a51471f4933a1c1a7577685af6068 Parents: be0f179 Author: xiezl <[email protected]> Authored: Thu Dec 10 21:40:13 2015 +0800 Committer: xiezl <[email protected]> Committed: Thu Dec 10 21:40:13 2015 +0800 ---------------------------------------------------------------------- include/singa/driver.h | 7 ++++++ src/driver.cc | 5 +++++ tool/python/singa.py | 1 + tool/python/singa/driver.i | 3 ++- tool/python/singa/driver.py | 1 + tool/python/singa/driver_wrap.cxx | 39 +++++++++++++++++++++++++++++++++- tool/python/singa/generatepy.sh | 4 ++-- 7 files changed, 56 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/9aaba610/include/singa/driver.h ---------------------------------------------------------------------- diff --git a/include/singa/driver.h b/include/singa/driver.h index 1c73db1..f28f8c6 100644 --- a/include/singa/driver.h +++ b/include/singa/driver.h @@ -46,6 +46,13 @@ class Driver { */ void Init(int argc, char** argv); /** + * Init SINGA LOG + * Used for python binding. Users can also directly call it as a C++ API. + * - init glog with given parameters + * + */ + void InitLog(char *arg); + /** * Update job configuration and call Train(const JobProto&) to start the * training. * http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/9aaba610/src/driver.cc ---------------------------------------------------------------------- diff --git a/src/driver.cc b/src/driver.cc index 816e0df..4ffd9b1 100644 --- a/src/driver.cc +++ b/src/driver.cc @@ -150,6 +150,11 @@ void Driver::Init(int argc, char **argv) { RegisterParamGenerator<UniformSqrtFanInOutGen>(kUniformSqrtFanInOut); } +void Driver::InitLog(char* arg) +{ + google::InitGoogleLogging(arg); +} + void Driver::Train(bool resume, const std::string str){ JobProto job_conf; job_conf.ParseFromString(str); http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/9aaba610/tool/python/singa.py ---------------------------------------------------------------------- diff --git a/tool/python/singa.py b/tool/python/singa.py index 6d03331..c179192 100755 --- a/tool/python/singa.py +++ b/tool/python/singa.py @@ -38,5 +38,6 @@ if __name__ == '__main__': Merge(s,j) b = j.SerializeToString() d = driver.Driver() + d.InitLog(sys.argv[0]) d.Init(sys.argv) d.Train(False,b) http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/9aaba610/tool/python/singa/driver.i ---------------------------------------------------------------------- diff --git a/tool/python/singa/driver.i b/tool/python/singa/driver.i index 0054943..56599b6 100644 --- a/tool/python/singa/driver.i +++ b/tool/python/singa/driver.i @@ -27,7 +27,7 @@ %include "argcargv.i" %apply (int ARGC, char **ARGV) { (int argc, char **argv) } %{ -#include "../include/singa/driver.h" +#include "singa/driver.h" %} namespace singa{ @@ -36,6 +36,7 @@ class Driver{ public: void Train(bool resume, const std::string job_conf); void Init(int argc, char **argv); +void InitLog(char* arg); }; } http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/9aaba610/tool/python/singa/driver.py ---------------------------------------------------------------------- diff --git a/tool/python/singa/driver.py b/tool/python/singa/driver.py index f84df9c..c5f3b4f 100644 --- a/tool/python/singa/driver.py +++ b/tool/python/singa/driver.py @@ -106,6 +106,7 @@ class Driver(_object): __repr__ = _swig_repr def Train(self, *args): return _driver.Driver_Train(self, *args) def Init(self, *args): return _driver.Driver_Init(self, *args) + def InitLog(self, *args): return _driver.Driver_InitLog(self, *args) def __init__(self): this = _driver.new_Driver() try: self.this.append(this) http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/9aaba610/tool/python/singa/driver_wrap.cxx ---------------------------------------------------------------------- diff --git a/tool/python/singa/driver_wrap.cxx b/tool/python/singa/driver_wrap.cxx index 9ff35e9..99756f1 100644 --- a/tool/python/singa/driver_wrap.cxx +++ b/tool/python/singa/driver_wrap.cxx @@ -3413,7 +3413,7 @@ SWIG_AsVal_ptrdiff_t (PyObject * obj, ptrdiff_t *val) #include <vector> -#include "../include/singa/driver.h" +#include "singa/driver.h" SWIGINTERN int @@ -3605,6 +3605,9 @@ SWIG_AsArgcArgv(PyObject *input, } } + + + #ifdef __cplusplus extern "C" { #endif @@ -4543,6 +4546,39 @@ fail: } +SWIGINTERN PyObject *_wrap_Driver_InitLog(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + singa::Driver *arg1 = (singa::Driver *) 0 ; + char *arg2 = (char *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + int res2 ; + char *buf2 = 0 ; + int alloc2 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"OO:Driver_InitLog",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_singa__Driver, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Driver_InitLog" "', argument " "1"" of type '" "singa::Driver *""'"); + } + arg1 = reinterpret_cast< singa::Driver * >(argp1); + res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "Driver_InitLog" "', argument " "2"" of type '" "char *""'"); + } + arg2 = reinterpret_cast< char * >(buf2); + (arg1)->InitLog(arg2); + resultobj = SWIG_Py_Void(); + if (alloc2 == SWIG_NEWOBJ) delete[] buf2; + return resultobj; +fail: + if (alloc2 == SWIG_NEWOBJ) delete[] buf2; + return NULL; +} + + SWIGINTERN PyObject *_wrap_new_Driver(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; singa::Driver *result = 0 ; @@ -4606,6 +4642,7 @@ static PyMethodDef SwigMethods[] = { { (char *)"SwigPyIterator_swigregister", SwigPyIterator_swigregister, METH_VARARGS, NULL}, { (char *)"Driver_Train", _wrap_Driver_Train, METH_VARARGS, NULL}, { (char *)"Driver_Init", _wrap_Driver_Init, METH_VARARGS, NULL}, + { (char *)"Driver_InitLog", _wrap_Driver_InitLog, METH_VARARGS, NULL}, { (char *)"new_Driver", _wrap_new_Driver, METH_VARARGS, NULL}, { (char *)"delete_Driver", _wrap_delete_Driver, METH_VARARGS, NULL}, { (char *)"Driver_swigregister", Driver_swigregister, METH_VARARGS, NULL}, http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/9aaba610/tool/python/singa/generatepy.sh ---------------------------------------------------------------------- diff --git a/tool/python/singa/generatepy.sh b/tool/python/singa/generatepy.sh index 5b224f9..22176a8 100755 --- a/tool/python/singa/generatepy.sh +++ b/tool/python/singa/generatepy.sh @@ -22,8 +22,8 @@ #!/usr/bin/env bash #The following commands are only for developers adding new py apis. swig -c++ -python driver.i -g++ -fPIC /../../../src/driver.cc driver_wrap.cxx -shared -o _driver.so \ +g++ -fPIC ../../../src/driver.cc driver_wrap.cxx -shared -o _driver.so \ -L../../../.libs/ -lsinga -DMSHADOW_USE_CUDA=0 \ -DMSHADOW_USE_CBLAS=1 -DMSHADOW_USE_MKL=0 -std=c++11 \ - -I/../../../include \ + -I../../../include \ -I/usr/include/python2.7/
