2

I have a R script that tries to install many packages (omitted all but showing just one):

install.packages("zoo")

Then I run

Rscript my_r.r

Then, I noticed it will try to compile some C code:

gcc -m64 -std=gnu99 -I"/usr/include/R" -DNDEBUG -I../inst/include -I"/latest/rsg_comm/r_packages/zoo/include" -I/usr/local/include  -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c any.c -o any.o

Is there a way to ask all packages to be compiled locally with -O3 and -mtune=native?

I noticed that there is a similar post that suggests using ~/R/.Makevars. But it seems like

  1. it will override all compiler flags instead of just those 2 I specify. Is there a way to specify?
  2. I have to download the source package of each source, which is not as convenient as just using install.packages("package_name"), which will figure the latest version, and go through a mirror, etc. Or there is a convenient way?
HCSF
  • 2,387
  • 1
  • 14
  • 40
  • 1
    Adding those flags to the .R/Makevars file doesn't override all flags and I wouldn't expect it to cause any issues. Maybe try it and see how it goes? If you get errors, adjust accordingly. Anecdotally, in my Makevars I have `-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe` flags and don't have issues with the zoo package, although I'm on macOS so YMMV (https://stackoverflow.com/a/65334247/12957340). – jared_mamrot Mar 16 '21 at 04:20
  • @jared_mamrot thanks for your suggestion. I just tried to install `zoo` with `CFLAGS= -O3 -Wall -mtune=native -march=native` in `Makevars`. It replaced all CFLAGS completely specified in `zoo` package, which specifies `-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic`. I could manually adjust my `Makevars` to get the remaining flags in. But it is painful to do so as I have 20+ packages to install. Any idea how to kind of "union" the `CFLAGS` automatically? – HCSF Mar 16 '21 at 08:35
  • 1
    Actually, I got some inspiration from you and this [post](https://stackoverflow.com/questions/52874345/is-gcc-flags-repetition-and-ordering-important). I use `Makevars` to append my `CFLAGS` to the existing `CFLAGS` and expect `gcc` will take the latter flag if there is a conflict (according to the post). Thanks! – HCSF Mar 16 '21 at 08:48

1 Answers1

3

You can edit your .R/Makevars file and append the desired flags using the += operator, e.g.

CFLAGS+= -O3 -Wall -mtune=native -march=native

The latter flag is used if there is a conflict, as you said in your comment below. In terms of compiling from source, you can do this via install.packages(), e.g.

install.packages("package_name", type = "source")
jared_mamrot
  • 22,354
  • 4
  • 21
  • 46