Re: [R] .Call function crashes initializing matrix
Hi Bill, You were right, thanks! Now I have: int size = 5000; double *matrix = (double *) R_alloc(size^2, sizeof(double)); Which works for larger sizes! But now I have discovered a new problem with it. When I go to set the value of the elements it *sometimes* crashes: for (i = 0; i (size - 1); i++) { for (j = i; j (size - 1); j++) { *(matrix + j*length + i) = 5; } } About ever tenth time I run this code I get: R(2535,0xa07f64e0) malloc: *** error for object 0x16830404: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug Any idea why this is happening? Thanks again!, Erik On Apr 10, 2010, at 5:35 PM, William Dunlap wrote: -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Erik Wright Sent: Saturday, April 10, 2010 3:23 PM To: r help Subject: [R] .Call function crashes initializing matrix Hello, I have a C function that I call from R using .Call. I recently discovered a bug in my code, and I am not sure if it is a problem with what I am doing in C or if it has something to do with my use of .Call. I have narrowed my problem down to these two C statements: int size = 5000; double matrix[size][size]; When I run this in R it crashes, with this message: *** caught segfault *** address 0xb4141d70, cause 'memory not mapped' You may be overflowing the stack. Allocate the space for 'matrix' with the allocation functions described in section 5.9 of Writing R Extensions. They allocate space on the 'heap', which has much more space than the stack (where space for 'automatic' variables is allocated). Typical stack sizes are on the order of few megabytes and typical heap sizes are in the gigabytes. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com It has no problem when size is smaller, like 50. Any idea what is going on? Thanks!, Erik sessionInfo() R version 2.10.1 (2009-12-14) i386-apple-darwin9.8.0 locale: [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods [7] base __ 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.
Re: [R] .Call function crashes initializing matrix
-Original Message- From: Erik Wright [mailto:eswri...@wisc.edu] Sent: Sunday, April 11, 2010 1:59 PM To: William Dunlap Cc: r help Subject: Re: [R] .Call function crashes initializing matrix Hi Bill, You were right, thanks! Now I have: int size = 5000; double *matrix = (double *) R_alloc(size^2, sizeof(double)); In C the expression x ^ y means the bitwise exclusive-or of x and y, not x to the y power. Hence size^2 is either size of size-2. Try using size*size instead. If you are on Linux you can run R under valgrind (see .valgrind.org) and valgrind will tell you when you read or write outside of allocated memory (or read uninitialized memory). That is a much more senstitive test of the code being written correctly than seeing if it crashes R or not. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com Which works for larger sizes! But now I have discovered a new problem with it. When I go to set the value of the elements it *sometimes* crashes: for (i = 0; i (size - 1); i++) { for (j = i; j (size - 1); j++) { *(matrix + j*length + i) = 5; } } About ever tenth time I run this code I get: R(2535,0xa07f64e0) malloc: *** error for object 0x16830404: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug Any idea why this is happening? Thanks again!, Erik On Apr 10, 2010, at 5:35 PM, William Dunlap wrote: -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Erik Wright Sent: Saturday, April 10, 2010 3:23 PM To: r help Subject: [R] .Call function crashes initializing matrix Hello, I have a C function that I call from R using .Call. I recently discovered a bug in my code, and I am not sure if it is a problem with what I am doing in C or if it has something to do with my use of .Call. I have narrowed my problem down to these two C statements: int size = 5000; double matrix[size][size]; When I run this in R it crashes, with this message: *** caught segfault *** address 0xb4141d70, cause 'memory not mapped' You may be overflowing the stack. Allocate the space for 'matrix' with the allocation functions described in section 5.9 of Writing R Extensions. They allocate space on the 'heap', which has much more space than the stack (where space for 'automatic' variables is allocated). Typical stack sizes are on the order of few megabytes and typical heap sizes are in the gigabytes. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com It has no problem when size is smaller, like 50. Any idea what is going on? Thanks!, Erik sessionInfo() R version 2.10.1 (2009-12-14) i386-apple-darwin9.8.0 locale: [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods [7] base __ 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.
[R] .Call function crashes initializing matrix
Hello, I have a C function that I call from R using .Call. I recently discovered a bug in my code, and I am not sure if it is a problem with what I am doing in C or if it has something to do with my use of .Call. I have narrowed my problem down to these two C statements: int size = 5000; double matrix[size][size]; When I run this in R it crashes, with this message: *** caught segfault *** address 0xb4141d70, cause 'memory not mapped' It has no problem when size is smaller, like 50. Any idea what is going on? Thanks!, Erik sessionInfo() R version 2.10.1 (2009-12-14) i386-apple-darwin9.8.0 locale: [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods [7] base __ 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.
Re: [R] .Call function crashes initializing matrix
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Erik Wright Sent: Saturday, April 10, 2010 3:23 PM To: r help Subject: [R] .Call function crashes initializing matrix Hello, I have a C function that I call from R using .Call. I recently discovered a bug in my code, and I am not sure if it is a problem with what I am doing in C or if it has something to do with my use of .Call. I have narrowed my problem down to these two C statements: int size = 5000; double matrix[size][size]; When I run this in R it crashes, with this message: *** caught segfault *** address 0xb4141d70, cause 'memory not mapped' You may be overflowing the stack. Allocate the space for 'matrix' with the allocation functions described in section 5.9 of Writing R Extensions. They allocate space on the 'heap', which has much more space than the stack (where space for 'automatic' variables is allocated). Typical stack sizes are on the order of few megabytes and typical heap sizes are in the gigabytes. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com It has no problem when size is smaller, like 50. Any idea what is going on? Thanks!, Erik sessionInfo() R version 2.10.1 (2009-12-14) i386-apple-darwin9.8.0 locale: [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods [7] base __ 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.
[R] .Call() function
Dear all, What is the usage of the .Call()? What is the meaning of the follows: S=.Call(RS_fractal_spectral_density_function_direct,x,as.vector(taper.),as.logical(center),as.logical(recenter),TRUE,as.integer(npad),COPY=rep(FALSE,6),CLASSES=c(rep(matrix,2),rep(logical,3),integer),PACKAGE=ifultools) And is a function of RS_fractal_spectral_density_function_direct? Can I get the detail of RS_fractal_spectral_density_function_direct? Thanks a lot! Best regards, Wang [[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.
Re: [R] .Call() function
sdlywjl666 wrote: Dear all, What is the usage of the .Call()? What is the meaning of the follows: S=.Call(RS_fractal_spectral_density_function_direct,x,as.vector(taper.),as.logical(center),as.logical(recenter),TRUE,as.integer(npad),COPY=rep(FALSE,6),CLASSES=c(rep(matrix,2),rep(logical,3),integer),PACKAGE=ifultools) .Call() is similar to .C() in that it calls up a compiled C function from a dynamic library that has been loaded by R. However there is one differece-- .C() is used to pass information using standard C variable types as: .C( 'myFunc', x = as.integer( someX ), y = as.double( someY ), PACKAGE = 'myPackage' ) Note that as.integer() and as.double() are used to ensure C receives objects of the proper type. This would be processed by a C function with the following definition: void myFunc( int* x, double* y ){ // C-level monkey business } .Call() is different in that it passes variables as R structures rather basic C types. Using .Call() you don't have to wrap the arguments in coercion functions like as.double(). The .Call() interface also expects that the C function will return an R object: .Call( 'myFunc', someX, someY, PACKAGE = 'myPackage' ) The C function now looks like: #include R.h #include Rdefines.h SEXP myFunc( SEXP x, SEXP y ){ // C-level monkey business with R objects } The other main interface is .External() which gathers all the arguments into a single SEXP which is passed to C. More information and examples of .C(), .Call() and .External() can be found in sections 5.2 and 5.10 of the Writing R Extensions manual. sdlywjl666 wrote: And is a function of RS_fractal_spectral_density_function_direct? Can I get the detail of RS_fractal_spectral_density_function_direct? Thanks a lot! Best regards, Wang That function exists as compiled code in a dynamic library loaded by the ifultools package so there is no way to directly view it from the R console. However, you can download the source tarball for ifultools from CRAN, untar it and scan the C files in the source directory: cd ifultools cd src grep RS_fractal_spectral_density_function_direct * Which reveals that it lives in the file RS_fra_sdf.c I hope this helps! -Charlie - Charlie Sharpsteen Undergraduate Environmental Resources Engineering Humboldt State University -- View this message in context: http://www.nabble.com/.Call%28%29--function-tp25903072p25903325.html Sent from the R help mailing list archive at Nabble.com. __ 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] call function immediately before plot.new()
Hi all, I would like to be able to call a custom function automatically before plot.new() is called (more specifically, before a new plot is created on the current graphics device). Recently I've been poking around in the help files of some of the low(er) level plotting functions, and I seem to remember something saying that you could somehow add a setting to call a function immediately before plot.new() is called (something kind of in the spirit of .First). I've been looking everywhere, and I can't find the documentation I'm thinking of, which makes me wonder if I'm just making all this up. Can this be done? Thanks, Jake [[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.
Re: [R] call function immediately before plot.new()
See: ?frame e.g. setHook(plot.new, function(...) cat(Starting plot\n)) On Mon, Apr 28, 2008 at 9:22 AM, Jake Michaelson [EMAIL PROTECTED] wrote: Hi all, I would like to be able to call a custom function automatically before plot.new() is called (more specifically, before a new plot is created on the current graphics device). Recently I've been poking around in the help files of some of the low(er) level plotting functions, and I seem to remember something saying that you could somehow add a setting to call a function immediately before plot.new() is called (something kind of in the spirit of .First). I've been looking everywhere, and I can't find the documentation I'm thinking of, which makes me wonder if I'm just making all this up. Can this be done? Thanks, Jake [[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.
Re: [R] call function immediately before plot.new()
Hi Jake, are you looking for argument panel.first of plot.default? ?plot.default panel.first: an expression to be evaluated after the plot axes are set up but before any plotting takes place. This can be useful for drawing background grids or scatterplot smooths. hth, Matthias Jake Michaelson wrote: Hi all, I would like to be able to call a custom function automatically before plot.new() is called (more specifically, before a new plot is created on the current graphics device). Recently I've been poking around in the help files of some of the low(er) level plotting functions, and I seem to remember something saying that you could somehow add a setting to call a function immediately before plot.new() is called (something kind of in the spirit of .First). I've been looking everywhere, and I can't find the documentation I'm thinking of, which makes me wonder if I'm just making all this up. Can this be done? Thanks, Jake [[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. -- Dr. Matthias Kohl www.stamats.de __ 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.
Re: [R] call function immediately before plot.new()
Thanks Matthias and Gabor. You're both right. Matthias, I think that *was* what I read although now I realize that Gabor's suggestion is more along the lines of what I want to do. Though I have found one situation where this wouldn't really work properly: par(mfrow=c(2,2)) plot(rnorm(10)) plot(rnorm(10)) plot(rnorm(10)) plot.new() Now my goal is actually to *only* call this custom function before the entire device is cleared for a new plot or set of plots. Since the above example plots multiple times in the same window, the function would be called each time, when really I just want it called once, right before the device window is cleared for another plot. Is there a way to do this or is this asking too much of R? Thanks, Jake On Mon, Apr 28, 2008 at 3:41 PM, Gabor Grothendieck [EMAIL PROTECTED] wrote: See: ?frame e.g. setHook(plot.new, function(...) cat(Starting plot\n)) On Mon, Apr 28, 2008 at 9:22 AM, Jake Michaelson [EMAIL PROTECTED] wrote: Hi all, I would like to be able to call a custom function automatically before plot.new() is called (more specifically, before a new plot is created on the current graphics device). Recently I've been poking around in the help files of some of the low(er) level plotting functions, and I seem to remember something saying that you could somehow add a setting to call a function immediately before plot.new() is called (something kind of in the spirit of .First). I've been looking everywhere, and I can't find the documentation I'm thinking of, which makes me wonder if I'm just making all this up. Can this be done? Thanks, Jake [[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. [[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.