[mlpack] Question about Armadillo

Ryan Curtin ryan at ratml.org
Tue Jun 27 10:41:34 EDT 2017


On Tue, Jun 27, 2017 at 09:10:06AM +0500, Kirill Mishchenko wrote:
> Hi mlpack community!
> 
> I’m not sure it is a right place to ask a question that I want to ask,
> but, anyway, maybe somebody will be eager to answer. Otherwise, just
> ignore this message.
> 
> I have encountered some unexpected behaviour with the Armadillo
> library. The following code snippet works as expected:
> 
> template<typename T>
> void f(const T& arg)
> {
>   arma::mat m = arg;
> }
> 
> BOOST_AUTO_TEST_CASE(RefTest)
> {
>   f(arma::join_rows(arma::zeros(2).t(), arma::zeros(2).t()));
> }
> 
> But if I run the following
> 
> BOOST_AUTO_TEST_CASE(RefTest2)
> {
>   const auto& ref = arma::join_rows(arma::zeros(2).t(), arma::zeros(2).t());
>   arma::mat m = ref;
> }
> 
> I get this error:
> 
> unknown location:0: fatal error: in "ArmaExtendTest/RefTest2": memory
> access violation at address: 0x00000000: no mapping at fault address
> 
> Surprisingly, if run the following code snippet
> 
> BOOST_AUTO_TEST_CASE(RefTest3)
> {
>   const auto& ref = arma::join_rows(arma::zeros(2), arma::zeros(2));
>   arma::mat m = ref;
> }
> 
> everything is ok again.
> 
> Are there any thoughts why I observe such behaviour?

I believe this is a result of strange inference by the compiler.  You
should double-check my explanation here, I'm not 100% sure that it is
correct, but my gut feeling is that it is...

Armadillo does lazy evaluation at compile time, so if you do, e.g., mat
+ mat, the type returned is not 'mat' but instead a type that represents
that two matrices should be added.  If you're interested I actually just
gave a talk on this yesterday, here are the slides:

http://www.ratml.org/presentation.pdf

Anyway, I think that the C++ compiler will infer strange things for
'auto' types, and as a result if you try to use auto with Armadillo, you
can end up with bizarre results, and it's often better to avoid it if
possible.

Hope this is helpful... let me know if I can clarify anything.

-- 
Ryan Curtin    | "If it's something that can be stopped, then just try to stop it!"
ryan at ratml.org |   - Skull Kid


More information about the mlpack mailing list