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

Contents of /trunk/paso/src/SparseMatrix_saveHB.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1315 - (show annotations)
Tue Sep 25 02:41:13 2007 UTC (11 years, 11 months ago) by ksteube
File MIME type: text/plain
File size: 6287 byte(s)
Copied more files from MPI branch to trunk

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

  ViewVC Help
Powered by ViewVC 1.1.26