forked from IntelLabs/SpMP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.cpp
66 lines (55 loc) · 1.37 KB
/
Vector.cpp
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
#include <cstdio>
#include "Utils.hpp"
#include "Vector.hpp"
#include "mm_io.h"
namespace SpMP
{
bool loadVectorMatrixMarket(const char *fileName, double **v, int *m, int *n)
{
FILE *fp = fopen(fileName, "r");
if (!fp) {
fprintf(stderr, "Failed to open file %s\n", fileName);
return false;
}
MM_typecode matcode;
if (mm_read_banner(fp, &matcode) != 0) {
fprintf(stderr, "Error: could not process Matrix Market banner.\n");
fclose(fp);
return false;
}
if (!mm_is_valid(matcode)) {
fprintf(stderr, "Error: invalid Matrix Market banner.\n");
fclose(fp);
return false;
}
if (!mm_is_array(matcode)) {
fprintf(stderr, "Error: only support arrays.\n");
fclose(fp);
return false;
}
if (mm_read_mtx_array_size(fp, m, n) != 0) {
fprintf(stderr, "Error: could not read array size.\n");
fclose(fp);
return false;
}
*v = MALLOC(double, (*m)*(*n));
for (int i = 0; i < *m; i++) {
for (int j = 0; j < *n - 1; j++) {
if (fscanf(fp, "%lg", *v + i*(*n) + j) != 1) {
fprintf(stderr, "Error: premature EOF.\n");
FREE(*v);
fclose(fp);
return false;
}
}
if (fscanf(fp, "%lg\n", *v + i*(*n) + (*n) - 1) != 1) {
fprintf(stderr, "Error: premature EOF.\n");
FREE(*v);
fclose(fp);
return false;
}
}
fclose(fp);
return true;
}
} // namespace SpMP