-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
233 lines (186 loc) · 5.92 KB
/
utils.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
#include "utils.h"
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
double get_time (void)
{
struct timeval tv;
gettimeofday (&tv, NULL);
return tv.tv_sec + 1e-6 * tv.tv_usec;
}
int get_size_with_padding(const int n)
{
#if defined(__AVX512F__)
// Round up n to the next multiple of 8.
return 8 * ((n + 7) / 8);
#else
// Round up n to the next multiple of 4.
return 4 * ((n + 3) / 4);
#endif
}
void scale_tile(int m, int n,
double *restrict const X, int ldX, const scaling_t *beta)
{
#ifdef INTSCALING
double alpha = ldexp(1.0, beta[0]);
#else
double alpha = beta[0];
#endif
#define X(i,j) X[(i) + (j) * ldX]
// Scale vector, if necessary.
if (alpha != 1.0) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
X(i,j) = alpha * X(i,j);
}
}
}
#undef X
}
// Scale everything that is not enclosed in (ilo:ihi,jlo:jhi)
void scale_excluding(int m, int n, int ilo, int ihi, int jlo, int jhi,
double *restrict const X, int ldX, const scaling_t *beta)
{
#ifdef INTSCALING
double alpha = ldexp(1.0, beta[0]);
#else
double alpha = beta[0];
#endif
#define X(i,j) X[(i) + (j) * ldX]
// Scale vector, if necessary.
if (alpha != 1.0) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if ((ilo <= i && i <= ihi) && (jlo <= j && j <= jhi)) {
// Skip. We are in the area that is to be spared.
}
else {
X(i,j) = alpha * X(i,j);
}
}
}
}
#undef X
}
// Scale the entries {0, 1, ..., n-1} \ {skip}
void scale_except(
int n, int skip, double *restrict const x, const scaling_t * beta)
{
#ifdef INTSCALING
double alpha = ldexp(1.0, beta[0]);
#else
double alpha = beta[0];
#endif
// Scale vector, if necessary.
if (alpha != 1.0) {
for (int i = 0; i < n; i++) {
if (i == skip) {
// Skip.
}
else {
x[i] = alpha * x[i];
}
}
}
}
void dgemm(
const char transa, const char transb,
const int m, const int n, const int k,
const double alpha, const double *restrict const A, const int ldA,
const double *restrict const B, const int ldB,
const double beta, double *restrict const C, const int ldC)
{
extern void dgemm_(
const char *transa, const char *transb,
const int *m, const int *n, const int *k,
const double *alpha, const double *a, const int *lda,
const double *b, const int *ldb,
const double *beta, double *c, const int *ldc);
dgemm_(&transa, &transb,
&m, &n, &k,
&alpha, A, &ldA,
B, &ldB,
&beta, C, &ldC);
}
void dgemv(
const char trans,
const int m, const int n,
const double alpha, const double *restrict const A, const int ldA,
const double *restrict x, int incx,
const double beta, double *restrict const y, int incy)
{
extern void dgemv_(
const char *trans,
const int *m, const int *n,
const double *alpha, const double *a, const int *lda,
const double *x, const int *incx,
const double *beta, double *y, const int *incy);
dgemv_(&trans,
&m, &n,
&alpha, A, &ldA,
x, &incx,
&beta, y, &incy);
}
double dlange(const char norm, const int m, const int n,
const double *restrict const A, const int ldA)
{
double *work = NULL;
if (norm == 'I' || norm == 'i') {
work = (double *) malloc(m * sizeof(double));
}
extern double dlange_(const char *norm, const int *m, const int *n,
const double *a, const int *ldA, double *work);
double nrm = dlange_(&norm, &m, &n, A, &ldA, work);
if (norm == 'I' || norm == 'i') {
free(work);
}
return nrm;
}
void dlaln2(int ltrans, int na, int nw, double smin, double ca, double *A,
int ldA, double d1, double d2, double *B, int ldB, double wr, double wi,
double *X, int ldX, double *scale, double *xnorm)
{
extern void dlaln2_(
const int *ltrans, const int *na, const int *nw, const double *smin,
const double *ca, const double *a, const int *lda, const double *d1,
const double *d2, const double *b, const int *ldb, const double *wr,
const double *wi, double *x, const int *ldx, double *scale,
double *xnorm, int *info);
int info;
dlaln2_(<rans, &na, &nw, &smin, &ca, A, &ldA, &d1, &d2, B, &ldB, &wr, &wi,
X, &ldX, scale, xnorm, &info);
if (info == 1)
printf("WARNING: DLALN2 had to perturb the entries\n");
}
void dgehrd(const int n, const int ilo, const int ihi,
double *A, const int ldA, double *tau)
{
extern void dgehrd_(
const int *n, const int *ilo, const int *ihi, double *A, const int *ldA,
double *tau, double *work, const int *lwork, int *info);
int info;
// Query optimal workspace size.
int lwork = -1;
double work_size = 0.0;
dgehrd_(&n, &ilo, &ihi, A, &ldA, tau, &work_size, &lwork, &info);
lwork = (int) work_size;
double *work = malloc(lwork * sizeof(double));
dgehrd_(&n, &ilo, &ihi, A, &ldA, tau, work, &lwork, &info);
free(work);
if (info != 0)
printf("WARNING: dgehrd had illegal %d-th argument.\n", info);
}
void dhseqr(const char job, const char compz,
const int n, const int ilo, const int ihi, double *H, const int ldH,
double *wr, double *wi, double *Z, const int ldZ,
double *work, const int lwork, int *info)
{
extern void dhseqr_(const char *job, const char *compz, const int *n,
const int *ilo, const int *ihi, double *H, const int *ldH,
double *wr, double *im, double *Z, const int *ldZ,
double *work, const int *lwork, int *info);
dhseqr_(&job, &compz, &n, &ilo, &ihi, H, &ldH, wr, wi, Z, &ldZ, work,
&lwork, info);
}