Skip to content

Commit

Permalink
Merge pull request #1 from dmlc/master
Browse files Browse the repository at this point in the history
update mxnet
  • Loading branch information
yanqingmen committed Oct 7, 2015
2 parents 41c1bb7 + be460fb commit 6839852
Show file tree
Hide file tree
Showing 44 changed files with 1,637 additions and 118 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Debug
*.swp
*.swo
*.swn
.ycm_extra_conf.py
.ycm_extra_conf.pyc

# Emacs
.clang_complete
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ ifneq ($(USE_CUDA_PATH), NONE)
NVCC=$(USE_CUDA_PATH)/bin/nvcc
endif

.PHONY: clean all test lint doc clean_all
.PHONY: clean all test lint doc clean_all rcpplint

all: lib/libmxnet.a lib/libmxnet.so $(BIN)

Expand Down Expand Up @@ -118,9 +118,12 @@ include tests/cpp/unittest.mk

test: $(TEST)

lint:
lint: rcpplint
python dmlc-core/scripts/lint.py mxnet ${LINT_LANG} include src scripts python

rcpplint:
python dmlc-core/scripts/lint.py mxnet-rcpp ${LINT_LANG} R-package/src

doxygen:
doxygen doc/Doxyfile

Expand Down
7 changes: 7 additions & 0 deletions R-package/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
\.o$
\.so$
\.dll$
^.*\.Rproj$
^\.Rproj\.user$

README.md
2 changes: 2 additions & 0 deletions R-package/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.Rhistory
R-package.Rproj
13 changes: 13 additions & 0 deletions R-package/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Package: mxnet
Type: Package
Title: MXNet
Version: 0.5
Date: 2015-10-02
Author: Qiang Kou
Maintainer: Your <your@email.com>
Description: MXNet is a deep learning framework designed for both efficiency and flexibility. It allows you to mix the flavours of deep learning programs together to maximize the efficiency and your productivity.
License: Apache-2.0
URL: https://github.com/dmlc/mxnet
BugReports: https://github.com/dmlc/mxnet/issues
Imports: Rcpp (>= 0.11.1)
LinkingTo: Rcpp
3 changes: 3 additions & 0 deletions R-package/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
useDynLib(mxnet)
exportPattern("^[[:alpha:]]+")
import(Rcpp)
7 changes: 7 additions & 0 deletions R-package/R/base.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require(methods)

loadModule("mxnet", TRUE)
setOldClass("mx.NDArray")
setMethod("+", signature(e1="mx.NDArray", e2="numeric"), function(e1, e2) {
mx.nd.internal.plus.scalar(e1, e2)
})
5 changes: 5 additions & 0 deletions R-package/R/ndarray.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#' NDArray
#'
#' Additional NDArray related operations
require(methods)

24 changes: 24 additions & 0 deletions R-package/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
MXNet R-Package
===============
This is an on-going effort to support mxnet in R, stay tuned.

Bleeding edge Installation
- First build ```../lib/libmxnet.so``` by following [Build Instruction](doc/build.md)
- Set the path to ```lib/libmxnet.so``` in ```LD_LIBRARY_PATH```, you can do it by modify ```~/.bashrc```
```bash
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/path/to/lib/libmxnet.so
```
- Type ```R CMD INSTALL R-package``` in the root folder.


