/[escript]/branches/arrexp_2137_win/paso/src/SparseMatrix_saveHB.c
ViewVC logotype

Contents of /branches/arrexp_2137_win/paso/src/SparseMatrix_saveHB.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2202 - (show annotations)
Fri Jan 9 01:28:32 2009 UTC (10 years, 4 months ago) by jfenwick
File MIME type: text/plain
File size: 6191 byte(s)
Branching the array experiments from version 2137.
The idea is to make the changes required for the c++ changes to compile 
on windows without bringing in the later python changes.


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: SparseMatrix is saved to Harwell-Boeing format */
18
19 /**************************************************************/
20
21 /* Copyright: ACcESS Australia 2005 */
22 /* Author: imran@esscc.uq.edu.au */
23
24 /**************************************************************/
25
26 #include "Paso.h"
27 #include "SparseMatrix.h"
28
29 /* TODO: Refactor the stuff in here into hbio, just like mmio! */
30
31 static dim_t M, N, nz;
32
33 static int calc_digits( int );
34 static void fmt_str( int, int, int*, int*, int*, char*, char* );
35 static void print_data( FILE*, int, int, int, char*, void*, int, int );
36 static void generate_HB( FILE*, dim_t*, dim_t*, double* );
37
38 /* function to get number of digits in an integer */
39 int calc_digits( int var )
40 {
41 int digits = 1;
42 while( (var/=10) )
43 digits++;
44
45 return digits;
46 }
47
48 /* function to generate the format string.
49 *
50 * use maxlen to determine no. of entries per line
51 * use nvalues to determine no. of lines
52 */
53 void fmt_str( int nvalues, int integer, int *width, int *nlines, int *nperline, char *pfmt, char *fmt )
54 {
55 int per_line;
56 int maxlen = *width;
57
58 if( integer && maxlen < 10 )
59 maxlen = 10;
60 else
61 maxlen = 13;
62
63 per_line = 80 / maxlen;
64 *nlines = nvalues / per_line;
65 if( nvalues % per_line )
66 (*nlines)++;
67
68 *nperline = per_line;
69 if( integer )
70 {
71 sprintf( pfmt, "(%dI%d)", per_line, maxlen );
72 sprintf( fmt, "%%%dd", maxlen );
73 }
74 else
75 {
76 sprintf( pfmt, "(1P%dE%d.6)", per_line, maxlen );
77 sprintf( fmt, "%%%d.6E", maxlen );
78 }
79 *width = maxlen;
80 }
81
82 /* function to print the actual data in the right format */
83 void print_data( FILE *fp, int n_perline, int width, int nval, char *fmt, void *ptr, int integer, int adjust )
84 {
85 double *data = ptr;
86 int entries_done = 0;
87 int padding, i;
88 char pad_fmt[10];
89
90 padding = 80 - n_perline*width;
91 sprintf( pad_fmt, "%%%dc", padding );
92
93 if( adjust != 1 )
94 adjust = 0;
95
96 if( integer )
97 {
98 dim_t *data = ptr;
99 for(i=0; i<nval; i++ )
100 {
101 fprintf( fp, fmt, data[i]+adjust );
102 entries_done++;
103 if( entries_done == n_perline )
104 {
105 if( padding )
106 fprintf( fp, pad_fmt, ' ' );
107 fprintf( fp, "\n" );
108 entries_done = 0;
109 }
110 }
111 }
112 else
113 {
114 for(i=0; i<nval; i++ )
115 {
116 fprintf( fp, fmt, data[i] );
117 entries_done++;
118 if( entries_done == n_perline )
119 {
120 if( padding )
121 fprintf( fp, pad_fmt, ' ' );
122 fprintf( fp, "\n" );
123 entries_done = 0;
124 }
125 }
126 }
127 if( entries_done )
128 {
129 sprintf( pad_fmt, "%%%dc\n", (80 - entries_done*width) );
130 fprintf( fp, pad_fmt, ' ' );
131 }
132 }
133
134 void generate_HB( FILE *fp, dim_t *col_ptr, dim_t *row_ind, double *val )
135 {
136 char buffer[81];
137
138 int val_lines, ind_lines, ptr_lines;
139 int val_perline, ind_perline, ptr_perline;
140 int val_width, ind_width, ptr_width;
141 char ptr_pfmt[7], ind_pfmt[7], val_pfmt[11];
142 char ptr_fmt[10], ind_fmt[10], val_fmt[10];
143
144 /* line 1 */
145 sprintf( buffer, "%-72s%-8s", "Matrix Title", "Key" );
146 buffer[80] = '\0';
147 fprintf( fp, "%s\n", buffer );
148
149 /* line 2 */
150 ptr_width = calc_digits( nz+1 );
151 fmt_str( N+1, 1, &ptr_width, &ptr_lines, &ptr_perline, ptr_pfmt, ptr_fmt );
152 ind_width = calc_digits( N );
153 fmt_str( nz, 1, &ind_width, &ind_lines, &ind_perline, ind_pfmt, ind_fmt );
154 val_width = 13;
155 fmt_str( nz, 0, &val_width, &val_lines, &val_perline, val_pfmt, val_fmt );
156 sprintf( buffer, "%14d%14d%14d%14d%14d%10c", (ptr_lines+ind_lines+val_lines), ptr_lines, ind_lines, val_lines, 0, ' ' );
157 buffer[80] = '\0';
158 fprintf( fp, "%s\n", buffer );
159
160 /* line 3 */
161 sprintf( buffer, "%c%c%c%11c%14d%14d%14d%14d%10c", 'R', 'U', 'A', ' ', M, N, nz, 0, ' ' );
162 buffer[80] = '\0';
163 fprintf( fp, "%s\n", buffer );
164
165 /* line 4 */
166 sprintf( buffer, "%16s%16s%20s%28c", ptr_pfmt, ind_pfmt, val_pfmt, ' ');
167 buffer[80]='\0';
168 fprintf( fp, "%s\n", buffer );
169
170 /* line 5 */
171 /* NOT PRESENT */
172
173 /* write the actual data */
174 print_data( fp, ptr_perline, ptr_width, (N+1), ptr_fmt, col_ptr, 1, 1 );
175 print_data( fp, ind_perline, ind_width, nz, ind_fmt, row_ind, 1, 1 );
176 print_data( fp, val_perline, val_width, nz, val_fmt, val, 0, 0 );
177 }
178
179 void Paso_SparseMatrix_saveHB_CSC( Paso_SparseMatrix *A_p, FILE* fileHandle_p ) {
180 int i, curr_col,j ;
181 int iPtr, iCol, ir, ic;
182 index_t index_offset=(A_p->type & MATRIX_FORMAT_OFFSET1 ? 1:0);
183 dim_t *row_ind=NULL, *col_ind = NULL, *col_ptr=NULL;
184 int nz = A_p->len;
185
186 if (A_p->val == NULL) {
187 Paso_setError(TYPE_ERROR,"Paso_SparseMatrix_saveHB_CSC: unsupported format detected.\n");
188 return;
189 }
190
191 if( A_p->row_block_size == 1 && A_p->col_block_size == 1 ) {
192 M = A_p->numRows;
193 N = A_p->numCols;
194 generate_HB( fileHandle_p, A_p->pattern->ptr, A_p->pattern->index, A_p->val );
195 } else {
196 M = A_p->numRows*A_p->row_block_size;
197 N = A_p->numCols*A_p->col_block_size;
198
199 row_ind = MEMALLOC( nz, dim_t );
200 col_ind = MEMALLOC( nz, dim_t );
201
202 i = 0;
203 for( iCol=0; iCol<A_p->pattern->numOutput; iCol++ )
204 for( ic=0; ic<A_p->col_block_size; ic++)
205 for( iPtr=A_p->pattern->ptr[iCol]-index_offset; iPtr<A_p->pattern->ptr[iCol+1]-index_offset; iPtr++)
206 for( ir=0; ir<A_p->row_block_size; ir++ ) {
207 row_ind[i] = (A_p->pattern->index[iPtr]-index_offset)*A_p->row_block_size+ir+1;
208 col_ind[i] = iCol*A_p->col_block_size+ic+1;
209 i++;
210 }
211 /* get the col_ptr */
212 col_ptr = MEMALLOC( (N+1), dim_t );
213
214 curr_col = 0;
215 for(j=0; (j<nz && curr_col<N); curr_col++ ) {
216 while( col_ind[j] != curr_col )
217 j++;
218 col_ptr[curr_col] = j;
219 }
220 col_ptr[N] = nz;
221
222 /* generate the HB file */
223 generate_HB( fileHandle_p, col_ptr, row_ind, A_p->val );
224
225 /* free the allocated memory */
226 MEMFREE( col_ptr );
227 MEMFREE( col_ind );
228 MEMFREE( row_ind );
229 }
230 return;
231 }

  ViewVC Help
Powered by ViewVC 1.1.26