/[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 2144 - (show annotations)
Wed Dec 10 00:31:03 2008 UTC (12 years, 7 months ago) by artak
File MIME type: text/plain
File size: 10265 byte(s)
Some bugs are fixed: FSCANF_CHECK is returned NULL does not matter what. There are still some issues with ..._loadMM.
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 #define FSCANF_CHECK(scan_ret, reason) { if (scan_ret == EOF) perror(reason); return NULL; }
31
32 static void swap( index_t*, index_t*, double*, int, int );
33 static void q_sort( index_t*, index_t*, double*, int, int );
34 /*static void print_entries( index_t*, index_t*, double* );*/
35
36 static int M, N, nz;
37
38
39 /* debug: print the entries */
40 /*
41 void print_entries( index_t *r, index_t *c, double *v )
42 {
43 int i;
44
45 for( i=0; i<nz; i++ )
46 {
47 printf( "(%ld, %ld) == %e\n", (long)r[i], (long)c[i], v[i] );
48 }
49 }
50 */
51
52 /* swap function */
53 void swap( index_t *r, index_t *c, double *v, int left, int right )
54 {
55 double v_temp;
56 index_t temp;
57
58 temp = r[left];
59 r[left] = r[right];
60 r[right] = temp;
61
62 temp = c[left];
63 c[left] = c[right];
64 c[right] = temp;
65
66 v_temp = v[left];
67 v[left] = v[right];
68 v[right] = v_temp;
69 }
70
71 void q_sort( index_t *row, index_t *col, double *val, int begin, int end )
72 {
73 int l, r;
74 index_t pivot, lval;
75
76 if( end > begin )
77 {
78 pivot = N * row[begin] + col[begin];
79 l = begin + 1;
80 r = end;
81
82 while( l < r )
83 {
84 lval = N * row[l] + col[l];
85 if( lval < pivot )
86 l++;
87 else
88 {
89 r--;
90 swap( row, col, val, l, r );
91 }
92 }
93 l--;
94 swap( row, col, val, begin, l );
95 q_sort( row, col, val, begin, l );
96 q_sort( row, col, val, r, end );
97 }
98 }
99
100 Paso_SystemMatrix* Paso_SystemMatrix_loadMM_toCSR( char *fileName_p )
101 {
102 index_t dist[2];
103 Paso_Distribution* input_dist=NULL, *output_dist=NULL;
104 index_t *col_ind = NULL;
105 index_t *row_ind = NULL;
106 index_t *row_ptr = NULL;
107 double *val = NULL;
108 FILE *fileHandle_p = NULL;
109 Paso_Pattern* mainPattern=NULL, *couplePattern=NULL;
110 Paso_SystemMatrixPattern *pattern = NULL;
111 Paso_SystemMatrix *out = NULL;
112 Paso_SharedComponents *send =NULL;
113 Paso_Connector *connector=NULL;
114 int i, curr_row, scan_ret;
115 MM_typecode matrixCode;
116 Paso_MPIInfo* mpi_info=Paso_MPIInfo_alloc( MPI_COMM_WORLD);
117 Paso_resetError();
118 if (mpi_info->size >1) {
119 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: support single processor only");
120 return NULL;
121 }
122 /* open the file */
123 fileHandle_p = fopen( fileName_p, "r" );
124 if( fileHandle_p == NULL )
125 {
126 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Cannot read file for reading.");
127 Paso_MPIInfo_free(mpi_info);
128 return NULL;
129 }
130
131 /* process banner */
132 if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
133 {
134 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Error processing MM banner.");
135 Paso_MPIInfo_free(mpi_info);
136 fclose( fileHandle_p );
137 return NULL;
138 }
139 if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
140 {
141
142 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSR: found Matrix Market type is not supported.");
143 Paso_MPIInfo_free(mpi_info);
144 fclose( fileHandle_p );
145 return NULL;
146 }
147
148 /* get matrix size */
149 if( mm_read_mtx_crd_size(fileHandle_p, &M, &N, &nz) != 0 )
150 {
151 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Could not parse matrix size");
152 Paso_MPIInfo_free(mpi_info);
153 fclose( fileHandle_p );
154 return NULL;
155 }
156
157 /* prepare storage */
158 col_ind = MEMALLOC( nz, index_t );
159 row_ind = MEMALLOC( nz, index_t );
160 val = MEMALLOC( nz, double );
161
162 row_ptr = MEMALLOC( (M+1), index_t );
163
164 if( col_ind == NULL || row_ind == NULL || val == NULL || row_ptr == NULL )
165 {
166 Paso_setError(MEMORY_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Could not allocate memory" );
167
168 Paso_MPIInfo_free(mpi_info);
169 fclose( fileHandle_p );
170 return NULL;
171 }
172
173 /* perform actual read of elements */
174 for( i=0; i<nz; i++ )
175 {
176 scan_ret = fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
177 if (scan_ret!=3)
178 {
179 MEMFREE( val );
180 MEMFREE( row_ind );
181 MEMFREE( col_ind );
182 MEMFREE( row_ptr );
183 Paso_MPIInfo_free(mpi_info);
184 fclose(fileHandle_p);
185 return NULL;
186 }
187 row_ind[i]--;
188 col_ind[i]--;
189 }
190 fclose( fileHandle_p );
191
192 /* sort the entries */
193 q_sort( row_ind, col_ind, val, 0, nz );
194
195 /* setup row_ptr */
196 curr_row = 0;
197 for( i=0; (i<nz && curr_row<M); curr_row++ )
198 {
199 while( row_ind[i] != curr_row )
200 i++;
201 row_ptr[curr_row] = i;
202 }
203 row_ptr[M] = nz;
204
205 /* create return value */
206 /* create F_SMP and F_SM */
207 dist[0]=0;
208 dist[1]=M;
209 output_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
210 dist[1]=N;
211 input_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
212 mainPattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,M,N,row_ptr,row_ind);
213 couplePattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,M,N,NULL,NULL);
214 send=Paso_SharedComponents_alloc(M,0,NULL,NULL,NULL,1,0,mpi_info);
215 connector=Paso_Connector_alloc(send,send);
216 pattern=Paso_SystemMatrixPattern_alloc(PATTERN_FORMAT_DEFAULT,output_dist,input_dist,
217 mainPattern,couplePattern,couplePattern,connector,connector);
218
219 out = Paso_SystemMatrix_alloc(MATRIX_FORMAT_DEFAULT, pattern, 1, 1);
220 /* copy values and cleanup temps */
221 for( i=0; i<nz; i++ ) out->mainBlock->val[i] = val[i];
222
223 Paso_SystemMatrixPattern_free(pattern);
224 Paso_Pattern_free(mainPattern);
225 Paso_Pattern_free(couplePattern);
226 Paso_Connector_free(connector);
227 Paso_Distribution_free(output_dist);
228 Paso_Distribution_free(input_dist);
229 Paso_SharedComponents_free(send);
230 Paso_MPIInfo_free(mpi_info);
231 MEMFREE( val );
232 MEMFREE( col_ind );
233 return out;
234 }
235
236 Paso_SystemMatrix* Paso_SystemMatrix_loadMM_toCSC( char *fileName_p )
237 {
238 index_t dist[2];
239 Paso_Distribution* input_dist=NULL, *output_dist=NULL;
240 FILE *fileHandle_p = NULL;
241 Paso_Pattern* mainPattern=NULL, *couplePattern=NULL;
242 Paso_SystemMatrixPattern *pattern = NULL;
243 Paso_SystemMatrix *out = NULL;
244 Paso_SharedComponents *send =NULL;
245 Paso_Connector *connector=NULL;
246 index_t *col_ind = NULL;
247 index_t *row_ind = NULL;
248 index_t *col_ptr = NULL;
249 double *val = NULL;
250 int i, curr_col=0, scan_ret;
251 MM_typecode matrixCode;
252 Paso_MPIInfo* mpi_info=Paso_MPIInfo_alloc( MPI_COMM_WORLD);
253 if (mpi_info->size >1) {
254 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSC: support single processor only");
255 return NULL;
256 }
257
258 Paso_resetError();
259
260 /* open the file */
261 fileHandle_p = fopen( fileName_p, "r" );
262 if( fileHandle_p == NULL )
263 {
264 Paso_setError(IO_ERROR,"Paso_SystemMatrix_loadMM_toCSC: File could not be opened for reading");
265 Paso_MPIInfo_free(mpi_info);
266 return NULL;
267 }
268
269 /* process banner */
270 if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
271 {
272 Paso_setError(IO_ERROR,"Paso_SystemMatrix_loadMM_toCSC: Error processing MM banner");
273 fclose( fileHandle_p );
274 Paso_MPIInfo_free(mpi_info);
275 return NULL;
276 }
277 if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
278 {
279 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSC: found Matrix Market type is not supported.");
280 fclose( fileHandle_p );
281 Paso_MPIInfo_free(mpi_info);
282 return NULL;
283 }
284
285 /* get matrix size */
286 if( mm_read_mtx_crd_size(fileHandle_p, &M, &N, &nz) != 0 )
287 {
288 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSC: found Matrix Market type is not supported.");
289 fclose( fileHandle_p );
290 Paso_MPIInfo_free(mpi_info);
291 return NULL;
292 }
293
294 /* prepare storage */
295 col_ind = MEMALLOC( nz, index_t );
296 row_ind = MEMALLOC( nz, index_t );
297 val = MEMALLOC( nz, double );
298
299 col_ptr = MEMALLOC( (N+1), index_t );
300
301
302 /* perform actual read of elements */
303 for( i=0; i<nz; i++ )
304 {
305 scan_ret = fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
306 if (scan_ret!=3)
307 {
308 MEMFREE( val );
309 MEMFREE( row_ind );
310 MEMFREE( col_ind );
311 MEMFREE( col_ptr );
312 Paso_MPIInfo_free(mpi_info);
313 fclose(fileHandle_p);
314 return NULL;
315 }
316 row_ind[i]--;
317 col_ind[i]--;
318 }
319 fclose( fileHandle_p );
320
321 /* sort the entries */
322 q_sort( col_ind, row_ind, val, 0, nz );
323
324 /* setup row_ptr */
325 for( i=0; (i<nz && curr_col<N); curr_col++ )
326 {
327 while( col_ind[i] != curr_col )
328 i++;
329 col_ptr[curr_col] = i;
330 }
331 col_ptr[N] = nz;
332
333 /* create F_SMP and F_SM */
334 dist[0]=0;
335 dist[1]=N;
336 output_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
337 dist[1]=M;
338 input_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
339 mainPattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,N,M,col_ptr,col_ind);
340 couplePattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,N,M,NULL,NULL);
341 send=Paso_SharedComponents_alloc(N,0,NULL,NULL,NULL,1,0,mpi_info);
342 connector=Paso_Connector_alloc(send,send);
343 pattern=Paso_SystemMatrixPattern_alloc(PATTERN_FORMAT_DEFAULT,output_dist,input_dist,
344 mainPattern,couplePattern,couplePattern,connector,connector);
345 out = Paso_SystemMatrix_alloc(MATRIX_FORMAT_CSC, pattern, 1, 1);
346 /* copy values and cleanup temps */
347 for( i=0; i<nz; i++ )
348 out->mainBlock->val[i] = val[i];
349
350 Paso_SystemMatrixPattern_free(pattern);
351 Paso_Pattern_free(mainPattern);
352 Paso_Pattern_free(couplePattern);
353 Paso_Connector_free(connector);
354 Paso_Distribution_free(output_dist);
355 Paso_Distribution_free(input_dist);
356 Paso_SharedComponents_free(send);
357 Paso_MPIInfo_free(mpi_info);
358 MEMFREE( val );
359 MEMFREE( row_ind );
360 return out;
361 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26