-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTPZIrregularBlocksMatrix.cpp
195 lines (159 loc) · 7.2 KB
/
TPZIrregularBlocksMatrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// Created by natalia on 14/05/19.
//
#include "TPZIrregularBlocksMatrix.h"
#include "MatMul.h"
#ifdef USING_MKL
#include <mkl.h>
#endif
TPZIrregularBlocksMatrix::TPZIrregularBlocksMatrix() : TPZMatrix<REAL>(), fBlocksInfo() {
#ifdef USING_CUDA
dBlocksInfo.dStorage.resize(0);
dBlocksInfo.dRowSizes.resize(0);
dBlocksInfo.dColSizes.resize(0);
dBlocksInfo.dRowFirstIndex.resize(0);
dBlocksInfo.dColFirstIndex.resize(0);
dBlocksInfo.dMatrixPosition.resize(0);
dBlocksInfo.dMatrixStride.resize(0);
dBlocksInfo.dRowPtr.resize(0);
dBlocksInfo.dColInd.resize(0);
fCudaCalls = new TPZCudaCalls();
#endif
this->Resize(0,0);
fBlocksInfo.fNumBlocks = -1;
fBlocksInfo.fStorage.resize(0);
fBlocksInfo.fRowSizes.resize(0);
fBlocksInfo.fColSizes.resize(0);
fBlocksInfo.fMatrixPosition.resize(0);
fBlocksInfo.fRowFirstIndex.resize(0);
fBlocksInfo.fColFirstIndex.resize(0);
fBlocksInfo.fMatrixStride.resize(0);
fBlocksInfo.fRowPtr.resize(0);
fBlocksInfo.fColInd.resize(0);
}
TPZIrregularBlocksMatrix::TPZIrregularBlocksMatrix(const int64_t rows,const int64_t cols) : TPZMatrix<REAL>(rows,cols), fBlocksInfo() {
#ifdef USING_CUDA
dBlocksInfo.dStorage.resize(0);
dBlocksInfo.dRowSizes.resize(0);
dBlocksInfo.dColSizes.resize(0);
dBlocksInfo.dRowFirstIndex.resize(0);
dBlocksInfo.dColFirstIndex.resize(0);
dBlocksInfo.dMatrixPosition.resize(0);
dBlocksInfo.dMatrixStride.resize(0);
dBlocksInfo.dRowPtr.resize(0);
dBlocksInfo.dColInd.resize(0);
fCudaCalls = new TPZCudaCalls();
#endif
fBlocksInfo.fNumBlocks = -1;
fBlocksInfo.fStorage.resize(0);
fBlocksInfo.fRowSizes.resize(0);
fBlocksInfo.fColSizes.resize(0);
fBlocksInfo.fMatrixPosition.resize(0);
fBlocksInfo.fRowFirstIndex.resize(0);
fBlocksInfo.fColFirstIndex.resize(0);
fBlocksInfo.fMatrixStride.resize(0);
fBlocksInfo.fRowPtr.resize(0);
fBlocksInfo.fColInd.resize(0);
}
TPZIrregularBlocksMatrix::~TPZIrregularBlocksMatrix() {
}
TPZIrregularBlocksMatrix::TPZIrregularBlocksMatrix(const TPZIrregularBlocksMatrix ©) {
TPZMatrix<REAL>::operator=(copy);
fBlocksInfo = copy.fBlocksInfo;
#ifdef USING_CUDA
dBlocksInfo = copy.dBlocksInfo;
fCudaCalls = copy.fCudaCalls;
#endif
}
TPZIrregularBlocksMatrix &TPZIrregularBlocksMatrix::operator=(const TPZIrregularBlocksMatrix ©) {
if(© == this){
return *this;
}
TPZMatrix<REAL>::operator=(copy);
fBlocksInfo = copy.fBlocksInfo;
#ifdef USING_CUDA
dBlocksInfo = copy.dBlocksInfo;
fCudaCalls = copy.fCudaCalls;
#endif
return *this;
}
void TPZIrregularBlocksMatrix::MultiplyVector(REAL *A, REAL *res, int opt) {
int nblocks = fBlocksInfo.fNumBlocks;
TPZVec<int> one(nblocks);
one.Fill(1);
#ifdef USING_CUDA
int rows = this->Rows();
int cols = this->Cols();
#ifdef USING_SPARSE
if(opt == 0) {
fCudaCalls->SpMV(0, 0, rows, cols, dBlocksInfo.dStorage.getSize(), 1., dBlocksInfo.dStorage.getData(), dBlocksInfo.dRowPtr.getData(), dBlocksInfo.dColInd.getData(), A, res);
} else {
fCudaCalls->SpMV(1, 0, rows, cols, dBlocksInfo.dStorage.getSize(), -1., dBlocksInfo.dStorage.getData(), dBlocksInfo.dRowPtr.getData(), dBlocksInfo.dColInd.getData(), A, res);
}
#else
TPZVecGPU<int> dOne(nblocks);
dOne.set(&one[0], nblocks);
if(opt == 0) {
fCudaCalls->Multiply(opt, dBlocksInfo.dRowSizes.getData(), dOne.getData(), dBlocksInfo.dColSizes.getData(), dBlocksInfo.dStorage.getData(), dBlocksInfo.dMatrixPosition.getData(), A, dBlocksInfo.dColFirstIndex.getData(), res, dBlocksInfo.dRowFirstIndex.getData(), 1., nblocks);
} else {
fCudaCalls->Multiply(opt, dBlocksInfo.dColSizes.getData(), dOne.getData(), dBlocksInfo.dRowSizes.getData(), dBlocksInfo.dStorage.getData(), dBlocksInfo.dMatrixPosition.getData(), A, dBlocksInfo.dRowFirstIndex.getData(), res, dBlocksInfo.dColFirstIndex.getData(), -1., nblocks);
}
#endif
#else
#ifdef USING_SPARSE
int rows = this->Rows();
int cols = this->Cols();
if(opt == 0) {
SpMV(0, rows, cols, 1., &fBlocksInfo.fStorage[0], &fBlocksInfo.fRowPtr[0], &fBlocksInfo.fColInd[0], A, res);
} else {
SpMV(1, rows, cols, -1., &fBlocksInfo.fStorage[0], &fBlocksInfo.fRowPtr[0], &fBlocksInfo.fColInd[0], A, res);
}
#else
if(opt == 0) {
MatrixMultiplication(opt, &fBlocksInfo.fRowSizes[0], &one[0], &fBlocksInfo.fColSizes[0], &fBlocksInfo.fStorage[0], &fBlocksInfo.fMatrixPosition[0], A, &fBlocksInfo.fColFirstIndex[0], res, &fBlocksInfo.fRowFirstIndex[0], 1., nblocks);
} else {
MatrixMultiplication(opt, &fBlocksInfo.fColSizes[0], &one[0], &fBlocksInfo.fRowSizes[0], &fBlocksInfo.fStorage[0], &fBlocksInfo.fMatrixPosition[0], A, &fBlocksInfo.fRowFirstIndex[0], res, &fBlocksInfo.fColFirstIndex[0], -1., nblocks);
}
#endif
#endif
}
#ifdef USING_CUDA
void TPZIrregularBlocksMatrix::TransferDataToGPU() {
dBlocksInfo.dStorage.resize(fBlocksInfo.fStorage.size());
dBlocksInfo.dStorage.set(&fBlocksInfo.fStorage[0], fBlocksInfo.fStorage.size());
dBlocksInfo.dRowSizes.resize(fBlocksInfo.fRowSizes.size());
dBlocksInfo.dRowSizes.set(&fBlocksInfo.fRowSizes[0], fBlocksInfo.fRowSizes.size());
dBlocksInfo.dColSizes.resize(fBlocksInfo.fColSizes.size());
dBlocksInfo.dColSizes.set(&fBlocksInfo.fColSizes[0], fBlocksInfo.fColSizes.size());
dBlocksInfo.dMatrixPosition.resize(fBlocksInfo.fMatrixPosition.size());
dBlocksInfo.dMatrixPosition.set(&fBlocksInfo.fMatrixPosition[0], fBlocksInfo.fMatrixPosition.size());
dBlocksInfo.dRowFirstIndex.resize(fBlocksInfo.fRowFirstIndex.size());
dBlocksInfo.dRowFirstIndex.set(&fBlocksInfo.fRowFirstIndex[0], fBlocksInfo.fRowFirstIndex.size());
dBlocksInfo.dColFirstIndex.resize(fBlocksInfo.fColFirstIndex.size());
dBlocksInfo.dColFirstIndex.set(&fBlocksInfo.fColFirstIndex[0], fBlocksInfo.fColFirstIndex.size());
dBlocksInfo.dMatrixStride.resize(fBlocksInfo.fMatrixStride.size());
dBlocksInfo.dMatrixStride.set(&fBlocksInfo.fMatrixStride[0], fBlocksInfo.fMatrixStride.size());
#ifdef USING_SPARSE
dBlocksInfo.dRowPtr.resize(fBlocksInfo.fRowPtr.size());
dBlocksInfo.dRowPtr.set(&fBlocksInfo.fRowPtr[0], fBlocksInfo.fRowPtr.size());
dBlocksInfo.dColInd.resize(fBlocksInfo.fColInd.size());
dBlocksInfo.dColInd.set(&fBlocksInfo.fColInd[0], fBlocksInfo.fColInd.size());
#endif
}
#endif
void TPZIrregularBlocksMatrix::CSRVectors() {
int64_t nblocks = fBlocksInfo.fNumBlocks;
int64_t rows = this->Rows();
int64_t cols = this->Cols();
fBlocksInfo.fRowPtr.resize(rows + 1);
fBlocksInfo.fColInd.resize(fBlocksInfo.fMatrixPosition[nblocks]);
for (int iel = 0; iel < nblocks; ++iel) {
for (int irow = 0; irow < fBlocksInfo.fRowSizes[iel]; ++irow) {
fBlocksInfo.fRowPtr[irow + fBlocksInfo.fRowFirstIndex[iel]] = fBlocksInfo.fMatrixPosition[iel] + irow * fBlocksInfo.fColSizes[iel];
for (int icol = 0; icol < fBlocksInfo.fColSizes[iel]; ++icol) {
fBlocksInfo.fColInd[icol + fBlocksInfo.fMatrixPosition[iel] + irow * fBlocksInfo.fColSizes[iel]] = icol + fBlocksInfo.fColFirstIndex[iel];
}
}
}
fBlocksInfo.fRowPtr[rows] = fBlocksInfo.fMatrixPosition[nblocks];
}