ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
base_matrix.h
Go to the documentation of this file.
1#ifndef BASE_MATRIX_H
2#define BASE_MATRIX_H
3
4#include <iostream>
5#include <mutex>
6#include <cassert>
7
8namespace hamilt
9{
18template <typename T>
20{
21 public:
22 // Constructor of class BaseMatrix
23 BaseMatrix(const int& nrow_, const int& ncol_, T* data_existed = nullptr);
24 // copy constructor
25 BaseMatrix(const BaseMatrix<T>& matrix);
26 // move constructor
27 BaseMatrix(BaseMatrix<T>&& matrix);
28 // Destructor of class BaseMatrix
30
36 void allocate(T* data_array = nullptr, bool if_zero = false);
37
41 void set_zero();
42
48 void add_array(T* array);
49
57 void add_element(int mu, int nu, const T& value)
58 {
59 #ifdef __DEBUG
60 assert(this->value_begin != nullptr);
61 #endif
62 int index = mu * this->ncol_local + nu;
63 value_begin[index] += value;
64 };
65
66 // for inside matrix
74 T& get_value(const size_t& i_row, const size_t& j_col) const
75 {
76 #ifdef __DEBUG
77 assert(this->value_begin != nullptr);
78 #endif
79 int index = i_row * this->ncol_local + j_col;
80 return value_begin[index];
81 };
82
86 T* get_pointer() const { return value_begin; };
87
88 // operator= for copy assignment
89 BaseMatrix& operator=(const BaseMatrix& other);
90
91 // operator= for move assignment
92 BaseMatrix& operator=(BaseMatrix&& other) noexcept;
93
97 size_t get_memory_size() const;
98
102 int get_col_size() const {return ncol_local;};
106 int get_row_size() const {return nrow_local;};
110 void set_size(const int& col_size_in, const int& row_size_in);
111
112 void add_array_ts(T* array)
113 {
114 std::lock_guard<std::mutex> lock(mtx);
115 const int size = nrow_local * ncol_local;
116 for (int i = 0; i < size; ++i)
117 {
118 value_begin[i] += array[i];
119 }
120 }
121
122 private:
123 bool allocated = false;
124
125 // pointer for accessing data
126 // two ways to arrange data:
127 // 1. allocate data itself
128 // 2. only access data but be arranged by RealSparseHamiltonian
129 T* value_begin = nullptr;
130
131 // int current_multiple = 0;
132
133 // for thread safe
134 mutable std::mutex mtx;
135 // number of rows and columns
136 int nrow_local = 0;
137 int ncol_local = 0;
138};
139
140} // namespace hamilt
141
142#endif
Definition base_matrix.h:20
T * get_pointer() const
get pointer of value from a submatrix
Definition base_matrix.h:86
std::mutex mtx
Definition base_matrix.h:134
void set_size(const int &col_size_in, const int &row_size_in)
set col_size and row_size
Definition base_matrix.cpp:195
void set_zero()
set value in the matrix to zero
Definition base_matrix.cpp:100
BaseMatrix & operator=(const BaseMatrix &other)
Definition base_matrix.cpp:125
void add_array(T *array)
add an array to the matrix
Definition base_matrix.cpp:110
int ncol_local
Definition base_matrix.h:137
int get_col_size() const
get col_size for this matrix
Definition base_matrix.h:102
bool allocated
Definition base_matrix.h:123
void add_element(int mu, int nu, const T &value)
add a single element to the matrix
Definition base_matrix.h:57
int nrow_local
Definition base_matrix.h:136
T & get_value(const size_t &i_row, const size_t &j_col) const
get value from a whole matrix
Definition base_matrix.h:74
int get_row_size() const
get row_size for this matrix
Definition base_matrix.h:106
void allocate(T *data_array=nullptr, bool if_zero=false)
allocate memory for the matrix if this->value_begin is not nullptr, it will be neglected if this->val...
Definition base_matrix.cpp:68
size_t get_memory_size() const
get total memory size of BaseMatrix
Definition base_matrix.cpp:183
T * value_begin
Definition base_matrix.h:129
~BaseMatrix()
Definition base_matrix.cpp:58
void add_array_ts(T *array)
Definition base_matrix.h:112
#define T
Definition exp.cpp:237
Definition hamilt.h:12