added NRVec::subvector

This commit is contained in:
Jiri Pittner 2022-06-23 19:34:17 +02:00
parent 0d5a893b95
commit 6c22365a48
2 changed files with 67 additions and 0 deletions

61
vec.cc
View File

@ -886,6 +886,67 @@ for(typename std::list<T>::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 <typename T>
const NRVec<T> NRVec<T>::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<T> r(n, getlocation());
if(!LA_traits<T>::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 <typename T>
void NRVec<T>::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<T>::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 * forced instantization in the corespoding object file
******************************************************************************/ ******************************************************************************/

6
vec.h
View File

@ -190,6 +190,12 @@ public:
//! perform deep-copy of given vector //! perform deep-copy of given vector
NRVec& operator|=(const NRVec &rhs); 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 //! relational operators
const bool operator!=(const NRVec &rhs) const const bool operator!=(const NRVec &rhs) const
{ {