/[escript]/trunk/esys2/finley/src/finleyC/System_loadMM.c
ViewVC logotype

Contents of /trunk/esys2/finley/src/finleyC/System_loadMM.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 117 - (show annotations)
Fri Apr 1 05:48:57 2005 UTC (15 years ago) by jgs
File MIME type: text/plain
File size: 4422 byte(s)
*** empty log message ***

1 /* $Id$ */
2
3 /**************************************************************/
4
5 /* Finley: Matrix Market format is loaded to a SystemMatrix */
6
7 /**************************************************************/
8
9 /* Copyrights by ACcESS Australia 2003,2004,2005 */
10 /* Author: imran@access.edu.au */
11
12 /**************************************************************/
13
14 #include "Finley.h"
15 #include "mmio.h"
16 #include "System.h"
17
18 static void swap( maybelong*, maybelong*, double*, int, int );
19 static void q_sort( maybelong*, maybelong*, double*, int, int );
20 static void print_entries( maybelong*, maybelong*, double* );
21
22 static int M, N, nz;
23
24
25 /* debug: print the entries */
26 void print_entries( maybelong *r, maybelong *c, double *v )
27 {
28 int i;
29
30 for( i=0; i<nz; i++ )
31 {
32 printf( "(%ld, %ld) == %e\n", r[i], c[i], v[i] );
33 }
34 }
35
36 /* swap function */
37 void swap( maybelong *r, maybelong *c, double *v, int left, int right )
38 {
39 double v_temp;
40 maybelong temp;
41
42 temp = r[left];
43 r[left] = r[right];
44 r[right] = temp;
45
46 temp = c[left];
47 c[left] = c[right];
48 c[right] = temp;
49
50 v_temp = v[left];
51 v[left] = v[right];
52 v[right] = v_temp;
53 }
54
55 void q_sort( maybelong *row, maybelong *col, double *val, int begin, int end )
56 {
57 int l, r;
58 maybelong pivot, lval;
59
60 if( end > begin )
61 {
62 pivot = N * row[begin] + col[begin];
63 l = begin + 1;
64 r = end;
65
66 while( l < r )
67 {
68 lval = N * row[l] + col[l];
69 if( lval < pivot )
70 l++;
71 else
72 {
73 r--;
74 swap( row, col, val, l, r );
75 }
76 }
77 l--;
78 swap( row, col, val, begin, l );
79 q_sort( row, col, val, begin, l );
80 q_sort( row, col, val, r, end );
81 }
82 }
83
84 Finley_SystemMatrix* Finley_SystemMatrix_loadMM_toCSR( char *fileName_p )
85 {
86 int i, curr_row;
87 MM_typecode matrixCode;
88
89 maybelong *col_ind = NULL;
90 maybelong *row_ind = NULL;
91 maybelong *row_ptr = NULL;
92 double *val = NULL;
93
94 Finley_SystemMatrixPattern *loc_pattern = NULL;
95 Finley_SystemMatrixType type = UNKNOWN;
96 Finley_SystemMatrix *out = NULL;
97
98 Finley_ErrorCode = NO_ERROR;
99
100 /* open the file */
101 FILE *fileHandle_p = fopen( fileName_p, "r" );
102 if( fileHandle_p == NULL )
103 {
104 Finley_ErrorCode = IO_ERROR;
105 sprintf( Finley_ErrorMsg, "File %s could not be opened for reading", fileName_p );
106 return NULL;
107 }
108
109 /* process banner */
110 if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
111 {
112 Finley_ErrorCode = IO_ERROR;
113 sprintf( Finley_ErrorMsg, "Error processing MM banner in file %s", fileName_p );
114
115 fclose( fileHandle_p );
116 return NULL;
117 }
118 if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
119 {
120 Finley_ErrorCode = TYPE_ERROR;
121 sprintf( Finley_ErrorMsg, "Sorry, Matrix Market type: [%s] is not supported", mm_typecode_to_str(matrixCode) );
122
123 fclose( fileHandle_p );
124 return NULL;
125 }
126
127 /* get matrix size */
128 if( mm_read_mtx_crd_size(fileHandle_p, &M, &N, &nz) != 0 )
129 {
130 Finley_ErrorCode = IO_ERROR;
131 sprintf( Finley_ErrorMsg, "Could not parse matrix size in %s", fileName_p );
132
133 fclose( fileHandle_p );
134 return NULL;
135 }
136
137 /* prepare storage */
138 col_ind = MEMALLOC( nz, maybelong );
139 row_ind = MEMALLOC( nz, maybelong );
140 val = MEMALLOC( nz, double );
141
142 row_ptr = MEMALLOC( (M+1), maybelong );
143
144 if( col_ind == NULL || row_ind == NULL || val == NULL || row_ptr == NULL )
145 {
146 Finley_ErrorCode = MEMORY_ERROR;
147 sprintf( Finley_ErrorMsg, "Could not allocate memory" );
148
149 fclose( fileHandle_p );
150 return NULL;
151 }
152
153 /* perform actual read of elements */
154 for( i=0; i<nz; i++ )
155 {
156 fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
157 row_ind[i]--;
158 col_ind[i]--;
159 }
160 fclose( fileHandle_p );
161
162 /* sort the entries */
163 q_sort( row_ind, col_ind, val, 0, nz );
164
165 /* setup row_ptr */
166 curr_row = 0;
167 for( i=0; (i<nz && curr_row<M); curr_row++ )
168 {
169 while( row_ind[i] != curr_row )
170 i++;
171 row_ptr[curr_row] = i;
172 }
173 row_ptr[M] = nz;
174
175 /* create F_SMP and F_SM */
176 loc_pattern = Finley_SystemMatrixPattern_alloc( M, row_ptr, col_ind );
177 if( Finley_ErrorCode != NO_ERROR )
178 return NULL;
179
180 type = CSR;
181 out = Finley_SystemMatrix_alloc( type, loc_pattern, 1, 1 );
182 if( Finley_ErrorCode != NO_ERROR )
183 return NULL;
184
185 /* copy values and cleanup temps */
186 for( i=0; i<nz; i++ )
187 out->val[i] = val[i];
188
189 MEMFREE( val );
190 MEMFREE( row_ind );
191
192 return out;
193 }
194
195
196 /*
197 * $Log$
198 * Revision 1.2 2005/04/01 05:48:56 jgs
199 * *** empty log message ***
200 *
201 * Revision 1.1.2.1 2005/03/30 05:00:31 imran
202 * Added a MM load (as CSR) function
203 *
204 *
205 */

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26