From b7d3a5d9778e857da177455039e6059769eaf023 Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Wed, 27 Oct 2021 23:24:41 +0200 Subject: [PATCH] vector and matrix contructors from arrays of arbirary size via nested templates --- mat.h | 40 ++++++++++++++++++++++++++++++++++++++++ t.cc | 10 +++++++++- vec.h | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/mat.h b/mat.h index 1b96dd3..e7d6017 100644 --- a/mat.h +++ b/mat.h @@ -83,6 +83,9 @@ public: //! inlined constructor creating matrix of given size filled with data located at given memory location NRMat(const T *a, const int n, const int m); + //! inlined constructor creating matrix of given size from an array + template inline NRMat(const T (&a)[R][C]); + //! inlined copy-constructor inline NRMat(const NRMat &rhs); @@ -490,6 +493,43 @@ NRMat::NRMat(const T &a, const int n, const int m, const GPUID loc) : nn(n), #endif } + +/***************************************************************************//** + * inline constructor creating vector from an array + ******************************************************************************/ + + +template template +inline NRMat::NRMat(const T (&a)[R][C]) : count(new int) { + nn = R; + mm = C; + *count = 1; + + +#ifdef CUDALA + if(location==cpu){ +#endif + #ifdef MATPTR + v = new T*[nn]; + v[0] = new T[nn*mm]; + for (register int i=1; i::is_plaindata()) memcpy(v[0], a, nn*mm*sizeof(T)); + else for( int i=0; i::is_plaindata()) memcpy(v, a, nn*mm*sizeof(T)); + else for( int i=0; i::is_plaindata()) laerror("only implemented for plain data"); + v = (T*) gpualloc(nn*mm*sizeof(T)); + cublasSetVector(nm, sizeof(T), a, 1, v, 1); + } +#endif +} + + /***************************************************************************//** * matrix constructor * @param[in] a value of type T intended for matrix inicialization diff --git a/t.cc b/t.cc index 16faeda..192f138 100644 --- a/t.cc +++ b/t.cc @@ -2284,7 +2284,7 @@ Polynomial qq=q.pow(n); cout <<"test binom "<<(p-qq).norm()<>n ; @@ -2294,5 +2294,13 @@ cout <<"non-zero = "<< ((n&1)?p.odd_powers():p.even_powers())< v({1,2,3,4}); +cout < m({{1,2,3},{4,5,6}}); +cout< inline NRVec(const T (&a)[SIZE]); //! inlined constructor creating vector of given size filled with data located at given memory location inline NRVec(const T *a, const int n); @@ -580,6 +583,35 @@ inline NRVec::NRVec(const T& a, const int n): nn(n), count(new int) { #endif } +/***************************************************************************//** + * inline constructor creating vector from an array + ******************************************************************************/ + + +template template +inline NRVec::NRVec(const T (&a)[SIZE]) : count(new int) { + nn = SIZE; + *count = 1; +#ifdef CUDALA + location = DEFAULT_LOC; + if(location == cpu){ +#endif + v = new T[nn]; + if(LA_traits::is_plaindata()) memcpy(v, a, nn*sizeof(T)); + else for( int i=0; i::is_plaindata()) laerror("only implemented for plain data"); + cublasSetVector(nn, sizeof(T), a, 1, v, 1); + TEST_CUBLAS("cublasSetVector"); + } +#endif +} + + + + /***************************************************************************//** * inline constructor creating vector of given size filled with given data