Tell Rstudio to use mpif90 instead of gfortran - r

In order to learn how to create a R packages that uses Fortran I created a simple one to calculate Pi with a monte carlo simulation. Now I'm trying to make the Fortran code parallel with MPI.
When I add use MPI to my fortran code and click the Build and Reload buton on Rstudio I get the following error.
==> R CMD INSTALL --no-multiarch --with-keep.source MyPi
* installing to library ‘/home/ignacio/R/x86_64-pc-linux-gnu-library/3.2’
* installing *source* package ‘MyPi’ ...
gfortran -fpic -g -O2 -fstack-protector-strong -c Fpi.f90 -o Fpi.o
/usr/lib/R/etc/Makeconf:161: recipe for target 'Fpi.o' failed
** libs
Fpi.f90:2.4:
use mpi
1
Fatal Error: Can't open module file 'mpi.mod' for reading at (1): No such file or directory
make: *** [Fpi.o] Error 1
ERROR: compilation failed for package ‘MyPi’
* removing ‘/home/ignacio/R/x86_64-pc-linux-gnu-library/3.2/MyPi’
* restoring previous ‘/home/ignacio/R/x86_64-pc-linux-gnu-library/3.2/MyPi’
Exited with status 1.
I think the problem is that Rstudio is trying to compile using gfortran instead of mpi90. I'm able to follow this post to do a very basic hello world, but there you compile the code with mpif90 -fpic -shared -o test.so test.f90
How can I tell Rstudio to use mpif90 -fpic -shared instead of gfortran?
I added this to src/Makevars
FC=mpif90
This is what I get:
==> R CMD INSTALL --no-multiarch --with-keep.source MyPi
* installing to library ‘/home/ignacio/R/x86_64-pc-linux-gnu-library/3.2’
* installing *source* package ‘MyPi’ ...
gfortran -fpic -g -O2 -fstack-protector-strong -c Fpi.f90 -o Fpi.o
/usr/lib/R/etc/Makeconf:161: recipe for target 'Fpi.o' failed
** libs
Fpi.f90:2.4:
USE MPI
1
Fatal Error: Can't open module file 'mpi.mod' for reading at (1): No such file or directory
make: *** [Fpi.o] Error 1
ERROR: compilation failed for package ‘MyPi’
* removing ‘/home/ignacio/R/x86_64-pc-linux-gnu-library/3.2/MyPi’
* restoring previous ‘/home/ignacio/R/x86_64-pc-linux-gnu-library/3.2/MyPi’
Exited with status 1.

You just need to set the FC flag in your Makevars file to indicate the fortran compiler you wish to use.
FC=mpif90
EDIT - The above doesn't work if included locally in your package
The FC macro will be overwritten when specified within a package Makevars because the src/Makevars file is read before $RHOME/etc/Makeconf which overwrites the FC macro. In order to change the default compiler is to create a Makevars file in a .R/ directory in your $HOME directory.
Then just create the file with the same line as above. Restart R and try to compile again (this worked on my Ubuntu system).

Related

R package build failing on Windows machines (AppVeyor) due to missing GSL - GNU Scientific Library

