📄 iohb.c
字号:
offset = 1-_SP_base; /* if base 0 storage is declared (via macro definition), */
/* then storage entries are offset by 1 */
ThisElement = (char *) malloc(Ptrwidth+1);
if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.");
*(ThisElement+Ptrwidth) = (char) NULL;
count=0;
for (i=0;i<Ptrcrd;i++)
{
fgets(line, BUFSIZ, in_file);
if ( sscanf(line,"%*s") < 0 )
IOHBTerminate("iohb.c: Null (or blank) line in pointer data region of HB file.\n");
col = 0;
for (ind = 0;ind<Ptrperline;ind++)
{
if (count > Ncol) break;
strncpy(ThisElement,line+col,Ptrwidth);
/*ThisElement = substr(line,col,Ptrwidth);*/
colptr[count] = atoi(ThisElement)-offset;
count++; col += Ptrwidth;
}
}
free(ThisElement);
/* Read row index array: */
ThisElement = (char *) malloc(Indwidth+1);
if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.");
*(ThisElement+Indwidth) = (char) NULL;
count = 0;
for (i=0;i<Indcrd;i++)
{
fgets(line, BUFSIZ, in_file);
if ( sscanf(line,"%*s") < 0 )
IOHBTerminate("iohb.c: Null (or blank) line in index data region of HB file.\n");
col = 0;
for (ind = 0;ind<Indperline;ind++)
{
if (count == Nnzero) break;
strncpy(ThisElement,line+col,Indwidth);
/*ThisElement = substr(line,col,Indwidth);*/
rowind[count] = atoi(ThisElement)-offset;
count++; col += Indwidth;
}
}
free(ThisElement);
/* Read array of values: AS CHARACTERS*/
if ( Type[0] != 'P' ) { /* Skip if pattern only */
if ( Type[0] == 'C' ) Nentries = 2*Nnzero;
else Nentries = Nnzero;
ThisElement = (char *) malloc(Valwidth+1);
if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.");
*(ThisElement+Valwidth) = (char) NULL;
count = 0;
for (i=0;i<Valcrd;i++)
{
fgets(line, BUFSIZ, in_file);
if ( sscanf(line,"%*s") < 0 )
IOHBTerminate("iohb.c: Null (or blank) line in value data region of HB file.\n");
if (Valflag == 'D') {
while( strchr(line,'D') ) *strchr(line,'D') = 'E';
}
col = 0;
for (ind = 0;ind<Valperline;ind++)
{
if (count == Nentries) break;
ThisElement = &val[count*Valwidth];
strncpy(ThisElement,line+col,Valwidth);
/*strncpy(ThisElement,substr(line,col,Valwidth),Valwidth);*/
if ( Valflag != 'F' && strchr(ThisElement,'E') == NULL ) {
/* insert a char prefix for exp */
last = strlen(ThisElement);
for (j=last+1;j>=0;j--) {
ThisElement[j] = ThisElement[j-1];
if ( ThisElement[j] == '+' || ThisElement[j] == '-' ) {
ThisElement[j-1] = Valflag;
break;
}
}
}
count++; col += Valwidth;
}
}
}
return 1;
}
int readHB_newmat_char(const char* filename, int* M, int* N, int* nonzeros, int** colptr,
int** rowind, char** val, char** Valfmt)
{
FILE *in_file;
int Nrhs;
int Ptrcrd, Indcrd, Valcrd, Rhscrd;
int Valperline, Valwidth, Valprec;
int Valflag; /* Indicates 'E','D', or 'F' float format */
char Title[73], Key[9], Type[4], Rhstype[4];
char Ptrfmt[17], Indfmt[17], Rhsfmt[21];
if ((in_file = fopen( filename, "r")) == NULL) {
fprintf(stderr,"Error: Cannot open file: %s\n",filename);
return 0;
}
*Valfmt = (char *)malloc(21*sizeof(char));
if ( *Valfmt == NULL ) IOHBTerminate("Insufficient memory for Valfmt.");
readHB_header(in_file, Title, Key, Type, M, N, nonzeros, &Nrhs,
Ptrfmt, Indfmt, (*Valfmt), Rhsfmt,
&Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
fclose(in_file);
ParseRfmt(*Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
*colptr = (int *)malloc((*N+1)*sizeof(int));
if ( *colptr == NULL ) IOHBTerminate("Insufficient memory for colptr.\n");
*rowind = (int *)malloc(*nonzeros*sizeof(int));
if ( *rowind == NULL ) IOHBTerminate("Insufficient memory for rowind.\n");
if ( Type[0] == 'C' ) {
/*
fprintf(stderr, "Warning: Reading complex data from HB file %s.\n",filename);
fprintf(stderr, " Real and imaginary parts will be interlaced in val[].\n");
*/
/* Malloc enough space for real AND imaginary parts of val[] */
*val = (char *)malloc(*nonzeros*Valwidth*sizeof(char)*2);
if ( *val == NULL ) IOHBTerminate("Insufficient memory for val.\n");
} else {
if ( Type[0] != 'P' ) {
/* Malloc enough space for real array val[] */
*val = (char *)malloc(*nonzeros*Valwidth*sizeof(char));
if ( *val == NULL ) IOHBTerminate("Insufficient memory for val.\n");
}
} /* No val[] space needed if pattern only */
return readHB_mat_char(filename, *colptr, *rowind, *val, *Valfmt);
}
int readHB_aux_char(const char* filename, const char AuxType, char b[])
{
/****************************************************************************/
/* This function opens and reads the specified file, placing auxilary */
/* vector(s) of the given type (if available) in b : */
/* Return value is the number of vectors successfully read. */
/* */
/* AuxType = 'F' full right-hand-side vector(s) */
/* AuxType = 'G' initial Guess vector(s) */
/* AuxType = 'X' eXact solution vector(s) */
/* */
/* ---------- */
/* **CAVEAT** */
/* ---------- */
/* Parsing real formats from Fortran is tricky, and this file reader */
/* does not claim to be foolproof. It has been tested for cases when */
/* the real values are printed consistently and evenly spaced on each */
/* line, with Fixed (F), and Exponential (E or D) formats. */
/* */
/* ** If the input file does not adhere to the H/B format, the ** */
/* ** results will be unpredictable. ** */
/* */
/****************************************************************************/
FILE *in_file;
int i,j,n,maxcol,start,stride,col,last,linel,nvecs,rhsi;
int Nrow, Ncol, Nnzero, Nentries,Nrhs;
int Ptrcrd, Indcrd, Valcrd, Rhscrd;
int Rhsperline, Rhswidth, Rhsprec;
int Rhsflag;
char Title[73], Key[9], Type[4], Rhstype[4];
char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
char line[BUFSIZ];
char *ThisElement;
if ((in_file = fopen( filename, "r")) == NULL) {
fprintf(stderr,"Error: Cannot open file: %s\n",filename);
return 0;
}
readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
Ptrfmt, Indfmt, Valfmt, Rhsfmt,
&Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
if (Nrhs <= 0)
{
fprintf(stderr, "Warn: Attempt to read auxillary vector(s) when none are present.\n");
return 0;
}
if (Rhstype[0] != 'F' )
{
fprintf(stderr,"Warn: Attempt to read auxillary vector(s) which are not stored in Full form.\n");
fprintf(stderr," Rhs must be specified as full. \n");
return 0;
}
/* If reading complex data, allow for interleaved real and imaginary values. */
if ( Type[0] == 'C' ) {
Nentries = 2*Nrow;
} else {
Nentries = Nrow;
}
nvecs = 1;
if ( Rhstype[1] == 'G' ) nvecs++;
if ( Rhstype[2] == 'X' ) nvecs++;
if ( AuxType == 'G' && Rhstype[1] != 'G' ) {
fprintf(stderr, "Warn: Attempt to read auxillary Guess vector(s) when none are present.\n");
return 0;
}
if ( AuxType == 'X' && Rhstype[2] != 'X' ) {
fprintf(stderr, "Warn: Attempt to read auxillary eXact solution vector(s) when none are present.\n");
return 0;
}
ParseRfmt(Rhsfmt, &Rhsperline, &Rhswidth, &Rhsprec,&Rhsflag);
maxcol = Rhsperline*Rhswidth;
/* Lines to skip before starting to read RHS values... */
n = Ptrcrd + Indcrd + Valcrd;
for (i = 0; i < n; i++)
fgets(line, BUFSIZ, in_file);
/* start - number of initial aux vector entries to skip */
/* to reach first vector requested */
/* stride - number of aux vector entries to skip between */
/* requested vectors */
if ( AuxType == 'F' ) start = 0;
else if ( AuxType == 'G' ) start = Nentries;
else start = (nvecs-1)*Nentries;
stride = (nvecs-1)*Nentries;
fgets(line, BUFSIZ, in_file);
linel= strchr(line,'\n')-line;
if ( sscanf(line,"%*s") < 0 )
IOHBTerminate("iohb.c: Null (or blank) line in auxillary vector data region of HB file.\n");
col = 0;
/* Skip to initial offset */
for (i=0;i<start;i++) {
col += Rhswidth;
if ( col >= ( maxcol<linel?maxcol:linel ) ) {
fgets(line, BUFSIZ, in_file);
linel= strchr(line,'\n')-line;
if ( sscanf(line,"%*s") < 0 )
IOHBTerminate("iohb.c: Null (or blank) line in auxillary vector data region of HB file.\n");
col = 0;
}
}
if (Rhsflag == 'D') {
while( strchr(line,'D') ) *strchr(line,'D') = 'E';
}
/* Read a vector of desired type, then skip to next */
/* repeating to fill Nrhs vectors */
for (rhsi=0;rhsi<Nrhs;rhsi++) {
for (i=0;i<Nentries;i++) {
if ( col >= ( maxcol<linel?maxcol:linel ) ) {
fgets(line, BUFSIZ, in_file);
linel= strchr(line,'\n')-line;
if ( sscanf(line,"%*s") < 0 )
IOHBTerminate("iohb.c: Null (or blank) line in auxillary vector data region of HB file.\n");
if (Rhsflag == 'D') {
while( strchr(line,'D') ) *strchr(line,'D') = 'E';
}
col = 0;
}
ThisElement = &b[i*Rhswidth];
strncpy(ThisElement,line+col,Rhswidth);
if ( Rhsflag != 'F' && strchr(ThisElement,'E') == NULL ) {
/* insert a char prefix for exp */
last = strlen(ThisElement);
for (j=last+1;j>=0;j--) {
ThisElement[j] = ThisElement[j-1];
if ( ThisElement[j] == '+' || ThisElement[j] == '-' ) {
ThisElement[j-1] = Rhsflag;
break;
}
}
}
col += Rhswidth;
}
b+=Nentries*Rhswidth;
/* Skip any interleaved Guess/eXact vectors */
for (i=0;i<stride;i++) {
col += Rhswidth;
if ( col >= ( maxcol<linel?maxcol:linel ) ) {
fgets(line, BUFSIZ, in_file);
linel= strchr(line,'\n')-line;
if ( sscanf(line,"%*s") < 0 )
IOHBTerminate("iohb.c: Null (or blank) line in auxillary vector data region of HB file.\n");
col = 0;
}
}
}
fclose(in_file);
return Nrhs;
}
int readHB_newaux_char(const char* filename, const char AuxType, char** b, char** Rhsfmt)
{
FILE *in_file;
int Ptrcrd, Indcrd, Valcrd, Rhscrd;
int Nrow,Ncol,Nnzero,Nrhs;
int Rhsperline, Rhswidth, Rhsprec;
int Rhsflag;
char Title[73], Key[9], Type[4], Rhstype[4];
char Ptrfmt[17], Indfmt[17], Valfmt[21];
if ((in_file = fopen( filename, "r")) == NULL) {
fprintf(stderr,"Error: Cannot open file: %s\n",filename);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -