Attaching existing double* pointer to newly created R's SEXP struct in C/C++
1
0
Entering edit mode
@mohammad-alkhamis-10862
Last seen 7.4 years ago
Canada/ Victoria, BC / University of Vi…

Hello everyone,

I have been trying hard to figure out how to attach already-existing double pointers to SEXP vectors in C. The problem I am facing is that I have data that is processed in GPU and results are copied back to host and stored as a row-major matrix (double*). Then, I have a SEXP vector to which I would like to attach each row to without having to copy data from each row individually to new to-be-attached SEXP vectors.

I know that SET_VECTOR_ELT can be used to attach a SEXP vector to another SEXP list. However, since the data coming from the GPU is not stored in SEXP vectors, I wouldn't be able to use SET_VECTOR_ELT unless I do the following:

  1. create an array of SEXP vectors
  2. copy the rows coming from the GPU to the corresponding SEXP vectors
  3. attach each SEXP vector to the final SEXP list using SET_VECTOR_ELT

Though, I am trying to avoid the above solution since it will have a performance penalty due to copying data from one location to another within the memory. Instead, I am looking for something like the following:

  1. create an array of SEXP vectors
  2. attach row pointers of the GPU-processed matrix to the corresponding SEXP vectors
  3. attach each SEXP vector to the final SEXP list using SET_VECTOR_ELT

 

Any hint will be appreciated.

Thanks a lot

R C++ • 1.4k views
ADD COMMENT
1
Entering edit mode
Aaron Lun ★ 28k
@alun
Last seen 17 minutes ago
The city by the bay

This question belongs on the BioC-devel mailing list, if it's a question about the development of an (intended) BioC package. But to throw in my two cents; I don't think your desired behaviour is possible. R's use of SEXP objects at the C API level allows it to internally manage its memory, garbage collection, etc. at the R level. If you use the allocVector and allocMatrix macros, then it's safe; but R's memory management may not behave properly if you force a manually allocated block of memory into the SEXP. The closest thing might be to use external pointers, see http://stackoverflow.com/questions/7032617/storing-c-objects-in-r.

ADD COMMENT
1
Entering edit mode

Agree that this isn' the appropriate forum. The other strategy is to allocate the vector in R and fill it elsewhere,

SEXP x = PROTECT(Rf_allocVector(REALSXP, 1000));
/* do something with the memory pointed to by REAL(x) */
UNPROTECT(1);
return x;

ADD REPLY
0
Entering edit mode

@Aaron: Thank you so much. That answers my question.

@Martin: yes I am aware of that. It is just for my purpose, I need a contiguous block of memory that will later be separated to different SEXP object. It now seems that copying to SEXP object is the safest way to go. Thanks for responding :)

I also apologize for posting to the wrong forum. I thought that the tags were sufficient to justify posting the question here!

ADD REPLY

Login before adding your answer.

Traffic: 529 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6