---------- Forwarded message ---------- From: s niani <riahina...@gmail.com> Date: Mon, May 30, 2011 at 9:20 AM Subject: problem with rmultinom function To: rcpp-devel@lists.r-forge.r-project.org
Hi, I sent this message a couple of days ago, but I haven't heard anything yet. I think you didnt receive it, that's why I send it again. I have a strange problem that I spent several days trying to fix it, but unfortunately I wasn't able to catch it. I'm running the rcpp code for 3000 iterations(running the same code for 3000 times) on different datasets. but for some datasets, in the final iterations (like 2667 th iteration) the code stops running and it jumps out of the R environment. and if I run the same code (without any changes) on the same dataset(still without any changes) the problem might never happen and it goes to the last iteration without any problem(or the problem may occur in another iteration). and the interesting part is that error code is different each time, I got error codes like: what(): invalid partial string match what(): 'prob' is missing what(): unused argument(s) (<environment>) and etc... I'm pretty sure that problem occurs in the line of code that I am using rmultinom function, but I included the necessary function Function rmultinom = stats["rmultinom"]; to be more clear I wrote a simple function and I attached it in this email. in this code I didn't read any dataset, I just used some variables to define the size of structures that I need to get from dataset. if you run the code with these values for P, K, unum,qnum, the error occurs in the first 10 iterations, but If you change the values of those parameters to smaller values, the error may never happen. I tried to used an cpp implementation for rmultinom, it works but the program would be as slow as R version. do you have any idea why this problem occur? or did anybody ever have the same problem or can give me some tips to fix it? ps: all datasets are generated through the sampe process(with a single simulation code) Thanks
#include "SamplingCode.h" int Cwhich(IntegerVector binVec){ int vecsize = binVec.size(); for ( int i =0; i < vecsize; i++) if (binVec(i) == 1) return i; cout << "soemthing went wrong, no value equal to one was found !" << endl; return -1; } RcppExport SEXP doGibbs(SEXP RmaxItr){//doGibbs <- function(Questions, K, P, gibbs.max.iter, Z.Q=NULL, Y.Q = NULL){ Environment stats("package:stats"); Function rmultinom = stats["rmultinom"]; Rcpp::RNGScope scope; // Rcpp::List questions(Rquestions); unsigned int K =50; unsigned int P = 50; unsigned int maxItr = Rcpp::as<int>(RmaxItr); Rcpp::List ZQ; Rcpp::List YQ; unsigned int unum=300; cout<<unum<<endl; cout<<"point1"<<endl; for(unsigned itr=0; itr<5000; itr++){ cout<<"itr"<<itr<<endl; for ( unsigned u=0; u < unum; u++) { Rcpp::List QZQ; unsigned qnum= 100; YQ.insert(u, rep(0, qnum )); for( unsigned q=0; q<qnum; q++){ as<Rcpp::IntegerVector>(YQ(u))(q) = Cwhich(rmultinom(1,1, rep(1.0/P,P) )); unsigned qlength=200; QZQ.insert(q, rep(0, qlength )); for( unsigned n=0; n<qlength; n++){ as<Rcpp::IntegerVector>(QZQ(q))(n)= Cwhich(rmultinom(1,1, rep(1.0/K,K) )); } } ZQ.insert(u, QZQ); } } }
#ifndef _CQA_H #define _CQA_H #include <Rcpp.h> #include <vector> #include <list> using namespace Rcpp; using namespace std; /* * note : RcppExport is an alias to `extern "C"` defined by Rcpp. * * It gives C calling convention to the rcpp_hello_world function so that * it can be called from .Call in R. Otherwise, the C++ compiler mangles the * name of the function and .Call can't find it. * * It is only useful to use RcppExport when the function is intended to be called * by .Call. See the thread http://thread.gmane.org/gmane.comp.lang.r.rcpp/649/focus=672 * on Rcpp-devel for a misuse of RcppExport */ RcppExport SEXP doGibbs(SEXP RmaxItr); #endif
_______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel