Repository navigation
Expand file tree
/
Copy pathlinalg.cpp
More file actions
108 lines (92 loc) · 2.42 KB
/
Copy pathlinalg.cpp
File metadata and controls
108 lines (92 loc) · 2.42 KB
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
// Copyright
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Underlying paper:
//
// Layer-Parallel Training of Deep Residual Neural Networks
// S. Guenther, L. Ruthotto, J.B. Schroder, E.C. Czr, and N.R. Gauger
//
// Download: https://arxiv.org/pdf/1812.04352.pdf
//
#include "linalg.hpp"
MyReal vecdot_par(int dimN, MyReal *x, MyReal *y, MPI_Comm comm) {
MyReal localdot, globaldot;
localdot = vecdot(dimN, x, y);
MPI_Allreduce(&localdot, &globaldot, 1, MPI_MyReal, MPI_SUM, comm);
return globaldot;
}
MyReal vecdot(int dimN, MyReal *x, MyReal *y) {
MyReal dotprod = 0.0;
for (int i = 0; i < dimN; i++) {
dotprod += x[i] * y[i];
}
return dotprod;
}
MyReal vecmax(int dimN, MyReal *x) {
MyReal max = -1e+12;
for (int i = 0; i < dimN; i++) {
if (x[i] > max) {
max = x[i];
}
}
return max;
}
int argvecmax(int dimN, MyReal *x) {
MyReal max = -1e+12;
int i_max;
for (int i = 0; i < dimN; i++) {
if (x[i] > max) {
max = x[i];
i_max = i;
}
}
return i_max;
}
MyReal vecnormsq(int dimN, MyReal *x) {
MyReal normsq = 0.0;
for (int i = 0; i < dimN; i++) {
normsq += pow(x[i], 2);
}
return normsq;
}
MyReal vecnorm_par(int dimN, MyReal *x, MPI_Comm comm) {
MyReal localnorm, globalnorm;
localnorm = vecnormsq(dimN, x);
MPI_Allreduce(&localnorm, &globalnorm, 1, MPI_MyReal, MPI_SUM, comm);
globalnorm = sqrt(globalnorm);
return globalnorm;
}
int vec_copy(int N, MyReal *u, MyReal *u_copy) {
for (int i = 0; i < N; i++) {
u_copy[i] = u[i];
}
return 0;
}
void vecvecT(int N, MyReal *x, MyReal *y, MyReal *XYT) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
XYT[i * N + j] = x[i] * y[j];
}
}
}
void matvec(int dimN, MyReal *H, MyReal *x, MyReal *Hx) {
MyReal sum_j;
for (int i = 0; i < dimN; i++) {
sum_j = 0.0;
for (int j = 0; j < dimN; j++) {
sum_j += H[i * dimN + j] * x[j];
}
Hx[i] = sum_j;
}
}