I am developing an R package called DescObs (https://github.com/MaaniBeigy/DescObs). It imports conf.limits.nct function from MBESS, which itself depends on another package called gsl. It shows no problems at all for R CMD check and the installation of DescObs on Linux machines assessed by Travis continuous integration platform (https://travis-ci.org/MaaniBeigy/DescObs). However, AppVeyor fails:
** libs
*** arch - i386
C:/Rtools/mingw_32/bin/gcc -I"c:/R/include" -DNDEBUG -I/include -O3 -Wall -std=gnu99 -mtune=generic -c airy.c -o airy.o
airy.c:1:29: fatal error: gsl/gsl_sf_airy.h: No such file or directory
#include <gsl/gsl_sf_airy.h>
^
compilation terminated.
make: *** [c:/R/etc/i386/Makeconf:208: airy.o] Error 1
ERROR: compilation failed for package 'gsl'
* removing 'c:/RLibrary/gsl'
Error in i.p(...) :
(converted from warning) installation of package 'gsl' had non-zero exit status
Calls: <Anonymous> ... with_rprofile_user -> with_envvar -> force -> force -> i.p
Execution halted
Command exited with code 1
I also tried the solution recommended in Building an R package that uses the GSL on Windows but it did not help. I also compiled it using mingw-w64 and gcc-4.6.3, which both showed the same error.
Moreover, I tried to install by - cmd: vcpkg install gsl:x64-windows, but did show the same error. Here, you can see my appveyor.yml.
Do you have any ideas?
Fortunately, my previous appveyor.yml could finally build it:
install:
- ps: Bootstrap
- cmd: git submodule update --init --recursive
- cmd: git clone https://github.com/CxxTest/cxxtest
- cmd: cd c:\tools\vcpkg
- cmd: vcpkg integrate install
- cmd: vcpkg install gsl:x64-windows
- cmd: vcpkg install fftw3:x64-windows
- cmd: cd "%APPVEYOR_BUILD_FOLDER%"
environment:
global:
USE_RTOOLS: true
NOT_CRAN: true
_R_CHECK_FORCE_SUGGESTS: true
_R_CHECK_CRAN_INCOMING_: true
#R_CHECK_ARGS: "--run-dontrun"
matrix:
- R_VERSION: devel
R_ARCH: x64
GCC_PATH: gcc-4.6.3
I do not know the exact reason, but I am glad it works.
edit: I faced the problem again in another package. This line by Owen McDonnell image: previous visual studio 2015 could solve the problem. Refer to cvcqv package for the full appveyor.yml.
I came to this thread because I faced what I think is a very similar situation when I tried to update the package gsl:
installing source package 'gsl' ...
** package 'gsl' successfully unpacked and MD5 sums checked
** using staged installation
********************************************** WARNING: this
package has a configure script
It probably needs manual configuration **********************************************
** libs "C:/rtools40/mingw64/bin/"gcc -I"C:/PROGRA~1/R/R-4.1.1/include" -DNDEBUG -I/include -O2 -Wall -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c airy.c -o airy.o airy.c:1:10: fatal error: gsl/gsl_sf_airy.h: No such file or
directory #include <gsl/gsl_sf_airy.h>
^~~~~~~~~~~~~~~~~~~ compilation terminated. make: *** [C:/PROGRA~1/R/R-4.1.1/etc/x64/Makeconf:238: airy.o] Error 1 ERROR:
compilation failed for package 'gsl'
removing 'C:/Users/shima/OneDrive/Documents/R/win-library/4.1/gsl'
restoring previous 'C:/Users/shima/OneDrive/Documents/R/win-library/4.1/gsl' Warning in
install.packages : installation of package ‘gsl’ had non-zero exit
status
This thread helped me find the solution, a warm thank you, along with this discussion and this one
For those of you on Windows, using Rtools, here are my steps to solve this issue, i.e. not being able to update gsl package in R under windows:
start Rtools Bash (found in apps, Rtools 4.0 in the Windows menu)
Run pacman -S mingw-w64-x86_64-gsl and confirm with yes (y)
I don't think it's relevant, but i ran pacman -Syu just before to update my Rtools and throwing the command pacman -Sl gave me the correct name of the package for the pacman command in 2.
Updating gsl in Rstudio now leads to a clean
DONE (gsl)

R package source install, no compiler

I am trying to install some packages from source on a linux (RHEL) server. But whenever the package requires some C++ compilation it fails.
For example, I try to install Ckmeans.1d.dp package. If I call R CMD INSTALL Ckmeans.1.dp_4.2.1.tar.gz I get the following output (truncated) where you can see the compiler is omitted.
...* installing *source* package ‘Ckmeans.1d.dp’ ...
** libs
I/usr/include/R -DNDEBUG -I/usr/local/include -c Ckmeans.1d.dp.cpp -o Ckmeans.1d.dp.o
make: I/usr/include/R: Command not found
...
-shared -L/usr/local/lib64 -o Ckmeans.1d.dp.so Ckmeans.1d.dp.o Ckmeans.1d.dp_main.o dynamic_prog.o fill_SMAWK.o fill_log_linear.o fill_quadratic.o select_levels.o weighted_select_levels.o -L/usr/lib64/R/lib -lR
/bin/sh: line 2: -shared: command not found
make: *** [Ckmeans.1d.dp.so] Error 127
ERROR: compilation failed for package ‘Ckmeans.1d.dp’
I am currently stuck with a server that only has R 3.1.1 and I do not have admin privileges. The $Rhome/etc/Makeconf has CXX and CC defined. I have also verified g++ and gcc are installed.
What could be happening here?
Invariably an environment variable with an 'empty' value gets expanded. Maybe (just guessing here) the package would use C++11, so $(CXX11} -shared gets expanded and ... becomes -shared and an error is triggered.
Look at the R settings, eg via less $(R HOME)/etc/Makevars and see what it expects.
I think you can get by, if you must, installing gcc et al below, say, ~/bin. Some packages will require a compiler...

R package DESCRIPTION - Remotes from Bioconductor should install binary

I am working on an R package (https://github.com/bgbrink/dropClust) and I was testing if it can be installed, since it depends on a number of packages both from CRAN and from Bioconductor. I specified the three dependencies from Bioconductor in the DESCRIPTION:
Remotes:
bioc::flowDensity,
bioc::SamSPECTRAL,
bioc::flowPeaks
However, when I try to install the package, the installation of flowPeaks fails, because the script downloades the source version of the package, which requires GSL to be present in order to be compiled.
* installing *source* package ‘flowPeaks’ ...
** libs
clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include `gsl-config --cflags` -fPIC -Wall -g -O2 -c Rpack.cpp -o Rpack.o
/bin/sh: gsl-config: command not found
In file included from Rpack.cpp:16:
./gvector_gmatrix.h:24:10: fatal error: 'gsl/gsl_math.h' file not found
#include <gsl/gsl_math.h>
^
1 error generated.
make: *** [Rpack.o] Error 1
ERROR: compilation failed for package ‘flowPeaks’
When I download the package manually from Bioconductor, everything works fine, because the script downloades the binary version of the package. Can I specify somewhere that this is the default behaviour? I tried options(pkgType = "binary") without success.
Edit: No new ideas, so I'm bumping it once.

Package Passes R CMD check in Linux, Windows Binary Fails

I submitted version 0.1-8 of a robust package I created to CRAN. R CMD Check passed on my computer runnning 64 bit Ubuntu and Rstudio. The Linux and Mac binaries are passing on CRAN. The windows binaries are failing on CRAN. The only difference between version 0.1-7 and 0.1-8 is I changed the namespace to adhere to the new version of R and I improved the documentation. None of the code has been changed.
https://cran.r-project.org/web/packages/robustreg/index.html
https://cran.r-project.org/web/checks/check_results_robustreg.html
Can anyone help me with the below error message?
* using log directory 'd:/Rcompile/CRANpkg/local/3.2/robustreg.Rcheck'
* using R version 3.2.1 (2015-06-18)
* using platform: x86_64-w64-mingw32 (64-bit)
* using session charset: ISO8859-1
* checking for file 'robustreg/DESCRIPTION' ... OK
* this is package 'robustreg' version '0.1-8'
* checking package namespace information ... OK
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking whether package 'robustreg' can be installed ... ERROR
Installation failed.
See 'd:/Rcompile/CRANpkg/local/3.2/robustreg.Rcheck/00install.out' for details.
* DONE
The installation logfile:
* installing *source* package 'robustreg' ...
** package 'robustreg' successfully unpacked and MD5 sums checked
** libs
*** arch - i386
make[1]: Entering directory `/cygdrive/d/temp/RtmpI9QsDz/R.INSTALL4b704d36335d/robustreg/src-i386'
g++ -I"D:/RCompile/recent/R-3.2.1/include" -I"d:/RCompile/CRANpkg/lib/3.2/Rcpp/include" -I"d:/RCompile/CRANpkg/lib/3.2/RcppArmadillo/include" -I"d:/RCompile/r-compiling/local/local320/include" -O3 -Wall -mtune=core2 -c RcppExports.cpp -o RcppExports.o
g++ -I"D:/RCompile/recent/R-3.2.1/include" -I"d:/RCompile/CRANpkg/lib/3.2/Rcpp/include" -I"d:/RCompile/CRANpkg/lib/3.2/RcppArmadillo/include" -I"d:/RCompile/r-compiling/local/local320/include" -O3 -Wall -mtune=core2 -c fit.cpp -o fit.o
g++ -shared -s -static-libgcc -o robustreg.dll tmp.def RcppExports.o fit.o -Ld:/RCompile/r-compiling/local/local320/lib/i386 -Ld:/RCompile/r-compiling/local/local320/lib -LD:/RCompile/recent/R-3.2.1/bin/i386 -lR
fit.o:fit.cpp:(.text$_ZN4arma27glue_times_redirect2_helperILb0EE5applyINS_3MatIdEENS_3ColIdEEEEvRNS3_INT_9elem_typeEEERKNS_4GlueIS7_T0_NS_10glue_timesEEE[void arma::glue_times_redirect2_helper<false>::apply<arma::Mat<double>, arma::Col<double> >(arma::Mat<arma::Mat<double>::elem_type>&, arma::Glue<arma::Mat<double>, arma::Col<double>, arma::glue_times> const&)]+0x262): undefined reference to `dgemv_'
fit.o:fit.cpp:(.text$_ZN4arma27glue_times_redirect2_helperILb0EE5applyINS_3MatIdEENS_3ColIdEEEEvRNS3_INT_9elem_typeEEERKNS_4GlueIS7_T0_NS_10glue_timesEEE[void arma::glue_times_redirect2_helper<false>::apply<arma::Mat<double>, arma::Col<double> >(arma::Mat<arma::Mat<double>::elem_type>&, arma::Glue<arma::Mat<double>, arma::Col<double>, arma::glue_times> const&)]+0x4a2): undefined reference to `dgemv_'
fit.o:fit.cpp:(.text$_ZN4arma27glue_times_redirect2_helperILb0EE5applyINS_3MatIdEENS_3ColIdEEEEvRNS3_INT_9elem_typeEEERKNS_4GlueIS7_T0_NS_10glue_timesEEE[void arma::glue_times_redirect2_helper<false>::apply<arma::Mat<double>, arma::Col<double> >(arma::Mat<arma::Mat<double>::elem_type>&, arma::Glue<arma::Mat<double>, arma::Col<double>, arma::glue_times> const&)]+0xb4c): undefined reference to `dgemv_'
collect2: ld returned 1 exit status
make[1]: Leaving directory `/cygdrive/d/temp/RtmpI9QsDz/R.INSTALL4b704d36335d/robustreg/src-i386'
make[1]: Entering directory `/cygdrive/d/temp/RtmpI9QsDz/R.INSTALL4b704d36335d/robustreg/src-i386'
make[1]: Leaving directory `/cygdrive/d/temp/RtmpI9QsDz/R.INSTALL4b704d36335d/robustreg/src-i386'
no DLL was created
ERROR: compilation failed for package 'robustreg'
* removing 'd:/Rcompile/CRANpkg/lib/3.2/robustreg'
* restoring previous 'd:/Rcompile/CRANpkg/lib/3.2/robustreg'
According to its automatically-created GitHub mirror and the src/ directory there in, you do not have a file src/Makevars.win. And you now have a linking error with LAPACK routines.
Simply add a file such as this one added when you use RcppArmadillo.package.skeleton() containing just
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
and you should be all set:
I am surprised this worked previously. You mostly got lucky--our documentation clearly says you need it.

error in R CMD build when editing c function in R package

I have modified a C function in the phmm package. Now I want to rebuild the package, but the following error occurred. How can I fix the error?
$ cd ~/Desktop/phmmw
$ R CMD build phmm
* checking for file ‘phmm/DESCRIPTION’ ... OK
* preparing ‘phmm’:
* checking DESCRIPTION meta-information ... OK
* cleaning src
* installing the package to re-build vignettes
-----------------------------------
* installing *source* package ‘phmm’ ...
** libs
*** arch - x86_64
gcc-4.2 -arch x86_64 -std=gnu99 -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/x86_64 -I/usr/local/include -fPIC -g -O2 -c arms.c -o arms.o
make: gcc-4.2: No such file or directory
make: *** [arms.o] Error 1
ERROR: compilation failed for package ‘phmm’
* removing ‘/private/var/folders/r0/9qv15p6j59j6jtfq775k6j380000gn/T/RtmpIPgvDE/Rinst48317ea7/phmm’
-----------------------------------
ERROR: package installation failed
Your R installation was configured with gcc in such a (suboptimal) was that the gcc-4.2 command was hardwired.
So you need to
either provide a command gcc-4.2, most commonly done via a symlink from gcc to gcc-4.2,
or change the R setting, presumably in the file $RHOME/etc/Makeconf where you need to look for CC
or change it on the fly by doing CC=gcc R CMD build phmm
You may need to modify more than just one CC setting, possibly CXX, LD and more. It may be as easy as always removing the -4.2 part.

Resources