/[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 2159 - (show annotations)
Tue Dec 16 03:27:16 2008 UTC (11 years, 2 months ago) by artak
File MIME type: text/plain
File size: 10289 byte(s)
Numerous bags are fixed for loading MatrixMarket files.
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,col_ind);
213 couplePattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,M,N,NULL,NULL);
214 dist[0]=M;
215 send=Paso_SharedComponents_alloc(M,0,NULL,NULL,dist,1,0,mpi_info);
216 dist[0]=0;
217 connector=Paso_Connector_alloc(send,send);
218 pattern=Paso_SystemMatrixPattern_alloc(PATTERN_FORMAT_DEFAULT,output_dist,input_dist,
219 mainPattern,couplePattern,couplePattern,connector,connector);
220
221 out = Paso_SystemMatrix_alloc(MATRIX_FORMAT_DEFAULT, pattern, 1, 1);
222 /* copy values and cleanup temps */
223 for( i=0; i<nz; i++ ) out->mainBlock->val[i] = val[i];
224
225 Paso_SystemMatrixPattern_free(pattern);
226 Paso_Pattern_free(mainPattern);
227 Paso_Pattern_free(couplePattern);
228 Paso_Connector_free(connector);
229 Paso_Distribution_free(output_dist);
230 Paso_Distribution_free(input_dist);
231 Paso_SharedComponents_free(send);
232 Paso_MPIInfo_free(mpi_info);
233 MEMFREE( val );
234 MEMFREE( row_ind );
235 return out;
236 }
237
238 Paso_SystemMatrix* Paso_SystemMatrix_loadMM_toCSC( char *fileName_p )
239 {
240 index_t dist[2];
241 Paso_Distribution* input_dist=NULL, *output_dist=NULL;
242 FILE *fileHandle_p = NULL;
243 Paso_Pattern* mainPattern=NULL, *couplePattern=NULL;
244 Paso_SystemMatrixPattern *pattern = NULL;
245 Paso_SystemMatrix *out = NULL;
246 Paso_SharedComponents *send =NULL;
247 Paso_Connector *connector=NULL;
248 index_t *col_ind = NULL;
249 index_t *row_ind = NULL;
250 index_t *col_ptr = NULL;
251 double *val = NULL;
252 int i, curr_col=0, scan_ret;
253 MM_typecode matrixCode;
254 Paso_MPIInfo* mpi_info=Paso_MPIInfo_alloc( MPI_COMM_WORLD);
255 if (mpi_info->size >1) {
256 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSC: support single processor only");
257 return NULL;
258 }
259
260 Paso_resetError();
261
262 /* open the file */
263 fileHandle_p = fopen( fileName_p, "r" );
264 if( fileHandle_p == NULL )
265 {
266 Paso_setError(IO_ERROR,"Paso_SystemMatrix_loadMM_toCSC: File could not be opened for reading");
267 Paso_MPIInfo_free(mpi_info);
268 return NULL;
269 }
270
271 /* process banner */
272 if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
273 {
274 Paso_setError(IO_ERROR,"Paso_SystemMatrix_loadMM_toCSC: Error processing MM banner");
275 fclose( fileHandle_p );
276 Paso_MPIInfo_free(mpi_info);
277 return NULL;
278 }
279 if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
280 {
281 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSC: found Matrix Market type is not supported.");
282 fclose( fileHandle_p );
283 Paso_MPIInfo_free(mpi_info);
284 return NULL;
285 }
286
287 /* get matrix size */
288 if( mm_read_mtx_crd_size(fileHandle_p, &M, &N, &nz) != 0 )
289 {
290 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSC: found Matrix Market type is not supported.");
291 fclose( fileHandle_p );
292 Paso_MPIInfo_free(mpi_info);
293 return NULL;
294 }
295
296 /* prepare storage */
297 col_ind = MEMALLOC( nz, index_t );
298 row_ind = MEMALLOC( nz, index_t );
299 val = MEMALLOC( nz, double );
300
301 col_ptr = MEMALLOC( (N+1), index_t );
302
303
304 /* perform actual read of elements */
305 for( i=0; i<nz; i++ )
306 {
307 scan_ret = fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
308 if (scan_ret!=3)
309 {
310 MEMFREE( val );
311 MEMFREE( row_ind );
312 MEMFREE( col_ind );
313 MEMFREE( col_ptr );
314 Paso_MPIInfo_free(mpi_info);
315 fclose(fileHandle_p);
316 return NULL;
317 }
318 row_ind[i]--;
319 col_ind[i]--;
320 }
321 fclose( fileHandle_p );
322
323 /* sort the entries */
324 q_sort( col_ind, row_ind, val, 0, nz );
325
326 /* setup row_ptr */
327 for( i=0; (i<nz && curr_col<N); curr_col++ )
328 {
329 while( col_ind[i] != curr_col )
330 i++;
331 col_ptr[curr_col] = i;
332 }
333 col_ptr[N] = nz;
334
335 /* create F_SMP and F_SM */
336 dist[0]=0;
337 dist[1]=N;
338 output_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
339 dist[1]=M;
340 input_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
341 mainPattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,N,M,col_ptr,col_ind);
342 couplePattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,N,M,NULL,NULL);
343 send=Paso_SharedComponents_alloc(N,0,NULL,NULL,NULL,1,0,mpi_info);
344 connector=Paso_Connector_alloc(send,send);
345 pattern=Paso_SystemMatrixPattern_alloc(PATTERN_FORMAT_DEFAULT,output_dist,input_dist,
346 mainPattern,couplePattern,couplePattern,connector,connector);
347 out = Paso_SystemMatrix_alloc(MATRIX_FORMAT_CSC, pattern, 1, 1);
348 /* copy values and cleanup temps */
349 for( i=0; i<nz; i++ )
350 out->mainBlock->val[i] = val[i];
351
352 Paso_SystemMatrixPattern_free(pattern);
353 Paso_Pattern_free(mainPattern);
354 Paso_Pattern_free(couplePattern);
355 Paso_Connector_free(connector);
356 Paso_Distribution_free(output_dist);
357 Paso_Distribution_free(input_dist);
358 Paso_SharedComponents_free(send);
359 Paso_MPIInfo_free(mpi_info);
360 MEMFREE( val );
361 MEMFREE( row_ind );
362 return out;
363 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26