Contributing Style Guide
------------------------
- Most C++ of R package heavily relies on [Rcpp](https://github.com/RcppCore/Rcpp).
- We follow Google's C++ Style guide on C++ code.
- This is mainly to be consistent with the rest of the project.
- Another reason is we will be able to check style automatically with a linter.
- You can check the style of the code by typing the following command at root folder.
```bash
make rcpplint
```
- When needed, you can disable the linter warning of certain line with ```// NOLINT(*)``` comments.
1 change: 1 addition & 0 deletions R-package/demo/00Index
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
basic_ndarray Basic ndarray operations
10 changes: 10 additions & 0 deletions R-package/demo/basic_ndarray.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require(mxnet)
require(methods)
x = as.array(c(1,2,3))

mat = mx.nd.array(x, mx.cpu(0))
mat = mx.nd.internal.plus(mat, mat)
xx = mx.nd.internal.as.array(mat)
print(class(mat))
print(xx)

33 changes: 33 additions & 0 deletions R-package/man/mxnet-package.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
\name{mxnet-package}
\alias{mxnet-package}
\alias{mxnet}
\docType{package}
\title{
A short title line describing what the package does
}
\description{
A more detailed description of what the package does. A length
of about one to five lines is recommended.
}
\details{
This section should provide a more detailed overview of how to use the
package, including the most important functions.
}
\author{
Your Name, email optional.

Maintainer: Your Name <your@email.com>
}
\references{
This optional section can contain literature or other references for
background information.
}
% Optionally other standard keywords, one per line,
% from the file KEYWORDS in the R documentation.
\keyword{ package }
\seealso{
Optional links to other man pages
}
\examples{
# Optional simple examples of the most important functions
}
24 changes: 24 additions & 0 deletions R-package/src/Makevars
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# _*_ mode: makefile; _*_
PKGROOT=../../

# This file is only used for compilation from github
# It will be replaced by more formal Rpackage structure
# Where PKGROOT moved to root directory

.PHONY: all mxlib
all: $(SHLIB)

$(SHLIB): mxlib
mxlib:
cd $(PKGROOT); make CXX="$(CXX)"; cd -
cp $(PKGROOT)/lib/libmxnet.so libmxnet.so
mkdir -p ../inst
mkdir -p ../inst/libs
cp $(PKGROOT)/lib/libmxnet.so ../inst/libs/libmxnet.so

# Need to add libmxnet.so to LD_LIBRARY_PATH
# TODO: make it install together with mxnet.so
LINKMXNET = -L../inst/libs -lmxnet

PKG_CPPFLAGS = -I$(PKGROOT)/include -I$(PKGROOT)/dmlc-core/include
PKG_LIBS = $(LINKMXNET) $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
112 changes: 112 additions & 0 deletions R-package/src/base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*!
* Copyright (c) 2015 by Contributors
* \file base.h
* \brief Rcpp interface of MXNet
* All the interface is done through C API,
* to achieve maximum portability when we need different compiler for libmxnet.
*/
#ifndef MXNET_RCPP_BASE_H_
#define MXNET_RCPP_BASE_H_

#include <Rcpp.h>
#include <dmlc/base.h>
#include <mxnet/c_api.h>

/*! \brief namespace of mxnet */
namespace mxnet {
/*! \brief namespace of R package */
namespace R {

/*! \brief LOG FATAL to report error to R console */
#define RLOG_FATAL ::Rcpp::Rcerr

/*!
* \brief Checking macro for Rcpp code, report error ro R console
* \code
* RCHECK(data.size() == 1) << "Data size must be 1";
* \endcode
*/
#define RCHECK(x) \
if (!(x)) RLOG_FATAL << "Check failed: " #x << ' ' /* NOLINT(*) */

/*!
* \brief protected MXNet C API call, report R error if happens.
* \param func Expression to call.
*/
#define MX_CALL(func) \
{ \
int e = (func); \
if (e != 0) { \
RLOG_FATAL << MXGetLastError(); \
} \
}

/*! \brief macro to be compatible with non c++11 env */
#if DMLC_USE_CXX11 == 0
#define nullptr NULL
#endif

/*! \brief Context of device enviroment */
struct Context {
/*! \brief The device ID of the context */
int dev_type;
/*! \brief The device ID of the context */
int dev_id;
/*! \brief The R object type of the context */
typedef Rcpp::List RObjectType;
/*! \brief default constructor */
Context() {}
/*!
* \brief Constructor
* \param src source R representation.
*/
explicit Context(const Rcpp::RObject& src) {
Rcpp::List list(src);
this->dev_id = list[1];
this->dev_type = list[2];
}
/*! \return R object representation of the context */
inline RObjectType RObject() const {
const char *dev_name = "cpu";
if (dev_type == kGPU) dev_name = "gpu";
return Rcpp::List::create(
Rcpp::Named("device") = dev_name,
Rcpp::Named("device_id") = dev_id,
Rcpp::Named("device_typeid") = dev_type);
}
/*!
* Create a CPU context.
* \param dev_id the device id.
* \return CPU Context.
*/
inline static RObjectType CPU(int dev_id = 0) {
Context ctx;
ctx.dev_type = kCPU;
ctx.dev_id = dev_id;
return ctx.RObject();
}
/*!
* Create a GPU context.
* \param dev_id the device id.
* \return GPU Context.
*/
inline static RObjectType GPU(int dev_id) {
Context ctx;
ctx.dev_type = kGPU;
ctx.dev_id = dev_id;
return ctx.RObject();
}
/*! \brief initialize all the Rcpp module functions */
inline static void InitRcppModule() {
using namespace Rcpp; // NOLINT(*);
function("mx.cpu", &CPU);
function("mx.gpu", &GPU);
}
/*! \brief the device type id for CPU */
static const int kCPU = 1;
/*! \brief the device type id for GPU */
static const int kGPU = 2;
};
} // namespace R
} // namespace mxnet
#endif // MXNET_RCPP_BASE_H_
17 changes: 17 additions & 0 deletions R-package/src/mxnet.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*!
* Copyright (c) 2015 by Contributors
* \file mxnet.cc
* \brief The registry of all module functions and objects
*/
#include <Rcpp.h>
#include "./base.h"
#include "./ndarray.h"


RCPP_MODULE(mxnet) {
using namespace Rcpp;
using namespace mxnet::R;
Context::InitRcppModule();
NDArray::InitRcppModule();
NDArrayFunction::InitRcppModule();
}
Loading

0 comments on commit 6839852

Please sign in to comment.