/[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 2280 - (show annotations)
Thu Feb 19 03:34:35 2009 UTC (11 years, 5 months ago) by artak
File MIME type: text/plain
File size: 11965 byte(s)
Some statistics changed in solver.c. In AMG stopping criteria now depend on levels specified. In loadMM conditions on pivot and lval reformulated to avoid numerical overflow.
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 int flag;
77
78 if( end > begin )
79 {
80 l = begin + 1;
81 r = end;
82
83 while( l < r )
84 {
85 if (row[l]<row[begin])
86 {
87 if (ABS(row[l]-row[begin])==1 && ABS(col[l]-col[begin])==N)
88 flag=0;
89 else
90 flag=1;
91 }
92 else if (row[l]==row[begin])
93 if (col[l]<col[begin])
94 flag=1;
95 else
96 flag=0;
97 else {
98 if (ABS(row[l]-row[begin])==1 && ABS(col[l]-col[begin])==N)
99 flag=1;
100 else
101 flag=0;
102 }
103
104 if(flag==1)
105 l++;
106 else {
107 r--;
108 swap( row, col, val, l, r );
109 }
110
111 }
112 l--;
113 swap( row, col, val, begin, l );
114 q_sort( row, col, val, begin, l );
115 q_sort( row, col, val, r, end );
116 }
117 }
118
119 Paso_SystemMatrix* Paso_SystemMatrix_loadMM_toCSR( char *fileName_p )
120 {
121 index_t dist[2];
122 Paso_Distribution* input_dist=NULL, *output_dist=NULL;
123 index_t *col_ind = NULL;
124 index_t *row_ind = NULL;
125 index_t *row_ptr = NULL;
126 double *val = NULL;
127 FILE *fileHandle_p = NULL;
128 Paso_Pattern* mainPattern=NULL, *couplePattern=NULL;
129 Paso_SystemMatrixPattern *pattern = NULL;
130 Paso_SystemMatrix *out = NULL;
131 Paso_SharedComponents *send =NULL;
132 Paso_Connector *connector=NULL;
133 int i, curr_row, scan_ret;
134 MM_typecode matrixCode;
135 Paso_MPIInfo* mpi_info=Paso_MPIInfo_alloc( MPI_COMM_WORLD);
136 Paso_resetError();
137 if (mpi_info->size >1) {
138 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: support single processor only");
139 return NULL;
140 }
141 /* open the file */
142 fileHandle_p = fopen( fileName_p, "r" );
143 if( fileHandle_p == NULL )
144 {
145 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Cannot read file for reading.");
146 Paso_MPIInfo_free(mpi_info);
147 return NULL;
148 }
149
150 /* process banner */
151 if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
152 {
153 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Error processing MM banner.");
154 Paso_MPIInfo_free(mpi_info);
155 fclose( fileHandle_p );
156 return NULL;
157 }
158 if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
159 {
160
161 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSR: found Matrix Market type is not supported.");
162 Paso_MPIInfo_free(mpi_info);
163 fclose( fileHandle_p );
164 return NULL;
165 }
166
167 /* get matrix size */
168 if( mm_read_mtx_crd_size(fileHandle_p, &M, &N, &nz) != 0 )
169 {
170 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Could not read sparse matrix size.");
171 Paso_MPIInfo_free(mpi_info);
172 fclose( fileHandle_p );
173 return NULL;
174 }
175
176 /* prepare storage */
177 col_ind = MEMALLOC( nz, index_t );
178 row_ind = MEMALLOC( nz, index_t );
179 val = MEMALLOC( nz, double );
180
181 row_ptr = MEMALLOC( (M+1), index_t );
182
183 if( col_ind == NULL || row_ind == NULL || val == NULL || row_ptr == NULL )
184 {
185 Paso_setError(MEMORY_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Could not allocate memory" );
186
187 Paso_MPIInfo_free(mpi_info);
188 fclose( fileHandle_p );
189 return NULL;
190 }
191
192 /* perform actual read of elements */
193 for( i=0; i<nz; i++ )
194 {
195 scan_ret = fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
196 if (scan_ret!=3)
197 {
198 MEMFREE( val );
199 MEMFREE( row_ind );
200 MEMFREE( col_ind );
201 MEMFREE( row_ptr );
202 Paso_MPIInfo_free(mpi_info);
203 fclose(fileHandle_p);
204 return NULL;
205 }
206 row_ind[i]--;
207 col_ind[i]--;
208 }
209 fclose( fileHandle_p );
210 /* sort the entries */
211 q_sort( row_ind, col_ind, val, 0, nz );
212
213 /* setup row_ptr */
214 curr_row = 0;
215 for( i=0; (i<nz && curr_row<M); curr_row++ )
216 {
217 while( row_ind[i] != curr_row ){
218 i++;
219 }
220 row_ptr[curr_row] = i;
221 }
222 row_ptr[M] = nz;
223
224 /* create return value */
225 /* create F_SMP and F_SM */
226 dist[0]=0;
227 dist[1]=M;
228 output_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
229 dist[1]=N;
230 input_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
231 mainPattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,M,N,row_ptr,col_ind);
232 couplePattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,M,N,NULL,NULL);
233 dist[0]=M;
234 send=Paso_SharedComponents_alloc(M,0,NULL,NULL,dist,1,0,mpi_info);
235 dist[0]=0;
236 connector=Paso_Connector_alloc(send,send);
237 pattern=Paso_SystemMatrixPattern_alloc(PATTERN_FORMAT_DEFAULT,output_dist,input_dist,
238 mainPattern,couplePattern,couplePattern,connector,connector);
239
240 out = Paso_SystemMatrix_alloc(MATRIX_FORMAT_DEFAULT, pattern, 1, 1);
241 /* copy values and cleanup temps */
242 for( i=0; i<nz; i++ ) out->mainBlock->val[i] = val[i];
243
244 Paso_SystemMatrixPattern_free(pattern);
245 Paso_Pattern_free(mainPattern);
246 Paso_Pattern_free(couplePattern);
247 Paso_Connector_free(connector);
248 Paso_Distribution_free(output_dist);
249 Paso_Distribution_free(input_dist);
250 Paso_SharedComponents_free(send);
251 Paso_MPIInfo_free(mpi_info);
252 MEMFREE( val );
253 MEMFREE( row_ind );
254 return out;
255 }
256
257 Paso_SystemMatrix* Paso_SystemMatrix_loadMM_toCSC( char *fileName_p )
258 {
259 index_t dist[2];
260 Paso_Distribution* input_dist=NULL, *output_dist=NULL;
261 FILE *fileHandle_p = NULL;
262 Paso_Pattern* mainPattern=NULL, *couplePattern=NULL;
263 Paso_SystemMatrixPattern *pattern = NULL;
264 Paso_SystemMatrix *out = NULL;
265 Paso_SharedComponents *send =NULL;
266 Paso_Connector *connector=NULL;
267 index_t *col_ind = NULL;
268 index_t *row_ind = NULL;
269 index_t *col_ptr = NULL;
270 double *val = NULL;
271 int i, curr_col=0, scan_ret;
272 MM_typecode matrixCode;
273 Paso_MPIInfo* mpi_info=Paso_MPIInfo_alloc( MPI_COMM_WORLD);
274 if (mpi_info->size >1) {
275 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSC: support single processor only");
276 return NULL;
277 }
278
279 Paso_resetError();
280
281 /* open the file */
282 fileHandle_p = fopen( fileName_p, "r" );
283 if( fileHandle_p == NULL )
284 {
285 Paso_setError(IO_ERROR,"Paso_SystemMatrix_loadMM_toCSC: File could not be opened for reading");
286 Paso_MPIInfo_free(mpi_info);
287 return NULL;
288 }
289
290 /* process banner */
291 if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
292 {
293 Paso_setError(IO_ERROR,"Paso_SystemMatrix_loadMM_toCSC: Error processing MM banner");
294 fclose( fileHandle_p );
295 Paso_MPIInfo_free(mpi_info);
296 return NULL;
297 }
298 if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
299 {
300 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSC: found Matrix Market type is not supported.");
301 fclose( fileHandle_p );
302 Paso_MPIInfo_free(mpi_info);
303 return NULL;
304 }
305
306 /* get matrix size */
307 if( mm_read_mtx_crd_size(fileHandle_p, &M, &N, &nz) != 0 )
308 {
309 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSC: found Matrix Market type is not supported.");
310 fclose( fileHandle_p );
311 Paso_MPIInfo_free(mpi_info);
312 return NULL;
313 }
314
315 /* prepare storage */
316 col_ind = MEMALLOC( nz, index_t );
317 row_ind = MEMALLOC( nz, index_t );
318 val = MEMALLOC( nz, double );
319
320 col_ptr = MEMALLOC( (N+1), index_t );
321
322
323 /* perform actual read of elements */
324 for( i=0; i<nz; i++ )
325 {
326 scan_ret = fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
327 if (scan_ret!=3)
328 {
329 MEMFREE( val );
330 MEMFREE( row_ind );
331 MEMFREE( col_ind );
332 MEMFREE( col_ptr );
333 Paso_MPIInfo_free(mpi_info);
334 fclose(fileHandle_p);
335 return NULL;
336 }
337 row_ind[i]--;
338 col_ind[i]--;
339 }
340 fclose( fileHandle_p );
341
342 /* sort the entries */
343 q_sort( col_ind, row_ind, val, 0, nz );
344
345 /* setup row_ptr */
346 for( i=0; (i<nz && curr_col<N); curr_col++ )
347 {
348 while( col_ind[i] != curr_col )
349 i++;
350 col_ptr[curr_col] = i;
351 }
352 col_ptr[N] = nz;
353
354 /* create F_SMP and F_SM */
355 dist[0]=0;
356 dist[1]=N;
357 output_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
358 dist[1]=M;
359 input_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
360 mainPattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,N,M,col_ptr,col_ind);
361 couplePattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,N,M,NULL,NULL);
362 send=Paso_SharedComponents_alloc(N,0,NULL,NULL,NULL,1,0,mpi_info);
363 connector=Paso_Connector_alloc(send,send);
364 pattern=Paso_SystemMatrixPattern_alloc(PATTERN_FORMAT_DEFAULT,output_dist,input_dist,
365 mainPattern,couplePattern,couplePattern,connector,connector);
366 out = Paso_SystemMatrix_alloc(MATRIX_FORMAT_CSC, pattern, 1, 1);
367 /* copy values and cleanup temps */
368 for( i=0; i<nz; i++ )
369 out->mainBlock->val[i] = val[i];
370
371 Paso_SystemMatrixPattern_free(pattern);
372 Paso_Pattern_free(mainPattern);
373 Paso_Pattern_free(couplePattern);
374 Paso_Connector_free(connector);
375 Paso_Distribution_free(output_dist);
376 Paso_Distribution_free(input_dist);
377 Paso_SharedComponents_free(send);
378 Paso_MPIInfo_free(mpi_info);
379 MEMFREE( val );
380 MEMFREE( row_ind );
381 return out;
382 }
383
384 void Paso_RHS_loadMM_toCSR( char *fileName_p, double *b, dim_t size)
385 {
386 FILE *fileHandle_p = NULL;
387 int i, scan_ret;
388 MM_typecode matrixCode;
389 Paso_resetError();
390 /* open the file */
391 fileHandle_p = fopen( fileName_p, "r" );
392 if( fileHandle_p == NULL )
393 {
394 Paso_setError(IO_ERROR, "Paso_RHS_loadMM_toCSR: Cannot read file for reading.");
395 }
396
397 /* process banner */
398 if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
399 {
400 Paso_setError(IO_ERROR, "Paso_RHS_loadMM_toCSR: Error processing MM banner.");
401 }
402 if( !(mm_is_real(matrixCode) && mm_is_general(matrixCode) && mm_is_array(matrixCode)) )
403 {
404
405 Paso_setError(TYPE_ERROR,"Paso_RHS_loadMM_toCSR: found Matrix Market type is not supported.");
406 }
407
408 /* get matrix size */
409 if( mm_read_mtx_array_size(fileHandle_p, &M, &N) != 0 )
410 {
411 Paso_setError(IO_ERROR, "Paso_RHS_loadMM_toCSR: Could not read sparse matrix size.");
412 }
413
414 if(M!=size){
415 Paso_setError(IO_ERROR, "Paso_RHS_loadMM_toCSR: Actual and provided sizes do not match.");
416 }
417
418 if (Paso_noError()) {
419 nz=M;
420 /* perform actual read of elements */
421 for( i=0; i<nz; i++ )
422 {
423 scan_ret = fscanf( fileHandle_p, "%le\n", &b[i] );
424 if (scan_ret!=1)
425 {
426 fclose(fileHandle_p);
427 Paso_setError(IO_ERROR, "Paso_RHS_loadMM_toCSR: Could not read some of the values.");
428 }
429 }
430 }
431 else {
432 fclose( fileHandle_p );
433 }
434
435 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26