diff --git a/Makefile b/Makefile index 44c719f6d4d9..16d7d2393736 100644 --- a/Makefile +++ b/Makefile @@ -683,36 +683,6 @@ cython3: cyclean: rm -rf python/mxnet/*/*.so python/mxnet/*/*.cpp -# R related shortcuts -rcpplint: - 3rdparty/dmlc-core/scripts/lint.py mxnet-rcpp ${LINT_LANG} R-package/src - -rpkg: - mkdir -p R-package/inst/libs - cp src/io/image_recordio.h R-package/src - cp -rf lib/libmxnet.so R-package/inst/libs - - if [ -e "lib/libtvm_runtime.so" ]; then \ - cp -rf lib/libtvm_runtime.so R-package/inst/libs; \ - fi - - mkdir -p R-package/inst/include - cp -rl include/* R-package/inst/include - Rscript -e "if(!require(devtools)){install.packages('devtools', repo = 'https://cloud.r-project.org/')}" - Rscript -e "if(!require(roxygen2)||packageVersion('roxygen2') < '6.1.1'){install.packages('roxygen2', repo = 'https://cloud.r-project.org/')}" - Rscript -e "library(devtools); library(methods); options(repos=c(CRAN='https://cloud.r-project.org/')); install_deps(pkg='R-package', dependencies = TRUE)" - cp R-package/dummy.NAMESPACE R-package/NAMESPACE - echo "import(Rcpp)" >> R-package/NAMESPACE - R CMD INSTALL R-package - Rscript -e "require(mxnet); mxnet:::mxnet.export('R-package'); warnings()" - rm R-package/NAMESPACE - Rscript -e "devtools::document('R-package');warnings()" - R CMD INSTALL R-package - -rpkgtest: - Rscript -e 'require(testthat);res<-test_dir("R-package/tests/testthat");if(!testthat:::all_passed(res)){stop("Test failures", call. = FALSE)}' - Rscript -e 'res<-covr:::package_coverage("R-package");fileConn<-file(paste("r-package_coverage_",toString(runif(1)),".json"));writeLines(covr:::to_codecov(res), fileConn);close(fileConn)' - scalaclean: (cd $(ROOTDIR)/scala-package && mvn clean) diff --git a/R-package/Makefile b/R-package/Makefile new file mode 100644 index 000000000000..68249033f595 --- /dev/null +++ b/R-package/Makefile @@ -0,0 +1,33 @@ +rcpplint: + 3rdparty/dmlc-core/scripts/lint.py mxnet-rcpp ${LINT_LANG} R-package/src + +rpkg: + mkdir -p R-package/inst/libs + cp src/io/image_recordio.h R-package/src + if [ -d "lib" ]; then \ + cp -rf lib/libmxnet.so R-package/inst/libs; \ + if [ -e "lib/libtvm_runtime.so" ]; then \ + cp -rf lib/libtvm_runtime.so R-package/inst/libs; \ + fi; \ + else \ + cp -rf build/libmxnet.so R-package/inst/libs; \ + if [ -e "build/libtvm_runtime.so" ]; then \ + cp -rf build/libtvm_runtime.so R-package/inst/libs; \ + fi; \ + fi + + mkdir -p R-package/inst/include + cp -rl include/* R-package/inst/include + Rscript -e "if(!require(devtools)){install.packages('devtools', repo = 'https://cloud.r-project.org/')}" + Rscript -e "if(!require(roxygen2)||packageVersion('roxygen2') < '6.1.1'){install.packages('roxygen2', repo = 'https://cloud.r-project.org/')}" + Rscript -e "library(devtools); library(methods); options(repos=c(CRAN='https://cloud.r-project.org/')); install_deps(pkg='R-package', dependencies = TRUE)" + cp R-package/dummy.NAMESPACE R-package/NAMESPACE # NAMESPACE will be replaced by devtools::document later + echo "import(Rcpp)" >> R-package/NAMESPACE + R CMD INSTALL R-package + Rscript -e "require(mxnet); mxnet:::mxnet.export('R-package'); warnings()" + Rscript -e "devtools::document('R-package');warnings()" + R CMD INSTALL R-package + +rpkgtest: + Rscript -e 'require(testthat);res<-test_dir("R-package/tests/testthat");if(!testthat:::all_passed(res)){stop("Test failures", call. = FALSE)}' + Rscript -e 'res<-covr:::package_coverage("R-package");fileConn<-file(paste("r-package_coverage_",toString(runif(1)),".json"));writeLines(covr:::to_codecov(res), fileConn);close(fileConn)' diff --git a/R-package/R/callback.R b/R-package/R/callback.R index 014f926bd9f5..04f31b727ed2 100644 --- a/R-package/R/callback.R +++ b/R-package/R/callback.R @@ -16,6 +16,7 @@ # under the License. #' @export mx.metric.logger +library(methods) mx.metric.logger <- setRefClass("mx.metric.logger", fields = list(train = "numeric", eval="numeric")) #' Log training metric each period diff --git a/R-package/R/zzz.R b/R-package/R/zzz.R index a4f03ace0cc1..1b185978b8e2 100644 --- a/R-package/R/zzz.R +++ b/R-package/R/zzz.R @@ -34,8 +34,18 @@ NULL .onLoad <- function(libname, pkgname) { # Require methods for older versions of R require(methods) - tryCatch(library.dynam("libmxnet", pkgname, libname, local=FALSE), error = function(e) { print('Loading local: inst/libs/libmxnet.so'); dyn.load("R-package/inst/libs/libmxnet.so", local=FALSE) }) - tryCatch(library.dynam("mxnet", pkgname, libname), error = function(e) { print('Loading local: src/mxnet.so'); dyn.load("R-package/src/mxnet.so") }) + tryCatch(library.dynam("libmxnet", pkgname, libname, local=FALSE), error = function(e) { + print('Loading libmxnet.so failed. Error:') + print(e) + print('Falling back to loading local: inst/libs/libmxnet.so') + dyn.load("R-package/inst/libs/libmxnet.so", local=FALSE) + }) + tryCatch(library.dynam("mxnet", pkgname, libname), error = function(e) { + print('Loading mxnet.so failed. Error:') + print(e) + print('Falling back to loading local: src/mxnet.so') + dyn.load("R-package/src/mxnet.so") + }) loadModule("mxnet", TRUE) init.symbol.methods() init.context.default() diff --git a/R-package/src/Makevars b/R-package/src/Makevars index c089c093389b..13df4d1db8eb 100644 --- a/R-package/src/Makevars +++ b/R-package/src/Makevars @@ -1,3 +1,4 @@ CXX_STD = CXX11 -PKG_CPPFLAGS = -I../inst/include -PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) +PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) `pkg-config --libs opencv` +PKG_CFLAGS = `pkg-config --cflags opencv` +PKG_CPPFLAGS = -I../inst/include `pkg-config --cflags opencv` `Rscript -e 'Rcpp:::CxxFlags()'` diff --git a/R-package/src/Makevars.win b/R-package/src/Makevars.win index c75a7583d1da..7be3d8f935b1 100644 --- a/R-package/src/Makevars.win +++ b/R-package/src/Makevars.win @@ -1,3 +1,2 @@ - PKG_CPPFLAGS = -I../inst/include PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) -L../inst/libs/x64/ -llibmxnet diff --git a/ci/docker/runtime_functions.sh b/ci/docker/runtime_functions.sh index b4b0baac2600..381cb567fbc0 100755 --- a/ci/docker/runtime_functions.sh +++ b/ci/docker/runtime_functions.sh @@ -1223,12 +1223,11 @@ unittest_ubuntu_cpu_R() { build_ccache_wrappers echo "MAKEFLAGS = -j"$(nproc) > ~/.R/Makevars # make -j not supported - make rpkg \ - USE_BLAS=openblas \ + make -f R-package/Makefile rpkg \ R_LIBS=/tmp/r-site-library R CMD INSTALL --library=/tmp/r-site-library R-package - make rpkgtest R_LIBS=/tmp/r-site-library + make -f R-package/Makefile rpkgtest R_LIBS=/tmp/r-site-library } unittest_ubuntu_minimal_R() { @@ -1239,8 +1238,7 @@ unittest_ubuntu_minimal_R() { build_ccache_wrappers echo "MAKEFLAGS = -j"$(nproc) > ~/.R/Makevars # make -j not supported - make rpkg \ - USE_BLAS=openblas \ + make -f R-package/Makefile rpkg \ R_LIBS=/tmp/r-site-library R CMD INSTALL --library=/tmp/r-site-library R-package @@ -1269,11 +1267,10 @@ unittest_ubuntu_gpu_R() { build_ccache_wrappers echo "MAKEFLAGS = -j"$(nproc) > ~/.R/Makevars # make -j not supported - make rpkg \ - USE_BLAS=openblas \ + make -f R-package/Makefile rpkg \ R_LIBS=/tmp/r-site-library R CMD INSTALL --library=/tmp/r-site-library R-package - make rpkgtest R_LIBS=/tmp/r-site-library R_GPU_ENABLE=1 + make -f R-package/Makefile rpkgtest R_LIBS=/tmp/r-site-library R_GPU_ENABLE=1 } unittest_ubuntu_cpu_julia() { diff --git a/docs/static_site/src/_includes/get_started/linux/r/cpu.md b/docs/static_site/src/_includes/get_started/linux/r/cpu.md index 88ca5dd39933..1077362c9a65 100644 --- a/docs/static_site/src/_includes/get_started/linux/r/cpu.md +++ b/docs/static_site/src/_includes/get_started/linux/r/cpu.md @@ -5,5 +5,6 @@ After you have setup R v3.4.4+ and MXNet, you can build and install the MXNet R {% highlight bash %} $ cd incubator-mxnet -$ make rpkg -{% endhighlight %} \ No newline at end of file +$ mkdir build; cd build; cmake -DUSE_CUDA=OFF ..; make -j $(nproc); cd .. +$ make -f R-package/Makefile rpkg +{% endhighlight %} diff --git a/docs/static_site/src/_includes/get_started/linux/r/gpu.md b/docs/static_site/src/_includes/get_started/linux/r/gpu.md index 16fbfd09d4d4..1ef16df25da5 100644 --- a/docs/static_site/src/_includes/get_started/linux/r/gpu.md +++ b/docs/static_site/src/_includes/get_started/linux/r/gpu.md @@ -10,7 +10,8 @@ MXNet as follows: {% highlight bash %} $ cd incubator-mxnet -$ make rpkg +$ mkdir build; cd build; cmake ..; make -j $(nproc); cd .. +$ make -f R-package/Makefile rpkg {% endhighlight %} -{% include /get_started/gpu_snippet.md %} \ No newline at end of file +{% include /get_started/gpu_snippet.md %} diff --git a/docs/static_site/src/pages/get_started/osx_setup.md b/docs/static_site/src/pages/get_started/osx_setup.md index 369edb36c148..3ba30300fb3e 100644 --- a/docs/static_site/src/pages/get_started/osx_setup.md +++ b/docs/static_site/src/pages/get_started/osx_setup.md @@ -202,7 +202,7 @@ Run the following commands to install the MXNet dependencies and build the MXNet cd R-package Rscript -e "library(devtools); library(methods); options(repos=c(CRAN='https://cran.rstudio.com')); install_deps(dependencies = TRUE)" cd .. - make rpkg + make -f R-package/Makefile rpkg ``` ## Install the MXNet Package for Julia diff --git a/docs/static_site/src/pages/get_started/ubuntu_setup.md b/docs/static_site/src/pages/get_started/ubuntu_setup.md index 65e8a625e496..de45e80feb56 100644 --- a/docs/static_site/src/pages/get_started/ubuntu_setup.md +++ b/docs/static_site/src/pages/get_started/ubuntu_setup.md @@ -363,23 +363,9 @@ make install ### Install the MXNet Package for R -Building *MXNet* from source is a 2 step process. -1. Build the *MXNet* core shared library, `libmxnet.so`, from source. -2. Build the R bindings. - -#### Quick MXNet-R Installation -You can quickly build MXNet-R with the following two scripts found in the `/docs/install` folder: - -```bash -git clone --recursive https://github.com/apache/incubator-mxnet.git mxnet -cd mxnet/docs/install -./install_mxnet_ubuntu_python.sh -./install_mxnet_ubuntu_r.sh -``` - -Or you can go through a manual process described next. - -#### Manual MXNet-R Installation +Before you build MXNet for R from source code, you must complete +[building the shared library](#build-the-shared-library). +source root directory to build the MXNet Perl package: **Minimum Requirements** 1. [GCC 4.8](https://gcc.gnu.org/gcc-4.8/) or later to compile C++ 11. @@ -389,60 +375,43 @@ Or you can go through a manual process described next. **Build the MXNet core shared library** -**Step 1** Install build tools and git. +**Step 1** Install R, cran-devtools and dependencies + +To install R and the devtools, run ```bash sudo apt-get update -sudo apt-get install -y build-essential git +sudo apt-get install -y r-base-core r-cran-devtools libcairo2-dev libxml2-dev ``` -**Step 2** Install OpenBLAS. +`libxml2-dev` is required for the `roxygen2` dependency and `libcairo2-dev` is +required for the suggested `imager` dependency. -*MXNet* uses [BLAS](https://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms) -and [LAPACK](https://en.wikipedia.org/wiki/LAPACK) libraries for accelerated numerical computations -on CPU machine. There are several flavors of BLAS/LAPACK libraries - [OpenBLAS](https://www.openblas.net/), -[ATLAS](http://math-atlas.sourceforge.net/) and [MKL](https://software.intel.com/en-us/intel-mkl). In this step we -install OpenBLAS. You can choose to install ATLAS or MKL. +To generate documentation, it is also required to install `roxygen2`. ```bash -sudo apt-get install -y libopenblas-dev liblapack-dev +R +> install.packages("roxygen2") +> Would you like to use a personal library instead? (y/n) y +> Would you like to create a personal library ... to install packages into? (y/n) y ``` -**Step 3** Install OpenCV. +Note: To successfully complete the next step, you need a personal R library. If +you were able to run `install.packages("roxygen2")` above, you either had +already, or you have successfully created a personal library just now. -*MXNet* uses [OpenCV](https://opencv.org/) for efficient image loading and augmentation operations. +**Step 2** Make and install the MXNet-R bindings. ```bash -sudo apt-get install -y libopencv-dev +make -f R-package/Makefile rpkg ``` -**Step 4** Download MXNet sources and build MXNet core shared library. You can clone the repository as described in the -following code block, or you may try the [download links](download) for your desired MXNet version. - -```bash -git clone --recursive https://github.com/apache/incubator-mxnet mxnet -cd mxnet -echo "USE_OPENCV = 1" >> ./config.mk -echo "USE_BLAS = openblas" >> ./config.mk -make -j $(nproc) -``` - -*Note* - USE_OPENCV and USE_BLAS are make file flags to set compilation options to use OpenCV and BLAS library. You can -explore and use more compilation options in `make/config.mk`. - -
- -**Step 5** Make and install the MXNet-R bindings. - -```bash -make rpkg -``` #### Verify MXNet-R Installation You can verify your MXNet-R installation as follows: ```bash -sudo -i R +R ``` At the R prompt enter the following: diff --git a/setup-utils/install-mxnet-ubuntu-r.sh b/setup-utils/install-mxnet-ubuntu-r.sh index ca46d7b37016..a07d9d8ef10e 100644 --- a/setup-utils/install-mxnet-ubuntu-r.sh +++ b/setup-utils/install-mxnet-ubuntu-r.sh @@ -59,7 +59,7 @@ sudo Rscript -e "library(devtools); library(methods); options(repos=c(CRAN='http cd .. echo "Compiling R package. This can take few minutes..." -sudo make rpkg +sudo make -f R-package/Makefile rpkg echo "Installing R package..." sudo R CMD INSTALL mxnet_current_r.tar.gz