我有興趣分析OS X(Mountain Lion 10.8.2)下的一些Rcpp代碼,但剖析器在運行時會崩潰。在OS X上剖析Rcpp代碼
玩具示例,使用inline
,只是設計爲花費足夠的時間讓探查器注意。
library(Rcpp)
library(inline)
src.cpp <- "
RNGScope scope;
int n = as<int>(n_);
double x = 0.0;
for (int i = 0; i < n; i++)
x += (unif_rand()-.5);
return wrap(x);"
src.c <- "
int i, n = INTEGER(n_)[0];
double x = 0.0;
GetRNGstate();
for (i = 0; i < n; i++)
x += (unif_rand()-.5);
PutRNGstate();
return ScalarReal(x);"
f.cpp <- cxxfunction(signature(n_="integer"), src.cpp, plugin="Rcpp")
f.c <- cfunction(signature(n_="integer"), src.c)
如果我使用GUI儀器(在Xcode,版本4.5(4523))或命令行sample
,兩者碰撞:器械馬上崩潰,而樣品崩潰之前完成處理樣品:
# (in R)
set.seed(1)
f.cpp(200000000L)
# (in a separate terminal window)
~ » sample R # this invokes the profiler
Sampling process 81337 for 10 seconds with 1 millisecond of run time between samples
Sampling completed, processing symbols...
[1] 81654 segmentation fault sample 81337
如果我做同樣的過程,但與C版本(即f.c(200000000L)
)這兩項文書和sample
做工精細,併產生像
Call graph:
1832 Thread_6890779 DispatchQueue_1: com.apple.main-thread (serial)
1832 start (in R) + 52 [0x100000e74]
1832 main (in R) + 27 [0x100000eeb]
1832 run_Rmainloop (in libR.dylib) + 80 [0x1000e4020]
1832 R_ReplConsole (in libR.dylib) + 161 [0x1000e3b11]
1832 Rf_ReplIteration (in libR.dylib) + 514 [0x1000e3822]
1832 Rf_eval (in libR.dylib) + 1010 [0x1000aa402]
1832 Rf_applyClosure (in libR.dylib) + 849 [0x1000af5d1]
1832 Rf_eval (in libR.dylib) + 1672 [0x1000aa698]
1832 do_dotcall (in libR.dylib) + 16315 [0x10007af3b]
1382 file1412f6e212474 (in file1412f6e212474.so) + 53 [0x1007fded5] file1412f6e212474.cpp:16
+ 862 unif_rand (in libR.dylib) + 1127,1099,... [0x10000b057,0x10000b03b,...]
+ 520 fixup (in libR.dylib) + 39,67,... [0x10000aab7,0x10000aad3,...]
356 file1412f6e212474 (in file1412f6e212474.so) + 70,61,... [0x1007fdee6,0x1007fdedd,...] file1412f6e212474.cpp:16
56 unif_rand (in libR.dylib) + 1133 [0x10000b05d]
38 DYLD-STUB$$unif_rand (in file1412f6e212474.so) + 0 [0x1007fdf1c]
輸出
我真的很感激一些建議,如果有什麼我做錯了,如果有一些其他的首選方式,或者如果這是不可能的。鑑於Rcpp的主要用途之一似乎是加速R代碼,我很驚訝沒有找到更多的信息,但也許我看錯了地方。
這是在OS X 10.8.2上,R 2.15.1(x86_64-apple-darwin9.8.0),Rcpp 0.9.15和g ++ --version報告「i686-apple-darwin11-llvm-g ++ - 4.2 (GCC)4.2.1(基於Apple Inc. build 5658)(LLVM build 2336.11.00)「。
溶液
得益於以下德克的回答,和他的談話在這裏http://dirk.eddelbuettel.com/papers/ismNov2009introHPCwithR.pdf,我至少使用谷歌perftools的部分解決方案。首先,從這裏安裝http://code.google.com/p/gperftools/,並在編譯C++代碼時將-lprofiler添加到PKG_LIBS。然後要麼
(a)運行R作爲CPUPROFILE=samples.log R
,運行所有代碼和退出(或使用RSCRIPT)
(b)利用兩個小的效用函數,以打開/關閉分析:
RcppExport SEXP start_profiler(SEXP str) {
ProfilerStart(as<const char*>(str));
return R_NilValue;
}
RcppExport SEXP stop_profiler() {
ProfilerStop();
return R_NilValue;
}
然後,R內,你可以做
.Call("start_profiler", "samples.log")
# code that calls C++ code to be profiled
.Call("stop_profiler")
無論哪種方式,文件samples.log
將包含分析信息。這可以在與
pprof --text /Library/Frameworks/R.framework/Resources/bin/exec/x86_64/R samples.log
產生像
Using local file /Library/Frameworks/R.framework/Resources/bin/exec/x86_64/R.
Using local file samples.log.
Removing __sigtramp from all stack traces.
Total: 112 samples
64 57.1% 57.1% 64 57.1% _unif_rand
30 26.8% 83.9% 30 26.8% _process_system_Renviron
14 12.5% 96.4% 101 90.2% _for_profile
3 2.7% 99.1% 3 2.7% Rcpp::internal::expr_eval_methods
1 0.9% 100.0% 1 0.9% _Rf_PrintValueRec
0 0.0% 100.0% 1 0.9% 0x0000000102bbc1ff
0 0.0% 100.0% 15 13.4% 0x00007fff5fbfe06f
0 0.0% 100.0% 1 0.9% _Rf_InitFunctionHashing
0 0.0% 100.0% 1 0.9% _Rf_PrintValueEnv
0 0.0% 100.0% 112 100.0% _Rf_ReplIteration
輸出,很可能是在一個真實的例子更多的信息來看。
+1 - perftools,gcc profiler或valgrind都可以提供幫助。 R是互動的事實確實會在這裏拋出一個扳手,因爲它增加了更多的代碼層。 –
當我升級我的XCode時,我遇到了這個問題,但我認爲它可能就是我。事情是,鯊魚以前完美地分析了Rcpp,但現在已經失效了。我想我們可以稱之爲Apple缺陷。 –