-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.c
211 lines (170 loc) · 5.88 KB
/
common.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
#include "common.h"
Matrix create_dense_matrix(const int num_row, const int num_col) {
Matrix A;
A.isSparse = false;
A.numRow = num_row;
A.numCol = num_col;
A.numNonzero = num_row * num_col;
A.values = (FLOAT*)mkl_malloc(num_row*num_col * sizeof(FLOAT));
A.rowIndex = NULL;
A.columns = NULL;
return A;
}
Matrix create_sparse_matrix(const int num_row, const int num_col, const int num_nonzero) {
Matrix A;
A.isSparse = true;
A.numRow = num_row;
A.numCol = num_col;
A.numNonzero = num_nonzero;
A.values = (FLOAT*)mkl_malloc(num_nonzero * sizeof(FLOAT));
A.columns = (int*)mkl_malloc(num_nonzero * sizeof(int));
A.rowIndex = (int*)mkl_malloc((num_row + 1) * sizeof(int));
return A;
}
void destroy_matrix(Matrix *A) {
A->numRow = 0;
A->numCol = 0;
A->numNonzero = 0;
if (A->values != NULL)
mkl_free(A->values);
A->values = NULL;
if (A->isSparse) {
mkl_free(A->columns);
A->columns = NULL;
mkl_free(A->rowIndex);
A->rowIndex = NULL;
}
}
Matrix copy_matrix(const Matrix A) {
Matrix B;
if (A.isSparse) {
B = create_sparse_matrix(A.numRow, A.numCol, A.numNonzero);
cblas_copy(A.numNonzero, A.values, 1, B.values, 1);
memcpy(B.columns, A.columns, A.numNonzero * sizeof(int));
memcpy(B.rowIndex, A.rowIndex, (A.numRow + 1) * sizeof(int));
}
else {
B = create_dense_matrix(A.numRow, A.numCol);
cblas_copy(A.numRow*A.numCol, A.values, 1, B.values, 1);
}
return B;
}
// read the matrix from data
Matrix read_matrix(const char *file_name) {
FILE *fp = fopen(file_name, "rb");
if (fp == NULL)
ERROR("input stream is not accessible");
bool is_sparse = 0;
int m, n;
fread(&is_sparse, sizeof(bool), 1, fp);
fread(&m, sizeof(int), 1, fp);
fread(&n, sizeof(int), 1, fp);
Matrix A;
if (is_sparse) {
int nnz; // number of non-zeros elements
fread(&nnz, sizeof(int), 1, fp);
A = create_sparse_matrix(m, n, nnz);
fread(A.rowIndex, sizeof(int), m + 1, fp);
fread(A.columns, sizeof(int), nnz, fp);
fread(A.values, sizeof(FLOAT), nnz, fp);
}
else {
A = create_dense_matrix(m, n);
fread(A.values, sizeof(FLOAT), m*n, fp);
}
fclose(fp);
return A;
}
// return the tranpose of A
Matrix tranpose_matrix(const Matrix A)
{
Matrix B;
if (A.isSparse) {
int n = max(A.numRow, A.numCol);
int *old_rowIndex;
if (A.numRow < n) {
old_rowIndex = (int*)mkl_malloc((n + 1) * sizeof(int));
memcpy(old_rowIndex, A.rowIndex, (A.numRow + 1) * sizeof(int));
for (int i = A.numRow + 1; i <= n; i++)
old_rowIndex[i] = A.rowIndex[A.numRow];
}
else
old_rowIndex = A.rowIndex;
int job[] = { 0, 0, 0, 0, 0, 1 };
int info;
Matrix B = create_sparse_matrix(A.numCol, A.numRow, A.numNonzero);
int *new_rowIndex;
if (B.numRow < n)
new_rowIndex = (int*)mkl_malloc((n + 1) * sizeof(int));
else
new_rowIndex = B.rowIndex;
mkl_csrcsc(job, &n, A.values, A.columns, old_rowIndex, B.values, B.columns, new_rowIndex, &info);
if (new_rowIndex != B.rowIndex) {
memcpy(B.rowIndex, new_rowIndex, (B.numRow + 1) * sizeof(int));
mkl_free(new_rowIndex);
}
if (old_rowIndex != A.rowIndex)
mkl_free(old_rowIndex);
return B;
}
else {
B = create_dense_matrix(A.numCol, A.numRow);
mkl_omatcopy('R', 'T', A.numRow, A.numCol, 1.0, A.values, A.numCol, B.values, B.numCol);
}
return B;
}
// return the total error of the factorization M = U*V'
FLOAT evaluate_factorization(const Matrix M, const Matrix U, const Matrix V)
{
ASSERT_EQUAL(M.numRow, U.numRow);
ASSERT_EQUAL(M.numCol, V.numRow);
ASSERT_EQUAL(U.numCol, V.numCol);
static size_t memory_size = MAX_MEMORY_SIZE;
FLOAT error = 0;
int row = 0;
if (M.isSparse) {
FLOAT *tmp = NULL;
int numRow = min(M.numRow, memory_size / (M.numCol * sizeof(FLOAT)));
tmp = (FLOAT*)mkl_malloc(numRow * M.numCol * sizeof(FLOAT));
// try to find a suitable memory size
while (tmp == NULL) {
numRow = numRow / 2 + (numRow % 2);
tmp = (FLOAT*)mkl_malloc(numRow * M.numCol * sizeof(FLOAT));
}
memory_size = numRow * M.numCol * sizeof(FLOAT);
int offset = 0;
while (offset < M.numRow) {
if (offset + numRow > M.numRow)
numRow = M.numRow - offset;
cblas_gemm(CblasRowMajor, CblasNoTrans, CblasTrans, numRow, M.numCol, U.numCol, 1.0,
U.values + offset * U.numCol, U.numCol, V.values, V.numCol, 0.0, tmp, M.numCol);
for (int i = 0; i < numRow; i++) {
int row_id = i + offset;
cblas_axpyi(M.rowIndex[row_id + 1] - M.rowIndex[row_id], -1.0, M.values + M.rowIndex[row_id],
M.columns + M.rowIndex[row_id], tmp + i * M.numCol);
}
FLOAT norm = cblas_nrm2(numRow * M.numCol, tmp, 1);
error += norm * norm;
offset += numRow;
}
mkl_free(tmp);
}
else {
Matrix tmp = create_dense_matrix(M.numRow, M.numCol);
// tmp = U * V'
cblas_gemm(CblasRowMajor, CblasNoTrans, CblasTrans, M.numRow, M.numCol, U.numCol, 1.0, U.values, U.numCol,
V.values, V.numCol, 0.0, tmp.values, tmp.numCol);
// tmp -= M
cblas_axpy(M.numRow * M.numCol, -1.0, M.values, 1, tmp.values, 1);
error = cblas_nrm2(M.numRow * M.numCol, tmp.values, 1);
error *= error;
destroy_matrix(&tmp);
}
return error;
}
void nonnegative_projection(Matrix X)
{
for (int i = 0; i < X.numNonzero; i++)
if (X.values[i] < 0)
X.values[i] = 0;
}