Skip to content

Instantly share code, notes, and snippets.

@thirdwing
Last active April 6, 2018 02:03
Show Gist options
  • Save thirdwing/89aa9bfc588ade138496e6932072152c to your computer and use it in GitHub Desktop.
Save thirdwing/89aa9bfc588ade138496e6932072152c to your computer and use it in GitHub Desktop.
MXNet R installation with GPU support on Windows

MXNet R installation with GPU support on Windows

Clone the MXNet github repo

git clone --recursive https://github.com/dmlc/mxnet

The --recursive is to clone all the submodules used by MXNet.

Download prebuilt shared libraries for Windows.

Please download mxnet_x64_vc14_gpu.7z and prebuildbase_win10_x64_vc14.7z from https://github.com/yajiedesign/mxnet/releases

Download cuDNN from NVIDIA website

Please download cuDNN from https://developer.nvidia.com/cudnn

Add necessary dlls into the R-package folder

  1. Please mkdir the folder structure: ./R-package/inst/libs/x64;

  2. Copy the following dlls into ./R-package/inst/libs/x64:

cublas64_80.dll
cudart64_80.dll
cudnn64_5.dll
curand64_80.dll
libgcc_s_seh-1.dll
libgfortran-3.dll
libmxnet.dll
libmxnet.lib
libopenblas.dll
libquadmath-0.dll
nvrtc64_80.dll

These dlls can be found in prebuildbase_win10_x64_vc14/3rdparty/cudart, prebuildbase_win10_x64_vc14/3rdparty/openblas/bin, mxnet_x64_vc14_gpu/build, mxnet_x64_vc14_gpu/lib and the cuDNN downloaded from NVIDIA.

Add necessary header files into the R-package folder

  1. Please mkdir the folder structure: ./R-package/inst/include;

  2. Copy the header files from dmlc, mxnet and nnvm into ./R-package/inst/include. It should look like:

./R-package/inst
└── include
    ├── dmlc
    ├── mxnet
    └── nnvm

Build the R package using Rtools

Now please open the Windows CMD and change the directory to the mxnet folder. Then use the following commands to build R package:

echo import(Rcpp) > R-package\NAMESPACE
echo import(methods) >> R-package\NAMESPACE
Rscript -e "install.packages('devtools', repos = 'https://cloud.r-project.org')"
cd R-package
Rscript -e "library(devtools); library(methods); options(repos=c(CRAN='https://cran.rstudio.com')); install_deps(dependencies = TRUE)"
cd ..

R CMD INSTALL --no-multiarch R-package

Rscript -e "require(mxnet); mxnet:::mxnet.export('R-package')"
rm R-package/NAMESPACE
Rscript -e "require(devtools); install_version('roxygen2', version = '5.0.1', repos = 'https://cloud.r-project.org/', quiet = TRUE)"
Rscript -e "require(roxygen2); roxygen2::roxygenise('R-package')"

R CMD INSTALL --build --no-multiarch R-package
@thirdwing
Copy link
Author

The installation guide has been test on a EC2 instance with GPU and Windows Server 2012.

mxnet_r_gpu_win

@Pr0Chris
Copy link

Pr0Chris commented Jul 4, 2017

Hi,thanks for your guide. I have a problem at the step of building R package, so I've got some questions.

  1. Where should mxnet folder be at the beginning of building R-package?
  2. Isn't there extra steps before "R CMD INSTALL --no-multiarch R-package"? I've have a problem with "working make -f "Makevars.win"...

@Statislove
Copy link

Hi! I have the same questions.
I update my cmd for "Rscript". But where should mxnet forder be at the begining of buildidn R-package?
I must add folder mxnet with dll files to R\win-library\3.3, am i right?

@thirdwing
Copy link
Author

After git clone --recursive https://github.com/dmlc/mxnet, you should have a folder called mxnet.

@miguel-data-sc
Copy link

miguel-data-sc commented Sep 9, 2017

EDIT: Solved. managed to install it with the prebuilt GPU package, problem was CUDA version was not up to date.

Hello, thank you for the instructions, unfortunately not working for me on Windows 10, R 3.4.1, Rtools 3.4 (tried also Rtools 3.3 and 3.2).

As soon as it begins, compilation fails with this message:
In file included from executor.cc:9:0: ./base.h:12:23: fatal error: dmlc/base.h: No such file or directory
#include <dmlc/base.h> ^
compilation terminated.

I would be be grateful to get any hint on what the problem can be.

@alihashmiii
Copy link

alihashmiii commented Mar 7, 2018

The above method does not work. I get an error at this point:

C:\Users\aliha\mxnet>R CMD INSTALL --no-multiarch R-package
`In R CMD INSTALL

  • installing to library 'C:/Users/aliha/Documents/R/win-library/3.4'
  • installing source package 'mxnet' ...
    ** libs
    Warning: running command 'make -f "Makevars.win" -f "C:/PROGRA1/R/R-3.4.3/etc/x64/Makeconf" -f "C:/PROGRA1/R/R-3.4.3/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="mxnet.dll" WIN=64 TCLBIN=64 OBJECTS="executor.o export.o im2rec.o io.o kvstore.o mxnet.o ndarray.o symbol.o"' had status 127
    ERROR: compilation failed for package 'mxnet'
  • removing 'C:/Users/aliha/Documents/R/win-library/3.4/mxnet'

@haimeh
Copy link

haimeh commented Mar 14, 2018

@thirdwing
Would it be possible to include how you installed opencv in your Windows Server session?
There seems to be some ambiguity as to the correct way.
#8936

@alihashmiii
Your particular issue seems to be that the c++ compiler is not found, more specifically, the Rtools is not set on your path correctly (status 127) and isn't mxnet bug.

@haimeh
Copy link

haimeh commented Apr 6, 2018

If you are looking for a specific cuda version for your installation you might have some luck copying the *.dll and *.lib files from a python installation.
For instance, if you wanted the cuda 9.1 build (which is not available from https://github.com/yajiedesign/mxnet/releases upon writing this) you could:

pip install mxnet-cu91

If you don't know where pip installs packages you can:

pip show mxnet-cu91

You would then go to the directory indicated by "Location:" and find the "mxnet" dir.
In my case it was: "C:\Python\Python36\Lib\site-packages\mxnet"
You would then follow the steps from the guide, but using the *.dll and *.lib from the python installation when possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment