arma_util.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_BINDINGS_GO_GONUM_ARMA_UTIL_HPP
13 #define MLPACK_BINDINGS_GO_GONUM_ARMA_UTIL_HPP
14 
15 // Include Armadillo via mlpack.
16 #include <mlpack/core/util/io.hpp>
17 #include <mlpack/core.hpp>
18 
19 namespace mlpack {
20 
26 template<typename T>
27 inline typename T::elem_type* GetMemory(T& m)
28 {
29  if (m.mem && m.n_elem <= arma::arma_config::mat_prealloc)
30  {
31  // We need to allocate new memory.
32  typename T::elem_type* mem =
33  arma::memory::acquire<typename T::elem_type>(m.n_elem);
34  arma::arrayops::copy(mem, m.memptr(), m.n_elem);
35  return mem;
36  }
37  else
38  {
39  arma::access::rw(m.mem_state) = 1;
40  // With Armadillo 10 and newer, we must set `n_alloc` to 0 so that
41  // Armadillo does not deallocate the memory.
42  #if ARMA_VERSION_MAJOR >= 10
43  arma::access::rw(m.n_alloc) = 0;
44  #endif
45  return m.memptr();
46  }
47 }
48 
49 } // namespace mlpack
50 
51 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
T::elem_type * GetMemory(T &m)
Return the matrix&#39;s allocated memory pointer, unless the matrix is using its internal preallocated me...
Definition: arma_util.hpp:27
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
auto copy(Range &&rng, OutputIt &&it) -> enable_if_t< is_range< Range >::value, decay_t< OutputIt > >
Definition: algorithm.hpp:677