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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2281 - (hide annotations)
Thu Feb 19 05:36:38 2009 UTC (11 years, 7 months ago) by artak
File MIME type: text/plain
File size: 12092 byte(s)
minor comments are added
1 ksteube 1312
2     /*******************************************************
3 ksteube 1811 *
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 dhawcroft 631
14 ksteube 1811
15 jgs 150 /**************************************************************/
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 artak 2274 #include "limits.h"
31    
32 ksteube 1887 #define FSCANF_CHECK(scan_ret, reason) { if (scan_ret == EOF) perror(reason); return NULL; }
33    
34 jgs 150 static void swap( index_t*, index_t*, double*, int, int );
35     static void q_sort( index_t*, index_t*, double*, int, int );
36 jfenwick 1981 /*static void print_entries( index_t*, index_t*, double* );*/
37 jgs 150
38     static int M, N, nz;
39    
40    
41     /* debug: print the entries */
42 jfenwick 1981 /*
43 jgs 150 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 jfenwick 1981 */
53 jgs 150
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 artak 2280 int flag;
77    
78 jgs 150 if( end > begin )
79     {
80     l = begin + 1;
81     r = end;
82    
83     while( l < r )
84     {
85 artak 2281 /* This all section is for checking lval<pivot, where
86     pivot=N*row[begin]+col[begin] and lval=N*row[l]+col[l]. */
87 artak 2280 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 artak 2281
107 artak 2280 if(flag==1)
108 jgs 150 l++;
109 artak 2280 else {
110 jgs 150 r--;
111     swap( row, col, val, l, r );
112     }
113 artak 2280
114 jgs 150 }
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 ksteube 1312 index_t dist[2];
125     Paso_Distribution* input_dist=NULL, *output_dist=NULL;
126 jgs 150 index_t *col_ind = NULL;
127     index_t *row_ind = NULL;
128     index_t *row_ptr = NULL;
129     double *val = NULL;
130 gross 1028 FILE *fileHandle_p = NULL;
131 ksteube 1312 Paso_Pattern* mainPattern=NULL, *couplePattern=NULL;
132     Paso_SystemMatrixPattern *pattern = NULL;
133 jgs 150 Paso_SystemMatrix *out = NULL;
134 ksteube 1312 Paso_SharedComponents *send =NULL;
135 gross 1552 Paso_Connector *connector=NULL;
136 ksteube 1887 int i, curr_row, scan_ret;
137 ksteube 1312 MM_typecode matrixCode;
138     Paso_MPIInfo* mpi_info=Paso_MPIInfo_alloc( MPI_COMM_WORLD);
139 gross 1028 Paso_resetError();
140 ksteube 1312 if (mpi_info->size >1) {
141     Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: support single processor only");
142     return NULL;
143     }
144 jgs 150 /* open the file */
145 gross 1028 fileHandle_p = fopen( fileName_p, "r" );
146 jgs 150 if( fileHandle_p == NULL )
147     {
148 gross 415 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Cannot read file for reading.");
149 ksteube 1312 Paso_MPIInfo_free(mpi_info);
150 jgs 150 return NULL;
151     }
152    
153     /* process banner */
154     if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
155     {
156 gross 415 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Error processing MM banner.");
157 ksteube 1312 Paso_MPIInfo_free(mpi_info);
158 jgs 150 fclose( fileHandle_p );
159     return NULL;
160     }
161     if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
162     {
163 gross 415
164     Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSR: found Matrix Market type is not supported.");
165 ksteube 1312 Paso_MPIInfo_free(mpi_info);
166 jgs 150 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 artak 2274 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Could not read sparse matrix size.");
174 ksteube 1312 Paso_MPIInfo_free(mpi_info);
175 jgs 150 fclose( fileHandle_p );
176     return NULL;
177     }
178 artak 2274
179 jgs 150 /* 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 gross 415 Paso_setError(MEMORY_ERROR, "Paso_SystemMatrix_loadMM_toCSR: Could not allocate memory" );
189 jgs 150
190 ksteube 1312 Paso_MPIInfo_free(mpi_info);
191 jgs 150 fclose( fileHandle_p );
192     return NULL;
193     }
194    
195     /* perform actual read of elements */
196     for( i=0; i<nz; i++ )
197     {
198 ksteube 1887 scan_ret = fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
199 artak 2144 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 jgs 150 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 artak 2274
216 jgs 150 /* setup row_ptr */
217     curr_row = 0;
218     for( i=0; (i<nz && curr_row<M); curr_row++ )
219     {
220 artak 2274 while( row_ind[i] != curr_row ){
221 jgs 150 i++;
222 artak 2274 }
223 jgs 150 row_ptr[curr_row] = i;
224     }
225     row_ptr[M] = nz;
226    
227 ksteube 1312 /* create return value */
228 jgs 150 /* create F_SMP and F_SM */
229 ksteube 1312 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 artak 2159 mainPattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,M,N,row_ptr,col_ind);
235 gross 1736 couplePattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,M,N,NULL,NULL);
236 artak 2159 dist[0]=M;
237     send=Paso_SharedComponents_alloc(M,0,NULL,NULL,dist,1,0,mpi_info);
238     dist[0]=0;
239 gross 1552 connector=Paso_Connector_alloc(send,send);
240 ksteube 1312 pattern=Paso_SystemMatrixPattern_alloc(PATTERN_FORMAT_DEFAULT,output_dist,input_dist,
241 gross 1552 mainPattern,couplePattern,couplePattern,connector,connector);
242 jgs 150
243 ksteube 1312 out = Paso_SystemMatrix_alloc(MATRIX_FORMAT_DEFAULT, pattern, 1, 1);
244 jgs 150 /* copy values and cleanup temps */
245 ksteube 1312 for( i=0; i<nz; i++ ) out->mainBlock->val[i] = val[i];
246 jgs 150
247 ksteube 1312 Paso_SystemMatrixPattern_free(pattern);
248     Paso_Pattern_free(mainPattern);
249     Paso_Pattern_free(couplePattern);
250 gross 1552 Paso_Connector_free(connector);
251 ksteube 1312 Paso_Distribution_free(output_dist);
252     Paso_Distribution_free(input_dist);
253     Paso_SharedComponents_free(send);
254     Paso_MPIInfo_free(mpi_info);
255 jgs 150 MEMFREE( val );
256 artak 2159 MEMFREE( row_ind );
257 jgs 150 return out;
258     }
259    
260     Paso_SystemMatrix* Paso_SystemMatrix_loadMM_toCSC( char *fileName_p )
261     {
262 ksteube 1312 index_t dist[2];
263     Paso_Distribution* input_dist=NULL, *output_dist=NULL;
264 gross 1028 FILE *fileHandle_p = NULL;
265 ksteube 1312 Paso_Pattern* mainPattern=NULL, *couplePattern=NULL;
266     Paso_SystemMatrixPattern *pattern = NULL;
267     Paso_SystemMatrix *out = NULL;
268     Paso_SharedComponents *send =NULL;
269 gross 1552 Paso_Connector *connector=NULL;
270 jgs 150 index_t *col_ind = NULL;
271     index_t *row_ind = NULL;
272     index_t *col_ptr = NULL;
273     double *val = NULL;
274 ksteube 1887 int i, curr_col=0, scan_ret;
275 ksteube 1312 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 jgs 150
282     Paso_resetError();
283    
284     /* open the file */
285 gross 1028 fileHandle_p = fopen( fileName_p, "r" );
286 jgs 150 if( fileHandle_p == NULL )
287     {
288 gross 415 Paso_setError(IO_ERROR,"Paso_SystemMatrix_loadMM_toCSC: File could not be opened for reading");
289 ksteube 1312 Paso_MPIInfo_free(mpi_info);
290 jgs 150 return NULL;
291     }
292    
293     /* process banner */
294     if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
295     {
296 gross 415 Paso_setError(IO_ERROR,"Paso_SystemMatrix_loadMM_toCSC: Error processing MM banner");
297 jgs 150 fclose( fileHandle_p );
298 ksteube 1312 Paso_MPIInfo_free(mpi_info);
299 jgs 150 return NULL;
300     }
301     if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
302     {
303 gross 415 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSC: found Matrix Market type is not supported.");
304 jgs 150 fclose( fileHandle_p );
305 ksteube 1312 Paso_MPIInfo_free(mpi_info);
306 jgs 150 return NULL;
307     }
308    
309     /* get matrix size */
310     if( mm_read_mtx_crd_size(fileHandle_p, &M, &N, &nz) != 0 )
311     {
312 gross 415 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSC: found Matrix Market type is not supported.");
313 jgs 150 fclose( fileHandle_p );
314 ksteube 1312 Paso_MPIInfo_free(mpi_info);
315 jgs 150 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 ksteube 1887 scan_ret = fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
330 artak 2144 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 jgs 150 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 ksteube 1312 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 gross 1736 mainPattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,N,M,col_ptr,col_ind);
364     couplePattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,N,M,NULL,NULL);
365 gross 1562 send=Paso_SharedComponents_alloc(N,0,NULL,NULL,NULL,1,0,mpi_info);
366 gross 1552 connector=Paso_Connector_alloc(send,send);
367 ksteube 1312 pattern=Paso_SystemMatrixPattern_alloc(PATTERN_FORMAT_DEFAULT,output_dist,input_dist,
368 gross 1552 mainPattern,couplePattern,couplePattern,connector,connector);
369 ksteube 1312 out = Paso_SystemMatrix_alloc(MATRIX_FORMAT_CSC, pattern, 1, 1);
370 jgs 150 /* copy values and cleanup temps */
371     for( i=0; i<nz; i++ )
372 ksteube 1312 out->mainBlock->val[i] = val[i];
373 jgs 150
374 ksteube 1312 Paso_SystemMatrixPattern_free(pattern);
375     Paso_Pattern_free(mainPattern);
376     Paso_Pattern_free(couplePattern);
377 gross 1552 Paso_Connector_free(connector);
378 ksteube 1312 Paso_Distribution_free(output_dist);
379     Paso_Distribution_free(input_dist);
380     Paso_SharedComponents_free(send);
381     Paso_MPIInfo_free(mpi_info);
382 jgs 150 MEMFREE( val );
383 artak 2144 MEMFREE( row_ind );
384 jgs 150 return out;
385     }
386 artak 2168
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