/[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 123 - (show annotations)
Fri Jul 8 04:08:13 2005 UTC (14 years, 9 months ago) by jgs
File MIME type: text/plain
File size: 7847 byte(s)
Merge of development branch back to main trunk on 2005-07-08

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( index_t*, index_t*, double*, int, int );
19 static void q_sort( index_t*, index_t*, double*, int, int );
20 static void print_entries( index_t*, index_t*, double* );
21
22 static int M, N, nz;
23
24
25 /* debug: print the entries */
26 void print_entries( index_t *r, index_t *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( index_t *r, index_t *c, double *v, int left, int right )
38 {
39 double v_temp;
40 index_t 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( index_t *row, index_t *col, double *val, int begin, int end )
56 {
57 int l, r;
58 index_t 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 index_t *col_ind = NULL;
90 index_t *row_ind = NULL;
91 index_t *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, index_t );
139 row_ind = MEMALLOC( nz, index_t );
140 val = MEMALLOC( nz, double );
141
142 row_ptr = MEMALLOC( (M+1), index_t );
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 Finley_SystemMatrix* Finley_SystemMatrix_loadMM_toCSC( char *fileName_p )
196 {
197 int i;
198 // int curr_row;
199 int curr_col;
200 MM_typecode matrixCode;
201
202 index_t *col_ind = NULL;
203 index_t *row_ind = NULL;
204 // index_t *row_ptr = NULL;
205 index_t *col_ptr = NULL;
206 double *val = NULL;
207
208 Finley_SystemMatrixPattern *loc_pattern = NULL;
209 Finley_SystemMatrixType type = UNKNOWN;
210 Finley_SystemMatrix *out = NULL;
211
212 Finley_ErrorCode = NO_ERROR;
213
214 /* open the file */
215 FILE *fileHandle_p = fopen( fileName_p, "r" );
216 if( fileHandle_p == NULL )
217 {
218 Finley_ErrorCode = IO_ERROR;
219 sprintf( Finley_ErrorMsg, "File %s could not be opened for reading", fileName_p );
220 return NULL;
221 }
222
223 /* process banner */
224 if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
225 {
226 Finley_ErrorCode = IO_ERROR;
227 sprintf( Finley_ErrorMsg, "Error processing MM banner in file %s", fileName_p );
228
229 fclose( fileHandle_p );
230 return NULL;
231 }
232 if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
233 {
234 Finley_ErrorCode = TYPE_ERROR;
235 sprintf( Finley_ErrorMsg, "Sorry, Matrix Market type: [%s] is not supported", mm_typecode_to_str(matrixCode) );
236
237 fclose( fileHandle_p );
238 return NULL;
239 }
240
241 /* get matrix size */
242 if( mm_read_mtx_crd_size(fileHandle_p, &M, &N, &nz) != 0 )
243 {
244 Finley_ErrorCode = IO_ERROR;
245 sprintf( Finley_ErrorMsg, "Could not parse matrix size in %s", fileName_p );
246
247 fclose( fileHandle_p );
248 return NULL;
249 }
250
251 /* prepare storage */
252 col_ind = MEMALLOC( nz, index_t );
253 row_ind = MEMALLOC( nz, index_t );
254 val = MEMALLOC( nz, double );
255
256 // row_ptr = MEMALLOC( (M+1), index_t );
257 col_ptr = MEMALLOC( (N+1), index_t );
258
259
260 // if( col_ind == NULL || row_ind == NULL || val == NULL || row_ptr == NULL )
261 if( col_ind == NULL || row_ind == NULL || val == NULL || col_ptr == NULL )
262 {
263 Finley_ErrorCode = MEMORY_ERROR;
264 sprintf( Finley_ErrorMsg, "Could not allocate memory" );
265
266 fclose( fileHandle_p );
267 return NULL;
268 }
269
270 /* perform actual read of elements */
271 for( i=0; i<nz; i++ )
272 {
273 fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
274 row_ind[i]--;
275 col_ind[i]--;
276 }
277 fclose( fileHandle_p );
278
279 /* sort the entries */
280 // q_sort( row_ind, col_ind, val, 0, nz );
281 q_sort( col_ind, row_ind, val, 0, nz );
282
283 /* setup row_ptr */
284 // curr_row = 0;
285 curr_col = 0;
286 // for( i=0; (i<nz && curr_row<M); curr_row++ )
287 for( i=0; (i<nz && curr_col<N); curr_col++ )
288 {
289 // while( row_ind[i] != curr_row )
290 while( col_ind[i] != curr_col )
291 i++;
292 // row_ptr[curr_row] = i;
293 col_ptr[curr_col] = i;
294 }
295 // row_ptr[M] = nz;
296 col_ptr[N] = nz;
297
298 /* create F_SMP and F_SM */
299 // loc_pattern = Finley_SystemMatrixPattern_alloc( M, row_ptr, col_ind );
300 loc_pattern = Finley_SystemMatrixPattern_alloc( N, col_ptr, row_ind );
301 if( Finley_ErrorCode != NO_ERROR )
302 return NULL;
303
304 // type = CSR;
305 type = CSC;
306 out = Finley_SystemMatrix_alloc( type, loc_pattern, 1, 1 );
307 if( Finley_ErrorCode != NO_ERROR )
308 return NULL;
309
310 /* copy values and cleanup temps */
311 for( i=0; i<nz; i++ )
312 out->val[i] = val[i];
313
314 MEMFREE( val );
315 // MEMFREE( row_ind );
316 MEMFREE( col_ind );
317
318 return out;
319 }
320
321
322 /*
323 * $Log$
324 * Revision 1.3 2005/07/08 04:07:58 jgs
325 * Merge of development branch back to main trunk on 2005-07-08
326 *
327 * Revision 1.2 2005/04/01 05:48:56 jgs
328 * *** empty log message ***
329 *
330 * Revision 1.1.2.3 2005/07/04 00:53:12 imran
331 * Added support for saving FSM to Harwell-Boeing format
332 * loadMM now reads to CSC as well (needs cleanup)
333 *
334 * Revision 1.1.2.2 2005/06/29 02:34:57 gross
335 * some changes towards 64 integers in finley
336 *
337 * Revision 1.1.2.1 2005/03/30 05:00:31 imran
338 * Added a MM load (as CSR) function
339 *
340 *
341 */

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26