/[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 1981 - (show annotations)
Thu Nov 6 05:27:33 2008 UTC (11 years, 2 months ago) by jfenwick
File MIME type: text/plain
File size: 9994 byte(s)
More warning removal.

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 FSCANF_CHECK(scan_ret, "fscanf: read elements 1")
178 row_ind[i]--;
179 col_ind[i]--;
180 }
181 fclose( fileHandle_p );
182
183 /* sort the entries */
184 q_sort( row_ind, col_ind, val, 0, nz );
185
186 /* setup row_ptr */
187 curr_row = 0;
188 for( i=0; (i<nz && curr_row<M); curr_row++ )
189 {
190 while( row_ind[i] != curr_row )
191 i++;
192 row_ptr[curr_row] = i;
193 }
194 row_ptr[M] = nz;
195
196 /* create return value */
197 /* create F_SMP and F_SM */
198 dist[0]=0;
199 dist[1]=M;
200 output_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
201 dist[1]=N;
202 input_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
203 mainPattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,M,N,row_ptr,row_ind);
204 couplePattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,M,N,NULL,NULL);
205 send=Paso_SharedComponents_alloc(M,0,NULL,NULL,NULL,1,0,mpi_info);
206 connector=Paso_Connector_alloc(send,send);
207 pattern=Paso_SystemMatrixPattern_alloc(PATTERN_FORMAT_DEFAULT,output_dist,input_dist,
208 mainPattern,couplePattern,couplePattern,connector,connector);
209
210 out = Paso_SystemMatrix_alloc(MATRIX_FORMAT_DEFAULT, pattern, 1, 1);
211 /* copy values and cleanup temps */
212 for( i=0; i<nz; i++ ) out->mainBlock->val[i] = val[i];
213
214 Paso_SystemMatrixPattern_free(pattern);
215 Paso_Pattern_free(mainPattern);
216 Paso_Pattern_free(couplePattern);
217 Paso_Connector_free(connector);
218 Paso_Distribution_free(output_dist);
219 Paso_Distribution_free(input_dist);
220 Paso_SharedComponents_free(send);
221 Paso_MPIInfo_free(mpi_info);
222 MEMFREE( val );
223 MEMFREE( row_ind );
224
225 return out;
226 }
227
228 Paso_SystemMatrix* Paso_SystemMatrix_loadMM_toCSC( char *fileName_p )
229 {
230 index_t dist[2];
231 Paso_Distribution* input_dist=NULL, *output_dist=NULL;
232 FILE *fileHandle_p = NULL;
233 Paso_Pattern* mainPattern=NULL, *couplePattern=NULL;
234 Paso_SystemMatrixPattern *pattern = NULL;
235 Paso_SystemMatrix *out = NULL;
236 Paso_SharedComponents *send =NULL;
237 Paso_Connector *connector=NULL;
238 index_t *col_ind = NULL;
239 index_t *row_ind = NULL;
240 index_t *col_ptr = NULL;
241 double *val = NULL;
242 int i, curr_col=0, scan_ret;
243 MM_typecode matrixCode;
244 Paso_MPIInfo* mpi_info=Paso_MPIInfo_alloc( MPI_COMM_WORLD);
245 if (mpi_info->size >1) {
246 Paso_setError(IO_ERROR, "Paso_SystemMatrix_loadMM_toCSC: support single processor only");
247 return NULL;
248 }
249
250 Paso_resetError();
251
252 /* open the file */
253 fileHandle_p = fopen( fileName_p, "r" );
254 if( fileHandle_p == NULL )
255 {
256 Paso_setError(IO_ERROR,"Paso_SystemMatrix_loadMM_toCSC: File could not be opened for reading");
257 Paso_MPIInfo_free(mpi_info);
258 return NULL;
259 }
260
261 /* process banner */
262 if( mm_read_banner(fileHandle_p, &matrixCode) != 0 )
263 {
264 Paso_setError(IO_ERROR,"Paso_SystemMatrix_loadMM_toCSC: Error processing MM banner");
265 fclose( fileHandle_p );
266 Paso_MPIInfo_free(mpi_info);
267 return NULL;
268 }
269 if( !(mm_is_real(matrixCode) && mm_is_sparse(matrixCode) && mm_is_general(matrixCode)) )
270 {
271 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSC: found Matrix Market type is not supported.");
272 fclose( fileHandle_p );
273 Paso_MPIInfo_free(mpi_info);
274 return NULL;
275 }
276
277 /* get matrix size */
278 if( mm_read_mtx_crd_size(fileHandle_p, &M, &N, &nz) != 0 )
279 {
280 Paso_setError(TYPE_ERROR,"Paso_SystemMatrix_loadMM_toCSC: found Matrix Market type is not supported.");
281 fclose( fileHandle_p );
282 Paso_MPIInfo_free(mpi_info);
283 return NULL;
284 }
285
286 /* prepare storage */
287 col_ind = MEMALLOC( nz, index_t );
288 row_ind = MEMALLOC( nz, index_t );
289 val = MEMALLOC( nz, double );
290
291 col_ptr = MEMALLOC( (N+1), index_t );
292
293
294 /* perform actual read of elements */
295 for( i=0; i<nz; i++ )
296 {
297 scan_ret = fscanf( fileHandle_p, "%d %d %le\n", &row_ind[i], &col_ind[i], &val[i] );
298 FSCANF_CHECK(scan_ret, "fscanf: read elements 2")
299 row_ind[i]--;
300 col_ind[i]--;
301 }
302 fclose( fileHandle_p );
303
304 /* sort the entries */
305 q_sort( col_ind, row_ind, val, 0, nz );
306
307 /* setup row_ptr */
308 for( i=0; (i<nz && curr_col<N); curr_col++ )
309 {
310 while( col_ind[i] != curr_col )
311 i++;
312 col_ptr[curr_col] = i;
313 }
314 col_ptr[N] = nz;
315
316 /* create F_SMP and F_SM */
317 dist[0]=0;
318 dist[1]=N;
319 output_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
320 dist[1]=M;
321 input_dist=Paso_Distribution_alloc(mpi_info, dist,1,0);
322 mainPattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,N,M,col_ptr,col_ind);
323 couplePattern=Paso_Pattern_alloc(PATTERN_FORMAT_DEFAULT,1,1,N,M,NULL,NULL);
324 send=Paso_SharedComponents_alloc(N,0,NULL,NULL,NULL,1,0,mpi_info);
325 connector=Paso_Connector_alloc(send,send);
326 pattern=Paso_SystemMatrixPattern_alloc(PATTERN_FORMAT_DEFAULT,output_dist,input_dist,
327 mainPattern,couplePattern,couplePattern,connector,connector);
328 out = Paso_SystemMatrix_alloc(MATRIX_FORMAT_CSC, pattern, 1, 1);
329 /* copy values and cleanup temps */
330 for( i=0; i<nz; i++ )
331 out->mainBlock->val[i] = val[i];
332
333 Paso_SystemMatrixPattern_free(pattern);
334 Paso_Pattern_free(mainPattern);
335 Paso_Pattern_free(couplePattern);
336 Paso_Connector_free(connector);
337 Paso_Distribution_free(output_dist);
338 Paso_Distribution_free(input_dist);
339 Paso_SharedComponents_free(send);
340 Paso_MPIInfo_free(mpi_info);
341 MEMFREE( val );
342 MEMFREE( col_ind );
343 return out;
344 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26