| 1 |
/* |
| 2 |
//@HEADER |
| 3 |
// *********************************************************************** |
| 4 |
// |
| 5 |
// AztecOO: An Object-Oriented Aztec Linear Solver Package |
| 6 |
// Copyright (2002) Sandia Corporation |
| 7 |
// |
| 8 |
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive |
| 9 |
// license for use of this work by or on behalf of the U.S. Government. |
| 10 |
// |
| 11 |
// Redistribution and use in source and binary forms, with or without |
| 12 |
// modification, are permitted provided that the following conditions are |
| 13 |
// met: |
| 14 |
// |
| 15 |
// 1. Redistributions of source code must retain the above copyright |
| 16 |
// notice, this list of conditions and the following disclaimer. |
| 17 |
// |
| 18 |
// 2. Redistributions in binary form must reproduce the above copyright |
| 19 |
// notice, this list of conditions and the following disclaimer in the |
| 20 |
// documentation and/or other materials provided with the distribution. |
| 21 |
// |
| 22 |
// 3. Neither the name of the Corporation nor the names of the |
| 23 |
// contributors may be used to endorse or promote products derived from |
| 24 |
// this software without specific prior written permission. |
| 25 |
// |
| 26 |
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY |
| 27 |
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 28 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 29 |
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE |
| 30 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 31 |
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 32 |
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 33 |
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 34 |
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 35 |
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 36 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 37 |
// |
| 38 |
// Questions? Contact Michael A. Heroux ([email protected]) |
| 39 |
// |
| 40 |
// *********************************************************************** |
| 41 |
//@HEADER |
| 42 |
*/ |
| 43 |
|
| 44 |
/* |
| 45 |
Fri Aug 15 16:29:47 EDT 1997 |
| 46 |
|
| 47 |
Harwell-Boeing File I/O in C |
| 48 |
V. 1.0 |
| 49 |
|
| 50 |
National Institute of Standards and Technology, MD. |
| 51 |
K.A. Remington |
| 52 |
|
| 53 |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 54 |
NOTICE |
| 55 |
|
| 56 |
Permission to use, copy, modify, and distribute this software and |
| 57 |
its documentation for any purpose and without fee is hereby granted |
| 58 |
provided that the above copyright notice appear in all copies and |
| 59 |
that both the copyright notice and this permission notice appear in |
| 60 |
supporting documentation. |
| 61 |
|
| 62 |
Neither the Author nor the Institution (National Institute of Standards |
| 63 |
and Technology) make any representations about the suitability of this |
| 64 |
software for any purpose. This software is provided "as is" without |
| 65 |
expressed or implied warranty. |
| 66 |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 67 |
|
| 68 |
--------------------- |
| 69 |
INTERFACE DESCRIPTION |
| 70 |
--------------------- |
| 71 |
--------------- |
| 72 |
QUERY FUNCTIONS |
| 73 |
--------------- |
| 74 |
|
| 75 |
FUNCTION: |
| 76 |
|
| 77 |
int readHB_info(const char *filename, int *M, int *N, int *nz, |
| 78 |
char **Type, int *Nrhs) |
| 79 |
|
| 80 |
DESCRIPTION: |
| 81 |
|
| 82 |
The readHB_info function opens and reads the header information from |
| 83 |
the specified Harwell-Boeing file, and reports back the number of rows |
| 84 |
and columns in the stored matrix (M and N), the number of nonzeros in |
| 85 |
the matrix (nz), the 3-character matrix type(Type), and the number of |
| 86 |
right-hand-sides stored along with the matrix (Nrhs). This function |
| 87 |
is designed to retrieve basic size information which can be used to |
| 88 |
allocate arrays. |
| 89 |
|
| 90 |
FUNCTION: |
| 91 |
|
| 92 |
int readHB_header(FILE* in_file, char* Title, char* Key, char* Type, |
| 93 |
int* Nrow, int* Ncol, int* Nnzero, int* Nrhs, |
| 94 |
char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt, |
| 95 |
int* Ptrcrd, int* Indcrd, int* Valcrd, int* Rhscrd, |
| 96 |
char *Rhstype) |
| 97 |
|
| 98 |
DESCRIPTION: |
| 99 |
|
| 100 |
More detailed than the readHB_info function, readHB_header() reads from |
| 101 |
the specified Harwell-Boeing file all of the header information. |
| 102 |
|
| 103 |
|
| 104 |
------------------------------ |
| 105 |
DOUBLE PRECISION I/O FUNCTIONS |
| 106 |
------------------------------ |
| 107 |
FUNCTION: |
| 108 |
|
| 109 |
int readHB_newmat_double(const char *filename, int *M, int *N, *int nz, |
| 110 |
int **colptr, int **rowind, double**val) |
| 111 |
|
| 112 |
int readHB_mat_double(const char *filename, int *colptr, int *rowind, |
| 113 |
double*val) |
| 114 |
|
| 115 |
|
| 116 |
DESCRIPTION: |
| 117 |
|
| 118 |
This function opens and reads the specified file, interpreting its |
| 119 |
contents as a sparse matrix stored in the Harwell/Boeing standard |
| 120 |
format. (See readHB_aux_double to read auxiliary vectors.) |
| 121 |
-- Values are interpreted as double precision numbers. -- |
| 122 |
|
| 123 |
The "mat" function uses _pre-allocated_ vectors to hold the index and |
| 124 |
nonzero value information. |
| 125 |
|
| 126 |
The "newmat" function allocates vectors to hold the index and nonzero |
| 127 |
value information, and returns pointers to these vectors along with |
| 128 |
matrix dimension and number of nonzeros. |
| 129 |
|
| 130 |
FUNCTION: |
| 131 |
|
| 132 |
int readHB_aux_double(const char* filename, const char AuxType, double b[]) |
| 133 |
|
| 134 |
int readHB_newaux_double(const char* filename, const char AuxType, double** b) |
| 135 |
|
| 136 |
DESCRIPTION: |
| 137 |
|
| 138 |
This function opens and reads from the specified file auxiliary vector(s). |
| 139 |
The char argument Auxtype determines which type of auxiliary vector(s) |
| 140 |
will be read (if present in the file). |
| 141 |
|
| 142 |
AuxType = 'F' right-hand-side |
| 143 |
AuxType = 'G' initial estimate (Guess) |
| 144 |
AuxType = 'X' eXact solution |
| 145 |
|
| 146 |
If Nrhs > 1, all of the Nrhs vectors of the given type are read and |
| 147 |
stored in column-major order in the vector b. |
| 148 |
|
| 149 |
The "newaux" function allocates a vector to hold the values retrieved. |
| 150 |
The "mat" function uses a _pre-allocated_ vector to hold the values. |
| 151 |
|
| 152 |
FUNCTION: |
| 153 |
|
| 154 |
int writeHB_mat_double(const char* filename, int M, int N, |
| 155 |
int nz, const int colptr[], const int rowind[], |
| 156 |
const double val[], int Nrhs, const double rhs[], |
| 157 |
const double guess[], const double exact[], |
| 158 |
const char* Title, const char* Key, const char* Type, |
| 159 |
char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt, |
| 160 |
const char* Rhstype) |
| 161 |
|
| 162 |
DESCRIPTION: |
| 163 |
|
| 164 |
The writeHB_mat_double function opens the named file and writes the specified |
| 165 |
matrix and optional auxiliary vector(s) to that file in Harwell-Boeing |
| 166 |
format. The format arguments (Ptrfmt,Indfmt,Valfmt, and Rhsfmt) are |
| 167 |
character strings specifying "Fortran-style" output formats -- as they |
| 168 |
would appear in a Harwell-Boeing file. They are used to produce output |
| 169 |
which is as close as possible to what would be produced by Fortran code, |
| 170 |
but note that "D" and "P" edit descriptors are not supported. |
| 171 |
If NULL, the following defaults will be used: |
| 172 |
Ptrfmt = Indfmt = "(8I10)" |
| 173 |
Valfmt = Rhsfmt = "(4E20.13)" |
| 174 |
|
| 175 |
----------------------- |
| 176 |
CHARACTER I/O FUNCTIONS |
| 177 |
----------------------- |
| 178 |
FUNCTION: |
| 179 |
|
| 180 |
int readHB_mat_char(const char* filename, int colptr[], int rowind[], |
| 181 |
char val[], char* Valfmt) |
| 182 |
int readHB_newmat_char(const char* filename, int* M, int* N, int* nonzeros, |
| 183 |
int** colptr, int** rowind, char** val, char** Valfmt) |
| 184 |
|
| 185 |
DESCRIPTION: |
| 186 |
|
| 187 |
This function opens and reads the specified file, interpreting its |
| 188 |
contents as a sparse matrix stored in the Harwell/Boeing standard |
| 189 |
format. (See readHB_aux_char to read auxiliary vectors.) |
| 190 |
-- Values are interpreted as char strings. -- |
| 191 |
(Used to translate exact values from the file into a new storage format.) |
| 192 |
|
| 193 |
The "mat" function uses _pre-allocated_ arrays to hold the index and |
| 194 |
nonzero value information. |
| 195 |
|
| 196 |
The "newmat" function allocates char arrays to hold the index |
| 197 |
and nonzero value information, and returns pointers to these arrays |
| 198 |
along with matrix dimension and number of nonzeros. |
| 199 |
|
| 200 |
FUNCTION: |
| 201 |
|
| 202 |
int readHB_aux_char(const char* filename, const char AuxType, char b[]) |
| 203 |
int readHB_newaux_char(const char* filename, const char AuxType, char** b, |
| 204 |
char** Rhsfmt) |
| 205 |
|
| 206 |
DESCRIPTION: |
| 207 |
|
| 208 |
This function opens and reads from the specified file auxiliary vector(s). |
| 209 |
The char argument Auxtype determines which type of auxiliary vector(s) |
| 210 |
will be read (if present in the file). |
| 211 |
|
| 212 |
AuxType = 'F' right-hand-side |
| 213 |
AuxType = 'G' initial estimate (Guess) |
| 214 |
AuxType = 'X' eXact solution |
| 215 |
|
| 216 |
If Nrhs > 1, all of the Nrhs vectors of the given type are read and |
| 217 |
stored in column-major order in the vector b. |
| 218 |
|
| 219 |
The "newaux" function allocates a character array to hold the values |
| 220 |
retrieved. |
| 221 |
The "mat" function uses a _pre-allocated_ array to hold the values. |
| 222 |
|
| 223 |
FUNCTION: |
| 224 |
|
| 225 |
int writeHB_mat_char(const char* filename, int M, int N, |
| 226 |
int nz, const int colptr[], const int rowind[], |
| 227 |
const char val[], int Nrhs, const char rhs[], |
| 228 |
const char guess[], const char exact[], |
| 229 |
const char* Title, const char* Key, const char* Type, |
| 230 |
char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt, |
| 231 |
const char* Rhstype) |
| 232 |
|
| 233 |
DESCRIPTION: |
| 234 |
|
| 235 |
The writeHB_mat_char function opens the named file and writes the specified |
| 236 |
matrix and optional auxiliary vector(s) to that file in Harwell-Boeing |
| 237 |
format. The format arguments (Ptrfmt,Indfmt,Valfmt, and Rhsfmt) are |
| 238 |
character strings specifying "Fortran-style" output formats -- as they |
| 239 |
would appear in a Harwell-Boeing file. Valfmt and Rhsfmt must accurately |
| 240 |
represent the character representation of the values stored in val[] |
| 241 |
and rhs[]. |
| 242 |
|
| 243 |
If NULL, the following defaults will be used for the integer vectors: |
| 244 |
Ptrfmt = Indfmt = "(8I10)" |
| 245 |
Valfmt = Rhsfmt = "(4E20.13)" |
| 246 |
|
| 247 |
|
| 248 |
*/ |
| 249 |
|
| 250 |
/*---------------------------------------------------------------------*/ |
| 251 |
/* If zero-based indexing is desired, _SP_base should be set to 0 */ |
| 252 |
/* This will cause indices read from H-B files to be decremented by 1 */ |
| 253 |
/* and indices written to H-B files to be incremented by 1 */ |
| 254 |
/* <<< Standard usage is _SP_base = 1 >>> */ |
| 255 |
#ifndef _SP_base |
| 256 |
#define _SP_base 1 |
| 257 |
#endif |
| 258 |
/*---------------------------------------------------------------------*/ |
| 259 |
|
| 260 |
#include "iohb.h" |
| 261 |
#include<stdio.h> |
| 262 |
#include<stdlib.h> |
| 263 |
#include<string.h> |
| 264 |
#include<math.h> |
| 265 |
#include<malloc.h> |
| 266 |
|
| 267 |
char* substr(const char* S, const int pos, const int len); |
| 268 |
void upcase(char* S); |
| 269 |
void IOHBTerminate(char* message); |
| 270 |
|
| 271 |
int readHB_info(const char* filename, int* M, int* N, int* nz, char** Type, |
| 272 |
int* Nrhs) |
| 273 |
{ |
| 274 |
/****************************************************************************/ |
| 275 |
/* The readHB_info function opens and reads the header information from */ |
| 276 |
/* the specified Harwell-Boeing file, and reports back the number of rows */ |
| 277 |
/* and columns in the stored matrix (M and N), the number of nonzeros in */ |
| 278 |
/* the matrix (nz), and the number of right-hand-sides stored along with */ |
| 279 |
/* the matrix (Nrhs). */ |
| 280 |
/* */ |
| 281 |
/* For a description of the Harwell Boeing standard, see: */ |
| 282 |
/* Duff, et al., ACM TOMS Vol.15, No.1, March 1989 */ |
| 283 |
/* */ |
| 284 |
/* ---------- */ |
| 285 |
/* **CAVEAT** */ |
| 286 |
/* ---------- */ |
| 287 |
/* ** If the input file does not adhere to the H/B format, the ** */ |
| 288 |
/* ** results will be unpredictable. ** */ |
| 289 |
/* */ |
| 290 |
/****************************************************************************/ |
| 291 |
FILE *in_file; |
| 292 |
int Ptrcrd, Indcrd, Valcrd, Rhscrd; |
| 293 |
int Nrow, Ncol, Nnzero; |
| 294 |
char *mat_type; |
| 295 |
char Title[73], Key[9], Rhstype[4]; |
| 296 |
char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21]; |
| 297 |
|
| 298 |
mat_type = (char *) malloc(4); |
| 299 |
if ( mat_type == NULL ) IOHBTerminate("Insufficient memory for mat_typen"); |
| 300 |
|
| 301 |
if ( (in_file = fopen( filename, "r")) == NULL ) { |
| 302 |
fprintf(stderr,"Error: Cannot open file: %s\n",filename); |
| 303 |
return 0; |
| 304 |
} |
| 305 |
|
| 306 |
readHB_header(in_file, Title, Key, mat_type, &Nrow, &Ncol, &Nnzero, Nrhs, |
| 307 |
Ptrfmt, Indfmt, Valfmt, Rhsfmt, |
| 308 |
&Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype); |
| 309 |
fclose(in_file); |
| 310 |
*Type = mat_type; |
| 311 |
*(*Type+3) = (char) NULL; |
| 312 |
*M = Nrow; |
| 313 |
*N = Ncol; |
| 314 |
*nz = Nnzero; |
| 315 |
if (Rhscrd == 0) {*Nrhs = 0;} |
| 316 |
|
| 317 |
/* In verbose mode, print some of the header information: */ |
| 318 |
/* |
| 319 |
if (verbose == 1) |
| 320 |
{ |
| 321 |
printf("Reading from Harwell-Boeing file %s (verbose on)...\n",filename); |
| 322 |
printf(" Title: %s\n",Title); |
| 323 |
printf(" Key: %s\n",Key); |
| 324 |
printf(" The stored matrix is %i by %i with %i nonzeros.\n", |
| 325 |
*M, *N, *nz ); |
| 326 |
printf(" %i right-hand--side(s) stored.\n",*Nrhs); |
| 327 |
} |
| 328 |
*/ |
| 329 |
|
| 330 |
return 1; |
| 331 |
|
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
int readHB_header(FILE* in_file, char* Title, char* Key, char* Type, |
| 337 |
int* Nrow, int* Ncol, int* Nnzero, int* Nrhs, |
| 338 |
char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt, |
| 339 |
int* Ptrcrd, int* Indcrd, int* Valcrd, int* Rhscrd, |
| 340 |
char *Rhstype) |
| 341 |
{ |
| 342 |
/*************************************************************************/ |
| 343 |
/* Read header information from the named H/B file... */ |
| 344 |
/*************************************************************************/ |
| 345 |
int Totcrd,Neltvl,Nrhsix; |
| 346 |
char line[BUFSIZ]; |
| 347 |
|
| 348 |
/* First line: */ |
| 349 |
fgets(line, BUFSIZ, in_file); |
| 350 |
if ( sscanf(line,"%*s") < 0 ) |
| 351 |
IOHBTerminate("iohb.c: Null (or blank) first line of HB file.\n"); |
| 352 |
(void) sscanf(line, "%72c%8[^\n]", Title, Key); |
| 353 |
*(Key+8) = (char) NULL; |
| 354 |
*(Title+72) = (char) NULL; |
| 355 |
|
| 356 |
/* Second line: */ |
| 357 |
fgets(line, BUFSIZ, in_file); |
| 358 |
if ( sscanf(line,"%*s") < 0 ) |
| 359 |
IOHBTerminate("iohb.c: Null (or blank) second line of HB file.\n"); |
| 360 |
if ( sscanf(line,"%i",&Totcrd) != 1) Totcrd = 0; |
| 361 |
if ( sscanf(line,"%*i%i",Ptrcrd) != 1) *Ptrcrd = 0; |
| 362 |
if ( sscanf(line,"%*i%*i%i",Indcrd) != 1) *Indcrd = 0; |
| 363 |
if ( sscanf(line,"%*i%*i%*i%i",Valcrd) != 1) *Valcrd = 0; |
| 364 |
if ( sscanf(line,"%*i%*i%*i%*i%i",Rhscrd) != 1) *Rhscrd = 0; |
| 365 |
|
| 366 |
/* Third line: */ |
| 367 |
fgets(line, BUFSIZ, in_file); |
| 368 |
if ( sscanf(line,"%*s") < 0 ) |
| 369 |
IOHBTerminate("iohb.c: Null (or blank) third line of HB file.\n"); |
| 370 |
if ( sscanf(line, "%3c", Type) != 1) |
| 371 |
IOHBTerminate("iohb.c: Invalid Type info, line 3 of Harwell-Boeing file.\n"); |
| 372 |
upcase(Type); |
| 373 |
if ( sscanf(line,"%*3c%i",Nrow) != 1) *Nrow = 0 ; |
| 374 |
if ( sscanf(line,"%*3c%*i%i",Ncol) != 1) *Ncol = 0 ; |
| 375 |
if ( sscanf(line,"%*3c%*i%*i%i",Nnzero) != 1) *Nnzero = 0 ; |
| 376 |
if ( sscanf(line,"%*3c%*i%*i%*i%i",&Neltvl) != 1) Neltvl = 0 ; |
| 377 |
|
| 378 |
/* Fourth line: */ |
| 379 |
fgets(line, BUFSIZ, in_file); |
| 380 |
if ( sscanf(line,"%*s") < 0 ) |
| 381 |
IOHBTerminate("iohb.c: Null (or blank) fourth line of HB file.\n"); |
| 382 |
if ( sscanf(line, "%16c",Ptrfmt) != 1) |
| 383 |
IOHBTerminate("iohb.c: Invalid format info, line 4 of Harwell-Boeing file.\n"); |
| 384 |
if ( sscanf(line, "%*16c%16c",Indfmt) != 1) |
| 385 |
IOHBTerminate("iohb.c: Invalid format info, line 4 of Harwell-Boeing file.\n"); |
| 386 |
if ( sscanf(line, "%*16c%*16c%20c",Valfmt) != 1) |
| 387 |
IOHBTerminate("iohb.c: Invalid format info, line 4 of Harwell-Boeing file.\n"); |
| 388 |
sscanf(line, "%*16c%*16c%*20c%20c",Rhsfmt); |
| 389 |
*(Ptrfmt+16) = (char) NULL; |
| 390 |
*(Indfmt+16) = (char) NULL; |
| 391 |
*(Valfmt+20) = (char) NULL; |
| 392 |
*(Rhsfmt+20) = (char) NULL; |
| 393 |
|
| 394 |
/* (Optional) Fifth line: */ |
| 395 |
if (*Rhscrd != 0 ) |
| 396 |
{ |
| 397 |
fgets(line, BUFSIZ, in_file); |
| 398 |
if ( sscanf(line,"%*s") < 0 ) |
| 399 |
IOHBTerminate("iohb.c: Null (or blank) fifth line of HB file.\n"); |
| 400 |
if ( sscanf(line, "%3c", Rhstype) != 1) |
| 401 |
IOHBTerminate("iohb.c: Invalid RHS type information, line 5 of Harwell-Boeing file.\n"); |
| 402 |
if ( sscanf(line, "%*3c%i", Nrhs) != 1) *Nrhs = 0; |
| 403 |
if ( sscanf(line, "%*3c%*i%i", &Nrhsix) != 1) Nrhsix = 0; |
| 404 |
} |
| 405 |
return 1; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
int readHB_mat_double(const char* filename, int colptr[], int rowind[], |
| 410 |
double val[]) |
| 411 |
{ |
| 412 |
/****************************************************************************/ |
| 413 |
/* This function opens and reads the specified file, interpreting its */ |
| 414 |
/* contents as a sparse matrix stored in the Harwell/Boeing standard */ |
| 415 |
/* format and creating compressed column storage scheme vectors to hold */ |
| 416 |
/* the index and nonzero value information. */ |
| 417 |
/* */ |
| 418 |
/* ---------- */ |
| 419 |
/* **CAVEAT** */ |
| 420 |
/* ---------- */ |
| 421 |
/* Parsing real formats from Fortran is tricky, and this file reader */ |
| 422 |
/* does not claim to be foolproof. It has been tested for cases when */ |
| 423 |
/* the real values are printed consistently and evenly spaced on each */ |
| 424 |
/* line, with Fixed (F), and Exponential (E or D) formats. */ |
| 425 |
/* */ |
| 426 |
/* ** If the input file does not adhere to the H/B format, the ** */ |
| 427 |
/* ** results will be unpredictable. ** */ |
| 428 |
/* */ |
| 429 |
/****************************************************************************/ |
| 430 |
FILE *in_file; |
| 431 |
int i,j,ind,col,offset,count,last,Nrhs; |
| 432 |
int Ptrcrd, Indcrd, Valcrd, Rhscrd; |
| 433 |
int Nrow, Ncol, Nnzero, Nentries; |
| 434 |
int Ptrperline, Ptrwidth, Indperline, Indwidth; |
| 435 |
int Valperline, Valwidth, Valprec; |
| 436 |
int Valflag; /* Indicates 'E','D', or 'F' float format */ |
| 437 |
char* ThisElement; |
| 438 |
char Title[73], Key[8], Type[4] = "XXX\0", Rhstype[4]; |
| 439 |
char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21]; |
| 440 |
char line[BUFSIZ]; |
| 441 |
|
| 442 |
if ( (in_file = fopen( filename, "r")) == NULL ) { |
| 443 |
fprintf(stderr,"Error: Cannot open file: %s\n",filename); |
| 444 |
return 0; |
| 445 |
} |
| 446 |
|
| 447 |
readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs, |
| 448 |
Ptrfmt, Indfmt, Valfmt, Rhsfmt, |
| 449 |
&Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype); |
| 450 |
|
| 451 |
/* Parse the array input formats from Line 3 of HB file */ |
| 452 |
ParseIfmt(Ptrfmt,&Ptrperline,&Ptrwidth); |
| 453 |
ParseIfmt(Indfmt,&Indperline,&Indwidth); |
| 454 |
if ( Type[0] != 'P' ) { /* Skip if pattern only */ |
| 455 |
ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag); |
| 456 |
} |
| 457 |
|
| 458 |
/* Read column pointer array: */ |
| 459 |
|
| 460 |
offset = 1-_SP_base; /* if base 0 storage is declared (via macro definition), */ |
| 461 |
/* then storage entries are offset by 1 */ |
| 462 |
|
| 463 |
ThisElement = (char *) malloc(Ptrwidth+1); |
| 464 |
if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement."); |
| 465 |
*(ThisElement+Ptrwidth) = (char) NULL; |
| 466 |
count=0; |
| 467 |
for (i=0;i<Ptrcrd;i++) |
| 468 |
{ |
| 469 |
fgets(line, BUFSIZ, in_file); |
| 470 |
if ( sscanf(line,"%*s") < 0 ) |
| 471 |
IOHBTerminate("iohb.c: Null (or blank) line in pointer data region of HB file.\n"); |
| 472 |
col = 0; |
| 473 |
for (ind = 0;ind<Ptrperline;ind++) |
| 474 |
{ |
| 475 |
if (count > Ncol) break; |
| 476 |
strncpy(ThisElement,line+col,Ptrwidth); |
| 477 |
/* ThisElement = substr(line,col,Ptrwidth); */ |
| 478 |
colptr[count] = atoi(ThisElement)-offset; |
| 479 |
count++; col += Ptrwidth; |
| 480 |
} |
| 481 |
} |
| 482 |
free(ThisElement); |
| 483 |
|
| 484 |
/* Read row index array: */ |
| 485 |
|
| 486 |
ThisElement = (char *) malloc(Indwidth+1); |
| 487 |
if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement."); |
| 488 |
*(ThisElement+Indwidth) = (char) NULL; |
| 489 |
count = 0; |
| 490 |
for (i=0;i<Indcrd;i++) |
| 491 |
{ |
| 492 |
fgets(line, BUFSIZ, in_file); |
| 493 |
if ( sscanf(line,"%*s") < 0 ) |
| 494 |
IOHBTerminate("iohb.c: Null (or blank) line in index data region of HB file.\n"); |
| 495 |
col = 0; |
| 496 |
for (ind = 0;ind<Indperline;ind++) |
| 497 |
{ |
| 498 |
if (count == Nnzero) break; |
| 499 |
strncpy(ThisElement,line+col,Indwidth); |
| 500 |
/* ThisElement = substr(line,col,Indwidth); */ |
| 501 |
rowind[count] = atoi(ThisElement)-offset; |
| 502 |
count++; col += Indwidth; |
| 503 |
} |
| 504 |
} |
| 505 |
free(ThisElement); |
| 506 |
|
| 507 |
/* Read array of values: */ |
| 508 |
|
| 509 |
if ( Type[0] != 'P' ) { /* Skip if pattern only */ |
| 510 |
|
| 511 |
if ( Type[0] == 'C' ) Nentries = 2*Nnzero; |
| 512 |
else Nentries = Nnzero; |
| 513 |
|
| 514 |
ThisElement = (char *) malloc(Valwidth+1); |
| 515 |
if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement."); |
| 516 |
*(ThisElement+Valwidth) = (char) NULL; |
| 517 |
count = 0; |
| 518 |
for (i=0;i<Valcrd;i++) |
| 519 |
{ |
| 520 |
fgets(line, BUFSIZ, in_file); |
| 521 |
if ( sscanf(line,"%*s") < 0 ) |
| 522 |
IOHBTerminate("iohb.c: Null (or blank) line in value data region of HB file.\n"); |
| 523 |
if (Valflag == 'D') { |
| 524 |
while( strchr(line,'D') ) *strchr(line,'D') = 'E'; |
| 525 |
/* *strchr(Valfmt,'D') = 'E'; */ |
| 526 |
} |
| 527 |
col = 0; |
| 528 |
for (ind = 0;ind<Valperline;ind++) |
| 529 |
{ |
| 530 |
if (count == Nentries) break; |
| 531 |
strncpy(ThisElement,line+col,Valwidth); |
| 532 |
/*ThisElement = substr(line,col,Valwidth);*/ |
| 533 |
if ( Valflag != 'F' && strchr(ThisElement,'E') == NULL ) { |
| 534 |
/* insert a char prefix for exp */ |
| 535 |
last = strlen(ThisElement); |
| 536 |
for (j=last+1;j>=0;j--) { |
| 537 |
ThisElement[j] = ThisElement[j-1]; |
| 538 |
if ( ThisElement[j] == '+' || ThisElement[j] == '-' ) { |
| 539 |
ThisElement[j-1] = Valflag; |
| 540 |
break; |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
val[count] = atof(ThisElement); |
| 545 |
count++; col += Valwidth; |
| 546 |
} |
| 547 |
} |
| 548 |
free(ThisElement); |
| 549 |
} |
| 550 |
|
| 551 |
fclose(in_file); |
| 552 |
return 1; |
| 553 |
} |
| 554 |
|
| 555 |
int readHB_newmat_double(const char* filename, int* M, int* N, int* nonzeros, |
| 556 |
int** colptr, int** rowind, double** val) |
| 557 |
{ |
| 558 |
int Nrhs; |
| 559 |
char *Type; |
| 560 |
|
| 561 |
readHB_info(filename, M, N, nonzeros, &Type, &Nrhs); |
| 562 |
|
| 563 |
*colptr = (int *)malloc((*N+1)*sizeof(int)); |
| 564 |
if ( *colptr == NULL ) IOHBTerminate("Insufficient memory for colptr.\n"); |
| 565 |
*rowind = (int *)malloc(*nonzeros*sizeof(int)); |
| 566 |
if ( *rowind == NULL ) IOHBTerminate("Insufficient memory for rowind.\n"); |
| 567 |
if ( Type[0] == 'C' ) { |
| 568 |
/* |
| 569 |
fprintf(stderr, "Warning: Reading complex data from HB file %s.\n",filename); |
| 570 |
fprintf(stderr, " Real and imaginary parts will be interlaced in val[].\n"); |
| 571 |
*/ |
| 572 |
/* Malloc enough space for real AND imaginary parts of val[] */ |
| 573 |
*val = (double *)malloc(*nonzeros*sizeof(double)*2); |
| 574 |
if ( *val == NULL ) IOHBTerminate("Insufficient memory for val.\n"); |
| 575 |
} else { |
| 576 |
if ( Type[0] != 'P' ) { |
| 577 |
/* Malloc enough space for real array val[] */ |
| 578 |
*val = (double *)malloc(*nonzeros*sizeof(double)); |
| 579 |
if ( *val == NULL ) IOHBTerminate("Insufficient memory for val.\n"); |
| 580 |
} |
| 581 |
} /* No val[] space needed if pattern only */ |
| 582 |
return readHB_mat_double(filename, *colptr, *rowind, *val); |
| 583 |
|
| 584 |
} |
| 585 |
|
| 586 |
int readHB_aux_double(const char* filename, const char AuxType, double b[]) |
| 587 |
{ |
| 588 |
/****************************************************************************/ |
| 589 |
/* This function opens and reads the specified file, placing auxiliary */ |
| 590 |
/* vector(s) of the given type (if available) in b. */ |
| 591 |
/* Return value is the number of vectors successfully read. */ |
| 592 |
/* */ |
| 593 |
/* AuxType = 'F' full right-hand-side vector(s) */ |
| 594 |
/* AuxType = 'G' initial Guess vector(s) */ |
| 595 |
/* AuxType = 'X' eXact solution vector(s) */ |
| 596 |
/* */ |
| 597 |
/* ---------- */ |
| 598 |
/* **CAVEAT** */ |
| 599 |
/* ---------- */ |
| 600 |
/* Parsing real formats from Fortran is tricky, and this file reader */ |
| 601 |
/* does not claim to be foolproof. It has been tested for cases when */ |
| 602 |
/* the real values are printed consistently and evenly spaced on each */ |
| 603 |
/* line, with Fixed (F), and Exponential (E or D) formats. */ |
| 604 |
/* */ |
| 605 |
/* ** If the input file does not adhere to the H/B format, the ** */ |
| 606 |
/* ** results will be unpredictable. ** */ |
| 607 |
/* */ |
| 608 |
/****************************************************************************/ |
| 609 |
FILE *in_file; |
| 610 |
int i,j,n,maxcol,start,stride,col,last,linel; |
| 611 |
int Ptrcrd, Indcrd, Valcrd, Rhscrd; |
| 612 |
int Nrow, Ncol, Nnzero, Nentries; |
| 613 |
int Nrhs, nvecs, rhsi; |
| 614 |
int Rhsperline, Rhswidth, Rhsprec; |
| 615 |
int Rhsflag; |
| 616 |
char *ThisElement; |
| 617 |
char Title[73], Key[9], Type[4] = "XXX\0", Rhstype[4]; |
| 618 |
char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21]; |
| 619 |
char line[BUFSIZ]; |
| 620 |
|
| 621 |
if ((in_file = fopen( filename, "r")) == NULL) { |
| 622 |
fprintf(stderr,"Error: Cannot open file: %s\n",filename); |
| 623 |
return 0; |
| 624 |
} |
| 625 |
|
| 626 |
readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs, |
| 627 |
Ptrfmt, Indfmt, Valfmt, Rhsfmt, |
| 628 |
&Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype); |
| 629 |
|
| 630 |
if (Nrhs <= 0) |
| 631 |
{ |
| 632 |
fprintf(stderr, "Warn: Attempt to read auxiliary vector(s) when none are present.\n"); |
| 633 |
return 0; |
| 634 |
} |
| 635 |
if (Rhstype[0] != 'F' ) |
| 636 |
{ |
| 637 |
fprintf(stderr,"Warn: Attempt to read auxiliary vector(s) which are not stored in Full form.\n"); |
| 638 |
fprintf(stderr," Rhs must be specified as full. \n"); |
| 639 |
return 0; |
| 640 |
} |
| 641 |
|
| 642 |
/* If reading complex data, allow for interleaved real and imaginary values. */ |
| 643 |
if ( Type[0] == 'C' ) { |
| 644 |
Nentries = 2*Nrow; |
| 645 |
} else { |
| 646 |
Nentries = Nrow; |
| 647 |
} |
| 648 |
|
| 649 |
nvecs = 1; |
| 650 |
|
| 651 |
if ( Rhstype[1] == 'G' ) nvecs++; |
| 652 |
if ( Rhstype[2] == 'X' ) nvecs++; |
| 653 |
|
| 654 |
if ( AuxType == 'G' && Rhstype[1] != 'G' ) { |
| 655 |
fprintf(stderr, "Warn: Attempt to read auxiliary Guess vector(s) when none are present.\n"); |
| 656 |
return 0; |
| 657 |
} |
| 658 |
if ( AuxType == 'X' && Rhstype[2] != 'X' ) { |
| 659 |
fprintf(stderr, "Warn: Attempt to read auxiliary eXact solution vector(s) when none are present.\n"); |
| 660 |
return 0; |
| 661 |
} |
| 662 |
|
| 663 |
ParseRfmt(Rhsfmt, &Rhsperline, &Rhswidth, &Rhsprec,&Rhsflag); |
| 664 |
maxcol = Rhsperline*Rhswidth; |
| 665 |
|
| 666 |
/* Lines to skip before starting to read RHS values... */ |
| 667 |
n = Ptrcrd + Indcrd + Valcrd; |
| 668 |
|
| 669 |
for (i = 0; i < n; i++) |
| 670 |
fgets(line, BUFSIZ, in_file); |
| 671 |
|
| 672 |
/* start - number of initial aux vector entries to skip */ |
| 673 |
/* to reach first vector requested */ |
| 674 |
/* stride - number of aux vector entries to skip between */ |
| 675 |
/* requested vectors */ |
| 676 |
if ( AuxType == 'F' ) start = 0; |
| 677 |
else if ( AuxType == 'G' ) start = Nentries; |
| 678 |
else start = (nvecs-1)*Nentries; |
| 679 |
stride = (nvecs-1)*Nentries; |
| 680 |
|
| 681 |
fgets(line, BUFSIZ, in_file); |
| 682 |
linel= strchr(line,'\n')-line; |
| 683 |
col = 0; |
| 684 |
/* Skip to initial offset */ |
| 685 |
|
| 686 |
for (i=0;i<start;i++) { |
| 687 |
if ( col >= ( maxcol<linel?maxcol:linel ) ) { |
| 688 |
fgets(line, BUFSIZ, in_file); |
| 689 |
linel= strchr(line,'\n')-line; |
| 690 |
col = 0; |
| 691 |
} |
| 692 |
col += Rhswidth; |
| 693 |
} |
| 694 |
if (Rhsflag == 'D') { |
| 695 |
while( strchr(line,'D') ) *strchr(line,'D') = 'E'; |
| 696 |
} |
| 697 |
|
| 698 |
/* Read a vector of desired type, then skip to next */ |
| 699 |
/* repeating to fill Nrhs vectors */ |
| 700 |
|
| 701 |
ThisElement = (char *) malloc(Rhswidth+1); |
| 702 |
if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement."); |
| 703 |
*(ThisElement+Rhswidth) = (char) NULL; |
| 704 |
for (rhsi=0;rhsi<Nrhs;rhsi++) { |
| 705 |
|
| 706 |
for (i=0;i<Nentries;i++) { |
| 707 |
if ( col >= ( maxcol<linel?maxcol:linel ) ) { |
| 708 |
fgets(line, BUFSIZ, in_file); |
| 709 |
linel= strchr(line,'\n')-line; |
| 710 |
if (Rhsflag == 'D') { |
| 711 |
while( strchr(line,'D') ) *strchr(line,'D') = 'E'; |
| 712 |
} |
| 713 |
col = 0; |
| 714 |
} |
| 715 |
strncpy(ThisElement,line+col,Rhswidth); |
| 716 |
/*ThisElement = substr(line, col, Rhswidth);*/ |
| 717 |
if ( Rhsflag != 'F' && strchr(ThisElement,'E') == NULL ) { |
| 718 |
/* insert a char prefix for exp */ |
| 719 |
last = strlen(ThisElement); |
| 720 |
for (j=last+1;j>=0;j--) { |
| 721 |
ThisElement[j] = ThisElement[j-1]; |
| 722 |
if ( ThisElement[j] == '+' || ThisElement[j] == '-' ) { |
| 723 |
ThisElement[j-1] = Rhsflag; |
| 724 |
break; |
| 725 |
} |
| 726 |
} |
| 727 |
} |
| 728 |
b[i] = atof(ThisElement); |
| 729 |
col += Rhswidth; |
| 730 |
} |
| 731 |
|
| 732 |
/* Skip any interleaved Guess/eXact vectors */ |
| 733 |
|
| 734 |
for (i=0;i<stride;i++) { |
| 735 |
if ( col >= ( maxcol<linel?maxcol:linel ) ) { |
| 736 |
fgets(line, BUFSIZ, in_file); |
| 737 |
linel= strchr(line,'\n')-line; |
| 738 |
col = 0; |
| 739 |
} |
| 740 |
col += Rhswidth; |
| 741 |
} |
| 742 |
|
| 743 |
} |
| 744 |
free(ThisElement); |
| 745 |
|
| 746 |
|
| 747 |
fclose(in_file); |
| 748 |
return Nrhs; |
| 749 |
} |
| 750 |
|
| 751 |
int readHB_newaux_double(const char* filename, const char AuxType, double** b) |
| 752 |
{ |
| 753 |
int Nrhs,M,N,nonzeros; |
| 754 |
char *Type; |
| 755 |
|
| 756 |
readHB_info(filename, &M, &N, &nonzeros, &Type, &Nrhs); |
| 757 |
if ( Nrhs <= 0 ) { |
| 758 |
fprintf(stderr,"Warn: Requested read of aux vector(s) when none are present.\n"); |
| 759 |
return 0; |
| 760 |
} else { |
| 761 |
if ( Type[0] == 'C' ) { |
| 762 |
fprintf(stderr, "Warning: Reading complex aux vector(s) from HB file %s.",filename); |
| 763 |
fprintf(stderr, " Real and imaginary parts will be interlaced in b[]."); |
| 764 |
*b = (double *)malloc(M*Nrhs*sizeof(double)*2); |
| 765 |
if ( *b == NULL ) IOHBTerminate("Insufficient memory for rhs.\n"); |
| 766 |
return readHB_aux_double(filename, AuxType, *b); |
| 767 |
} else { |
| 768 |
*b = (double *)malloc(M*Nrhs*sizeof(double)); |
| 769 |
if ( *b == NULL ) IOHBTerminate("Insufficient memory for rhs.\n"); |
| 770 |
return readHB_aux_double(filename, AuxType, *b); |
| 771 |
} |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
int writeHB_mat_double(const char* filename, int M, int N, |
| 776 |
int nz, const int colptr[], const int rowind[], |
| 777 |
const double val[], int Nrhs, const double rhs[], |
| 778 |
const double guess[], const double exact[], |
| 779 |
const char* Title, const char* Key, const char* Type, |
| 780 |
char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt, |
| 781 |
const char* Rhstype) |
| 782 |
{ |
| 783 |
/****************************************************************************/ |
| 784 |
/* The writeHB function opens the named file and writes the specified */ |
| 785 |
/* matrix and optional right-hand-side(s) to that file in Harwell-Boeing */ |
| 786 |
/* format. */ |
| 787 |
/* */ |
| 788 |
/* For a description of the Harwell Boeing standard, see: */ |
| 789 |
/* Duff, et al., ACM TOMS Vol.15, No.1, March 1989 */ |
| 790 |
/* */ |
| 791 |
/****************************************************************************/ |
| 792 |
FILE *out_file; |
| 793 |
int i,j,entry,offset,acount,linemod; |
| 794 |
int totcrd, ptrcrd, indcrd, valcrd, rhscrd; |
| 795 |
int nvalentries, nrhsentries; |
| 796 |
int Ptrperline, Ptrwidth, Indperline, Indwidth; |
| 797 |
int Rhsperline, Rhswidth, Rhsprec; |
| 798 |
int Rhsflag; |
| 799 |
int Valperline, Valwidth, Valprec; |
| 800 |
int Valflag; /* Indicates 'E','D', or 'F' float format */ |
| 801 |
char pformat[16],iformat[16],vformat[19],rformat[19]; |
| 802 |
|
| 803 |
if ( Type[0] == 'C' ) { |
| 804 |
nvalentries = 2*nz; |
| 805 |
nrhsentries = 2*M; |
| 806 |
} else { |
| 807 |
nvalentries = nz; |
| 808 |
nrhsentries = M; |
| 809 |
} |
| 810 |
|
| 811 |
if ( filename != NULL ) { |
| 812 |
if ( (out_file = fopen( filename, "w")) == NULL ) { |
| 813 |
fprintf(stderr,"Error: Cannot open file: %s\n",filename); |
| 814 |
return 0; |
| 815 |
} |
| 816 |
} else out_file = stdout; |
| 817 |
|
| 818 |
if ( Ptrfmt == NULL ) Ptrfmt = "(8I10)"; |
| 819 |
ParseIfmt(Ptrfmt,&Ptrperline,&Ptrwidth); |
| 820 |
sprintf(pformat,"%%%dd",Ptrwidth); |
| 821 |
ptrcrd = (N+1)/Ptrperline; |
| 822 |
if ( (N+1)%Ptrperline != 0) ptrcrd++; |
| 823 |
|
| 824 |
if ( Indfmt == NULL ) Indfmt = Ptrfmt; |
| 825 |
ParseIfmt(Indfmt,&Indperline,&Indwidth); |
| 826 |
sprintf(iformat,"%%%dd",Indwidth); |
| 827 |
indcrd = nz/Indperline; |
| 828 |
if ( nz%Indperline != 0) indcrd++; |
| 829 |
|
| 830 |
if ( Type[0] != 'P' ) { /* Skip if pattern only */ |
| 831 |
if ( Valfmt == NULL ) Valfmt = "(4E20.13)"; |
| 832 |
ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag); |
| 833 |
if (Valflag == 'D') *strchr(Valfmt,'D') = 'E'; |
| 834 |
if (Valflag == 'F') |
| 835 |
sprintf(vformat,"%% %d.%df",Valwidth,Valprec); |
| 836 |
else |
| 837 |
sprintf(vformat,"%% %d.%dE",Valwidth,Valprec); |
| 838 |
valcrd = nvalentries/Valperline; |
| 839 |
if ( nvalentries%Valperline != 0) valcrd++; |
| 840 |
} else valcrd = 0; |
| 841 |
|
| 842 |
if ( Nrhs > 0 ) { |
| 843 |
if ( Rhsfmt == NULL ) Rhsfmt = Valfmt; |
| 844 |
ParseRfmt(Rhsfmt,&Rhsperline,&Rhswidth,&Rhsprec, &Rhsflag); |
| 845 |
if (Rhsflag == 'F') |
| 846 |
sprintf(rformat,"%% %d.%df",Rhswidth,Rhsprec); |
| 847 |
else |
| 848 |
sprintf(rformat,"%% %d.%dE",Rhswidth,Rhsprec); |
| 849 |
if (Rhsflag == 'D') *strchr(Rhsfmt,'D') = 'E'; |
| 850 |
rhscrd = nrhsentries/Rhsperline; |
| 851 |
if ( nrhsentries%Rhsperline != 0) rhscrd++; |
| 852 |
if ( Rhstype[1] == 'G' ) rhscrd+=rhscrd; |
| 853 |
if ( Rhstype[2] == 'X' ) rhscrd+=rhscrd; |
| 854 |
rhscrd*=Nrhs; |
| 855 |
} else rhscrd = 0; |
| 856 |
|
| 857 |
totcrd = 4+ptrcrd+indcrd+valcrd+rhscrd; |
| 858 |
|
| 859 |
|
| 860 |
/* Print header information: */ |
| 861 |
|
| 862 |
fprintf(out_file,"%-72s%-8s\n%14d%14d%14d%14d%14d\n",Title, Key, totcrd, |
| 863 |
ptrcrd, indcrd, valcrd, rhscrd); |
| 864 |
fprintf(out_file,"%3s%11s%14d%14d%14d\n",Type," ", M, N, nz); |
| 865 |
fprintf(out_file,"%-16s%-16s%-20s", Ptrfmt, Indfmt, Valfmt); |
| 866 |
if ( Nrhs != 0 ) { |
| 867 |
/* Print Rhsfmt on fourth line and */ |
| 868 |
/* optional fifth header line for auxiliary vector information: */ |
| 869 |
fprintf(out_file,"%-20s\n%-14s%d\n",Rhsfmt,Rhstype,Nrhs); |
| 870 |
} else fprintf(out_file,"\n"); |
| 871 |
|
| 872 |
offset = 1-_SP_base; /* if base 0 storage is declared (via macro definition), */ |
| 873 |
/* then storage entries are offset by 1 */ |
| 874 |
|
| 875 |
/* Print column pointers: */ |
| 876 |
for (i=0;i<N+1;i++) |
| 877 |
{ |
| 878 |
entry = colptr[i]+offset; |
| 879 |
fprintf(out_file,pformat,entry); |
| 880 |
if ( (i+1)%Ptrperline == 0 ) fprintf(out_file,"\n"); |
| 881 |
} |
| 882 |
|
| 883 |
if ( (N+1) % Ptrperline != 0 ) fprintf(out_file,"\n"); |
| 884 |
|
| 885 |
/* Print row indices: */ |
| 886 |
for (i=0;i<nz;i++) |
| 887 |
{ |
| 888 |
entry = rowind[i]+offset; |
| 889 |
fprintf(out_file,iformat,entry); |
| 890 |
if ( (i+1)%Indperline == 0 ) fprintf(out_file,"\n"); |
| 891 |
} |
| 892 |
|
| 893 |
if ( nz % Indperline != 0 ) fprintf(out_file,"\n"); |
| 894 |
|
| 895 |
/* Print values: */ |
| 896 |
|
| 897 |
if ( Type[0] != 'P' ) { /* Skip if pattern only */ |
| 898 |
|
| 899 |
for (i=0;i<nvalentries;i++) |
| 900 |
{ |
| 901 |
fprintf(out_file,vformat,val[i]); |
| 902 |
if ( (i+1)%Valperline == 0 ) fprintf(out_file,"\n"); |
| 903 |
} |
| 904 |
|
| 905 |
if ( nvalentries % Valperline != 0 ) fprintf(out_file,"\n"); |
| 906 |
|
| 907 |
/* If available, print right hand sides, |
| 908 |
guess vectors and exact solution vectors: */ |
| 909 |
acount = 1; |
| 910 |
linemod = 0; |
| 911 |
if ( Nrhs > 0 ) { |
| 912 |
for (i=0;i<Nrhs;i++) |
| 913 |
{ |
| 914 |
for ( j=0;j<nrhsentries;j++ ) { |
| 915 |
fprintf(out_file,rformat,rhs[j]); |
| 916 |
if ( acount++%Rhsperline == linemod ) fprintf(out_file,"\n"); |
| 917 |
} |
| 918 |
if ( (acount-1)%Rhsperline != linemod ) { |
| 919 |
fprintf(out_file,"\n"); |
| 920 |
linemod = (acount-1)%Rhsperline; |
| 921 |
} |
| 922 |
rhs += nrhsentries; |
| 923 |
if ( Rhstype[1] == 'G' ) { |
| 924 |
for ( j=0;j<nrhsentries;j++ ) { |
| 925 |
fprintf(out_file,rformat,guess[j]); |
| 926 |
if ( acount++%Rhsperline == linemod ) fprintf(out_file,"\n"); |
| 927 |
} |
| 928 |
if ( (acount-1)%Rhsperline != linemod ) { |
| 929 |
fprintf(out_file,"\n"); |
| 930 |
linemod = (acount-1)%Rhsperline; |
| 931 |
} |
| 932 |
guess += nrhsentries; |
| 933 |
} |
| 934 |
if ( Rhstype[2] == 'X' ) { |
| 935 |
for ( j=0;j<nrhsentries;j++ ) { |
| 936 |
fprintf(out_file,rformat,exact[j]); |
| 937 |
if ( acount++%Rhsperline == linemod ) fprintf(out_file,"\n"); |
| 938 |
} |
| 939 |
if ( (acount-1)%Rhsperline != linemod ) { |
| 940 |
fprintf(out_file,"\n"); |
| 941 |
linemod = (acount-1)%Rhsperline; |
| 942 |
} |
| 943 |
exact += nrhsentries; |
| 944 |
} |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
} |
| 949 |
|
| 950 |
if ( fclose(out_file) != 0){ |
| 951 |
fprintf(stderr,"Error closing file in writeHB_mat_double().\n"); |
| 952 |
return 0; |
| 953 |
} else return 1; |
| 954 |
|
| 955 |
} |
| 956 |
|
| 957 |
int readHB_mat_char(const char* filename, int colptr[], int rowind[], |
| 958 |
char val[], char* Valfmt) |
| 959 |
{ |
| 960 |
/****************************************************************************/ |
| 961 |
/* This function opens and reads the specified file, interpreting its */ |
| 962 |
/* contents as a sparse matrix stored in the Harwell/Boeing standard */ |
| 963 |
/* format and creating compressed column storage scheme vectors to hold */ |
| 964 |
/* the index and nonzero value information. */ |
| 965 |
/* */ |
| 966 |
/* ---------- */ |
| 967 |
/* **CAVEAT** */ |
| 968 |
/* ---------- */ |
| 969 |
/* Parsing real formats from Fortran is tricky, and this file reader */ |
| 970 |
/* does not claim to be foolproof. It has been tested for cases when */ |
| 971 |
/* the real values are printed consistently and evenly spaced on each */ |
| 972 |
/* line, with Fixed (F), and Exponential (E or D) formats. */ |
| 973 |
/* */ |
| 974 |
/* ** If the input file does not adhere to the H/B format, the ** */ |
| 975 |
/* ** results will be unpredictable. ** */ |
| 976 |
/* */ |
| 977 |
/****************************************************************************/ |
| 978 |
FILE *in_file; |
| 979 |
int i,j,ind,col,offset,count,last; |
| 980 |
int Nrow,Ncol,Nnzero,Nentries,Nrhs; |
| 981 |
int Ptrcrd, Indcrd, Valcrd, Rhscrd; |
| 982 |
int Ptrperline, Ptrwidth, Indperline, Indwidth; |
| 983 |
int Valperline, Valwidth, Valprec; |
| 984 |
int Valflag; /* Indicates 'E','D', or 'F' float format */ |
| 985 |
char* ThisElement; |
| 986 |
char line[BUFSIZ]; |
| 987 |
char Title[73], Key[8], Type[4] = "XXX\0", Rhstype[4]; |
| 988 |
char Ptrfmt[17], Indfmt[17], Rhsfmt[21]; |
| 989 |
|
| 990 |
if ( (in_file = fopen( filename, "r")) == NULL ) { |
| 991 |
fprintf(stderr,"Error: Cannot open file: %s\n",filename); |
| 992 |
return 0; |
| 993 |
} |
| 994 |
|
| 995 |
readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs, |
| 996 |
Ptrfmt, Indfmt, Valfmt, Rhsfmt, |
| 997 |
&Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype); |
| 998 |
|
| 999 |
/* Parse the array input formats from Line 3 of HB file */ |
| 1000 |
ParseIfmt(Ptrfmt,&Ptrperline,&Ptrwidth); |
| 1001 |
ParseIfmt(Indfmt,&Indperline,&Indwidth); |
| 1002 |
if ( Type[0] != 'P' ) { /* Skip if pattern only */ |
| 1003 |
ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag); |
| 1004 |
if (Valflag == 'D') { |
| 1005 |
*strchr(Valfmt,'D') = 'E'; |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
/* Read column pointer array: */ |
| 1010 |
|
| 1011 |
offset = 1-_SP_base; /* if base 0 storage is declared (via macro definition), */ |
| 1012 |
/* then storage entries are offset by 1 */ |
| 1013 |
|
| 1014 |
ThisElement = (char *) malloc(Ptrwidth+1); |
| 1015 |
if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement."); |
| 1016 |
*(ThisElement+Ptrwidth) = (char) NULL; |
| 1017 |
count=0; |
| 1018 |
for (i=0;i<Ptrcrd;i++) |
| 1019 |
{ |
| 1020 |
fgets(line, BUFSIZ, in_file); |
| 1021 |
if ( sscanf(line,"%*s") < 0 ) |
| 1022 |
IOHBTerminate("iohb.c: Null (or blank) line in pointer data region of HB file.\n"); |
| 1023 |
col = 0; |
| 1024 |
for (ind = 0;ind<Ptrperline;ind++) |
| 1025 |
{ |
| 1026 |
if (count > Ncol) break; |
| 1027 |
strncpy(ThisElement,line+col,Ptrwidth); |
| 1028 |
/*ThisElement = substr(line,col,Ptrwidth);*/ |
| 1029 |
colptr[count] = atoi(ThisElement)-offset; |
| 1030 |
count++; col += Ptrwidth; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
free(ThisElement); |
| 1034 |
|
| 1035 |
/* Read row index array: */ |
| 1036 |
|
| 1037 |
ThisElement = (char *) malloc(Indwidth+1); |
| 1038 |
if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement."); |
| 1039 |
*(ThisElement+Indwidth) = (char) NULL; |
| 1040 |
count = 0; |
| 1041 |
for (i=0;i<Indcrd;i++) |
| 1042 |
{ |
| 1043 |
fgets(line, BUFSIZ, in_file); |
| 1044 |
if ( sscanf(line,"%*s") < 0 ) |
| 1045 |
IOHBTerminate("iohb.c: Null (or blank) line in index data region of HB file.\n"); |
| 1046 |
col = 0; |
| 1047 |
for (ind = 0;ind<Indperline;ind++) |
| 1048 |
{ |
| 1049 |
if (count == Nnzero) break; |
| 1050 |
strncpy(ThisElement,line+col,Indwidth); |
| 1051 |
/*ThisElement = substr(line,col,Indwidth);*/ |
| 1052 |
rowind[count] = atoi(ThisElement)-offset; |
| 1053 |
count++; col += Indwidth; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
free(ThisElement); |
| 1057 |
|
| 1058 |
/* Read array of values: AS CHARACTERS*/ |
| 1059 |
|
| 1060 |
if ( Type[0] != 'P' ) { /* Skip if pattern only */ |
| 1061 |
|
| 1062 |
if ( Type[0] == 'C' ) Nentries = 2*Nnzero; |
| 1063 |
else Nentries = Nnzero; |
| 1064 |
|
| 1065 |
ThisElement = (char *) malloc(Valwidth+1); |
| 1066 |
if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement."); |
| 1067 |
*(ThisElement+Valwidth) = (char) NULL; |
| 1068 |
count = 0; |
| 1069 |
for (i=0;i<Valcrd;i++) |
| 1070 |
{ |
| 1071 |
fgets(line, BUFSIZ, in_file); |
| 1072 |
if ( sscanf(line,"%*s") < 0 ) |
| 1073 |
IOHBTerminate("iohb.c: Null (or blank) line in value data region of HB file.\n"); |
| 1074 |
if (Valflag == 'D') { |
| 1075 |
while( strchr(line,'D') ) *strchr(line,'D') = 'E'; |
| 1076 |
} |
| 1077 |
col = 0; |
| 1078 |
for (ind = 0;ind<Valperline;ind++) |
| 1079 |
{ |
| 1080 |
if (count == Nentries) break; |
| 1081 |
ThisElement = &val[count*Valwidth]; |
| 1082 |
strncpy(ThisElement,line+col,Valwidth); |
| 1083 |
/*strncpy(ThisElement,substr(line,col,Valwidth),Valwidth);*/ |
| 1084 |
if ( Valflag != 'F' && strchr(ThisElement,'E') == NULL ) { |
| 1085 |
/* insert a char prefix for exp */ |
| 1086 |
last = strlen(ThisElement); |
| 1087 |
for (j=last+1;j>=0;j--) { |
| 1088 |
ThisElement[j] = ThisElement[j-1]; |
| 1089 |
if ( ThisElement[j] == '+' || ThisElement[j] == '-' ) { |
| 1090 |
ThisElement[j-1] = Valflag; |
| 1091 |
break; |
| 1092 |
} |
| 1093 |
} |
| 1094 |
} |
| 1095 |
count++; col += Valwidth; |
| 1096 |
} |
| 1097 |
} |
| 1098 |
} |
| 1099 |
|
| 1100 |
return 1; |
| 1101 |
} |
| 1102 |
|
| 1103 |
int readHB_newmat_char(const char* filename, int* M, int* N, int* nonzeros, int** colptr, |
| 1104 |
int** rowind, char** val, char** Valfmt) |
| 1105 |
{ |
| 1106 |
FILE *in_file; |
| 1107 |
int Nrhs; |
| 1108 |
int Ptrcrd, Indcrd, Valcrd, Rhscrd; |
| 1109 |
int Valperline, Valwidth, Valprec; |
| 1110 |
int Valflag; /* Indicates 'E','D', or 'F' float format */ |
| 1111 |
char Title[73], Key[9], Type[4] = "XXX\0", Rhstype[4]; |
| 1112 |
char Ptrfmt[17], Indfmt[17], Rhsfmt[21]; |
| 1113 |
|
| 1114 |
if ((in_file = fopen( filename, "r")) == NULL) { |
| 1115 |
fprintf(stderr,"Error: Cannot open file: %s\n",filename); |
| 1116 |
return 0; |
| 1117 |
} |
| 1118 |
|
| 1119 |
*Valfmt = (char *)malloc(21*sizeof(char)); |
| 1120 |
if ( *Valfmt == NULL ) IOHBTerminate("Insufficient memory for Valfmt."); |
| 1121 |
readHB_header(in_file, Title, Key, Type, M, N, nonzeros, &Nrhs, |
| 1122 |
Ptrfmt, Indfmt, (*Valfmt), Rhsfmt, |
| 1123 |
&Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype); |
| 1124 |
fclose(in_file); |
| 1125 |
ParseRfmt(*Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag); |
| 1126 |
|
| 1127 |
*colptr = (int *)malloc((*N+1)*sizeof(int)); |
| 1128 |
if ( *colptr == NULL ) IOHBTerminate("Insufficient memory for colptr.\n"); |
| 1129 |
*rowind = (int *)malloc(*nonzeros*sizeof(int)); |
| 1130 |
if ( *rowind == NULL ) IOHBTerminate("Insufficient memory for rowind.\n"); |
| 1131 |
if ( Type[0] == 'C' ) { |
| 1132 |
/* |
| 1133 |
fprintf(stderr, "Warning: Reading complex data from HB file %s.\n",filename); |
| 1134 |
fprintf(stderr, " Real and imaginary parts will be interlaced in val[].\n"); |
| 1135 |
*/ |
| 1136 |
/* Malloc enough space for real AND imaginary parts of val[] */ |
| 1137 |
*val = (char *)malloc(*nonzeros*Valwidth*sizeof(char)*2); |
| 1138 |
if ( *val == NULL ) IOHBTerminate("Insufficient memory for val.\n"); |
| 1139 |
} else { |
| 1140 |
if ( Type[0] != 'P' ) { |
| 1141 |
/* Malloc enough space for real array val[] */ |
| 1142 |
*val = (char *)malloc(*nonzeros*Valwidth*sizeof(char)); |
| 1143 |
if ( *val == NULL ) IOHBTerminate("Insufficient memory for val.\n"); |
| 1144 |
} |
| 1145 |
} /* No val[] space needed if pattern only */ |
| 1146 |
return readHB_mat_char(filename, *colptr, *rowind, *val, *Valfmt); |
| 1147 |
|
| 1148 |
} |
| 1149 |
|
| 1150 |
int readHB_aux_char(const char* filename, const char AuxType, char b[]) |
| 1151 |
{ |
| 1152 |
/****************************************************************************/ |
| 1153 |
/* This function opens and reads the specified file, placing auxilary */ |
| 1154 |
/* vector(s) of the given type (if available) in b : */ |
| 1155 |
/* Return value is the number of vectors successfully read. */ |
| 1156 |
/* */ |
| 1157 |
/* AuxType = 'F' full right-hand-side vector(s) */ |
| 1158 |
/* AuxType = 'G' initial Guess vector(s) */ |
| 1159 |
/* AuxType = 'X' eXact solution vector(s) */ |
| 1160 |
/* */ |
| 1161 |
/* ---------- */ |
| 1162 |
/* **CAVEAT** */ |
| 1163 |
/* ---------- */ |
| 1164 |
/* Parsing real formats from Fortran is tricky, and this file reader */ |
| 1165 |
/* does not claim to be foolproof. It has been tested for cases when */ |
| 1166 |
/* the real values are printed consistently and evenly spaced on each */ |
| 1167 |
/* line, with Fixed (F), and Exponential (E or D) formats. */ |
| 1168 |
/* */ |
| 1169 |
/* ** If the input file does not adhere to the H/B format, the ** */ |
| 1170 |
/* ** results will be unpredictable. ** */ |
| 1171 |
/* */ |
| 1172 |
/****************************************************************************/ |
| 1173 |
FILE *in_file; |
| 1174 |
int i,j,n,maxcol,start,stride,col,last,linel,nvecs,rhsi; |
| 1175 |
int Nrow, Ncol, Nnzero, Nentries,Nrhs; |
| 1176 |
int Ptrcrd, Indcrd, Valcrd, Rhscrd; |
| 1177 |
int Rhsperline, Rhswidth, Rhsprec; |
| 1178 |
int Rhsflag; |
| 1179 |
char Title[73], Key[9], Type[4] = "XXX\0", Rhstype[4]; |
| 1180 |
char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21]; |
| 1181 |
char line[BUFSIZ]; |
| 1182 |
char *ThisElement; |
| 1183 |
|
| 1184 |
if ((in_file = fopen( filename, "r")) == NULL) { |
| 1185 |
fprintf(stderr,"Error: Cannot open file: %s\n",filename); |
| 1186 |
return 0; |
| 1187 |
} |
| 1188 |
|
| 1189 |
readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs, |
| 1190 |
Ptrfmt, Indfmt, Valfmt, Rhsfmt, |
| 1191 |
&Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype); |
| 1192 |
|
| 1193 |
if (Nrhs <= 0) |
| 1194 |
{ |
| 1195 |
fprintf(stderr, "Warn: Attempt to read auxiliary vector(s) when none are present.\n"); |
| 1196 |
return 0; |
| 1197 |
} |
| 1198 |
if (Rhstype[0] != 'F' ) |
| 1199 |
{ |
| 1200 |
fprintf(stderr,"Warn: Attempt to read auxiliary vector(s) which are not stored in Full form.\n"); |
| 1201 |
fprintf(stderr," Rhs must be specified as full. \n"); |
| 1202 |
return 0; |
| 1203 |
} |
| 1204 |
|
| 1205 |
/* If reading complex data, allow for interleaved real and imaginary values. */ |
| 1206 |
if ( Type[0] == 'C' ) { |
| 1207 |
Nentries = 2*Nrow; |
| 1208 |
} else { |
| 1209 |
Nentries = Nrow; |
| 1210 |
} |
| 1211 |
|
| 1212 |
nvecs = 1; |
| 1213 |
|
| 1214 |
if ( Rhstype[1] == 'G' ) nvecs++; |
| 1215 |
if ( Rhstype[2] == 'X' ) nvecs++; |
| 1216 |
|
| 1217 |
if ( AuxType == 'G' && Rhstype[1] != 'G' ) { |
| 1218 |
fprintf(stderr, "Warn: Attempt to read auxiliary Guess vector(s) when none are present.\n"); |
| 1219 |
return 0; |
| 1220 |
} |
| 1221 |
if ( AuxType == 'X' && Rhstype[2] != 'X' ) { |
| 1222 |
fprintf(stderr, "Warn: Attempt to read auxiliary eXact solution vector(s) when none are present.\n"); |
| 1223 |
return 0; |
| 1224 |
} |
| 1225 |
|
| 1226 |
ParseRfmt(Rhsfmt, &Rhsperline, &Rhswidth, &Rhsprec,&Rhsflag); |
| 1227 |
maxcol = Rhsperline*Rhswidth; |
| 1228 |
|
| 1229 |
/* Lines to skip before starting to read RHS values... */ |
| 1230 |
n = Ptrcrd + Indcrd + Valcrd; |
| 1231 |
|
| 1232 |
for (i = 0; i < n; i++) |
| 1233 |
fgets(line, BUFSIZ, in_file); |
| 1234 |
|
| 1235 |
/* start - number of initial aux vector entries to skip */ |
| 1236 |
/* to reach first vector requested */ |
| 1237 |
/* stride - number of aux vector entries to skip between */ |
| 1238 |
/* requested vectors */ |
| 1239 |
if ( AuxType == 'F' ) start = 0; |
| 1240 |
else if ( AuxType == 'G' ) start = Nentries; |
| 1241 |
else start = (nvecs-1)*Nentries; |
| 1242 |
stride = (nvecs-1)*Nentries; |
| 1243 |
|
| 1244 |
fgets(line, BUFSIZ, in_file); |
| 1245 |
linel= strchr(line,'\n')-line; |
| 1246 |
if ( sscanf(line,"%*s") < 0 ) |
| 1247 |
IOHBTerminate("iohb.c: Null (or blank) line in auxiliary vector data region of HB file.\n"); |
| 1248 |
col = 0; |
| 1249 |
/* Skip to initial offset */ |
| 1250 |
|
| 1251 |
for (i=0;i<start;i++) { |
| 1252 |
col += Rhswidth; |
| 1253 |
if ( col >= ( maxcol<linel?maxcol:linel ) ) { |
| 1254 |
fgets(line, BUFSIZ, in_file); |
| 1255 |
linel= strchr(line,'\n')-line; |
| 1256 |
if ( sscanf(line,"%*s") < 0 ) |
| 1257 |
IOHBTerminate("iohb.c: Null (or blank) line in auxiliary vector data region of HB file.\n"); |
| 1258 |
col = 0; |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
if (Rhsflag == 'D') { |
| 1263 |
while( strchr(line,'D') ) *strchr(line,'D') = 'E'; |
| 1264 |
} |
| 1265 |
/* Read a vector of desired type, then skip to next */ |
| 1266 |
/* repeating to fill Nrhs vectors */ |
| 1267 |
|
| 1268 |
for (rhsi=0;rhsi<Nrhs;rhsi++) { |
| 1269 |
|
| 1270 |
for (i=0;i<Nentries;i++) { |
| 1271 |
if ( col >= ( maxcol<linel?maxcol:linel ) ) { |
| 1272 |
fgets(line, BUFSIZ, in_file); |
| 1273 |
linel= strchr(line,'\n')-line; |
| 1274 |
if ( sscanf(line,"%*s") < 0 ) |
| 1275 |
IOHBTerminate("iohb.c: Null (or blank) line in auxiliary vector data region of HB file.\n"); |
| 1276 |
if (Rhsflag == 'D') { |
| 1277 |
while( strchr(line,'D') ) *strchr(line,'D') = 'E'; |
| 1278 |
} |
| 1279 |
col = 0; |
| 1280 |
} |
| 1281 |
ThisElement = &b[i*Rhswidth]; |
| 1282 |
strncpy(ThisElement,line+col,Rhswidth); |
| 1283 |
if ( Rhsflag != 'F' && strchr(ThisElement,'E') == NULL ) { |
| 1284 |
/* insert a char prefix for exp */ |
| 1285 |
last = strlen(ThisElement); |
| 1286 |
for (j=last+1;j>=0;j--) { |
| 1287 |
ThisElement[j] = ThisElement[j-1]; |
| 1288 |
if ( ThisElement[j] == '+' || ThisElement[j] == '-' ) { |
| 1289 |
ThisElement[j-1] = Rhsflag; |
| 1290 |
break; |
| 1291 |
} |
| 1292 |
} |
| 1293 |
} |
| 1294 |
col += Rhswidth; |
| 1295 |
} |
| 1296 |
b+=Nentries*Rhswidth; |
| 1297 |
|
| 1298 |
/* Skip any interleaved Guess/eXact vectors */ |
| 1299 |
|
| 1300 |
for (i=0;i<stride;i++) { |
| 1301 |
col += Rhswidth; |
| 1302 |
if ( col >= ( maxcol<linel?maxcol:linel ) ) { |
| 1303 |
fgets(line, BUFSIZ, in_file); |
| 1304 |
linel= strchr(line,'\n')-line; |
| 1305 |
if ( sscanf(line,"%*s") < 0 ) |
| 1306 |
IOHBTerminate("iohb.c: Null (or blank) line in auxiliary vector data region of HB file.\n"); |
| 1307 |
col = 0; |
| 1308 |
} |
| 1309 |
} |
| 1310 |
|
| 1311 |
} |
| 1312 |
|
| 1313 |
|
| 1314 |
fclose(in_file); |
| 1315 |
return Nrhs; |
| 1316 |
} |
| 1317 |
|
| 1318 |
int readHB_newaux_char(const char* filename, const char AuxType, char** b, char** Rhsfmt) |
| 1319 |
{ |
| 1320 |
FILE *in_file; |
| 1321 |
int Ptrcrd, Indcrd, Valcrd, Rhscrd; |
| 1322 |
int Nrow,Ncol,Nnzero,Nrhs; |
| 1323 |
int Rhsperline, Rhswidth, Rhsprec; |
| 1324 |
int Rhsflag; |
| 1325 |
char Title[73], Key[9], Type[4] = "XXX\0", Rhstype[4]; |
| 1326 |
char Ptrfmt[17], Indfmt[17], Valfmt[21]; |
| 1327 |
|
| 1328 |
if ((in_file = fopen( filename, "r")) == NULL) { |
| 1329 |
fprintf(stderr,"Error: Cannot open file: %s\n",filename); |
| 1330 |
return 0; |
| 1331 |
} |
| 1332 |
|
| 1333 |
*Rhsfmt = (char *)malloc(21*sizeof(char)); |
| 1334 |
if ( *Rhsfmt == NULL ) IOHBTerminate("Insufficient memory for Rhsfmt."); |
| 1335 |
readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs, |
| 1336 |
Ptrfmt, Indfmt, Valfmt, (*Rhsfmt), |
| 1337 |
&Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype); |
| 1338 |
fclose(in_file); |
| 1339 |
if ( Nrhs == 0 ) { |
| 1340 |
fprintf(stderr,"Warn: Requested read of aux vector(s) when none are present.\n"); |
| 1341 |
return 0; |
| 1342 |
} else { |
| 1343 |
ParseRfmt(*Rhsfmt,&Rhsperline,&Rhswidth,&Rhsprec,&Rhsflag); |
| 1344 |
if ( Type[0] == 'C' ) { |
| 1345 |
fprintf(stderr, "Warning: Reading complex aux vector(s) from HB file %s.",filename); |
| 1346 |
fprintf(stderr, " Real and imaginary parts will be interlaced in b[]."); |
| 1347 |
*b = (char *)malloc(Nrow*Nrhs*Rhswidth*sizeof(char)*2); |
| 1348 |
if ( *b == NULL ) IOHBTerminate("Insufficient memory for rhs.\n"); |
| 1349 |
return readHB_aux_char(filename, AuxType, *b); |
| 1350 |
} else { |
| 1351 |
*b = (char *)malloc(Nrow*Nrhs*Rhswidth*sizeof(char)); |
| 1352 |
if ( *b == NULL ) IOHBTerminate("Insufficient memory for rhs.\n"); |
| 1353 |
return readHB_aux_char(filename, AuxType, *b); |
| 1354 |
} |
| 1355 |
} |
| 1356 |
} |
| 1357 |
|
| 1358 |
int writeHB_mat_char(const char* filename, int M, int N, |
| 1359 |
int nz, const int colptr[], const int rowind[], |
| 1360 |
const char val[], int Nrhs, const char rhs[], |
| 1361 |
const char guess[], const char exact[], |
| 1362 |
const char* Title, const char* Key, const char* Type, |
| 1363 |
char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt, |
| 1364 |
const char* Rhstype) |
| 1365 |
{ |
| 1366 |
/****************************************************************************/ |
| 1367 |
/* The writeHB function opens the named file and writes the specified */ |
| 1368 |
/* matrix and optional right-hand-side(s) to that file in Harwell-Boeing */ |
| 1369 |
/* format. */ |
| 1370 |
/* */ |
| 1371 |
/* For a description of the Harwell Boeing standard, see: */ |
| 1372 |
/* Duff, et al., ACM TOMS Vol.15, No.1, March 1989 */ |
| 1373 |
/* */ |
| 1374 |
/****************************************************************************/ |
| 1375 |
FILE *out_file; |
| 1376 |
int i,j,acount,linemod,entry,offset; |
| 1377 |
int totcrd, ptrcrd, indcrd, valcrd, rhscrd; |
| 1378 |
int nvalentries, nrhsentries; |
| 1379 |
int Ptrperline, Ptrwidth, Indperline, Indwidth; |
| 1380 |
int Rhsperline, Rhswidth, Rhsprec; |
| 1381 |
int Rhsflag; |
| 1382 |
int Valperline, Valwidth, Valprec; |
| 1383 |
int Valflag; /* Indicates 'E','D', or 'F' float format */ |
| 1384 |
char pformat[16],iformat[16],vformat[19],rformat[19]; |
| 1385 |
|
| 1386 |
if ( Type[0] == 'C' ) { |
| 1387 |
nvalentries = 2*nz; |
| 1388 |
nrhsentries = 2*M; |
| 1389 |
} else { |
| 1390 |
nvalentries = nz; |
| 1391 |
nrhsentries = M; |
| 1392 |
} |
| 1393 |
|
| 1394 |
if ( filename != NULL ) { |
| 1395 |
if ( (out_file = fopen( filename, "w")) == NULL ) { |
| 1396 |
fprintf(stderr,"Error: Cannot open file: %s\n",filename); |
| 1397 |
return 0; |
| 1398 |
} |
| 1399 |
} else out_file = stdout; |
| 1400 |
|
| 1401 |
if ( Ptrfmt == NULL ) Ptrfmt = "(8I10)"; |
| 1402 |
ParseIfmt(Ptrfmt,&Ptrperline,&Ptrwidth); |
| 1403 |
sprintf(pformat,"%%%dd",Ptrwidth); |
| 1404 |
|
| 1405 |
if ( Indfmt == NULL ) Indfmt = Ptrfmt; |
| 1406 |
ParseIfmt(Indfmt,&Indperline,&Indwidth); |
| 1407 |
sprintf(iformat,"%%%dd",Indwidth); |
| 1408 |
|
| 1409 |
if ( Type[0] != 'P' ) { /* Skip if pattern only */ |
| 1410 |
if ( Valfmt == NULL ) Valfmt = "(4E20.13)"; |
| 1411 |
ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag); |
| 1412 |
sprintf(vformat,"%%%ds",Valwidth); |
| 1413 |
} |
| 1414 |
|
| 1415 |
ptrcrd = (N+1)/Ptrperline; |
| 1416 |
if ( (N+1)%Ptrperline != 0) ptrcrd++; |
| 1417 |
|
| 1418 |
indcrd = nz/Indperline; |
| 1419 |
if ( nz%Indperline != 0) indcrd++; |
| 1420 |
|
| 1421 |
valcrd = nvalentries/Valperline; |
| 1422 |
if ( nvalentries%Valperline != 0) valcrd++; |
| 1423 |
|
| 1424 |
if ( Nrhs > 0 ) { |
| 1425 |
if ( Rhsfmt == NULL ) Rhsfmt = Valfmt; |
| 1426 |
ParseRfmt(Rhsfmt,&Rhsperline,&Rhswidth,&Rhsprec, &Rhsflag); |
| 1427 |
sprintf(rformat,"%%%ds",Rhswidth); |
| 1428 |
rhscrd = nrhsentries/Rhsperline; |
| 1429 |
if ( nrhsentries%Rhsperline != 0) rhscrd++; |
| 1430 |
if ( Rhstype[1] == 'G' ) rhscrd+=rhscrd; |
| 1431 |
if ( Rhstype[2] == 'X' ) rhscrd+=rhscrd; |
| 1432 |
rhscrd*=Nrhs; |
| 1433 |
} else rhscrd = 0; |
| 1434 |
|
| 1435 |
totcrd = 4+ptrcrd+indcrd+valcrd+rhscrd; |
| 1436 |
|
| 1437 |
|
| 1438 |
/* Print header information: */ |
| 1439 |
|
| 1440 |
fprintf(out_file,"%-72s%-8s\n%14d%14d%14d%14d%14d\n",Title, Key, totcrd, |
| 1441 |
ptrcrd, indcrd, valcrd, rhscrd); |
| 1442 |
fprintf(out_file,"%3s%11s%14d%14d%14d\n",Type," ", M, N, nz); |
| 1443 |
fprintf(out_file,"%-16s%-16s%-20s", Ptrfmt, Indfmt, Valfmt); |
| 1444 |
if ( Nrhs != 0 ) { |
| 1445 |
/* Print Rhsfmt on fourth line and */ |
| 1446 |
/* optional fifth header line for auxiliary vector information: */ |
| 1447 |
fprintf(out_file,"%-20s\n%-14s%d\n",Rhsfmt,Rhstype,Nrhs); |
| 1448 |
} else fprintf(out_file,"\n"); |
| 1449 |
|
| 1450 |
offset = 1-_SP_base; /* if base 0 storage is declared (via macro definition), */ |
| 1451 |
/* then storage entries are offset by 1 */ |
| 1452 |
|
| 1453 |
/* Print column pointers: */ |
| 1454 |
for (i=0;i<N+1;i++) |
| 1455 |
{ |
| 1456 |
entry = colptr[i]+offset; |
| 1457 |
fprintf(out_file,pformat,entry); |
| 1458 |
if ( (i+1)%Ptrperline == 0 ) fprintf(out_file,"\n"); |
| 1459 |
} |
| 1460 |
|
| 1461 |
if ( (N+1) % Ptrperline != 0 ) fprintf(out_file,"\n"); |
| 1462 |
|
| 1463 |
/* Print row indices: */ |
| 1464 |
for (i=0;i<nz;i++) |
| 1465 |
{ |
| 1466 |
entry = rowind[i]+offset; |
| 1467 |
fprintf(out_file,iformat,entry); |
| 1468 |
if ( (i+1)%Indperline == 0 ) fprintf(out_file,"\n"); |
| 1469 |
} |
| 1470 |
|
| 1471 |
if ( nz % Indperline != 0 ) fprintf(out_file,"\n"); |
| 1472 |
|
| 1473 |
/* Print values: */ |
| 1474 |
|
| 1475 |
if ( Type[0] != 'P' ) { /* Skip if pattern only */ |
| 1476 |
for (i=0;i<nvalentries;i++) |
| 1477 |
{ |
| 1478 |
fprintf(out_file,vformat,val+i*Valwidth); |
| 1479 |
if ( (i+1)%Valperline == 0 ) fprintf(out_file,"\n"); |
| 1480 |
} |
| 1481 |
|
| 1482 |
if ( nvalentries % Valperline != 0 ) fprintf(out_file,"\n"); |
| 1483 |
|
| 1484 |
/* Print right hand sides: */ |
| 1485 |
acount = 1; |
| 1486 |
linemod=0; |
| 1487 |
if ( Nrhs > 0 ) { |
| 1488 |
for (j=0;j<Nrhs;j++) { |
| 1489 |
for (i=0;i<nrhsentries;i++) |
| 1490 |
{ |
| 1491 |
fprintf(out_file,rformat,rhs+i*Rhswidth); |
| 1492 |
if ( acount++%Rhsperline == linemod ) fprintf(out_file,"\n"); |
| 1493 |
} |
| 1494 |
if ( acount%Rhsperline != linemod ) { |
| 1495 |
fprintf(out_file,"\n"); |
| 1496 |
linemod = (acount-1)%Rhsperline; |
| 1497 |
} |
| 1498 |
if ( Rhstype[1] == 'G' ) { |
| 1499 |
for (i=0;i<nrhsentries;i++) |
| 1500 |
{ |
| 1501 |
fprintf(out_file,rformat,guess+i*Rhswidth); |
| 1502 |
if ( acount++%Rhsperline == linemod ) fprintf(out_file,"\n"); |
| 1503 |
} |
| 1504 |
if ( acount%Rhsperline != linemod ) { |
| 1505 |
fprintf(out_file,"\n"); |
| 1506 |
linemod = (acount-1)%Rhsperline; |
| 1507 |
} |
| 1508 |
} |
| 1509 |
if ( Rhstype[2] == 'X' ) { |
| 1510 |
for (i=0;i<nrhsentries;i++) |
| 1511 |
{ |
| 1512 |
fprintf(out_file,rformat,exact+i*Rhswidth); |
| 1513 |
if ( acount++%Rhsperline == linemod ) fprintf(out_file,"\n"); |
| 1514 |
} |
| 1515 |
if ( acount%Rhsperline != linemod ) { |
| 1516 |
fprintf(out_file,"\n"); |
| 1517 |
linemod = (acount-1)%Rhsperline; |
| 1518 |
} |
| 1519 |
} |
| 1520 |
} |
| 1521 |
} |
| 1522 |
|
| 1523 |
} |
| 1524 |
|
| 1525 |
if ( fclose(out_file) != 0){ |
| 1526 |
fprintf(stderr,"Error closing file in writeHB_mat_char().\n"); |
| 1527 |
return 0; |
| 1528 |
} else return 1; |
| 1529 |
|
| 1530 |
} |
| 1531 |
|
| 1532 |
int ParseIfmt(char* fmt, int* perline, int* width) |
| 1533 |
{ |
| 1534 |
/*************************************************/ |
| 1535 |
/* Parse an *integer* format field to determine */ |
| 1536 |
/* width and number of elements per line. */ |
| 1537 |
/*************************************************/ |
| 1538 |
char *tmp; |
| 1539 |
if (fmt == NULL ) { |
| 1540 |
*perline = 0; *width = 0; return 0; |
| 1541 |
} |
| 1542 |
upcase(fmt); |
| 1543 |
tmp = strchr(fmt,'('); |
| 1544 |
tmp = substr(fmt,tmp - fmt + 1, strchr(fmt,'I') - tmp - 1); |
| 1545 |
*perline = atoi(tmp); |
| 1546 |
tmp = strchr(fmt,'I'); |
| 1547 |
tmp = substr(fmt,tmp - fmt + 1, strchr(fmt,')') - tmp - 1); |
| 1548 |
return *width = atoi(tmp); |
| 1549 |
} |
| 1550 |
|
| 1551 |
int ParseRfmt(char* fmt, int* perline, int* width, int* prec, int* flag) |
| 1552 |
{ |
| 1553 |
/*************************************************/ |
| 1554 |
/* Parse a *real* format field to determine */ |
| 1555 |
/* width and number of elements per line. */ |
| 1556 |
/* Also sets flag indicating 'E' 'F' 'P' or 'D' */ |
| 1557 |
/* format. */ |
| 1558 |
/*************************************************/ |
| 1559 |
char* tmp; |
| 1560 |
char* tmp2; |
| 1561 |
char* tmp3; |
| 1562 |
int len; |
| 1563 |
|
| 1564 |
if (fmt == NULL ) { |
| 1565 |
*perline = 0; |
| 1566 |
*width = 0; |
| 1567 |
flag = NULL; |
| 1568 |
return 0; |
| 1569 |
} |
| 1570 |
|
| 1571 |
upcase(fmt); |
| 1572 |
if (strchr(fmt,'(') != NULL) fmt = strchr(fmt,'('); |
| 1573 |
if (strchr(fmt,')') != NULL) { |
| 1574 |
tmp2 = strchr(fmt,')'); |
| 1575 |
while ( strchr(tmp2+1,')') != NULL ) { |
| 1576 |
tmp2 = strchr(tmp2+1,')'); |
| 1577 |
} |
| 1578 |
*(tmp2+1) = (int) NULL; |
| 1579 |
} |
| 1580 |
if (strchr(fmt,'P') != NULL) /* Remove any scaling factor, which */ |
| 1581 |
{ /* affects output only, not input */ |
| 1582 |
if (strchr(fmt,'(') != NULL) { |
| 1583 |
tmp = strchr(fmt,'P'); |
| 1584 |
if ( *(++tmp) == ',' ) tmp++; |
| 1585 |
tmp3 = strchr(fmt,'(')+1; |
| 1586 |
len = tmp-tmp3; |
| 1587 |
tmp2 = tmp3; |
| 1588 |
while ( *(tmp2+len) != (int) NULL ) { |
| 1589 |
*tmp2=*(tmp2+len); |
| 1590 |
tmp2++; |
| 1591 |
} |
| 1592 |
*(strchr(fmt,')')+1) = (int) NULL; |
| 1593 |
} |
| 1594 |
} |
| 1595 |
if (strchr(fmt,'E') != NULL) { |
| 1596 |
*flag = 'E'; |
| 1597 |
} else if (strchr(fmt,'D') != NULL) { |
| 1598 |
*flag = 'D'; |
| 1599 |
} else if (strchr(fmt,'F') != NULL) { |
| 1600 |
*flag = 'F'; |
| 1601 |
} else { |
| 1602 |
fprintf(stderr,"Real format %s in H/B file not supported.\n",fmt); |
| 1603 |
return 0; |
| 1604 |
} |
| 1605 |
tmp = strchr(fmt,'('); |
| 1606 |
tmp = substr(fmt,tmp - fmt + 1, strchr(fmt,*flag) - tmp - 1); |
| 1607 |
*perline = atoi(tmp); |
| 1608 |
tmp = strchr(fmt,*flag); |
| 1609 |
if ( strchr(fmt,'.') ) { |
| 1610 |
*prec = atoi( substr( fmt, strchr(fmt,'.') - fmt + 1, strchr(fmt,')') - strchr(fmt,'.')-1) ); |
| 1611 |
tmp = substr(fmt,tmp - fmt + 1, strchr(fmt,'.') - tmp - 1); |
| 1612 |
} else { |
| 1613 |
tmp = substr(fmt,tmp - fmt + 1, strchr(fmt,')') - tmp - 1); |
| 1614 |
} |
| 1615 |
return *width = atoi(tmp); |
| 1616 |
} |
| 1617 |
|
| 1618 |
char* substr(const char* S, const int pos, const int len) |
| 1619 |
{ |
| 1620 |
int i; |
| 1621 |
char *SubS; |
| 1622 |
if ( pos+len <= strlen(S)) { |
| 1623 |
SubS = (char *)malloc(len+1); |
| 1624 |
if ( SubS == NULL ) IOHBTerminate("Insufficient memory for SubS."); |
| 1625 |
for (i=0;i<len;i++) SubS[i] = S[pos+i]; |
| 1626 |
SubS[len] = (char) NULL; |
| 1627 |
} else { |
| 1628 |
SubS = NULL; |
| 1629 |
} |
| 1630 |
return SubS; |
| 1631 |
} |
| 1632 |
|
| 1633 |
#include<ctype.h> |
| 1634 |
void upcase(char* S) |
| 1635 |
{ |
| 1636 |
/* Convert S to uppercase */ |
| 1637 |
int i,len; |
| 1638 |
len = strlen(S); |
| 1639 |
for (i=0;i< len;i++) |
| 1640 |
S[i] = toupper(S[i]); |
| 1641 |
} |
| 1642 |
|
| 1643 |
void IOHBTerminate(char* message) |
| 1644 |
{ |
| 1645 |
fprintf(stderr,message); |
| 1646 |
exit(1); |
| 1647 |
} |
| 1648 |
|