From 6c22365a484b97e1084fafc7144ab46aa3a9c20e Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Thu, 23 Jun 2022 19:34:17 +0200 Subject: [PATCH] added NRVec::subvector --- vec.cc | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ vec.h | 6 ++++++ 2 files changed, 67 insertions(+) diff --git a/vec.cc b/vec.cc index 13379ff..459a477 100644 --- a/vec.cc +++ b/vec.cc @@ -886,6 +886,67 @@ for(typename std::list::const_iterator i=l.begin(); i!=l.end(); ++i) (*this)[ } +/***************************************************************************//** + * extract block subvector + * @param[in] from starting position + * @param[in] to final position + * @return extracted block subvector + ******************************************************************************/ +template +const NRVec NRVec::subvector(const int from, const int to) const +{ +#ifdef DEBUG + if(from<0 || from>=nn|| to<0 || to>=nn || from>to){ + laerror("invalid subvector specification"); + } +#endif + const int n = to - from + 1; + NRVec r(n, getlocation()); + if(!LA_traits::is_plaindata()) laerror("only implemented for plain data"); + +#ifdef CUDALA + if(location == cpu){ +#endif + memcpy(r.v, v+from, n*sizeof(T)); +#ifdef CUDALA + }else{ + if(sizeof(T)%sizeof(float) != 0) laerror("cpu memcpy alignment problem"); + cublasScopy(n*sizeof(T)/sizeof(float), (const float *)(v+from), 1, (float*)r.v, 1); + TEST_CUBLAS("cublasScopy"); + } +#endif + return r; +} + +/***************************************************************************//** + * places given vector as subvector at given position + * @param[in] from coordinate + * @param[in] rhs input vector + ******************************************************************************/ +template +void NRVec::storesubvector(const int from, const NRVec &rhs) +{ + const int to = from + rhs.size() - 1; +#ifdef DEBUG + if(from<0 || from>=nn || to>=nn) laerror("bad indices in storesubvector"); +#endif + SAME_LOC(*this, rhs); + if(!LA_traits::is_plaindata()) laerror("only implemented for plain data"); + + #ifdef CUDALA + if(location == cpu){ + #endif + memcpy(v+from, rhs.v, rhs.size()*sizeof(T)); + + #ifdef CUDALA + }else{ + if(sizeof(T)%sizeof(float) != 0) laerror("cpu memcpy alignment problem"); + cublasScopy(rhs.size()*sizeof(T)/sizeof(float), (const float *) (rhs.v), 1, (float *)(v + from), 1); + } + #endif +} + + /***************************************************************************//** * forced instantization in the corespoding object file ******************************************************************************/ diff --git a/vec.h b/vec.h index f9789b1..a1b7a8d 100644 --- a/vec.h +++ b/vec.h @@ -190,6 +190,12 @@ public: //! perform deep-copy of given vector NRVec& operator|=(const NRVec &rhs); + //! extract specified subvector + const NRVec subvector(const int from, const int to) const; + + //! store given vector at given position into the current vector + void storesubvector(const int from, const NRVec &rhs); + //! relational operators const bool operator!=(const NRVec &rhs) const {