// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au) // Copyright 2008-2016 National ICT Australia (NICTA) // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ------------------------------------------------------------------------ //! \addtogroup eop_core //! @{ #undef arma_applier_1u #undef arma_applier_1a #undef arma_applier_2 #undef arma_applier_3 #undef operatorA #undef arma_applier_1_mp #undef arma_applier_2_mp #undef arma_applier_3_mp #if defined(ARMA_SIMPLE_LOOPS) #define arma_applier_1u(operatorA) \ {\ for(uword i=0; i::process(P[i], k);\ }\ } #else #define arma_applier_1u(operatorA) \ {\ uword i,j;\ \ for(i=0, j=1; j::process(tmp_i, k);\ tmp_j = eop_core::process(tmp_j, k);\ \ out_mem[i] operatorA tmp_i;\ out_mem[j] operatorA tmp_j;\ }\ \ if(i < n_elem)\ {\ out_mem[i] operatorA eop_core::process(P[i], k);\ }\ } #endif #if defined(ARMA_SIMPLE_LOOPS) #define arma_applier_1a(operatorA) \ {\ for(uword i=0; i::process(P.at_alt(i), k);\ }\ } #else #define arma_applier_1a(operatorA) \ {\ uword i,j;\ \ for(i=0, j=1; j::process(tmp_i, k);\ tmp_j = eop_core::process(tmp_j, k);\ \ out_mem[i] operatorA tmp_i;\ out_mem[j] operatorA tmp_j;\ }\ \ if(i < n_elem)\ {\ out_mem[i] operatorA eop_core::process(P.at_alt(i), k);\ }\ } #endif #define arma_applier_2(operatorA) \ {\ if(n_rows != 1)\ {\ for(uword col=0; col::process(tmp_i, k);\ tmp_j = eop_core::process(tmp_j, k);\ \ *out_mem operatorA tmp_i; out_mem++;\ *out_mem operatorA tmp_j; out_mem++;\ }\ \ if(i < n_rows)\ {\ *out_mem operatorA eop_core::process(P.at(i,col), k); out_mem++;\ }\ }\ }\ else\ {\ for(uword count=0; count < n_cols; ++count)\ {\ out_mem[count] operatorA eop_core::process(P.at(0,count), k);\ }\ }\ } #define arma_applier_3(operatorA) \ {\ for(uword slice=0; slice::process(tmp_i, k);\ tmp_j = eop_core::process(tmp_j, k);\ \ *out_mem operatorA tmp_i; out_mem++; \ *out_mem operatorA tmp_j; out_mem++; \ }\ \ if(i < n_rows)\ {\ *out_mem operatorA eop_core::process(P.at(i,col,slice), k); out_mem++; \ }\ }\ }\ } #if defined(ARMA_USE_OPENMP) #define arma_applier_1_mp(operatorA) \ {\ const int n_threads = mp_thread_limit::get();\ _Pragma("omp parallel for schedule(static) num_threads(n_threads)")\ for(uword i=0; i::process(P[i], k);\ }\ } #define arma_applier_2_mp(operatorA) \ {\ const int n_threads = mp_thread_limit::get();\ if(n_cols == 1)\ {\ _Pragma("omp parallel for schedule(static) num_threads(n_threads)")\ for(uword count=0; count < n_rows; ++count)\ {\ out_mem[count] operatorA eop_core::process(P.at(count,0), k);\ }\ }\ else\ if(n_rows == 1)\ {\ _Pragma("omp parallel for schedule(static) num_threads(n_threads)")\ for(uword count=0; count < n_cols; ++count)\ {\ out_mem[count] operatorA eop_core::process(P.at(0,count), k);\ }\ }\ else\ {\ _Pragma("omp parallel for schedule(static) num_threads(n_threads)")\ for(uword col=0; col < n_cols; ++col)\ {\ for(uword row=0; row < n_rows; ++row)\ {\ out.at(row,col) operatorA eop_core::process(P.at(row,col), k);\ }\ }\ }\ } #define arma_applier_3_mp(operatorA) \ {\ const int n_threads = mp_thread_limit::get();\ _Pragma("omp parallel for schedule(static) num_threads(n_threads)")\ for(uword slice=0; slice::process(P.at(row,col,slice), k);\ }\ }\ } #else #define arma_applier_1_mp(operatorA) arma_applier_1u(operatorA) #define arma_applier_2_mp(operatorA) arma_applier_2(operatorA) #define arma_applier_3_mp(operatorA) arma_applier_3(operatorA) #endif // // matrices template template arma_hot inline void eop_core::apply(outT& out, const eOp& x) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; // NOTE: we're assuming that the matrix has already been set to the correct size and there is no aliasing; // size setting and alias checking is done by either the Mat contructor or operator=() const eT k = x.aux; eT* out_mem = out.memptr(); const bool use_mp = (arma_config::openmp) && (eOp::use_mp || (is_same_type::value && (is_cx::yes || x.aux != eT(2)))); if(Proxy::use_at == false) { const uword n_elem = x.get_n_elem(); if(use_mp && mp_gate::eval(n_elem)) { typename Proxy::ea_type P = x.P.get_ea(); arma_applier_1_mp(=); } else { if(memory::is_aligned(out_mem)) { memory::mark_as_aligned(out_mem); if(x.P.is_aligned()) { typename Proxy::aligned_ea_type P = x.P.get_aligned_ea(); arma_applier_1a(=); } else { typename Proxy::ea_type P = x.P.get_ea(); arma_applier_1u(=); } } else { typename Proxy::ea_type P = x.P.get_ea(); arma_applier_1u(=); } } } else { const uword n_rows = x.get_n_rows(); const uword n_cols = x.get_n_cols(); const Proxy& P = x.P; if(use_mp && mp_gate::eval(x.get_n_elem())) { arma_applier_2_mp(=); } else { arma_applier_2(=); } } } template template arma_hot inline void eop_core::apply_inplace_plus(Mat& out, const eOp& x) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const uword n_rows = x.get_n_rows(); const uword n_cols = x.get_n_cols(); arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "addition"); const eT k = x.aux; eT* out_mem = out.memptr(); const bool use_mp = (arma_config::openmp) && (eOp::use_mp || (is_same_type::value && (is_cx::yes || x.aux != eT(2)))); if(Proxy::use_at == false) { const uword n_elem = x.get_n_elem(); if(use_mp && mp_gate::eval(n_elem)) { typename Proxy::ea_type P = x.P.get_ea(); arma_applier_1_mp(+=); } else { if(memory::is_aligned(out_mem)) { memory::mark_as_aligned(out_mem); if(x.P.is_aligned()) { typename Proxy::aligned_ea_type P = x.P.get_aligned_ea(); arma_applier_1a(+=); } else { typename Proxy::ea_type P = x.P.get_ea(); arma_applier_1u(+=); } } else { typename Proxy::ea_type P = x.P.get_ea(); arma_applier_1u(+=); } } } else { const Proxy& P = x.P; if(use_mp && mp_gate::eval(x.get_n_elem())) { arma_applier_2_mp(+=); } else { arma_applier_2(+=); } } } template template arma_hot inline void eop_core::apply_inplace_minus(Mat& out, const eOp& x) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const uword n_rows = x.get_n_rows(); const uword n_cols = x.get_n_cols(); arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "subtraction"); const eT k = x.aux; eT* out_mem = out.memptr(); const bool use_mp = (arma_config::openmp) && (eOp::use_mp || (is_same_type::value && (is_cx::yes || x.aux != eT(2)))); if(Proxy::use_at == false) { const uword n_elem = x.get_n_elem(); if(use_mp && mp_gate::eval(n_elem)) { typename Proxy::ea_type P = x.P.get_ea(); arma_applier_1_mp(-=); } else { if(memory::is_aligned(out_mem)) { memory::mark_as_aligned(out_mem); if(x.P.is_aligned()) { typename Proxy::aligned_ea_type P = x.P.get_aligned_ea(); arma_applier_1a(-=); } else { typename Proxy::ea_type P = x.P.get_ea(); arma_applier_1u(-=); } } else { typename Proxy::ea_type P = x.P.get_ea(); arma_applier_1u(-=); } } } else { const Proxy& P = x.P; if(use_mp && mp_gate::eval(x.get_n_elem())) { arma_applier_2_mp(-=); } else { arma_applier_2(-=); } } } template template arma_hot inline void eop_core::apply_inplace_schur(Mat& out, const eOp& x) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const uword n_rows = x.get_n_rows(); const uword n_cols = x.get_n_cols(); arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "element-wise multiplication"); const eT k = x.aux; eT* out_mem = out.memptr(); const bool use_mp = (arma_config::openmp) && (eOp::use_mp || (is_same_type::value && (is_cx::yes || x.aux != eT(2)))); if(Proxy::use_at == false) { const uword n_elem = x.get_n_elem(); if(use_mp && mp_gate::eval(n_elem)) { typename Proxy::ea_type P = x.P.get_ea(); arma_applier_1_mp(*=); } else { if(memory::is_aligned(out_mem)) { memory::mark_as_aligned(out_mem); if(x.P.is_aligned()) { typename Proxy::aligned_ea_type P = x.P.get_aligned_ea(); arma_applier_1a(*=); } else { typename Proxy::ea_type P = x.P.get_ea(); arma_applier_1u(*=); } } else { typename Proxy::ea_type P = x.P.get_ea(); arma_applier_1u(*=); } } } else { const Proxy& P = x.P; if(use_mp && mp_gate::eval(x.get_n_elem())) { arma_applier_2_mp(*=); } else { arma_applier_2(*=); } } } template template arma_hot inline void eop_core::apply_inplace_div(Mat& out, const eOp& x) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const uword n_rows = x.get_n_rows(); const uword n_cols = x.get_n_cols(); arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "element-wise division"); const eT k = x.aux; eT* out_mem = out.memptr(); const bool use_mp = (arma_config::openmp) && (eOp::use_mp || (is_same_type::value && (is_cx::yes || x.aux != eT(2)))); if(Proxy::use_at == false) { const uword n_elem = x.get_n_elem(); if(use_mp && mp_gate::eval(n_elem)) { typename Proxy::ea_type P = x.P.get_ea(); arma_applier_1_mp(/=); } else { if(memory::is_aligned(out_mem)) { memory::mark_as_aligned(out_mem); if(x.P.is_aligned()) { typename Proxy::aligned_ea_type P = x.P.get_aligned_ea(); arma_applier_1a(/=); } else { typename Proxy::ea_type P = x.P.get_ea(); arma_applier_1u(/=); } } else { typename Proxy::ea_type P = x.P.get_ea(); arma_applier_1u(/=); } } } else { const Proxy& P = x.P; if(use_mp && mp_gate::eval(x.get_n_elem())) { arma_applier_2_mp(/=); } else { arma_applier_2(/=); } } } // // cubes template template arma_hot inline void eop_core::apply(Cube& out, const eOpCube& x) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; // NOTE: we're assuming that the matrix has already been set to the correct size and there is no aliasing; // size setting and alias checking is done by either the Mat contructor or operator=() const eT k = x.aux; eT* out_mem = out.memptr(); const bool use_mp = (arma_config::openmp) && (eOpCube::use_mp || (is_same_type::value && (is_cx::yes || x.aux != eT(2)))); if(ProxyCube::use_at == false) { const uword n_elem = out.n_elem; if(use_mp && mp_gate::eval(n_elem)) { typename ProxyCube::ea_type P = x.P.get_ea(); arma_applier_1_mp(=); } else { if(memory::is_aligned(out_mem)) { memory::mark_as_aligned(out_mem); if(x.P.is_aligned()) { typename ProxyCube::aligned_ea_type P = x.P.get_aligned_ea(); arma_applier_1a(=); } else { typename ProxyCube::ea_type P = x.P.get_ea(); arma_applier_1u(=); } } else { typename ProxyCube::ea_type P = x.P.get_ea(); arma_applier_1u(=); } } } else { const uword n_rows = x.get_n_rows(); const uword n_cols = x.get_n_cols(); const uword n_slices = x.get_n_slices(); const ProxyCube& P = x.P; if(use_mp && mp_gate::eval(x.get_n_elem())) { arma_applier_3_mp(=); } else { arma_applier_3(=); } } } template template arma_hot inline void eop_core::apply_inplace_plus(Cube& out, const eOpCube& x) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const uword n_rows = x.get_n_rows(); const uword n_cols = x.get_n_cols(); const uword n_slices = x.get_n_slices(); arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "addition"); const eT k = x.aux; eT* out_mem = out.memptr(); const bool use_mp = (arma_config::openmp) && (eOpCube::use_mp || (is_same_type::value && (is_cx::yes || x.aux != eT(2)))); if(ProxyCube::use_at == false) { const uword n_elem = out.n_elem; if(use_mp && mp_gate::eval(n_elem)) { typename ProxyCube::ea_type P = x.P.get_ea(); arma_applier_1_mp(+=); } else { if(memory::is_aligned(out_mem)) { memory::mark_as_aligned(out_mem); if(x.P.is_aligned()) { typename ProxyCube::aligned_ea_type P = x.P.get_aligned_ea(); arma_applier_1a(+=); } else { typename ProxyCube::ea_type P = x.P.get_ea(); arma_applier_1u(+=); } } else { typename ProxyCube::ea_type P = x.P.get_ea(); arma_applier_1u(+=); } } } else { const ProxyCube& P = x.P; if(use_mp && mp_gate::eval(x.get_n_elem())) { arma_applier_3_mp(+=); } else { arma_applier_3(+=); } } } template template arma_hot inline void eop_core::apply_inplace_minus(Cube& out, const eOpCube& x) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const uword n_rows = x.get_n_rows(); const uword n_cols = x.get_n_cols(); const uword n_slices = x.get_n_slices(); arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "subtraction"); const eT k = x.aux; eT* out_mem = out.memptr(); const bool use_mp = (arma_config::openmp) && (eOpCube::use_mp || (is_same_type::value && (is_cx::yes || x.aux != eT(2)))); if(ProxyCube::use_at == false) { const uword n_elem = out.n_elem; if(use_mp && mp_gate::eval(n_elem)) { typename ProxyCube::ea_type P = x.P.get_ea(); arma_applier_1_mp(-=); } else { if(memory::is_aligned(out_mem)) { memory::mark_as_aligned(out_mem); if(x.P.is_aligned()) { typename ProxyCube::aligned_ea_type P = x.P.get_aligned_ea(); arma_applier_1a(-=); } else { typename ProxyCube::ea_type P = x.P.get_ea(); arma_applier_1u(-=); } } else { typename ProxyCube::ea_type P = x.P.get_ea(); arma_applier_1u(-=); } } } else { const ProxyCube& P = x.P; if(use_mp && mp_gate::eval(x.get_n_elem())) { arma_applier_3_mp(-=); } else { arma_applier_3(-=); } } } template template arma_hot inline void eop_core::apply_inplace_schur(Cube& out, const eOpCube& x) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const uword n_rows = x.get_n_rows(); const uword n_cols = x.get_n_cols(); const uword n_slices = x.get_n_slices(); arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "element-wise multiplication"); const eT k = x.aux; eT* out_mem = out.memptr(); const bool use_mp = (arma_config::openmp) && (eOpCube::use_mp || (is_same_type::value && (is_cx::yes || x.aux != eT(2)))); if(ProxyCube::use_at == false) { const uword n_elem = out.n_elem; if(use_mp && mp_gate::eval(n_elem)) { typename ProxyCube::ea_type P = x.P.get_ea(); arma_applier_1_mp(*=); } else { if(memory::is_aligned(out_mem)) { memory::mark_as_aligned(out_mem); if(x.P.is_aligned()) { typename ProxyCube::aligned_ea_type P = x.P.get_aligned_ea(); arma_applier_1a(*=); } else { typename ProxyCube::ea_type P = x.P.get_ea(); arma_applier_1u(*=); } } else { typename ProxyCube::ea_type P = x.P.get_ea(); arma_applier_1u(*=); } } } else { const ProxyCube& P = x.P; if(use_mp && mp_gate::eval(x.get_n_elem())) { arma_applier_3_mp(*=); } else { arma_applier_3(*=); } } } template template arma_hot inline void eop_core::apply_inplace_div(Cube& out, const eOpCube& x) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const uword n_rows = x.get_n_rows(); const uword n_cols = x.get_n_cols(); const uword n_slices = x.get_n_slices(); arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "element-wise division"); const eT k = x.aux; eT* out_mem = out.memptr(); const bool use_mp = (arma_config::openmp) && (eOpCube::use_mp || (is_same_type::value && (is_cx::yes || x.aux != eT(2)))); if(ProxyCube::use_at == false) { const uword n_elem = out.n_elem; if(use_mp && mp_gate::eval(n_elem)) { typename ProxyCube::ea_type P = x.P.get_ea(); arma_applier_1_mp(/=); } else { if(memory::is_aligned(out_mem)) { memory::mark_as_aligned(out_mem); if(x.P.is_aligned()) { typename ProxyCube::aligned_ea_type P = x.P.get_aligned_ea(); arma_applier_1a(/=); } else { typename ProxyCube::ea_type P = x.P.get_ea(); arma_applier_1u(/=); } } else { typename ProxyCube::ea_type P = x.P.get_ea(); arma_applier_1u(/=); } } } else { const ProxyCube& P = x.P; if(use_mp && mp_gate::eval(x.get_n_elem())) { arma_applier_3_mp(/=); } else { arma_applier_3(/=); } } } // // common template template arma_inline eT eop_core::process(const eT, const eT) { arma_stop_logic_error("eop_core::process(): unhandled eop_type"); return eT(0); } template<> template arma_inline eT eop_core::process(const eT val, const eT k) { return val + k; } template<> template arma_inline eT eop_core::process(const eT val, const eT k) { return k - val; } template<> template arma_inline eT eop_core::process(const eT val, const eT k) { return val - k; } template<> template arma_inline eT eop_core::process(const eT val, const eT k) { return val * k; } template<> template arma_inline eT eop_core::process(const eT val, const eT k) { return k / val; } template<> template arma_inline eT eop_core::process(const eT val, const eT k) { return val / k; } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return val*val; } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::neg(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::sqrt(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::log(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::log2(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::log10(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return arma::trunc_log(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::log1p(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::exp(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::exp2(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::exp10(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return arma::trunc_exp(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::expm1(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::cos(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::sin(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::tan(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::acos(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::asin(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::atan(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::cosh(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::sinh(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::tanh(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::acosh(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::asinh(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::atanh(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return arma_sinc(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::direct_eps(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::arma_abs(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return arma_arg::eval(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::conj(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT k) { return eop_aux::pow(val, k); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::floor(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::ceil(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::round(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::trunc(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return arma_sign(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::erf(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::erfc(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::lgamma(val); } template<> template arma_inline eT eop_core::process(const eT val, const eT ) { return eop_aux::tgamma(val); } #undef arma_applier_1u #undef arma_applier_1a #undef arma_applier_2 #undef arma_applier_3 #undef arma_applier_1_mp #undef arma_applier_2_mp #undef arma_applier_3_mp //! @}