Graphviz 14.1.3~dev.20260227.0545
Loading...
Searching...
No Matches
matrix_market.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "config.h"
12
13#include "matrix_market.h"
14#include "mmio.h"
15#include <sparse/SparseMatrix.h>
16#include <stdbool.h>
17#include <stdio.h>
18#include <util/alloc.h>
19
21 matrix_shape_t shape;
22 double *val = NULL;
23 int m, n;
24 void *vp = NULL;
26 int c;
27
28 if ((c = fgetc(f)) != '%') {
29 ungetc(c, f);
30 return NULL;
31 }
32 ungetc(c, f);
33 if (mm_read_banner(f, &shape) != 0) {
34#ifdef DEBUG
35 printf("Could not process Matrix Market banner.\n");
36#endif
37 return NULL;
38 }
39
40 /* find out size of sparse matrix .... */
41 size_t nz;
42 if (mm_read_mtx_crd_size(f, &m, &n, &nz) != 0) {
43 return NULL;
44 }
45 /* reseve memory for matrices */
46
47 int *I = gv_calloc(nz, sizeof(int));
48 int *J = gv_calloc(nz, sizeof(int));
49
50 val = gv_calloc(nz, sizeof(double));
51 for (size_t i = 0; i < nz; i++) {
52 int num = fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i]);
53 if (num != 3) {
54 goto done;
55 }
56 I[i]--; /* adjust from 1-based to 0-based */
57 J[i]--;
58 }
59 if (shape == MS_SYMMETRIC) {
60 I = gv_recalloc(I, nz, 2 * nz, sizeof(int));
61 J = gv_recalloc(J, nz, 2 * nz, sizeof(int));
62 val = gv_recalloc(val, nz, 2 * nz, sizeof(double));
63 const size_t nzold = nz;
64 for (size_t i = 0; i < nzold; i++) {
65 if (I[i] != J[i]) {
66 I[nz] = J[i];
67 J[nz] = I[i];
68 val[nz++] = val[i];
69 }
70 }
71 } else if (shape == MS_SKEW) {
72 I = gv_recalloc(I, nz, 2 * nz, sizeof(int));
73 J = gv_recalloc(J, nz, 2 * nz, sizeof(int));
74 val = gv_recalloc(val, nz, 2 * nz, sizeof(double));
75 const size_t nzold = nz;
76 for (size_t i = 0; i < nzold; i++) {
77 if (I[i] == J[i]) { // skew symm should have no diag
78 goto done;
79 }
80 I[nz] = J[i];
81 J[nz] = I[i];
82 val[nz++] = -val[i];
83 }
84 } else if (shape == MS_HERMITIAN) {
85 goto done;
86 }
87 vp = val;
88
90 sizeof(double));
91done:
92 free(I);
93 free(J);
94 free(val);
95
96 if (A != NULL && shape == MS_SYMMETRIC) {
97 A->is_symmetric = true;
98 A->is_pattern_symmetric = true;
99 }
100
101 return A;
102}
SparseMatrix SparseMatrix_from_coordinate_arrays(size_t nz, int m, int n, int *irn, int *jcn, const void *val, int type, size_t sz)
@ MATRIX_TYPE_REAL
Memory allocation wrappers that exit on failure.
static void * gv_recalloc(void *ptr, size_t old_nmemb, size_t new_nmemb, size_t size)
Definition alloc.h:73
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
#define A(n, t)
Definition expr.h:76
#define I
Definition expr.h:71
void free(void *)
node NULL
Definition grammar.y:181
SparseMatrix SparseMatrix_import_matrix_market(FILE *f)
import Matrix Market
int mm_read_mtx_crd_size(FILE *f, int *M, int *N, size_t *nz)
Definition mmio.c:84
int mm_read_banner(FILE *f, matrix_shape_t *shape)
Definition mmio.c:31
Matrix Market I/O API
matrix_shape_t
Definition mmio.h:32
@ MS_SYMMETRIC
Definition mmio.h:32
@ MS_SKEW
Definition mmio.h:32
@ MS_HERMITIAN
Definition mmio.h:32