Alireza Mahani
2011-Jul-18 15:52 UTC
[Rd] Manipulating single-precision (float) arrays in .Call functions
I am writing a wrapper function in C++ that calls a GPU kernel. My array type for the GPU kernel is float, so I would like my wrapper function to receive float arrays from R. I understand that I can use 'as.single' in R to copy a double-precision vector from R in single-precision format while using the '.C' interface, but is there any way to do something similar for '.Call'? Given that the latter passes pointers from R to C/C++, I'm guessing this may be impossible, but I wanted to double-check. If you can suggest a solution, a small code sample would be much appreciated. The reason I prefer '.Call' to '.C' is because the former passes pointers and therefore creates less data transfer overhead as opposed to the latter which copies data. Is this argument controversial? -Alireza -- View this message in context: http://r.789695.n4.nabble.com/Manipulating-single-precision-float-arrays-in-Call-functions-tp3675684p3675684.html Sent from the R devel mailing list archive at Nabble.com.
Duncan Murdoch
2011-Jul-18 16:19 UTC
[Rd] Manipulating single-precision (float) arrays in .Call functions
On 18/07/2011 11:52 AM, Alireza Mahani wrote:> I am writing a wrapper function in C++ that calls a GPU kernel. My array type > for the GPU kernel is float, so I would like my wrapper function to receive > float arrays from R. I understand that I can use 'as.single' in R to copy a > double-precision vector from R in single-precision format while using the > '.C' interface, but is there any way to do something similar for '.Call'? > Given that the latter passes pointers from R to C/C++, I'm guessing this may > be impossible, but I wanted to double-check. If you can suggest a solution, > a small code sample would be much appreciated. > > The reason I prefer '.Call' to '.C' is because the former passes pointers > and therefore creates less data transfer overhead as opposed to the latter > which copies data. Is this argument controversial?R has no native type holding singles. It exports a double to a single vector in .C if you ask it to, but that's not available in .Call. You'll need to do the copying yourself. Duncan Murdoch
Reasonably Related Threads
- Best practices for writing R functions
- Performance of .C and .Call functions vs. native R code
- Is it possible to pass a function argument from R to compiled code in C?
- How to safely using OpenMP pragma inside a .C() function?
- Assigning a new name to object loaded with "load()"