forked from apache/mxnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dmlc/master
update mxnet
- Loading branch information
Showing
44 changed files
with
1,637 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,8 @@ Debug | |
*.swp | ||
*.swo | ||
*.swn | ||
.ycm_extra_conf.py | ||
.ycm_extra_conf.pyc | ||
|
||
# Emacs | ||
.clang_complete | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
\.o$ | ||
\.so$ | ||
\.dll$ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ | ||
|
||
README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.Rhistory | ||
R-package.Rproj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
useDynLib(mxnet) | ||
exportPattern("^[[:alpha:]]+") | ||
import(Rcpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#' NDArray | ||
#' | ||
#' Additional NDArray related operations | ||
require(methods) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
basic_ndarray Basic ndarray operations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.