-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.hpp
169 lines (120 loc) · 4.3 KB
/
utilities.hpp
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
#ifndef UTILITIES_H
#define UTILITIES_H
#include"string"
#include"petscksp.h"
#include"petsc_interface.h"
#include"../spectrum_generator/generator.hpp"
#include<mpi.h>
struct MatrixElement {
PetscScalar val;
PetscReal mod;
PetscInt col;
PetscInt row;
};
inline void AddSep(std::string *buffer,char sep){
*buffer += sep;
}
inline void ReduceMaxMatrix(void *in, void *out, int *len, int *dptr){
MatrixElement *input = (MatrixElement *)(in);
MatrixElement *output = (MatrixElement *)(out);
for(int i = 0; i < *len; i++){
if(input[i].mod > output[i].mod){
output[i].mod = input[i].mod;
output[i].val = input[i].val;
output[i].col = input[i].col;
output[i].row = input[i].row;
}
}
}
inline void ReduceMinMatrix(void *in, void *out, int *len, int *dptr){
MatrixElement *input = (MatrixElement *)(in);
MatrixElement *output = (MatrixElement *)(out);
for(int i = 0; i < *len; i++){
if(input[i].mod < output[i].mod){
output[i].mod = input[i].mod;
output[i].val = input[i].val;
output[i].col = input[i].col;
output[i].row = input[i].row;
}
}
}
inline MPI_Datatype InitMatrixElementMPIDataType(){
MPI_Datatype MPI_MatrixElement;
int block_lengths[4];
MPI_Aint displacements[4];
MPI_Datatype typelist[4] = {MPIU_SCALAR, MPIU_REAL, MPIU_INT, MPIU_INT};
block_lengths[0] = 1; displacements[0] = (MPI_Aint)offsetof(struct MatrixElement, val);
block_lengths[1] = 1; displacements[1] = (MPI_Aint)offsetof(struct MatrixElement, mod);
block_lengths[2] = 1; displacements[2] = (MPI_Aint)offsetof(struct MatrixElement, col);
block_lengths[3] = 1; displacements[3] = (MPI_Aint)offsetof(struct MatrixElement, row);
MPI_Type_create_struct(4, block_lengths, displacements, typelist,&MPI_MatrixElement);
MPI_Type_commit(&MPI_MatrixElement);
return MPI_MatrixElement;
}
inline void CleanMPIDatatype(MPI_Datatype *data){
MPI_Type_free(data);
}
inline MPI_Op InitMinMatrixMPIReduction(){
MPI_Op MyMPI_Reduction;
MPI_Op_create(&ReduceMinMatrix,1,&MyMPI_Reduction);
return MyMPI_Reduction;
}
inline MPI_Op InitMaxMatrixMPIReduction(){
MPI_Op MyMPI_Reduction;
MPI_Op_create(&ReduceMaxMatrix,1,&MyMPI_Reduction);
return MyMPI_Reduction;
}
inline void CleanMPIOperation(MPI_Op *operation){
MPI_Op_free(operation);
}
inline void GetBounds(int rank, int size, int n, int *first, int *last){
*last = n/size,first;
*first = rank*(*last);
if(rank < n%size){
(*last) ++;
(*first) += rank;
} else (*first) += n%size;
(*last) += (*first)-1;
}
inline void MinRow(PetscInt row_nnz, const PetscInt *row_cols, const PetscScalar *row_vals, MatrixElement *m){
m->col = row_cols[0]; m->val = row_vals[0]; m->mod = (PetscReal)sqrt(PetscSqr(PetscRealPart(m->val)) + PetscSqr(PetscImaginaryPart(m->val)));
PetscReal module_temp;
for(int j = 1; j < row_nnz; j++){
module_temp = (PetscReal)sqrt(PetscSqr(PetscRealPart(row_vals[j])) + PetscSqr(PetscImaginaryPart(row_vals[j])));
if(module_temp < m->mod){
m->mod = module_temp;
m->val = row_vals[j];
m->col = row_cols[j];
}
}
}
inline void MaxRow(PetscInt row_nnz, const PetscInt *row_cols, const PetscScalar *row_vals, MatrixElement *m){
m->col = row_cols[0]; m->val = row_vals[0]; m->mod = (PetscReal)sqrt(PetscSqr(PetscRealPart(m->val)) + PetscSqr(PetscImaginaryPart(m->val)));
PetscReal module_temp;
for(int j = 1; j < row_nnz; j++){
module_temp = (PetscReal)sqrt(PetscSqr(PetscRealPart(row_vals[j])) + PetscSqr(PetscImaginaryPart(row_vals[j])));
if(module_temp > m->mod){
m->mod = module_temp;
m->val = row_vals[j];
m->col = row_cols[j];
}
}
}
inline void MinMaxRow(PetscInt row_nnz, const PetscInt *row_cols, const PetscScalar *row_vals, MatrixElement *min, MatrixElement *max){
max->col = row_cols[0]; max->val = row_vals[0]; max->mod = (PetscReal)sqrt(PetscSqr(PetscRealPart(max->val)) + PetscSqr(PetscImaginaryPart(max->val)));
min->col = max->col; min->val = max->val; min->mod = max->mod;
PetscReal module_temp;
for(int j = 1; j < row_nnz; j++){
module_temp = (PetscReal)sqrt(PetscSqr(PetscRealPart(row_vals[j])) + PetscSqr(PetscImaginaryPart(row_vals[j])));
if(module_temp < min->mod){
min->mod = module_temp;
min->val = row_vals[j];
min->col = row_cols[j];
}else if(module_temp > max->mod){
max->mod = module_temp;
max->val = row_vals[j];
max->col = row_cols[j];
}
}
}
#endif