/[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 2274 - (hide annotations)
Tue Feb 17 04:20:29 2009 UTC (12 years, 8 months ago) by artak
File MIME type: text/plain
File size: 12438 byte(s)
Bug fixed with type overflowing, namely in q_sort pivot and rval variables now unsigned long instead of int. Extra check is added to avoid this in the future.
1 ksteube 1312
2     /*******************************************************
3 ksteube 1811 *
4     * Copyright (c) 2003-2008 by University of Queensland
5     * Earth Systems Science Computational Center (ESSCC)
6     * http://www.uq.edu.au/esscc
7     *
8     * Primary Business: Queensland, Australia
9     * Licensed under the Open Software License version 3.0
10     * http://www.opensource.org/licenses/osl-3.0.php
11     *
12     *******************************************************/
13 dhawcroft 631
14 ksteube 1811
15 jgs 150 /**************************************************************/
16    
17     /* Paso: Matrix Market format is loaded to a SystemMatrix */
18    
19     /**************************************************************/
20    
21     /* Copyrights by ACcESS Australia 2003,2004,2005 */
22     /* Author: imran@access.edu.au */
23    
24     /**************************************************************/
25    
26     #include "Paso.h"
27     #include "mmio.h"
28     #include "SystemMatrix.h"
29    
30 artak 2274 #include "limits.h"
31    
32 ksteube 1887 #define FSCANF_CHECK(scan_ret, reason) { if (scan_ret == EOF) perror(reason); return NULL; }
33    
34 jgs 150 static void swap( index_t*, index_t*, double*, int, int );
35     static void q_sort( index_t*, index_t*, double*, int, int );
36 jfenwick 1981 /*static void print_entries( index_t*, index_t*, double* );*/
37 jgs 150
38     static int M, N, nz;
39    
40    
41     /* debug: print the entries */
42 jfenwick 1981 /*
43 jgs 150 void print_entries( index_t *r, index_t *c, double *v )
44     {
45     int i;
46    
47     for( i=0; i<nz; i++ )
48     {
49     printf( "(%ld, %ld) == %e\n", (long)r[i], (long)c[i], v[i] );
50     }
51     }
52 jfenwick 1981 */
53 jgs 150
54     /* swap function */
55     void swap( index_t *r, index_t *c, double *v, int left, int right )
56     {
57     double v_temp;
58     index_t temp;
59    
60     temp = r[left];
61     r[left] = r[right];
62     r[right] = temp;
63    
64     temp = c[left];
65     c[left] = c[right];
66     c[right] = temp;
67    
68     v_temp = v[left];
69     v[left] = v[right];
70     v[right] = v_temp;
71     }
72    
73     void q_sort( index_t *row, index_t *col, double *val, int begin, int end )
74     {
75     int l, r;
76 artak 2274 unsigned long pivot, lval;
77 jgs 150
78 artak 2274
79 jgs 150 if( end > begin )
80     {
81 artak 2274 pivot = ((unsigned long)N) * row[begin]+col[begin] ;
82 jgs 150 l = begin + 1;
83     r = end;
84    
85     while( l < r )
86     {
87 artak 2274 lval = ((unsigned long)N) * row[l]+col[l];
88 jgs 150 if( lval < pivot )
89     l++;
90     else
91     {
92     r--;
93     swap( row, col, val, l, r );
94     }
95     }
96     l--;
97     swap( row, col, val, begin, l );
98     q_sort( row, col, val, begin, l );
99     q_sort( row, col, val, r, end );
100     }
101     }
102    
103     Paso_SystemMatrix* Paso_SystemMatrix_loadMM_toCSR( char *fileName_p )
104     {
105 ksteube 1312 index_t dist[2];
106     Paso_Distribution* input_dist=NULL, *output_dist=NULL;
107 jgs 150 index_t *col_ind = NULL;
108     index_t *row_ind = NULL;
109     index_t *row_ptr = NULL;
110     double *val = NULL;
111 gross 1028 FILE *fileHandle_p = NULL;
112 ksteube 1312 Paso_Pattern* mainPattern=NULL, *couplePattern=NULL;
113     Paso_SystemMatrixPattern *pattern = NULL;
114 jgs 150 Paso_SystemMatrix *out = NULL;
115 ksteube 1312 Paso_SharedComponents *send =NULL;
116 gross 1552 Paso_Connector *connector=NULL;
117 ksteube 1887 int i, curr_row, scan_ret;
118 ksteube 1312 MM_typecode matrixCode;
119     Paso_MPIInfo* mpi_info=Paso_MPIInfo_alloc( MPI_COMM_WORLD);
120 gross 1028 Paso_resetError();
121 ksteube 1312 if (mpi_info->size >1) {
122     Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: support single processor only");
123     return NULL;
124     }
125 jgs 150 /* open the file */
126 gross 1028 fileHandle_p = fopen( fileName_p, "r" );
127 jgs 150 if( fileHandle_p == NULL )
128     {
129 gross 415 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Cannot read file for reading.");
130 ksteube 1312 Paso_MPIInfo_free(mpi_info);
131 jgs 150 return NULL;
132     }
133    
134     /* process banner */
135     if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
136     {
137 gross 415 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Error processing MM banner.");
138 ksteube 1312 Paso_MPIInfo_free(mpi_info);
139 jgs 150 fclose( fileHandle_p );
140     return NULL;
141     }
142     if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
143     {
144 gross 415
145     Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSR: found Matrix Market type is not supported.");
146 ksteube 1312 Paso_MPIInfo_free(mpi_info);
147 jgs 150 fclose( fileHandle_p );
148     return NULL;
149     }
150    
151     /* get matrix size */
152     if( mm_read_mtx_crd_size(fileHandle_p, &M, &N, &nz) != 0 )
153     {
154 artak 2274 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Could not read sparse matrix size.");
155 ksteube 1312 Paso_MPIInfo_free(mpi_info);
156 jgs 150 fclose( fileHandle_p );
157     return NULL;
158     }
159 artak 2274
160     /* Check whether we can handle current matrix size.
161     In the q_sort algorithm we use N*M+N expression which should be in the limits of "unsigned long".*/
162     if( M>=(ULONG_MAX-N)/N )
163     {
164     Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Matrix size is too big.");
165     Paso_MPIInfo_free(mpi_info);
166     fclose( fileHandle_p );
167     return NULL;
168     }
169 jgs 150
170     /* prepare storage */
171     col_ind = MEMALLOC( nz, index_t );
172     row_ind = MEMALLOC( nz, index_t );
173     val = MEMALLOC( nz, double );
174    
175     row_ptr = MEMALLOC( (M+1), index_t );
176    
177     if( col_ind == NULL || row_ind == NULL || val == NULL || row_ptr == NULL )
178     {
179 gross 415 Paso_setError(MEMORY_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Could not allocate memory" );
180 jgs 150
181 ksteube 1312 Paso_MPIInfo_free(mpi_info);
182 jgs 150 fclose( fileHandle_p );
183     return NULL;
184     }
185    
186     /* perform actual read of elements */
187     for( i=0; i<nz; i++ )
188     {
189 ksteube 1887 scan_ret = fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
190 artak 2144 if (scan_ret!=3)
191     {
192     MEMFREE( val );
193     MEMFREE( row_ind );
194     MEMFREE( col_ind );
195     MEMFREE( row_ptr );
196     Paso_MPIInfo_free(mpi_info);
197     fclose(fileHandle_p);
198     return NULL;
199     }
200 jgs 150 row_ind[i]--;
201     col_ind[i]--;
202     }
203     fclose( fileHandle_p );
204     /* sort the entries */
205     q_sort( row_ind, col_ind, val, 0, nz );
206 artak 2274
207 jgs 150 /* setup row_ptr */
208     curr_row = 0;
209     for( i=0; (i<nz && curr_row<M); curr_row++ )
210     {
211 artak 2274 while( row_ind[i] != curr_row ){
212 jgs 150 i++;
213 artak 2274 }
214 jgs 150 row_ptr[curr_row] = i;
215     }
216     row_ptr[M] = nz;
217    
218 ksteube 1312 /* create return value */
219 jgs 150 /* create F_SMP and F_SM */
220 ksteube 1312 dist[0]=0;
221     dist[1]=M;
222     output_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
223     dist[1]=N;
224     input_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
225 artak 2159 mainPattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,M,N,row_ptr,col_ind);
226 gross 1736 couplePattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,M,N,NULL,NULL);
227 artak 2159 dist[0]=M;
228     send=Paso_SharedComponents_alloc(M,0,NULL,NULL,dist,1,0,mpi_info);
229     dist[0]=0;
230 gross 1552 connector=Paso_Connector_alloc(send,send);
231 ksteube 1312 pattern=Paso_SystemMatrixPattern_alloc(PATTERN_FORMAT_DEFAULT,output_dist,input_dist,
232 gross 1552 mainPattern,couplePattern,couplePattern,connector,connector);
233 jgs 150
234 ksteube 1312 out = Paso_SystemMatrix_alloc(MATRIX_FORMAT_DEFAULT, pattern, 1, 1);
235 jgs 150 /* copy values and cleanup temps */
236 ksteube 1312 for( i=0; i<nz; i++ ) out->mainBlock->val[i] = val[i];
237 jgs 150
238 ksteube 1312 Paso_SystemMatrixPattern_free(pattern);
239     Paso_Pattern_free(mainPattern);
240     Paso_Pattern_free(couplePattern);
241 gross 1552 Paso_Connector_free(connector);
242 ksteube 1312 Paso_Distribution_free(output_dist);
243     Paso_Distribution_free(input_dist);
244     Paso_SharedComponents_free(send);
245     Paso_MPIInfo_free(mpi_info);
246 jgs 150 MEMFREE( val );
247 artak 2159 MEMFREE( row_ind );
248 jgs 150 return out;
249     }
250    
251     Paso_SystemMatrix* Paso_SystemMatrix_loadMM_toCSC( char *fileName_p )
252     {
253 ksteube 1312 index_t dist[2];
254     Paso_Distribution* input_dist=NULL, *output_dist=NULL;
255 gross 1028 FILE *fileHandle_p = NULL;
256 ksteube 1312 Paso_Pattern* mainPattern=NULL, *couplePattern=NULL;
257     Paso_SystemMatrixPattern *pattern = NULL;
258     Paso_SystemMatrix *out = NULL;
259     Paso_SharedComponents *send =NULL;
260 gross 1552 Paso_Connector *connector=NULL;
261 jgs 150 index_t *col_ind = NULL;
262     index_t *row_ind = NULL;
263     index_t *col_ptr = NULL;
264     double *val = NULL;
265 ksteube 1887 int i, curr_col=0, scan_ret;
266 ksteube 1312 MM_typecode matrixCode;
267     Paso_MPIInfo* mpi_info=Paso_MPIInfo_alloc( MPI_COMM_WORLD);
268     if (mpi_info->size >1) {
269     Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSC: support single processor only");
270     return NULL;
271     }
272 jgs 150
273     Paso_resetError();
274    
275     /* open the file */
276 gross 1028 fileHandle_p = fopen( fileName_p, "r" );
277 jgs 150 if( fileHandle_p == NULL )
278     {
279 gross 415 Paso_setError(IO_ERROR,"Paso_SystemMatrix_loadMM_toCSC: File could not be opened for reading");
280 ksteube 1312 Paso_MPIInfo_free(mpi_info);
281 jgs 150 return NULL;
282     }
283    
284     /* process banner */
285     if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
286     {
287 gross 415 Paso_setError(IO_ERROR,"Paso_SystemMatrix_loadMM_toCSC: Error processing MM banner");
288 jgs 150 fclose( fileHandle_p );
289 ksteube 1312 Paso_MPIInfo_free(mpi_info);
290 jgs 150 return NULL;
291     }
292     if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
293     {
294 gross 415 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSC: found Matrix Market type is not supported.");
295 jgs 150 fclose( fileHandle_p );
296 ksteube 1312 Paso_MPIInfo_free(mpi_info);
297 jgs 150 return NULL;
298     }
299    
300     /* get matrix size */
301     if( mm_read_mtx_crd_size(fileHandle_p, &M, &N, &nz) != 0 )
302     {
303 gross 415 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSC: found Matrix Market type is not supported.");
304 jgs 150 fclose( fileHandle_p );
305 ksteube 1312 Paso_MPIInfo_free(mpi_info);
306 jgs 150 return NULL;
307     }
308    
309 artak 2274 /* Check whether we can handle current matrix size.
310     In the q_sort algorithm we use N*M+N expression which should be in the limits of "unsigned long".*/
311     if( M>=(ULONG_MAX-N)/N )
312     {
313     Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSC: Matrix size is too big.");
314     Paso_MPIInfo_free(mpi_info);
315     fclose( fileHandle_p );
316     return NULL;
317     }
318    
319 jgs 150 /* prepare storage */
320     col_ind = MEMALLOC( nz, index_t );
321     row_ind = MEMALLOC( nz, index_t );
322     val = MEMALLOC( nz, double );
323    
324     col_ptr = MEMALLOC( (N+1), index_t );
325    
326    
327     /* perform actual read of elements */
328     for( i=0; i<nz; i++ )
329     {
330 ksteube 1887 scan_ret = fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
331 artak 2144 if (scan_ret!=3)
332     {
333     MEMFREE( val );
334     MEMFREE( row_ind );
335     MEMFREE( col_ind );
336     MEMFREE( col_ptr );
337     Paso_MPIInfo_free(mpi_info);
338     fclose(fileHandle_p);
339     return NULL;
340     }
341 jgs 150 row_ind[i]--;
342     col_ind[i]--;
343     }
344     fclose( fileHandle_p );
345    
346     /* sort the entries */
347     q_sort( col_ind, row_ind, val, 0, nz );
348    
349     /* setup row_ptr */
350     for( i=0; (i<nz && curr_col<N); curr_col++ )
351     {
352     while( col_ind[i] != curr_col )
353     i++;
354     col_ptr[curr_col] = i;
355     }
356     col_ptr[N] = nz;
357    
358     /* create F_SMP and F_SM */
359 ksteube 1312 dist[0]=0;
360     dist[1]=N;
361     output_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
362     dist[1]=M;
363     input_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
364 gross 1736 mainPattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,N,M,col_ptr,col_ind);
365     couplePattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,N,M,NULL,NULL);
366 gross 1562 send=Paso_SharedComponents_alloc(N,0,NULL,NULL,NULL,1,0,mpi_info);
367 gross 1552 connector=Paso_Connector_alloc(send,send);
368 ksteube 1312 pattern=Paso_SystemMatrixPattern_alloc(PATTERN_FORMAT_DEFAULT,output_dist,input_dist,
369 gross 1552 mainPattern,couplePattern,couplePattern,connector,connector);
370 ksteube 1312 out = Paso_SystemMatrix_alloc(MATRIX_FORMAT_CSC, pattern, 1, 1);
371 jgs 150 /* copy values and cleanup temps */
372     for( i=0; i<nz; i++ )
373 ksteube 1312 out->mainBlock->val[i] = val[i];
374 jgs 150
375 ksteube 1312 Paso_SystemMatrixPattern_free(pattern);
376     Paso_Pattern_free(mainPattern);
377     Paso_Pattern_free(couplePattern);
378 gross 1552 Paso_Connector_free(connector);
379 ksteube 1312 Paso_Distribution_free(output_dist);
380     Paso_Distribution_free(input_dist);
381     Paso_SharedComponents_free(send);
382     Paso_MPIInfo_free(mpi_info);
383 jgs 150 MEMFREE( val );
384 artak 2144 MEMFREE( row_ind );
385 jgs 150 return out;
386     }
387 artak 2168
388     void Paso_RHS_loadMM_toCSR( char *fileName_p, double *b, dim_t size)
389     {
390     FILE *fileHandle_p = NULL;
391     int i, scan_ret;
392     MM_typecode matrixCode;
393     Paso_resetError();
394     /* open the file */
395     fileHandle_p = fopen( fileName_p, "r" );
396     if( fileHandle_p == NULL )
397     {
398     Paso_setError(IO_ERROR, "Paso_RHS_loadMM_toCSR: Cannot read file for reading.");
399     }
400    
401     /* process banner */
402     if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
403     {
404     Paso_setError(IO_ERROR, "Paso_RHS_loadMM_toCSR: Error processing MM banner.");
405     }
406     if( !(mm_is_real(matrixCode) && mm_is_general(matrixCode) && mm_is_array(matrixCode)) )
407     {
408    
409     Paso_setError(TYPE_ERROR,"Paso_RHS_loadMM_toCSR: found Matrix Market type is not supported.");
410     }
411    
412     /* get matrix size */
413     if( mm_read_mtx_array_size(fileHandle_p, &M, &N) != 0 )
414     {
415     Paso_setError(IO_ERROR, "Paso_RHS_loadMM_toCSR: Could not read sparse matrix size.");
416     }
417    
418     if(M!=size){
419     Paso_setError(IO_ERROR, "Paso_RHS_loadMM_toCSR: Actual and provided sizes do not match.");
420     }
421    
422     if (Paso_noError()) {
423     nz=M;
424     /* perform actual read of elements */
425     for( i=0; i<nz; i++ )
426     {
427     scan_ret = fscanf( fileHandle_p, "%le\n", &b[i] );
428     if (scan_ret!=1)
429     {
430     fclose(fileHandle_p);
431     Paso_setError(IO_ERROR, "Paso_RHS_loadMM_toCSR: Could not read some of the values.");
432     }
433     }
434     }
435     else {
436     fclose( fileHandle_p );
437     }
438    
439     }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26