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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2274 - (show annotations)
Tue Feb 17 04:20:29 2009 UTC (10 years, 11 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
2 /*******************************************************
3 *
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
14
15 /**************************************************************/
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 #include "limits.h"
31
32 #define FSCANF_CHECK(scan_ret, reason) { if (scan_ret == EOF) perror(reason); return NULL; }
33
34 static void swap( index_t*, index_t*, double*, int, int );
35 static void q_sort( index_t*, index_t*, double*, int, int );
36 /*static void print_entries( index_t*, index_t*, double* );*/
37
38 static int M, N, nz;
39
40
41 /* debug: print the entries */
42 /*
43 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 */
53
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 unsigned long pivot, lval;
77
78
79 if( end > begin )
80 {
81 pivot = ((unsigned long)N) * row[begin]+col[begin] ;
82 l = begin + 1;
83 r = end;
84
85 while( l < r )
86 {
87 lval = ((unsigned long)N) * row[l]+col[l];
88 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 index_t dist[2];
106 Paso_Distribution* input_dist=NULL, *output_dist=NULL;
107 index_t *col_ind = NULL;
108 index_t *row_ind = NULL;
109 index_t *row_ptr = NULL;
110 double *val = NULL;
111 FILE *fileHandle_p = NULL;
112 Paso_Pattern* mainPattern=NULL, *couplePattern=NULL;
113 Paso_SystemMatrixPattern *pattern = NULL;
114 Paso_SystemMatrix *out = NULL;
115 Paso_SharedComponents *send =NULL;
116 Paso_Connector *connector=NULL;
117 int i, curr_row, scan_ret;
118 MM_typecode matrixCode;
119 Paso_MPIInfo* mpi_info=Paso_MPIInfo_alloc( MPI_COMM_WORLD);
120 Paso_resetError();
121 if (mpi_info->size >1) {
122 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: support single processor only");
123 return NULL;
124 }
125 /* open the file */
126 fileHandle_p = fopen( fileName_p, "r" );
127 if( fileHandle_p == NULL )
128 {
129 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Cannot read file for reading.");
130 Paso_MPIInfo_free(mpi_info);
131 return NULL;
132 }
133
134 /* process banner */
135 if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
136 {
137 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Error processing MM banner.");
138 Paso_MPIInfo_free(mpi_info);
139 fclose( fileHandle_p );
140 return NULL;
141 }
142 if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
143 {
144
145 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSR: found Matrix Market type is not supported.");
146 Paso_MPIInfo_free(mpi_info);
147 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 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Could not read sparse matrix size.");
155 Paso_MPIInfo_free(mpi_info);
156 fclose( fileHandle_p );
157 return NULL;
158 }
159
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
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 Paso_setError(MEMORY_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Could not allocate memory" );
180
181 Paso_MPIInfo_free(mpi_info);
182 fclose( fileHandle_p );
183 return NULL;
184 }
185
186 /* perform actual read of elements */
187 for( i=0; i<nz; i++ )
188 {
189 scan_ret = fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
190 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 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
207 /* setup row_ptr */
208 curr_row = 0;
209 for( i=0; (i<nz && curr_row<M); curr_row++ )
210 {
211 while( row_ind[i] != curr_row ){
212 i++;
213 }
214 row_ptr[curr_row] = i;
215 }
216 row_ptr[M] = nz;
217
218 /* create return value */
219 /* create F_SMP and F_SM */
220 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 mainPattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,M,N,row_ptr,col_ind);
226 couplePattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,M,N,NULL,NULL);
227 dist[0]=M;
228 send=Paso_SharedComponents_alloc(M,0,NULL,NULL,dist,1,0,mpi_info);
229 dist[0]=0;
230 connector=Paso_Connector_alloc(send,send);
231 pattern=Paso_SystemMatrixPattern_alloc(PATTERN_FORMAT_DEFAULT,output_dist,input_dist,
232 mainPattern,couplePattern,couplePattern,connector,connector);
233
234 out = Paso_SystemMatrix_alloc(MATRIX_FORMAT_DEFAULT, pattern, 1, 1);
235 /* copy values and cleanup temps */
236 for( i=0; i<nz; i++ ) out->mainBlock->val[i] = val[i];
237
238 Paso_SystemMatrixPattern_free(pattern);
239 Paso_Pattern_free(mainPattern);
240 Paso_Pattern_free(couplePattern);
241 Paso_Connector_free(connector);
242 Paso_Distribution_free(output_dist);
243 Paso_Distribution_free(input_dist);
244 Paso_SharedComponents_free(send);
245 Paso_MPIInfo_free(mpi_info);
246 MEMFREE( val );
247 MEMFREE( row_ind );
248 return out;
249 }
250
251 Paso_SystemMatrix* Paso_SystemMatrix_loadMM_toCSC( char *fileName_p )
252 {
253 index_t dist[2];
254 Paso_Distribution* input_dist=NULL, *output_dist=NULL;
255 FILE *fileHandle_p = NULL;
256 Paso_Pattern* mainPattern=NULL, *couplePattern=NULL;
257 Paso_SystemMatrixPattern *pattern = NULL;
258 Paso_SystemMatrix *out = NULL;
259 Paso_SharedComponents *send =NULL;
260 Paso_Connector *connector=NULL;
261 index_t *col_ind = NULL;
262 index_t *row_ind = NULL;
263 index_t *col_ptr = NULL;
264 double *val = NULL;
265 int i, curr_col=0, scan_ret;
266 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
273 Paso_resetError();
274
275 /* open the file */
276 fileHandle_p = fopen( fileName_p, "r" );
277 if( fileHandle_p == NULL )
278 {
279 Paso_setError(IO_ERROR,"Paso_SystemMatrix_loadMM_toCSC: File could not be opened for reading");
280 Paso_MPIInfo_free(mpi_info);
281 return NULL;
282 }
283
284 /* process banner */
285 if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
286 {
287 Paso_setError(IO_ERROR,"Paso_SystemMatrix_loadMM_toCSC: Error processing MM banner");
288 fclose( fileHandle_p );
289 Paso_MPIInfo_free(mpi_info);
290 return NULL;
291 }
292 if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
293 {
294 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSC: found Matrix Market type is not supported.");
295 fclose( fileHandle_p );
296 Paso_MPIInfo_free(mpi_info);
297 return NULL;
298 }
299
300 /* get matrix size */
301 if( mm_read_mtx_crd_size(fileHandle_p, &M, &N, &nz) != 0 )
302 {
303 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSC: found Matrix Market type is not supported.");
304 fclose( fileHandle_p );
305 Paso_MPIInfo_free(mpi_info);
306 return NULL;
307 }
308
309 /* 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 /* 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 scan_ret = fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
331 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 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 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 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 send=Paso_SharedComponents_alloc(N,0,NULL,NULL,NULL,1,0,mpi_info);
367 connector=Paso_Connector_alloc(send,send);
368 pattern=Paso_SystemMatrixPattern_alloc(PATTERN_FORMAT_DEFAULT,output_dist,input_dist,
369 mainPattern,couplePattern,couplePattern,connector,connector);
370 out = Paso_SystemMatrix_alloc(MATRIX_FORMAT_CSC, pattern, 1, 1);
371 /* copy values and cleanup temps */
372 for( i=0; i<nz; i++ )
373 out->mainBlock->val[i] = val[i];
374
375 Paso_SystemMatrixPattern_free(pattern);
376 Paso_Pattern_free(mainPattern);
377 Paso_Pattern_free(couplePattern);
378 Paso_Connector_free(connector);
379 Paso_Distribution_free(output_dist);
380 Paso_Distribution_free(input_dist);
381 Paso_SharedComponents_free(send);
382 Paso_MPIInfo_free(mpi_info);
383 MEMFREE( val );
384 MEMFREE( row_ind );
385 return out;
386 }
387
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