-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.c
334 lines (315 loc) · 7.7 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
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#include "utils.h"
#ifdef _MSC_VER
double drand48()
{
return (double)rand()/(RAND_MAX + 1);
}
#endif /* _MSC_VER */
/*
* Allocate a dense matrix
* m - number of rows
* n - number of columns
* dense - newly allocated matrix.
*/
void alloc_dense(int m, int n, double **dense)
{
*dense = malloc(m*n*sizeof(**dense));
}
/*
* Free a dense matrix
* dense - dense matrix, may be NULL
*/
void free_dense(double **dense)
{
if (!*dense) {
return;
}
free(*dense);
*dense = NULL;
}
/*
* Zero a dense matrix
* m - number of rows
* n - number of columns
* dense - matrix to zero.
*/
void zero_dense(int m, int n, double *dense)
{
int i, j;
for (j = 0; j < n; j++) {
for (i = 0; i < m; i++) {
dense[j*m + i] = 0;
}
}
}
/*
* Allocate a sparse matrix in coordinate format.
* m - number of rows
* n - number of columns
* NZ - number of nonzeros
* sparse - newly allocated matrix.
*/
void alloc_sparse(int m, int n, int NZ, COO *sparse)
{
COO sp = calloc(1, sizeof(struct _p_COO));
sp->m = m;
sp->n = n;
sp->NZ = NZ;
sp->coords = calloc(NZ, sizeof(struct coord));
sp->data = calloc(NZ, sizeof(double));
*sparse = sp;
}
/*
* Free a sparse matrix.
* sparse - sparse matrix, may be NULL
*/
void free_sparse(COO *sparse)
{
COO sp = *sparse;
if (!sp) {
return;
}
free(sp->coords);
free(sp->data);
free(sp);
*sparse = NULL;
}
/*
* Convert a sparse matrix to dense format in column major format.
*
* sparse - The sparse matrix to convert
* dense - pointer to output dense matrix (will be allocated)
*/
void convert_sparse_to_dense(const COO sparse, double **dense)
{
int n;
int i, j;
alloc_dense(sparse->m, sparse->n, dense);
zero_dense(sparse->m, sparse->n, *dense);
for (n = 0; n < sparse->NZ; n++) {
i = sparse->coords[n].i;
j = sparse->coords[n].j;
(*dense)[j * sparse->m + i] = sparse->data[n];
}
}
/*
* Convert a dense matrix in column major format to sparse.
* Entries with absolute value < 1e-15 are flushed to zero and not
* stored in the sparse format.
*
* dense - the dense array
* m - number of rows
* n - number of columns
* sparse - output sparse matrix (allocated by this routine)
*/
void convert_dense_to_sparse(const double *dense, int m, int n,
COO *sparse)
{
int i, j, NZ;
COO sp;
NZ = 0;
/* Figure out how many nonzeros we're going to have. */
for (j = 0; j < n; j++) {
for (i = 0; i < m; i++) {
double val = dense[j*m + i];
if (fabs(val) > 1e-15) {
NZ++;
}
}
}
alloc_sparse(m, n, NZ, &sp);
NZ = 0;
/* Fill up the sparse matrix */
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
double val = dense[j*m + i];
if (fabs(val) > 1e-15) {
sp->coords[NZ].i = i;
sp->coords[NZ].j = j;
sp->data[NZ] = val;
NZ++;
}
}
}
*sparse = sp;
}
/*
* Create a random sparse matrix
*
* m - number of rows
* n - number of columns
* frac - fraction of entries that should be nonzero
* sparse - newly allocated random matrix.
*/
void random_matrix(int m, int n, double frac, COO *sparse)
{
int i, j;
double *d;
alloc_dense(m, n, &d);
for (j = 0; j < n; j++) {
for (i = 0; i < m; i++) {
if (drand48() < frac) {
d[j*m + i] = drand48();
} else {
d[j*m + i] = 0.0;
}
}
}
convert_dense_to_sparse(d, m, n, sparse);
free_dense(&d);
}
/*
* Read a sparse matrix from a file.
*
* file - The filename to read
* sparse - The newly read sparse matrix (allocated here)
*/
void read_sparse(const char *file, COO *sparse)
{
COO sp;
int i, j, k, m, n, NZ;
double val;
int c;
FILE *f = fopen(file, "r");
if (!f) {
fprintf(stderr, "Unable to open %s for reading.\n", file);
exit(1);
}
c = fscanf(f, "%d %d %d\n", &m, &n, &NZ);
if (c != 3) {
fprintf(stderr, "File format incorrect on line 1, expecting 3 integers, got %d\n", c);
fclose(f);
exit(1);
}
if (NZ > (uint64_t)m*n) {
fprintf(stderr, "More nonzeros (%d) than matrix entries (%d x %d)!\n", NZ, m, n);
fclose(f);
exit(1);
}
alloc_sparse(m, n, NZ, &sp);
k = 0;
while ((c = fscanf(f, "%d %d %lg\n", &i, &j, &val)) == 3) {
if (k >= NZ) {
fprintf(stderr, "File has nonzero lines than expected (%d)\n", NZ);
fclose(f);
free_sparse(&sp);
exit(1);
}
if (i >= m || j >= n) {
fprintf(stderr, "Entry on line %d incorrect, index (%d, %d) out of bounds for %d x %d matrix\n", k + 2, i, j, m, n);
fclose(f);
free_sparse(&sp);
exit(1);
}
sp->coords[k].i = i;
sp->coords[k].j = j;
sp->data[k] = val;
k++;
}
if (k != NZ) {
fprintf(stderr, "File has fewer lines (%d) than expected (%d)\n",
k, NZ);
fclose(f);
free_sparse(&sp);
exit(1);
}
*sparse = sp;
fclose(f);
}
/*
* Write a sparse matrix to a file.
*
* f - The file handle.
* sp - The sparse matrix to write.
*/
void write_sparse(FILE *f, COO sp)
{
int i;
fprintf(f, "%d %d %d\n", sp->m, sp->n, sp->NZ);
for (i = 0; i < sp->NZ; i++) {
fprintf(f, "%d %d %.15g\n", sp->coords[i].i, sp->coords[i].j, sp->data[i]);
}
}
/*
* Print a sparse matrix to stdout
*
* sp - The sparse matrix to print.
*/
void print_sparse(COO sp)
{
write_sparse(stdout, sp);
}
void read_sparse_binary(const char *file, COO *sparse)
{
COO sp;
int m, n, NZ;
size_t nread;
FILE *f = fopen(file, "r");
if (!f) {
fprintf(stderr, "Unable to open %s for reading.\n", file);
exit(1);
}
nread = fread(&m, sizeof(m), 1, f);
if (nread != 1) {
fprintf(stderr, "Did not read rows from file\n");
exit(1);
}
nread = fread(&n, sizeof(n), 1, f);
if (nread != 1) {
fprintf(stderr, "Did not read columns from file\n");
exit(1);
}
nread = fread(&NZ, sizeof(NZ), 1, f);
if (nread != 1) {
fprintf(stderr, "Did not read number of nonzeros from file\n");
exit(1);
}
alloc_sparse(m, n, NZ, &sp);
nread = fread(sp->coords, sizeof(*sp->coords), NZ, f);
if (nread != NZ) {
fprintf(stderr, "Did not read nonzero locations from file\n");
exit(1);
}
nread = fread(sp->data, sizeof(*sp->data), NZ, f);
if (nread != NZ) {
fprintf(stderr, "Did not read nonzero values from file\n");
exit(1);
}
*sparse = sp;
fclose(f);
}
void write_sparse_binary(FILE *f, COO sp)
{
size_t nwrite;
nwrite = fwrite(&(sp->m), sizeof(sp->m), 1, f);
if (nwrite != 1) {
fprintf(stderr, "Could not write rows to output file\n");
exit(1);
}
nwrite = fwrite(&(sp->n), sizeof(sp->n), 1, f);
if (nwrite != 1) {
fprintf(stderr, "Could not write columns to output file\n");
exit(1);
}
nwrite = fwrite(&(sp->NZ), sizeof(sp->NZ), 1, f);
if (nwrite != 1) {
fprintf(stderr, "Could not write number of nonzeros to output file\n");
exit(1);
}
nwrite = fwrite(sp->coords, sizeof(*sp->coords), sp->NZ, f);
if (nwrite != sp->NZ) {
fprintf(stderr, "Could not write nonzero locations to output file\n");
exit(1);
}
nwrite = fwrite(sp->data, sizeof(*sp->data), sp->NZ, f);
if (nwrite != sp->NZ) {
fprintf(stderr, "Could not write nonzero values to output file\n");
exit(1);
}
}