gpt4 book ai didi

How to use gcc compiling flags in R?(如何使用GCC编辑R中的旗帜?)

翻译 作者:bug小助手 更新时间:2023-10-26 22:46:04 31 4
gpt4 key购买 nike



I'm using the following lines in an R file to compile a C file:

我在R文件中使用以下几行代码来编译C文件:


system2('R', c('CMD', 'SHLIB', 'My_C_File.c'))
dyn.load(paste0("My_C_File", .Platform$dynlib.ext))

I would like to optimize as much as possible the speed of this C code.
So I want to use the gcc -O3 flag.

我想尽可能地优化这段C代码的速度。所以我想用GCC-03的旗帜。


How do I insert this flag in the previous 2 lines?

如何在前面的两行中插入此标志?


更多回答

why not rccp? then you can use Makevars and Makevars.win

为什么不是RCCP?然后你可以使用Makevar和Makevars.win

Because I already coded everything in C.

因为我已经用C编写了所有代码。

???? r-bloggers.com/2021/07/using-r-calling-c-code-with-rcpp

????r-bloggers.com/2021/07/using-r-calling-c-code-with-rcpp

Thanks. I found a way with withr: with_makevars(c(CFLAGS = "-O3"), system("R CMD SHLIB --preclean -c foo.c")) (example from the withr documentation)

谢谢。我找到了一种使用with r的方法:with_makevars(c(CFLAGS=“-03”),system(“R CMD SHLIB--prelean-c foo.c”))(来自with r文档的示例)

The canonical answer is to edit either the file ~/.R/Makevars or to set environment variables such as PKG_CFLAGS, PKG_CXX11FLAGS and so on. It's all pretty well document and will get picked up by R CMD COMPILE etc.

标准答案是编辑~/.R/Makevars文件或设置环境变量,如PKG_CFLAGS、PKG_CXX11FLAGS等。这一切都是非常好的文档,并将被R、CMD、编译器等拾取。

优秀答案推荐

I found a way with withr: with_makevars(c(CFLAGS = "-O3"), system("R CMD SHLIB --preclean -c foo.c")) (example from the withr documentation)

我找到了一种使用with r的方法:with_makevars(c(CFLAGS=“-03”),system(“R CMD SHLIB--prelean-c foo.c”))(来自with r文档的示例)


更多回答

That's really not a very good approach. See my comment above, the Writing R Extensions manual, or a bazillion prior questions. The inline package can also help you make this less manual.

这可不是个好办法请参阅我上面的评论,编写R扩展手册,或者之前的无数问题。内联包还可以帮助您减少手动操作。

Sorry, but I don't understand why it is not a very good approach. Thanks for inline, I'm looking into it.

对不起,我不明白为什么这不是一个很好的方法。谢谢你的内联,我正在调查。

You enforce a dependency on another package when all you really need is Sys.setenv(CFLAGS="-O3") (which, as an aside, is likely already a local default from R). The system() call is clumsys. R has functions for that already -- follow the implementation of the various R CMD ... commands. Anyway, good luck.

当您真正需要的只是Sys.setenv(CFLAGS=“-o3”)(顺便说一句,它很可能已经是R的本地缺省值)时,您可以强制依赖另一个包。System()调用是笨拙的。R已经有了这方面的功能--遵循各种R CMD的实现...命令。不管怎样,祝你好运。

Yes, I know, I indeed need a new package in my approach. Well, I didn't know CFLAGS was an environmental variable in the first place. BTW the flag -O3 doesn't appear in my default winshlib.mk file; the default compiler flags are anyway not mentioned in the Writing R Extensions manual. My experience until now is that it's all very cryptic and not well documented. "The system() call is clumsys. R has functions for that already": I don't understand what you mean. In my code I used system2('R', c('CMD', 'SHLIB', '2._IGTs_-_Graph_routines_NoHash_C.c')).

是的,我知道,我的方法确实需要一个新的方案。嗯,我一开始不知道CFLAGS是一个环境变量。顺便说一句,我的默认winshlib.mk文件中没有出现-o3标志;不管怎样,在《编写R扩展》手册中都没有提到默认的编译器标志。我的经验是,到目前为止,这一切都非常神秘,没有很好的记录。“system()调用很笨拙。R已经有相应的函数了”:我不明白你的意思。在我的代码中,我使用了system 2(‘R’,c(‘CMD’,‘SHLIB’,‘2._IGTS_-_GRAPH_ROUTINES_NoHash_C.C’))。

The thing is that R CMD ... is just a driver for make (!!) where CFLAGS is a variable that can be set in a Makefile or via the env.var. Fourty years of Unix practice you can trust without needed one-off clutches via a special R wrapper functions. Trust the basic process!

问题是R CMD..。只是Make(!!)的驱动程序其中,CFLAGS是可以在生成文件中或通过env.var设置的变量。经过40年的Unix实践,您可以通过一个特殊的R包装器函数来信任,而无需使用一次性离合器。相信基本的流程!

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com