#ifndef _FMATRIX_H #define _FMATRIX_H CPS_START_NAMESPACE template class basicMatrix{ T* tt; int rows, cols; int fsize; //number of elements void free(){ if(tt!=NULL) sfree("basicMatrix","~basicMatrix","free",tt); } void alloc(const int &_rows, const int &_cols, T const* cp = NULL){ if(_rows != rows || _cols != cols){ free(); rows = _rows; cols = _cols; fsize = rows*cols; tt = (T*)smalloc("basicMatrix", "basicMatrix", "alloc" , sizeof(T) * fsize); } if(cp != NULL) for(int i=0;i &r){ alloc(r.rows,r.cols,r.tt); } T* ptr(){ return tt;} void resize(const int &_rows, const int &_cols){ alloc(_rows,_cols); } inline const T & operator()(const int &i, const int &j) const{ return tt[j + cols*i]; } inline T & operator()(const int &i, const int &j){ return tt[j + cols*i]; } inline const int &nRows() const{ return rows; } inline const int &nCols() const{ return cols; } ~basicMatrix(){ free(); } }; //A matrix of complex numbers and some useful associated methods template class fMatrix{ mf_Complex* tt; int rows, cols; int fsize; //number of elements void free_matrix(){ if(tt!=NULL) sfree("fMatrix","~fMatrix","free",tt); } void alloc_matrix(const int _rows, const int _cols, mf_Complex const* cp = NULL){ if(_rows != rows || _cols != cols){ free_matrix(); rows = _rows; cols = _cols; fsize = rows*cols; tt = (mf_Complex*)smalloc("fMatrix", "fMatrix", "alloc" , sizeof(mf_Complex) * fsize); } if(cp == NULL) zero(); else for(int i=0;i &r): rows(0), cols(0), fsize(0),tt(NULL){ alloc_matrix(r.rows,r.cols,r.tt); } mf_Complex *ptr(){ return tt;} void resize(const int _rows, const int _cols){ alloc_matrix(_rows,_cols); } void zero(){ for(int i=0;i &r){ for(int i=0;i void rearrangeTsrcTsep(fMatrix &m){ int Lt = GJP.Tnodes()*GJP.TnodeSites(); if(m.nRows()!=Lt || m.nCols()!=Lt) ERR.General("","rearrangeTsrcTsep(fMatrix &)","Expect an Lt*Lt matrix\n"); fMatrix tmp(m); for(int tsnk=0;tsnk class fVector{ mf_Complex* tt; int fsize; void free_mem(){ if(tt!=NULL) sfree("fVector","~fVector","free",tt); } void alloc_mem(const int _elems, mf_Complex const* cp = NULL){ if(_elems != fsize){ free_mem(); fsize = _elems; tt = (mf_Complex*)smalloc("fVector", "fVector", "alloc" , sizeof(mf_Complex) * fsize); } if(cp == NULL) zero(); else for(int i=0;i &r): fsize(0),tt(NULL){ alloc_mem(r.fsize,r.tt); } mf_Complex *ptr(){ return tt;} void resize(const int _elems){ alloc_mem(_elems); } void zero(){ for(int i=0;i class basicComplexArray: public AllocPolicy{ protected: int thread_size; //size of each thread unit int nthread; int size; //total size mf_Complex *con; public: basicComplexArray(): size(0), con(NULL){} basicComplexArray(const int &_thread_size, const int &_nthread = 1): size(0), con(NULL){ resize(_thread_size,_nthread); } void free_mem(){ if(con != NULL){ AllocPolicy::_free(con); con = NULL; } } void resize(const int &_thread_size, const int &_nthread = 1){ free_mem(); thread_size = _thread_size; nthread = _nthread; size = _thread_size * _nthread; this->_alloc(&con, size*sizeof(mf_Complex)); memset((void*)con, 0, size * sizeof(mf_Complex)); } ~basicComplexArray(){ free_mem(); } inline const mf_Complex & operator[](const int i) const{ return con[i]; } inline mf_Complex & operator[](const int i){ return con[i]; } inline mf_Complex & operator()(const int i, const int thread){ return con[i + thread * thread_size]; } int nElementsTotal() const{ return size; } int nElementsPerThread() const{ return thread_size; } int nThreads() const{ return nthread; } //Sum (reduce) over all threads void threadSum(){ if(nthread == 1) return; basicComplexArray tmp(thread_size,1); #pragma omp parallel for for(int i=0;icon,size); //QMP_sum_array( (typename mf_Complex::value_type*)con,2*size); } }; CPS_END_NAMESPACE #endif