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 // Cross-type variant: cast each element of `array` to T before accumulating.
123 // Selected by overload resolution only when TSrc != T (the non-template
124 // overload is preferred when types match).
125 template <typename TSrc>
126 void add_array_ts(const TSrc* array)
127 {
128 std::lock_guard<std::mutex> lock(mtx);
129 const int size = nrow_local * ncol_local;
130 for (int i = 0; i < size; ++i)
131 {
132 value_begin[i] += static_cast<T>(array[i]);
133 }
134 }
135
136 private:
137 bool allocated = false;
138
139 // pointer for accessing data
140 // two ways to arrange data:
141 // 1. allocate data itself
142 // 2. only access data but be arranged by RealSparseHamiltonian
143 T* value_begin = nullptr;
144
145 // int current_multiple = 0;
146
147 // for thread safe
148 mutable std::mutex mtx;
149 // number of rows and columns
150 int nrow_local = 0;
151 int ncol_local = 0;
152};
153
154} // namespace hamilt
155
156#endif
const std::complex< double > i
Definition cal_pLpR.cpp:46
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:148
void add_array_ts(const TSrc *array)
Definition base_matrix.h:126
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:151
int get_col_size() const
get col_size for this matrix
Definition base_matrix.h:102
bool allocated
Definition base_matrix.h:137
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:150
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:143
~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:13