/* * ===================================================================================== * * Filename: sparse_matrix.h * * Description: * * Version: 1.0 * Created: 06/20/2007 02:40:16 PM EDT * Revision: none * Compiler: gcc * * Author: Nikolaos Vasiloglou (NV), nvasil@ieee.org * Company: Georgia Tech Fastlab-ESP Lab * * ===================================================================================== */ #ifndef SPARSE_MATRIX_H_ #define SPARSE_MATRIX_H_ #include #include #include #include #include "fastlib/fastlib.h" using namespace std; namespace sparse { template class Matrix { public: typedef pair NonZeroElement_t; template friend class Matrix; Matrix(){ allocation_flag_=false; } ~Matrix(){}; void Init(index_t dimension, int32 max_non_zero) { dimension_=dimension; max_non_zero_=max_non_zero; index_t alloc_size = dimension_ * max_non_zero_ * sizeof(NonZeroElement_t); ptr_rows_ = (NonZeroElement_t *)mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); if (ptr_rows_==MAP_FAILED) { FATAL("Couldn't allocate memory for the ptr_rows_, error: %s\n", strerror(errno)); } allocation_flag_=true; row_elements_=(int32 *)mmap(NULL, dimension_*sizeof(int32) , PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); Fill(); } void Init(NonZeroElement_t *ptr_rows, index_t dimension, int32 max_non_zero) { ptr_rows_=ptr_rows; dimension_=dimension; max_non_zero_=max_non_zero; row_elements_=(int32 *)mmap(NULL, dimension_*sizeof(int32) , PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); Fill(); } void Init(string file) { FILE *fp=fopen(file.c_str(), "r"); if (fp==NULL) { FATAL("Error %s while trying to open %s\n", strerror(errno), file.c_str()); } fscanf(fp, "%i %i\n", &dimension_, &max_non_zero_); index_t alloc_size = dimension_ * max_non_zero_ * sizeof(NonZeroElement_t); ptr_rows_ = (NonZeroElement_t *)mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); if (ptr_rows_==MAP_FAILED) { FATAL("Couldn't allocate memory for the ptr_rows_, error: %s\n", strerror(errno)); } allocation_flag_=true; row_elements_=(int32 *)mmap(NULL, dimension_*sizeof(int32) , PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); Fill(); long long i,j; double val; while (feof(fp)==false) { fscanf(fp,"%lli %lli %lg\n", &i, &j, &val); this->set(i, j, val); } fclose(fp); } void Destruct() { if (allocation_flag_==true) { index_t alloc_size = dimension_ * max_non_zero_ * sizeof(NonZeroElement_t); munmap(ptr_rows_, alloc_size); } munmap(row_elements_, dimension_*sizeof(int32)); } void ToTextFile(string filename) { FILE *fp; fp=fopen(filename.c_str(), "w"); if (fp==NULL) { FATAL("Error %s while trying to open %s\n", strerror(errno), filename.c_str()); } fprintf(fp, "%lli\n", (long long)dimension_); for(index_t i=0; i=0, "Tried to access row %lli > %lli\n", (signed long long)i, (signed long long)dimension_); for(int32 k=0; k=0, "Tried to access row %lli > %lli\n", (signed long long)i, (signed long long)dimension_); DEBUG_ASSERT_MSG(row_elements_[i] value) { high=mid-1; } else { if (ptr_rows_[mid].first < value) { low=mid+1; } else { return mid; } } } return -1; } }; // solves the problem A*x=b // where A is sparse semipositive definite template void ConjugateGradient(Matrix &A, T *b, T* x, T tolerance); template inline T VectorDotProduct(T* a, T* b, index_t size) { T result=0; for(index_t i=0; i inline void VectorPlus(T* a, T* b, index_t size, T* c) { for(index_t i=0; i inline void VectorPlusTimes(T* a, T scalar, T* b, index_t size, T* c) { for(index_t i=0; i inline void VectorMinusTimes(T* a, T scalar, T* b, index_t size, T* c) { for(index_t i=0; i inline void VectorMinus(T* a, T* b, index_t size, T* c) { for(index_t i=0; i inline void VectorMultiplyScalar(T* vector_in, T scalar, index_t size, T* vector_out) { for(index_t i=0; i inline T *NewVector(index_t size) { T* out = (T*)mmap(NULL, size*sizeof(T), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); if (madvise(out, size*sizeof(T), MADV_SEQUENTIAL)==-1) { NONFATAL("Advising failed error %s\n", strerror(errno)); } return out; } template inline void DeleteVector(T* vector, index_t size) { if (munmap(vector, size*sizeof(T))<0) { NONFATAL("Failed to unmap memory, error:%s\n", strerror(errno)); } } template T *ReadVectorFromFile(string filename) { FILE *fp; fp=fopen(filename.c_str(), "r"); if (fp==NULL) { FATAL("Error %s while trying to open %s\n", strerror(errno), filename.c_str()); } long long size; fscanf(fp, "%lli\n", &size); T *ptr = NewVector(size); index_t i=0; while (!feof(fp)) { double value; fscanf(fp, "%lg", &value); ptr[i]=(T)value; i++; } fclose(fp); return ptr; } template void WriteVectorToFile(string filename, T *vec, index_t dimension) { FILE *fp; fp=fopen(filename.c_str(), "w"); if (fp==NULL) { FATAL("Error %s while trying to open %s\n", strerror(errno), filename.c_str()); } fprintf(fp, "%lli\n", dimension); for(index_t i=0; i