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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4829 - (show annotations)
Thu Apr 3 04:02:53 2014 UTC (5 years, 5 months ago) by caltinay
File size: 6832 byte(s)
checkpointing some SparseMatrix cleanup.

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

Properties

Name Value
svn:mergeinfo /branches/amg_from_3530/paso/src/SparseMatrix_saveHB.cpp:3531-3826 /branches/lapack2681/paso/src/SparseMatrix_saveHB.cpp:2682-2741 /branches/pasowrap/paso/src/SparseMatrix_saveHB.cpp:3661-3674 /branches/py3_attempt2/paso/src/SparseMatrix_saveHB.cpp:3871-3891 /branches/restext/paso/src/SparseMatrix_saveHB.cpp:2610-2624 /branches/ripleygmg_from_3668/paso/src/SparseMatrix_saveHB.cpp:3669-3791 /branches/stage3.0/paso/src/SparseMatrix_saveHB.cpp:2569-2590 /branches/symbolic_from_3470/paso/src/SparseMatrix_saveHB.cpp:3471-3974 /branches/symbolic_from_3470/ripley/test/python/paso/src/SparseMatrix_saveHB.cpp:3517-3974 /release/3.0/paso/src/SparseMatrix_saveHB.cpp:2591-2601 /trunk/paso/src/SparseMatrix_saveHB.cpp:4257-4344 /trunk/ripley/test/python/paso/src/SparseMatrix_saveHB.cpp:3480-3515

  ViewVC Help
Powered by ViewVC 1.1.26