On Mon, Dec 17, 2012 at 8:02 AM, William Dunlap <wdunlap@tibco.com> wrote:

> The problem almost certainly lies in the parts of the code that you have
> not shown, the R-language code including the .Call or, more likely in my
> opinion, the C++ code for Projector.


I would have blamed the unPROTECTed coerceVector() result in the getDim
macro.

   -thomas



> You can get different values in
> Projector's Lsum matrix if
>    (a) Projector doesn't assign values to all of them (allocMatrix
> allocates memory
>          with random contents - you must fill it with values)
>    (b) Projector reads or writes memory that it doesn't own.
> Using valgrind can help find both sorts of errors.
>
> I am suspicious about your reversal of the dimensions of Lsum when
> you allocate it, using dimG[1],dimG[0].  Does this mean you are doing
> some odd pointer arithmetic in Projector that doesn't agree with R's
> conventions?
>
> I tried adding a simple Projector() and saw no problems aside from
> the random numbers in the unassigned elements of Lsum and some
> of the usual unassign values in the guts of Rprintf's formatting code.
>
> extern "C" {
> SEXP Projector5(SEXP L, SEXP G, SEXP W, SEXP xymod, SEXP modif);
> }
>
> //* the Projector part  *//
> void Projector(double *L, int *dimL, double *G, int *dimG, double *W, int
> *dimW,
>  int *xymod, int *dimxy, double *modif, int *dimif, double *Lsum)
> {
>     Rprintf("dimG=c(%d,%d)\n", dimG[0], dimG[1]);
>     double* pLsum = Lsum;
>     bool even = true ;
>     for(int j=0 ; j < dimG[0] ; j++) {
>         for(int i=0 ; i < dimG[1] ; i++) {
>             if (even) {
>                 *pLsum = 1000 * (i+1) + (j+1);
>             } else {
>                 // do not touch odd elements of pLsum
>             }
>             even = !even;
>             pLsum++;
>         }
>     }
> }
>
> called with
> > .Call("Projector5", matrix(1:6,2,3), matrix(701:720,4,5),
> matrix(2^(1:12),2,6), 1:7, 1:10)
> dimG=c(4,5)
>      [,1]       [,2] [,3]       [,4]
> [1,] 1001 7.954e-322 1003 4.002e-322
> [2,]    0  2.002e+03    0  2.004e+03
> [3,] 3001  0.000e+00 3003  0.000e+00
> [4,]    0  4.002e+03    0  4.004e+03
> [5,] 5001  0.000e+00 5003  0.000e+00
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>
> > -----Original Message-----
> > From: r-help-bounces@r-project.org [mailto:r-help-bounces@r-project.org]
> On Behalf
> > Of Shangru Li
> > Sent: Sunday, December 16, 2012 10:20 AM
> > To: r-help@r-project.org
> > Subject: [R] interfacing with .Call
> >
> > Hi
> >
> > My code is as following:
> >
> > #include <R.h>
> > #include <Rinternals.h>
> >
> > //* the Projector part  *//
> > void Projector(double *L, int *dimL, double *G, int *dimG, double *W, int
> > *dimW, int *xymod, int *dimxy, double *modif, int *dimif, double *Lsum)
> > { ...}
> >
> > //* the interface part *//
> > #define getDim(A) INTEGER(coerceVector(getAttrib(A,R_DimSymbol), INTSXP))
> >
> > SEXP Projector5(SEXP L, SEXP G, SEXP W, SEXP xymod, SEXP modif)
> > {
> >     //* digest SEXPs from R *//
> >     int *dimL, *dimG, *dimW, *dimxy, *dimif;
> >     double *lptr, *gptr, *wptr, *ifptr;
> >     int *xyptr;
> >     dimL=getDim(L);
> >     PROTECT(L=coerceVector(L, REALSXP));
> >     lptr=REAL(L);
> >     dimG=getDim(G);
> >     PROTECT(G=coerceVector(G, REALSXP));
> >     gptr=REAL(G);
> >     dimW=getDim(W);
> >     PROTECT(W=coerceVector(W, REALSXP));
> >     wptr=REAL(W);
> >     dimxy=getDim(xymod);
> >     PROTECT(xymod=coerceVector(xymod, INTSXP));
> >     xyptr=INTEGER(xymod);
> >     dimif=getDim(modif);
> >     PROTECT(modif=coerceVector(modif, REALSXP));
> >     ifptr=REAL(modif);
> >
> >     //* create SEXP to hold the answer *//
> >     SEXP ans;
> >     double *ansptr;
> >     PROTECT(ans=allocMatrix(REALSXP, dimG[1], dimG[0]));
> >     ansptr=REAL(ans);
> >
> >     //* calculate the result *//
> >     Projector(lptr, dimL, gptr, dimG, wptr, dimW, xyptr, dimxy, ifptr,
> > dimif, ansptr);
> >
> >     //* wrap up and return the result to R *//
> >     UNPROTECT(6);
> >     return(ans);
> > }
> >
> > The function "Projector" works well and actually the interface with .C
> > works OK.
> > The question is that I can compile it in R, but ".Call" returns different
> > result each time with same inputs. Could anybody tell me why? Thanks!
> >
> > Regards
> > Shangru
> >
> > --
> > Department of Mathematics,
> > National University of Singapore,
> > Blk S17, 10 Lower Kent Ridge Road,
> > 119076
> >
> >
> >
> >
> >
> > --
> > Department of Mathematics,
> > National University of Singapore,
> > Blk S17, 10 Lower Kent Ridge Road,
> > 119076
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Thomas Lumley
Professor of Biostatistics
University of Auckland

	[[alternative HTML version deleted]]


