/[escript]/trunk/paso/src/SystemMatrix_loadMM.c
ViewVC logotype

Annotation of /trunk/paso/src/SystemMatrix_loadMM.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 155 - (hide annotations)
Wed Nov 9 02:02:19 2005 UTC (13 years, 11 months ago) by jgs
File MIME type: text/plain
File size: 7176 byte(s)
move all directories from trunk/esys2 into trunk and remove esys2

1 jgs 150 /* $Id$ */
2    
3     /**************************************************************/
4    
5     /* Paso: 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 "Paso.h"
15     #include "mmio.h"
16     #include "SystemMatrix.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", (long)r[i], (long)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     Paso_SystemMatrix* Paso_SystemMatrix_loadMM_toCSR( char *fileName_p )
85     {
86     int i, curr_row;
87     MM_typecode matrixCode;
88     Paso_resetError();
89    
90     index_t *col_ind = NULL;
91     index_t *row_ind = NULL;
92     index_t *row_ptr = NULL;
93     double *val = NULL;
94    
95     Paso_SystemMatrixPattern *loc_pattern = NULL;
96     Paso_SystemMatrixType type = UNKNOWN;
97     Paso_SystemMatrix *out = NULL;
98    
99     /* open the file */
100     FILE *fileHandle_p = fopen( fileName_p, "r" );
101     if( fileHandle_p == NULL )
102     {
103     Paso_setError(IO_ERROR, "__FILE__: Cannot read file for reading.");
104     return NULL;
105     }
106    
107     /* process banner */
108     if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
109     {
110     Paso_setError(IO_ERROR, "__FILE__: Error processing MM banner.");
111     fclose( fileHandle_p );
112     return NULL;
113     }
114     if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
115     {
116     Paso_setError(TYPE_ERROR,"__FILE__: found Matrix Market type is not supported.");
117     fclose( fileHandle_p );
118     return NULL;
119     }
120    
121     /* get matrix size */
122     if( mm_read_mtx_crd_size(fileHandle_p, &M, &N, &nz) != 0 )
123     {
124     Paso_setError(IO_ERROR, "__FILE__: Could not parse matrix size");
125     fclose( fileHandle_p );
126     return NULL;
127     }
128    
129     /* prepare storage */
130     col_ind = MEMALLOC( nz, index_t );
131     row_ind = MEMALLOC( nz, index_t );
132     val = MEMALLOC( nz, double );
133    
134     row_ptr = MEMALLOC( (M+1), index_t );
135    
136     if( col_ind == NULL || row_ind == NULL || val == NULL || row_ptr == NULL )
137     {
138     Paso_setError(MEMORY_ERROR, "__FILE__: Could not allocate memory" );
139    
140     fclose( fileHandle_p );
141     return NULL;
142     }
143    
144     /* perform actual read of elements */
145     for( i=0; i<nz; i++ )
146     {
147     fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
148     row_ind[i]--;
149     col_ind[i]--;
150     }
151     fclose( fileHandle_p );
152    
153     /* sort the entries */
154     q_sort( row_ind, col_ind, val, 0, nz );
155    
156     /* setup row_ptr */
157     curr_row = 0;
158     for( i=0; (i<nz && curr_row<M); curr_row++ )
159     {
160     while( row_ind[i] != curr_row )
161     i++;
162     row_ptr[curr_row] = i;
163     }
164     row_ptr[M] = nz;
165    
166     /* create F_SMP and F_SM */
167     loc_pattern = Paso_SystemMatrixPattern_alloc( M, row_ptr, col_ind );
168     if(! Paso_noError() )
169     return NULL;
170    
171     type = CSR;
172     out = Paso_SystemMatrix_alloc( type, loc_pattern, 1, 1 );
173     if(! Paso_noError() )
174     return NULL;
175    
176     /* copy values and cleanup temps */
177     for( i=0; i<nz; i++ )
178     out->val[i] = val[i];
179    
180     MEMFREE( val );
181     MEMFREE( row_ind );
182    
183     return out;
184     }
185    
186     Paso_SystemMatrix* Paso_SystemMatrix_loadMM_toCSC( char *fileName_p )
187     {
188     int i;
189     // int curr_row;
190     int curr_col;
191     MM_typecode matrixCode;
192    
193     index_t *col_ind = NULL;
194     index_t *row_ind = NULL;
195     // index_t *row_ptr = NULL;
196     index_t *col_ptr = NULL;
197     double *val = NULL;
198    
199     Paso_SystemMatrixPattern *loc_pattern = NULL;
200     Paso_SystemMatrixType type = UNKNOWN;
201     Paso_SystemMatrix *out = NULL;
202    
203     Paso_resetError();
204    
205     /* open the file */
206     FILE *fileHandle_p = fopen( fileName_p, "r" );
207     if( fileHandle_p == NULL )
208     {
209     Paso_setError(IO_ERROR,"__FILE__: File could not be opened for reading");
210     return NULL;
211     }
212    
213     /* process banner */
214     if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
215     {
216     Paso_setError(IO_ERROR,"__FILE__: Error processing MM banner");
217     fclose( fileHandle_p );
218     return NULL;
219     }
220     if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
221     {
222     Paso_setError(TYPE_ERROR,"__FILE__: found Matrix Market type is not supported.");
223     fclose( fileHandle_p );
224     return NULL;
225     }
226    
227     /* get matrix size */
228     if( mm_read_mtx_crd_size(fileHandle_p, &M, &N, &nz) != 0 )
229     {
230     Paso_setError(TYPE_ERROR,"__FILE__: found Matrix Market type is not supported.");
231     fclose( fileHandle_p );
232     return NULL;
233     }
234    
235     /* prepare storage */
236     col_ind = MEMALLOC( nz, index_t );
237     row_ind = MEMALLOC( nz, index_t );
238     val = MEMALLOC( nz, double );
239    
240     // row_ptr = MEMALLOC( (M+1), index_t );
241     col_ptr = MEMALLOC( (N+1), index_t );
242    
243    
244     // if( col_ind == NULL || row_ind == NULL || val == NULL || row_ptr == NULL )
245     if( col_ind == NULL || row_ind == NULL || val == NULL || col_ptr == NULL )
246     {
247     Paso_setError(MEMORY_ERROR,"Could not allocate memory" );
248     fclose( fileHandle_p );
249     return NULL;
250     }
251    
252     /* perform actual read of elements */
253     for( i=0; i<nz; i++ )
254     {
255     fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
256     row_ind[i]--;
257     col_ind[i]--;
258     }
259     fclose( fileHandle_p );
260    
261     /* sort the entries */
262     // q_sort( row_ind, col_ind, val, 0, nz );
263     q_sort( col_ind, row_ind, val, 0, nz );
264    
265     /* setup row_ptr */
266     // curr_row = 0;
267     curr_col = 0;
268     // for( i=0; (i<nz && curr_row<M); curr_row++ )
269     for( i=0; (i<nz && curr_col<N); curr_col++ )
270     {
271     // while( row_ind[i] != curr_row )
272     while( col_ind[i] != curr_col )
273     i++;
274     // row_ptr[curr_row] = i;
275     col_ptr[curr_col] = i;
276     }
277     // row_ptr[M] = nz;
278     col_ptr[N] = nz;
279    
280     /* create F_SMP and F_SM */
281     // loc_pattern = Paso_SystemMatrixPattern_alloc( M, row_ptr, col_ind );
282     loc_pattern = Paso_SystemMatrixPattern_alloc( N, col_ptr, row_ind );
283     if(! Paso_noError() )
284     return NULL;
285    
286     // type = CSR;
287     type = CSC;
288     out = Paso_SystemMatrix_alloc( type, loc_pattern, 1, 1 );
289     if(! Paso_noError() )
290     return NULL;
291    
292     /* copy values and cleanup temps */
293     for( i=0; i<nz; i++ )
294     out->val[i] = val[i];
295    
296     MEMFREE( val );
297     // MEMFREE( row_ind );
298     MEMFREE( col_ind );
299    
300     return out;
301     }
302    
303    
304     /*
305     * $Log$
306     * Revision 1.2 2005/09/15 03:44:39 jgs
307     * Merge of development branch dev-02 back to main trunk on 2005-09-15
308     *
309     * Revision 1.1.2.1 2005/09/05 06:29:48 gross
310     * These files have been extracted from finley to define a stand alone libray for iterative
311     * linear solvers on the ALTIX. main entry through Paso_solve. this version compiles but
312     * has not been tested yet.
313     *
314     *
315     */

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26