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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26