title: Eigen, how to access the underlying array of a MatrixBase<Derived>
body: <p>I need to access the array that contains the data of a MatrixBase Eigen matrix.</p>
  
  <p>The Eigen library has the data() method which returns a pointer to an array, however it is only accessible from a Matrix <a href="http://eigen.tuxfamily.org/dox/classEigen_1_1PlainObjectBase.html#a40dcc334db75411e168691d9bce08c3a" rel="noreferrer">type</a>. The <a href="http://eigen.tuxfamily.org/dox/classEigen_1_1MatrixBase.html" rel="noreferrer">MatrixBase</a> doesn't have a similar method, even though the MatrixBase class is supposed to act as a template and the actual type should be just a Matrix. If I try to access MatrixBase.data() I get a compile time error:</p>
  
  <pre><code>template &lt;typename ScalarA, typename Index, typename DerivedB, typename DerivedC&gt;
  void uscgemv(float alpha, 
       const USCMatrix&lt;ScalarA,Index&gt; &amp;a,
       const MatrixBase&lt;DerivedB&gt; &amp;b,
       const MatrixBase&lt;DerivedC&gt; &amp;c_const)
  {
      //...some code
      float * bMat = b.data();
      ///more code
  }
  </code></pre>
  
  <p>This code produces the following compile time error.</p>
  
  <pre><code>error: ‘const class Eigen::MatrixBase&lt;Eigen::CwiseNullaryOp&lt;Eigen::internal::scalar_constant_op&lt;float&gt;, Eigen::Matrix&lt;float, -1, 1&gt; &gt; &gt;’ has no member named ‘data’
   float * bMat = b.data();
  </code></pre>
  
  <p>So I have to resort to gimmicks such as...</p>
  
  <pre><code>float * bMat;
  int bRows = b.rows();
  int bCols = b.cols();
  mallocPinnedMemory(&amp;bMat, bRows*bCols*sizeof(float));
  Eigen::Map&lt;Matrix&lt;float, Dynamic, Dynamic&gt; &gt; bmat_temp(bMat, bRows, bCols);
  bmat_temp = b;  //THis is SLOW, we should avoid it.
  </code></pre>
  
  <p>Then I can access the bMat array...</p>
  
  <p>Those copies back-and-forth are the biggest cost in the gpu matrix multiplication, as I essentially I have to make an extra copy, before even coping to the device...</p>
  
  <p>I can't use Eigen-magma, as this is sparse matrix-in-a-weird-format to a dense matrix (or sometimes vector) multiplication so I can't use any of the automatic gpu functions there. Also I would much rather not declare the matrices as something else, because that would require changing A LOT of lines of code across the whole program (which I didn't write).</p>
  
  <p>EDIT: A static cast solution was proposed:</p>
  
  <pre><code>float * bMat = (static_cast&lt;Eigen::Matrix&lt;float, Eigen::Dynamic, Eigen::Dynamic&gt; &gt;(b)).data();
  </code></pre>
  
  <p>However I get segfault the first time I try to access an element of the array bMat.</p>
  
  <p>EDIT 2: I'm looking for a zero copy way to access the underlying arrays. I need to only be able to read b, but I also need to able to write to c. Currently c is unconst-d with the following macro:</p>
  
  <pre><code>#define UNCONST(t,c,uc) Eigen::MatrixBase&lt;t&gt; &amp;uc = const_cast&lt;Eigen::MatrixBase&lt;t&gt;&amp;&gt;(c);
  </code></pre>
  
  <p>EDIT 3: After cross posting to <a href="https://forum.kde.org/viewtopic.php?f=74&amp;t=122277&amp;p=316712#p316712" rel="noreferrer">Eigen Forums</a> it would seem I can't do better than the suggested answer.</p>