arma_util.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_BINDINGS_PYTHON_CYTHON_ARMA_UTIL_HPP
13 #define MLPACK_BINDINGS_PYTHON_CYTHON_ARMA_UTIL_HPP
14 
15 // Include Armadillo via mlpack.
16 #include <mlpack/core.hpp>
17 
21 template<typename T>
22 void SetMemState(T& t, int state)
23 {
24  const_cast<arma::uhword&>(t.mem_state) = state;
25  // If we just "released" the memory, so that the matrix does not own it, with
26  // Armadillo 10 we must also ensure that the matrix does not deallocate the
27  // memory by specifying `n_alloc = 0`.
28  #if ARMA_VERSION_MAJOR >= 10
29  const_cast<arma::uword&>(t.n_alloc) = 0;
30  #endif
31 }
32 
36 template<typename T>
37 size_t GetMemState(T& t)
38 {
39  // Fake the memory state if we are using preallocated memory---since we will
40  // end up copying that memory, NumPy can own it.
41  if (t.mem && t.n_elem <= arma::arma_config::mat_prealloc)
42  return 0;
43 
44  return (size_t) t.mem_state;
45 }
46 
52 template<typename T>
53 inline typename T::elem_type* GetMemory(T& m)
54 {
55  if (m.mem && m.n_elem <= arma::arma_config::mat_prealloc)
56  {
57  // We need to allocate new memory.
58  typename T::elem_type* mem =
59  arma::memory::acquire<typename T::elem_type>(m.n_elem);
60  arma::arrayops::copy(mem, m.memptr(), m.n_elem);
61  return mem;
62  }
63  else
64  {
65  return m.memptr();
66  }
67 }
68 
69 #endif
void SetMemState(T &t, int state)
Set the memory state of the given Armadillo object.
Definition: arma_util.hpp:22
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:53
size_t GetMemState(T &t)
Get the memory state of the given Armadillo object.
Definition: arma_util.hpp:37
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