-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-task.c
177 lines (146 loc) · 4.86 KB
/
update-task.c
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
#include "utils.h"
#include "defines.h"
#include "norm.h"
#include "update-task.h"
#include "robust.h"
#include <stdlib.h>
#include <stdio.h>
#include <mm_malloc.h>
#include <string.h>
#include <math.h>
void update(
int m, int n, int k,
omp_lock_t *lock,
const double alpha, double *restrict const Ain, int ldAin,
const double ainnorm, const scaling_t ainscale,
double *restrict const B, int ldB, const double bnorm,
double *restrict const C, int ldC, double *restrict const cnorm,
scaling_t *restrict const cscale)
{
#ifndef NDEBUG
printf("update (%dx%d)(%dx%d)\n", m, k, k, n);
printf("update thread id = %d\n", omp_get_thread_num());
#endif
// Scaling of C.
scaling_t cscaling = *cscale;
// Local scaling factor.
scaling_t zeta;
// Pointer to A - either a copy or the original memory.
double *A;
// Local norm of A.
double anorm;
// Status flag if A or C have to be rescaled.
int rescale_A = 0;
int rescale_C = 0;
////////////////////////////////////////////////////////////////////////////
// Compute scaling factor.
////////////////////////////////////////////////////////////////////////////
while (!omp_test_lock(lock)) {
#pragma omp taskyield
;
}
// Copy norm.
anorm = ainnorm;
// Bound right-hand side C.
*cnorm = matrix_infnorm(m, n, C, ldC);
// Simulate consistent scaling.
if (cscaling < ainscale) {
// The common scaling factor is cscale.
const double s = compute_upscaling(cscaling, ainscale);
// Mark A for scaling. Physical rescaling is deferred.
rescale_A = 1;
// Update norm.
anorm = s * ainnorm;
}
else if (ainscale < cscaling) {
// The common scaling factor is ascale.
const double s = compute_upscaling(ainscale, cscaling);
// Mark C for scaling. Physical rescaling is deferred.
rescale_C = 1;
// Update norm.
*cnorm = s * (*cnorm);
}
else {
// Nothing to do. C and A are consistently scaled.
}
// Compute scaling factor needed to survive the linear update.
zeta = protect_update(anorm, bnorm, *cnorm);
#ifdef INTSCALING
if (zeta != 0) {
rescale_A = 1;
rescale_C = 1;
}
#else
if (zeta != 1.0) {
rescale_A = 1;
rescale_C = 1;
}
#endif
// If A has to be rescaled, take a copy and do the scaling on the copy.
if (rescale_A) {
A = (double *) _mm_malloc((size_t)ldAin * k * sizeof(double), ALIGNMENT);
if (cscaling < ainscale) {
// Copy A and simultaneously rescale.
double s = compute_combined_upscaling(cscaling, ainscale, zeta);
for (int j = 0; j < k; j++)
for (int i = 0; i < m; i++)
A[i + ldAin * j] = s * Ain[i + ldAin * j];
}
else if (ainscale < cscaling) {
// Copy A and simultaneously rescale with robust update factor.
double s = convert_scaling(zeta);
for (int j = 0; j < k; j++)
for (int i = 0; i < m; i++)
A[i + ldAin * j] = s * Ain[i + ldAin * j];
}
}
else {
// A does not have to be rescaled. Operate on original memory.
A = Ain;
}
// If C has to be rescaled, directly modify C.
if (rescale_C) {
if (cscaling < ainscale) {
const double s = convert_scaling(zeta);
for (int j = 0; j < n; j++)
for (int i = 0; i < m; i++)
C[i + j * ldC] = s * C[i + j * ldC];
}
else if (ainscale < cscaling) {
const double s = compute_combined_upscaling(ainscale, cscaling, zeta);
for (int j = 0; j < n; j++)
for (int i = 0; i < m; i++)
C[i + j * ldC] = s * C[i + j * ldC];
}
else {
// A and C are consistently scaled.
const double s = convert_scaling(zeta);
for (int j = 0; j < n; j++)
for (int i = 0; i < m; i++)
C[i + j * ldC] = s * C[i + j * ldC];
}
}
// Update global scaling of Y.
#ifdef INTSCALING
*cscale = min(cscaling, ainscale) + zeta;
#else
*cscale = minf(cscaling, ainscale) * zeta;
#endif
////////////////////////////////////////////////////////////////////////////
// Compute update.
////////////////////////////////////////////////////////////////////////////
// C := C - alpha * A * B.
//(mxn) (mxk)(kxn)
dgemm('N', 'N',
m, n, k,
-alpha, A, ldAin,
B, ldB,
1.0, C, ldC);
omp_unset_lock(lock);
////////////////////////////////////////////////////////////////////////////
// Free workspace.
////////////////////////////////////////////////////////////////////////////
if (rescale_A) {
_mm_free(A);
}
}