-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathdmvec_rank.c
347 lines (305 loc) · 7.85 KB
/
dmvec_rank.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright (c) Harri Rautila, 2012,2013
// This file is part of github.com/hrautila/matops package. It is free software,
// distributed under the terms of GNU Lesser General Public License Version 3, or
// any later version. See the COPYING tile included in this archive.
#include <stdio.h>
#include <stdint.h>
#include "cmops.h"
// update 1 column of A matrix (a0) with vector X scaled with elements y0
static inline
void _inner_mv_daxpy(double *a0, const double *x, int incX,
const double *y0, double alpha, int nRE)
{
register int i;
register double cf0, cf1;
cf0 = alpha * y0[0];
for (i = 0; i < nRE-3; i += 4) {
a0[0] += x[0] * cf0;
x += incX;
a0[1] += x[0] * cf0;
x += incX;
a0[2] += x[0] * cf0;
x += incX;
a0[3] += x[0] * cf0;
x += incX;
a0 += 4;
}
if (i == nRE)
return;
if (i < nRE-1) {
a0[0] += x[0] * cf0;
x += incX;
a0[1] += x[0] * cf0;
x += incX;
a0 += 2;
i += 2;
}
if (i < nRE) {
a0[0] += x[0] * cf0;
x += incX;
}
}
// update 2 columns of A matrix (a0, a1) with vector X scaled with elements y0, y1
static inline
void _inner_mv_daxpy2(double *a0, double *a1, const double *x, int incX,
const double *y0, const double *y1, double alpha, int nRE)
{
register int i;
register double cf0, cf1;
cf0 = alpha * y0[0];
cf1 = alpha * y1[0];
for (i = 0; i < nRE-3; i += 4) {
a0[0] += x[0] * cf0;
a1[0] += x[0] * cf1;
x += incX;
a0[1] += x[0] * cf0;
a1[1] += x[0] * cf1;
x += incX;
a0[2] += x[0] * cf0;
a1[2] += x[0] * cf1;
x += incX;
a0[3] += x[0] * cf0;
a1[3] += x[0] * cf1;
x += incX;
a0 += 4;
a1 += 4;
}
if (i == nRE)
return;
if (i < nRE-1) {
a0[0] += x[0] * cf0;
a1[0] += x[0] * cf1;
x += incX;
a0[1] += x[0] * cf0;
a1[1] += x[0] * cf1;
x += incX;
a0 += 2;
a1 += 2;
i += 2;
}
if (i < nRE) {
a0[0] += x[0] * cf0;
a1[0] += x[0] * cf1;
x += incX;
i++;
}
}
static void
_dmvec_vpur_rank(mdata_t *A, const mvec_t *X, const mvec_t *Y, double alpha,
int S, int L, int R, int E)
{
register const double *y, *x;
register double *Ac;
int j, nRE, nSL;
Ac = &A->md[S*A->step + R];
y = &Y->md[S*Y->inc];
x = &X->md[R*X->inc];
nRE = E - R;
nSL = L - S;
for (j = 0; j < nSL-1; j += 2) {
//Ar = Ac;
_inner_mv_daxpy2(Ac, Ac+A->step, x, X->inc, y, y+Y->inc, alpha, nRE);
y += 2*Y->inc;
Ac += 2*A->step;
}
if (j == nSL)
return;
if (j < nSL) {
_inner_mv_daxpy(Ac, x, X->inc, y, alpha, nRE);
}
}
// A = A + alpha * x * y.T; A is M*N, x is M*1, Y is N*1
void dmvec_rank(mdata_t *A, const mvec_t *X, const mvec_t *Y, double alpha,
int S, int L, int R, int E,
int NB, int MB)
{
int i, j, nI, nJ;
if (NB <= 0) {
NB = L - S;
}
if (MB <= 0) {
MB = E - R;
}
for (j = S; j < L; j += NB) {
nJ = L - j < NB ? L - j : NB;
for (i = R; i < E; i += MB) {
nI = E - i < MB ? E - i : MB;
_dmvec_vpur_rank(A, X, Y, alpha, j, j+nJ, i, i+nI);
}
}
}
static void
_dmvec_vpur_syr_upper(mdata_t *A, const mvec_t *X0, const mvec_t *X1,
double alpha, int S, int L, int R, int E)
{
// assert(S==R && L==E)
register int i, j, nRE, nSL;
register double *Ac, *Ar0, *Ar1, *At, cf;
register const double *x0, *x1, *Xc;
Xc = &X0->md[R*X0->inc];
x1 = &X1->md[S*X1->inc];
//At = &A->md[S*A->step+R];
nRE = E - R;
nSL = L - S;
//printf("R=%d, E=%d, E-R: %d, S=%d, L=%d, L-S: %d\n", R, E, E-R, S, L, L-S);
Ac = &A->md[S*A->step+R];
for (j = 0; j < nSL-1; j += 2) {
x0 = Xc;
Ar0 = Ac;
Ar1 = Ar0 + A->step;
// add to 2 columns
_inner_mv_daxpy2(Ac, Ac+A->step, x0, X0->inc, x1, x1+X1->inc, alpha, S+j+1);
// diagonal entry on 2nd column is missing ... row j
cf = alpha * x1[X1->inc];
Ar1[S+j+1] += x0[(S+j+1)*X0->inc] * cf;
//printf("A=\n"); print_tile(At, A->step, E-R, L-S);
x1 += 2*X1->inc;
Ac += 2*A->step;
}
if (j == nSL)
return;
if (j < nSL) {
x0 = Xc;
_inner_mv_daxpy(Ac, x0, X0->inc, x1, alpha, nRE);
}
}
static void
_dmvec_vpur_syr_lower(mdata_t *A, const mvec_t *X0, const mvec_t *X1,
double alpha, int S, int L, int R, int E)
{
// assert(S==R && L==E)
register int i, j, nRE, nSL, nSE;
register double *Ac, *Ar0, *Ar1, *At, cf;
register const double *x0, *x1, *Xc;
Xc = &X0->md[R*X0->inc];
x1 = &X1->md[R*X1->inc];
At = &A->md[S*A->step+R];
nRE = E - R;
nSL = L - S;
nSE = E - S;
//printf("start: Xc[%d] = %.1f\n", R, Xc[0]);
Ac = &A->md[S*A->step + R];
for (j = 0; j < nSL-1; j += 2) {
x0 = Xc;
Ar0 = Ac + j;
//printf("j=%d, x0=%.1f, x1=%.1f\n", j, x0[0], x1[0]);
// do diagonal entry on 1st column ... row j
cf = alpha * x1[0];
Ar0[0] += x0[0] * cf;
Ar0++;
x0 += X0->inc;
// add to 2 columns
_inner_mv_daxpy2(Ar0, Ar0+A->step, x0, X0->inc, x1, x1+X1->inc, alpha, nSE-j-1);
//printf("A=\n"); print_tile(At, A->step, E-R, L-S);
x1 += 2*X1->inc;
Ac += 2*A->step;
Xc += 2*X0->inc;
}
if (j == nSL)
return;
if (j < nSL) {
x0 = Xc;
//x1 = Xc;
//printf("last column: j=%d, nSE=%d\n", j, nSE);
_inner_mv_daxpy(Ac+j, x0, X0->inc, x1, alpha, nSE-j);
}
}
// A = A + alpha * x * y.T; A is N*N symmetric lower|upper, x is N*1
void dmvec_symv_rank(mdata_t *A, const mvec_t *X, double alpha, int flags,
int S, int L, int NB)
{
int i, j, nI, nJ, sR, sE;
if (L-S <= 0) {
return;
}
if (NB <= 0) {
NB = L - S;
}
if (flags & MTX_UPPER) {
for (j = S; j < L; j += NB) {
nJ = L - j < NB ? L - j : NB;
_dmvec_vpur_syr_upper(A, X, X, alpha, j, j+nJ, 0, j+nJ);
}
} else {
for (j = S; j < L; j += NB) {
nJ = L - j < NB ? L - j : NB;
_dmvec_vpur_syr_lower(A, X, X, alpha, j, j+nJ, j, L);
}
}
}
void dmvec_trmv_upd(mdata_t *A, const mvec_t *X, const mvec_t *Y, double alpha, int flags,
int S, int L, int NB)
{
int i, j, nI, nJ, sR, sE;
if (L-S <= 0) {
return;
}
if (NB <= 0) {
NB = L - S;
}
if (flags & MTX_UPPER) {
for (j = S; j < L; j += NB) {
nJ = L - j < NB ? L - j : NB;
_dmvec_vpur_syr_upper(A, X, Y, alpha, j, j+nJ, 0, j+nJ);
}
} else {
for (j = S; j < L; j += NB) {
nJ = L - j < NB ? L - j : NB;
_dmvec_vpur_syr_lower(A, X, Y, alpha, j, j+nJ, j, L);
}
}
}
static void
_dmvec_vpur_syr2(mdata_t *A, const mvec_t *X, const mvec_t *Y, double alpha,
int flags, int S, int L, int R, int E)
{
register int i, j, nRE, nSL;
register double *Ac, *Ar, cf;
register const double *y, *x;
Ac = &A->md[S*A->step + R];
y = &Y->md[S*Y->inc];
x = &X->md[R*X->inc];
nRE = E - R;
nSL = L - S;
for (j = 0; j < nSL-1; j += 2) {
Ar = Ac;
_inner_mv_daxpy2(Ac, Ac+A->step, x, X->inc, y, y+Y->inc, alpha, nRE);
y += 2*Y->inc;
Ac += 2*A->step;
}
if (j == nSL)
return;
if (j < nSL) {
_inner_mv_daxpy(Ac, x, X->inc, y, alpha, nRE);
}
}
// A = A + alpha * x * y.T; A is N*N symmetric lower|upper,
// x is N*1 or 1*N, Y is N*1 or 1*N
void dmvec_symv_rank2(mdata_t *A, const mvec_t *X, const mvec_t *Y,
double alpha, int flags,
int S, int L, int NB)
{
int i, j, nI, nJ, sR, sE;
if (L - S <= 0) {
return;
}
if (NB <= 0) {
NB = L - S;
}
if (flags & MTX_UPPER) {
for (j = S; j < L; j += NB) {
nJ = L - j < NB ? L - j : NB;
_dmvec_vpur_syr_upper(A, X, Y, alpha, j, j+nJ, 0, j+nJ);
_dmvec_vpur_syr_upper(A, Y, X, alpha, j, j+nJ, 0, j+nJ);
}
} else {
for (j = S; j < L; j += NB) {
nJ = L - j < NB ? L - j : NB;
_dmvec_vpur_syr_lower(A, X, Y, alpha, j, j+nJ, j, L);
_dmvec_vpur_syr_lower(A, Y, X, alpha, j, j+nJ, j, L);
}
}
}
// Local Variables:
// indent-tabs-mode: nil
// End: