Skip to content

[Rcpp-devel] Rcpp: Modification of input argument: Undefined behaviour

5 messages · David Shih, Yixuan Qiu, Dirk Eddelbuettel +1 more

#
Hello,

There was an earlier post on this subject, but based on my experimentation, the behaviour of modifying input argument is different depending on how the matrix was initialization and other factors...

I wrote a Rcpp function to modify an input matrix. After calling this function, the input matrix is modified under some circumstances and not modified under others. The behaviour is the same on repeat runs and on both Linux (3.16.3-1-ARCH) and Mac (OSX 10.9).

The R script, C++ code, and the results are available on Bitbucket:

https://bitbucket.org/dshih/rcpp_inplace

I don't quite understand when the input matrix is modified in place by the Rcpp function and when the input matrix is be copied on write in the Rcpp function.

When I stay within Rcpp/C++, a input argument can be modified in-place by a function. (This feature was critical to my optimization.)

After the code returns to R, I would expect either copy-on-write or in-place modification but not both.

What is the correct behaviour?


Thank you,

David J. H. Shih

The Hospital for Sick Children
Peter Gilgan Centre for Research and Learning
686 Bay St
17th floor, Room 17.9707
Toronto, ON  M5G 0A4
Canada
Tel:  (416) 813-7654 x309157
#
Hello David,
The general answer to your question is, if the type of your matrix (integer
or numeric) in R is different from the one you declare in Rcpp, Rcpp will
make a copy and cast it to the appropriate type.
For example, 1:12 is of type integer, and if you pass it as a
NumericMatrix, Rcpp will implicitly copy the whole matrix, so no matter
what modification you did, the original matrix will not change.

Best,
Yixuan
On Nov 26, 2014 1:33 AM, "David Shih" <david.shih at mail.utoronto.ca> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20141126/3c84fac1/attachment.html>
#
On 26 November 2014 at 02:13, Yixuan Qiu wrote:
| Hello David,
| The general answer to your question is, if the type of your matrix (integer or
| numeric) in R is different from the one you declare in Rcpp, Rcpp will make a
| copy and cast it to the appropriate type.

Precisely.  

| For example, 1:12 is of type integer, and if you pass it as a NumericMatrix,
| Rcpp will implicitly copy the whole matrix, so no matter what modification you
| did, the original matrix will not change.

This is discussed in a few places, often a standard topic in our (older)
slide decks and came up here a few times.

Rcpp generally uses a very light wrapper around a direct pointer to the
actual R object, so changes affect it.  However, in two cases you are
shielded: when an implicit copy is made (often from integer to numeric as
Yixuan said) or when an explicit copy is requested via clone().

Dirk
 
| Best,
| Yixuan
|
| On Nov 26, 2014 1:33 AM, "David Shih" <david.shih at mail.utoronto.ca> wrote:
| 
|     Hello,
| 
|     There was an earlier post on this subject, but based on my experimentation,
|     the behaviour of modifying input argument is different depending on how the
|     matrix was initialization and other factors...
| 
|     I wrote a Rcpp function to modify an input matrix. After calling this
|     function, the input matrix is modified under some circumstances and not
|     modified under others. The behaviour is the same on repeat runs and on both
|     Linux (3.16.3-1-ARCH) and Mac (OSX 10.9).
| 
|     The R script, C++ code, and the results are available on Bitbucket:
| 
|     https://bitbucket.org/dshih/rcpp_inplace
| 
|     I don't quite understand when the input matrix is modified in place by the
|     Rcpp function and when the input matrix is be copied on write in the Rcpp
|     function.
| 
|     When I stay within Rcpp/C++, a input argument can be modified in-place by a
|     function. (This feature was critical to my optimization.)
| 
|     After the code returns to R, I would expect either copy-on-write or
|     in-place modification but not both.
| 
|     What is the correct behaviour?
| 
| 
|     Thank you,
| 
|     David J. H. Shih
| 
|     The Hospital for Sick Children
|     Peter Gilgan Centre for Research and Learning
|     686 Bay St
|     17th floor, Room 17.9707
|     Toronto, ON? M5G 0A4
|     Canada
|     Tel:? (416) 813-7654 x309157
|     _______________________________________________
|     Rcpp-devel mailing list
|     Rcpp-devel at lists.r-forge.r-project.org
|     https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
| 
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
#
Hi Yixuan,


You're right!


Whether the input matrix can be modified by a function that takes NumericMatrix is determined by whether the input matrix is a NumericMatrix or an IntegerMatrix (the latter needs to be copied and converted). The `mode()` function was pretty useless for determining a matrix object's type, but as at least `is.integer()` is predictive.


When I wrote another function that takes an IntegerMatrix, it was able to modify an input IntegerMatrix in-place.


Thank you,

David
#
object's type

Use the storage.mode() function.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, Nov 26, 2014 at 9:06 AM, David Shih <david.shih at mail.utoronto.ca>
wrote:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20141126/f13f9c54/attachment.html>