224 char*
substr(
const char* S,
const int pos,
const int len);
228 int readHB_info(
const char* filename,
int* M,
int*
N,
int* nz,
char** Type,
249 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
250 int Nrow, Ncol, Nnzero;
252 char Title[73], Key[9], Rhstype[4];
253 char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
255 mat_type = (
char *) malloc(4);
256 if ( mat_type == NULL )
IOHBTerminate(
"Insufficient memory for mat_typen");
258 if ( (in_file = fopen( filename,
"r")) == NULL ) {
259 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
263 readHB_header(in_file, Title, Key, mat_type, &Nrow, &Ncol, &Nnzero, Nrhs,
264 Ptrfmt, Indfmt, Valfmt, Rhsfmt,
265 &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
268 *(*Type+3) = (
char) NULL;
272 if (Rhscrd == 0) {*Nrhs = 0;}
294 int* Nrow,
int* Ncol,
int* Nnzero,
int* Nrhs,
295 char* Ptrfmt,
char* Indfmt,
char* Valfmt,
char* Rhsfmt,
296 int* Ptrcrd,
int* Indcrd,
int* Valcrd,
int* Rhscrd,
302 int Totcrd,Neltvl,Nrhsix;
306 fgets(line, BUFSIZ, in_file);
307 if ( sscanf(line,
"%*s") < 0 )
308 IOHBTerminate(
"iohb.c: Null (or blank) first line of HB file.\n");
309 (void) sscanf(line,
"%72c%8[^\n]", Title, Key);
310 *(Key+8) = (
char) NULL;
311 *(Title+72) = (
char) NULL;
314 fgets(line, BUFSIZ, in_file);
315 if ( sscanf(line,
"%*s") < 0 )
316 IOHBTerminate(
"iohb.c: Null (or blank) second line of HB file.\n");
317 if ( sscanf(line,
"%i",&Totcrd) != 1) Totcrd = 0;
318 if ( sscanf(line,
"%*i%i",Ptrcrd) != 1) *Ptrcrd = 0;
319 if ( sscanf(line,
"%*i%*i%i",Indcrd) != 1) *Indcrd = 0;
320 if ( sscanf(line,
"%*i%*i%*i%i",Valcrd) != 1) *Valcrd = 0;
321 if ( sscanf(line,
"%*i%*i%*i%*i%i",Rhscrd) != 1) *Rhscrd = 0;
324 fgets(line, BUFSIZ, in_file);
325 if ( sscanf(line,
"%*s") < 0 )
326 IOHBTerminate(
"iohb.c: Null (or blank) third line of HB file.\n");
327 if ( sscanf(line,
"%3c", Type) != 1)
328 IOHBTerminate(
"iohb.c: Invalid Type info, line 3 of Harwell-Boeing file.\n");
330 if ( sscanf(line,
"%*3c%i",Nrow) != 1) *Nrow = 0 ;
331 if ( sscanf(line,
"%*3c%*i%i",Ncol) != 1) *Ncol = 0 ;
332 if ( sscanf(line,
"%*3c%*i%*i%i",Nnzero) != 1) *Nnzero = 0 ;
333 if ( sscanf(line,
"%*3c%*i%*i%*i%i",&Neltvl) != 1) Neltvl = 0 ;
336 fgets(line, BUFSIZ, in_file);
337 if ( sscanf(line,
"%*s") < 0 )
338 IOHBTerminate(
"iohb.c: Null (or blank) fourth line of HB file.\n");
339 if ( sscanf(line,
"%16c",Ptrfmt) != 1)
340 IOHBTerminate(
"iohb.c: Invalid format info, line 4 of Harwell-Boeing file.\n");
341 if ( sscanf(line,
"%*16c%16c",Indfmt) != 1)
342 IOHBTerminate(
"iohb.c: Invalid format info, line 4 of Harwell-Boeing file.\n");
343 if ( sscanf(line,
"%*16c%*16c%20c",Valfmt) != 1)
344 IOHBTerminate(
"iohb.c: Invalid format info, line 4 of Harwell-Boeing file.\n");
345 sscanf(line,
"%*16c%*16c%*20c%20c",Rhsfmt);
346 *(Ptrfmt+16) = (
char) NULL;
347 *(Indfmt+16) = (
char) NULL;
348 *(Valfmt+20) = (
char) NULL;
349 *(Rhsfmt+20) = (
char) NULL;
354 fgets(line, BUFSIZ, in_file);
355 if ( sscanf(line,
"%*s") < 0 )
356 IOHBTerminate(
"iohb.c: Null (or blank) fifth line of HB file.\n");
357 if ( sscanf(line,
"%3c", Rhstype) != 1)
358 IOHBTerminate(
"iohb.c: Invalid RHS type information, line 5 of Harwell-Boeing file.\n");
359 if ( sscanf(line,
"%*3c%i", Nrhs) != 1) *Nrhs = 0;
360 if ( sscanf(line,
"%*3c%*i%i", &Nrhsix) != 1) Nrhsix = 0;
388 int i,j,ind,col,offset,count,last,Nrhs;
389 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
390 int Nrow, Ncol, Nnzero, Nentries;
391 int Ptrperline, Ptrwidth, Indperline, Indwidth;
392 int Valperline, Valwidth, Valprec;
395 char Title[73], Key[8], Type[4] =
"XXX\0", Rhstype[4];
396 char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
399 if ( (in_file = fopen( filename,
"r")) == NULL ) {
400 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
404 readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
405 Ptrfmt, Indfmt, Valfmt, Rhsfmt,
406 &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
411 if ( Type[0] !=
'P' ) {
412 ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
420 ThisElement = (
char *) malloc(Ptrwidth+1);
421 if ( ThisElement == NULL )
IOHBTerminate(
"Insufficient memory for ThisElement.");
422 *(ThisElement+Ptrwidth) = (
char) NULL;
424 for (i=0;i<Ptrcrd;i++)
426 fgets(line, BUFSIZ, in_file);
427 if ( sscanf(line,
"%*s") < 0 )
428 IOHBTerminate(
"iohb.c: Null (or blank) line in pointer data region of HB file.\n");
430 for (ind = 0;ind<Ptrperline;ind++)
432 if (count > Ncol)
break;
433 strncpy(ThisElement,line+col,Ptrwidth);
435 colptr[count] = atoi(ThisElement)-offset;
436 count++; col += Ptrwidth;
443 ThisElement = (
char *) malloc(Indwidth+1);
444 if ( ThisElement == NULL )
IOHBTerminate(
"Insufficient memory for ThisElement.");
445 *(ThisElement+Indwidth) = (
char) NULL;
447 for (i=0;i<Indcrd;i++)
449 fgets(line, BUFSIZ, in_file);
450 if ( sscanf(line,
"%*s") < 0 )
451 IOHBTerminate(
"iohb.c: Null (or blank) line in index data region of HB file.\n");
453 for (ind = 0;ind<Indperline;ind++)
455 if (count == Nnzero)
break;
456 strncpy(ThisElement,line+col,Indwidth);
458 rowind[count] = atoi(ThisElement)-offset;
459 count++; col += Indwidth;
466 if ( Type[0] !=
'P' ) {
468 if ( Type[0] ==
'C' ) Nentries = 2*Nnzero;
469 else Nentries = Nnzero;
471 ThisElement = (
char *) malloc(Valwidth+1);
472 if ( ThisElement == NULL )
IOHBTerminate(
"Insufficient memory for ThisElement.");
473 *(ThisElement+Valwidth) = (
char) NULL;
475 for (i=0;i<Valcrd;i++)
477 fgets(line, BUFSIZ, in_file);
478 if ( sscanf(line,
"%*s") < 0 )
479 IOHBTerminate(
"iohb.c: Null (or blank) line in value data region of HB file.\n");
480 if (Valflag ==
'D') {
481 while( strchr(line,
'D') ) *strchr(line,
'D') =
'E';
485 for (ind = 0;ind<Valperline;ind++)
487 if (count == Nentries)
break;
488 strncpy(ThisElement,line+col,Valwidth);
490 if ( Valflag !=
'F' && strchr(ThisElement,
'E') == NULL ) {
492 last = strlen(ThisElement);
493 for (j=last+1;j>=0;j--) {
494 ThisElement[j] = ThisElement[j-1];
495 if ( ThisElement[j] ==
'+' || ThisElement[j] ==
'-' ) {
496 ThisElement[j-1] = Valflag;
501 val[count] = atof(ThisElement);
502 count++; col += Valwidth;
513 int** colptr,
int** rowind,
double** val)
518 readHB_info(filename, M, N, nonzeros, &Type, &Nrhs);
520 *colptr = (
int *)malloc((*N+1)*
sizeof(int));
521 if ( *colptr == NULL )
IOHBTerminate(
"Insufficient memory for colptr.\n");
522 *rowind = (
int *)malloc(*nonzeros*
sizeof(
int));
523 if ( *rowind == NULL )
IOHBTerminate(
"Insufficient memory for rowind.\n");
524 if ( Type[0] ==
'C' ) {
530 *val = (
double *)malloc(*nonzeros*
sizeof(
double)*2);
531 if ( *val == NULL )
IOHBTerminate(
"Insufficient memory for val.\n");
533 if ( Type[0] !=
'P' ) {
535 *val = (
double *)malloc(*nonzeros*
sizeof(
double));
536 if ( *val == NULL )
IOHBTerminate(
"Insufficient memory for val.\n");
567 int i,j,
n,maxcol,start,stride,col,last,linel;
568 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
569 int Nrow, Ncol, Nnzero, Nentries;
570 int Nrhs, nvecs, rhsi;
571 int Rhsperline, Rhswidth, Rhsprec;
574 char Title[73], Key[9], Type[4] =
"XXX\0", Rhstype[4];
575 char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
578 if ((in_file = fopen( filename,
"r")) == NULL) {
579 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
583 readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
584 Ptrfmt, Indfmt, Valfmt, Rhsfmt,
585 &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
589 fprintf(stderr,
"Warn: Attempt to read auxillary vector(s) when none are present.\n");
592 if (Rhstype[0] !=
'F' )
594 fprintf(stderr,
"Warn: Attempt to read auxillary vector(s) which are not stored in Full form.\n");
595 fprintf(stderr,
" Rhs must be specified as full. \n");
600 if ( Type[0] ==
'C' ) {
608 if ( Rhstype[1] ==
'G' ) nvecs++;
609 if ( Rhstype[2] ==
'X' ) nvecs++;
611 if ( AuxType ==
'G' && Rhstype[1] !=
'G' ) {
612 fprintf(stderr,
"Warn: Attempt to read auxillary Guess vector(s) when none are present.\n");
615 if ( AuxType ==
'X' && Rhstype[2] !=
'X' ) {
616 fprintf(stderr,
"Warn: Attempt to read auxillary eXact solution vector(s) when none are present.\n");
620 ParseRfmt(Rhsfmt, &Rhsperline, &Rhswidth, &Rhsprec,&Rhsflag);
621 maxcol = Rhsperline*Rhswidth;
624 n = Ptrcrd + Indcrd + Valcrd;
626 for (i = 0; i < n; i++)
627 fgets(line, BUFSIZ, in_file);
633 if ( AuxType ==
'F' ) start = 0;
634 else if ( AuxType ==
'G' ) start = Nentries;
635 else start = (nvecs-1)*Nentries;
636 stride = (nvecs-1)*Nentries;
638 fgets(line, BUFSIZ, in_file);
639 linel= strchr(line,
'\n')-line;
643 for (i=0;i<start;i++) {
644 if ( col >= ( maxcol<linel?maxcol:linel ) ) {
645 fgets(line, BUFSIZ, in_file);
646 linel= strchr(line,
'\n')-line;
651 if (Rhsflag ==
'D') {
652 while( strchr(line,
'D') ) *strchr(line,
'D') =
'E';
658 ThisElement = (
char *) malloc(Rhswidth+1);
659 if ( ThisElement == NULL )
IOHBTerminate(
"Insufficient memory for ThisElement.");
660 *(ThisElement+Rhswidth) = (
char) NULL;
661 for (rhsi=0;rhsi<Nrhs;rhsi++) {
663 for (i=0;i<Nentries;i++) {
664 if ( col >= ( maxcol<linel?maxcol:linel ) ) {
665 fgets(line, BUFSIZ, in_file);
666 linel= strchr(line,
'\n')-line;
667 if (Rhsflag ==
'D') {
668 while( strchr(line,
'D') ) *strchr(line,
'D') =
'E';
672 strncpy(ThisElement,line+col,Rhswidth);
674 if ( Rhsflag !=
'F' && strchr(ThisElement,
'E') == NULL ) {
676 last = strlen(ThisElement);
677 for (j=last+1;j>=0;j--) {
678 ThisElement[j] = ThisElement[j-1];
679 if ( ThisElement[j] ==
'+' || ThisElement[j] ==
'-' ) {
680 ThisElement[j-1] = Rhsflag;
685 b[i] = atof(ThisElement);
691 for (i=0;i<stride;i++) {
692 if ( col >= ( maxcol<linel?maxcol:linel ) ) {
693 fgets(line, BUFSIZ, in_file);
694 linel= strchr(line,
'\n')-line;
710 int Nrhs,M,
N,nonzeros;
713 readHB_info(filename, &M, &N, &nonzeros, &Type, &Nrhs);
715 fprintf(stderr,
"Warn: Requested read of aux vector(s) when none are present.\n");
718 if ( Type[0] ==
'C' ) {
719 fprintf(stderr,
"Warning: Reading complex aux vector(s) from HB file %s.",filename);
720 fprintf(stderr,
" Real and imaginary parts will be interlaced in b[].");
721 *b = (
double *)malloc(M*Nrhs*
sizeof(
double)*2);
722 if ( *b == NULL )
IOHBTerminate(
"Insufficient memory for rhs.\n");
725 *b = (
double *)malloc(M*Nrhs*
sizeof(
double));
726 if ( *b == NULL )
IOHBTerminate(
"Insufficient memory for rhs.\n");
733 int nz,
const int colptr[],
const int rowind[],
734 const double val[],
int Nrhs,
const double rhs[],
735 const double guess[],
const double exact[],
736 const char* Title,
const char* Key,
const char* Type,
737 char* Ptrfmt,
char* Indfmt,
char* Valfmt,
char* Rhsfmt,
750 int i,j,entry,offset,acount,linemod;
751 int totcrd, ptrcrd, indcrd, valcrd, rhscrd;
752 int nvalentries, nrhsentries;
753 int Ptrperline, Ptrwidth, Indperline, Indwidth;
754 int Rhsperline, Rhswidth, Rhsprec;
756 int Valperline, Valwidth, Valprec;
758 char pformat[16],iformat[16],vformat[19],rformat[19];
760 if ( Type[0] ==
'C' ) {
768 if ( filename != NULL ) {
769 if ( (out_file = fopen( filename,
"w")) == NULL ) {
770 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
773 }
else out_file = stdout;
775 if ( Ptrfmt == NULL ) Ptrfmt =
"(8I10)";
777 sprintf(pformat,
"%%%dd",Ptrwidth);
778 ptrcrd = (N+1)/Ptrperline;
779 if ( (N+1)%Ptrperline != 0) ptrcrd++;
781 if ( Indfmt == NULL ) Indfmt = Ptrfmt;
783 sprintf(iformat,
"%%%dd",Indwidth);
784 indcrd = nz/Indperline;
785 if ( nz%Indperline != 0) indcrd++;
787 if ( Type[0] !=
'P' ) {
788 if ( Valfmt == NULL ) Valfmt =
"(4E20.13)";
789 ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
790 if (Valflag ==
'D') *strchr(Valfmt,
'D') =
'E';
792 sprintf(vformat,
"%% %d.%df",Valwidth,Valprec);
794 sprintf(vformat,
"%% %d.%dE",Valwidth,Valprec);
795 valcrd = nvalentries/Valperline;
796 if ( nvalentries%Valperline != 0) valcrd++;
800 if ( Rhsfmt == NULL ) Rhsfmt = Valfmt;
801 ParseRfmt(Rhsfmt,&Rhsperline,&Rhswidth,&Rhsprec, &Rhsflag);
803 sprintf(rformat,
"%% %d.%df",Rhswidth,Rhsprec);
805 sprintf(rformat,
"%% %d.%dE",Rhswidth,Rhsprec);
806 if (Rhsflag ==
'D') *strchr(Rhsfmt,
'D') =
'E';
807 rhscrd = nrhsentries/Rhsperline;
808 if ( nrhsentries%Rhsperline != 0) rhscrd++;
809 if ( Rhstype[1] ==
'G' ) rhscrd+=rhscrd;
810 if ( Rhstype[2] ==
'X' ) rhscrd+=rhscrd;
814 totcrd = 4+ptrcrd+indcrd+valcrd+rhscrd;
819 fprintf(out_file,
"%-72s%-8s\n%14d%14d%14d%14d%14d\n",Title, Key, totcrd,
820 ptrcrd, indcrd, valcrd, rhscrd);
821 fprintf(out_file,
"%3s%11s%14d%14d%14d\n",Type,
" ", M, N, nz);
822 fprintf(out_file,
"%-16s%-16s%-20s", Ptrfmt, Indfmt, Valfmt);
826 fprintf(out_file,
"%-20s\n%-14s%d\n",Rhsfmt,Rhstype,Nrhs);
827 }
else fprintf(out_file,
"\n");
835 entry = colptr[i]+offset;
836 fprintf(out_file,pformat,entry);
837 if ( (i+1)%Ptrperline == 0 ) fprintf(out_file,
"\n");
840 if ( (N+1) % Ptrperline != 0 ) fprintf(out_file,
"\n");
845 entry = rowind[i]+offset;
846 fprintf(out_file,iformat,entry);
847 if ( (i+1)%Indperline == 0 ) fprintf(out_file,
"\n");
850 if ( nz % Indperline != 0 ) fprintf(out_file,
"\n");
854 if ( Type[0] !=
'P' ) {
856 for (i=0;i<nvalentries;i++)
858 fprintf(out_file,vformat,val[i]);
859 if ( (i+1)%Valperline == 0 ) fprintf(out_file,
"\n");
862 if ( nvalentries % Valperline != 0 ) fprintf(out_file,
"\n");
871 for ( j=0;j<nrhsentries;j++ ) {
872 fprintf(out_file,rformat,rhs[j]);
873 if ( acount++%Rhsperline == linemod ) fprintf(out_file,
"\n");
875 if ( (acount-1)%Rhsperline != linemod ) {
876 fprintf(out_file,
"\n");
877 linemod = (acount-1)%Rhsperline;
880 if ( Rhstype[1] ==
'G' ) {
881 for ( j=0;j<nrhsentries;j++ ) {
882 fprintf(out_file,rformat,guess[j]);
883 if ( acount++%Rhsperline == linemod ) fprintf(out_file,
"\n");
885 if ( (acount-1)%Rhsperline != linemod ) {
886 fprintf(out_file,
"\n");
887 linemod = (acount-1)%Rhsperline;
889 guess += nrhsentries;
891 if ( Rhstype[2] ==
'X' ) {
892 for ( j=0;j<nrhsentries;j++ ) {
893 fprintf(out_file,rformat,exact[j]);
894 if ( acount++%Rhsperline == linemod ) fprintf(out_file,
"\n");
896 if ( (acount-1)%Rhsperline != linemod ) {
897 fprintf(out_file,
"\n");
898 linemod = (acount-1)%Rhsperline;
900 exact += nrhsentries;
907 if ( fclose(out_file) != 0){
908 fprintf(stderr,
"Error closing file in writeHB_mat_double().\n");
915 char val[],
char* Valfmt)
936 int i,j,ind,col,offset,count,last;
937 int Nrow,Ncol,Nnzero,Nentries,Nrhs;
938 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
939 int Ptrperline, Ptrwidth, Indperline, Indwidth;
940 int Valperline, Valwidth, Valprec;
944 char Title[73], Key[8], Type[4] =
"XXX\0", Rhstype[4];
945 char Ptrfmt[17], Indfmt[17], Rhsfmt[21];
947 if ( (in_file = fopen( filename,
"r")) == NULL ) {
948 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
952 readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
953 Ptrfmt, Indfmt, Valfmt, Rhsfmt,
954 &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
959 if ( Type[0] !=
'P' ) {
960 ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
961 if (Valflag ==
'D') {
962 *strchr(Valfmt,
'D') =
'E';
971 ThisElement = (
char *) malloc(Ptrwidth+1);
972 if ( ThisElement == NULL )
IOHBTerminate(
"Insufficient memory for ThisElement.");
973 *(ThisElement+Ptrwidth) = (
char) NULL;
975 for (i=0;i<Ptrcrd;i++)
977 fgets(line, BUFSIZ, in_file);
978 if ( sscanf(line,
"%*s") < 0 )
979 IOHBTerminate(
"iohb.c: Null (or blank) line in pointer data region of HB file.\n");
981 for (ind = 0;ind<Ptrperline;ind++)
983 if (count > Ncol)
break;
984 strncpy(ThisElement,line+col,Ptrwidth);
986 colptr[count] = atoi(ThisElement)-offset;
987 count++; col += Ptrwidth;
994 ThisElement = (
char *) malloc(Indwidth+1);
995 if ( ThisElement == NULL )
IOHBTerminate(
"Insufficient memory for ThisElement.");
996 *(ThisElement+Indwidth) = (
char) NULL;
998 for (i=0;i<Indcrd;i++)
1000 fgets(line, BUFSIZ, in_file);
1001 if ( sscanf(line,
"%*s") < 0 )
1002 IOHBTerminate(
"iohb.c: Null (or blank) line in index data region of HB file.\n");
1004 for (ind = 0;ind<Indperline;ind++)
1006 if (count == Nnzero)
break;
1007 strncpy(ThisElement,line+col,Indwidth);
1009 rowind[count] = atoi(ThisElement)-offset;
1010 count++; col += Indwidth;
1017 if ( Type[0] !=
'P' ) {
1019 if ( Type[0] ==
'C' ) Nentries = 2*Nnzero;
1020 else Nentries = Nnzero;
1022 ThisElement = (
char *) malloc(Valwidth+1);
1023 if ( ThisElement == NULL )
IOHBTerminate(
"Insufficient memory for ThisElement.");
1024 *(ThisElement+Valwidth) = (
char) NULL;
1026 for (i=0;i<Valcrd;i++)
1028 fgets(line, BUFSIZ, in_file);
1029 if ( sscanf(line,
"%*s") < 0 )
1030 IOHBTerminate(
"iohb.c: Null (or blank) line in value data region of HB file.\n");
1031 if (Valflag ==
'D') {
1032 while( strchr(line,
'D') ) *strchr(line,
'D') =
'E';
1035 for (ind = 0;ind<Valperline;ind++)
1037 if (count == Nentries)
break;
1038 ThisElement = &val[count*Valwidth];
1039 strncpy(ThisElement,line+col,Valwidth);
1041 if ( Valflag !=
'F' && strchr(ThisElement,
'E') == NULL ) {
1043 last = strlen(ThisElement);
1044 for (j=last+1;j>=0;j--) {
1045 ThisElement[j] = ThisElement[j-1];
1046 if ( ThisElement[j] ==
'+' || ThisElement[j] ==
'-' ) {
1047 ThisElement[j-1] = Valflag;
1052 count++; col += Valwidth;
1061 int** rowind,
char** val,
char** Valfmt)
1065 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
1066 int Valperline, Valwidth, Valprec;
1068 char Title[73], Key[9], Type[4] =
"XXX\0", Rhstype[4];
1069 char Ptrfmt[17], Indfmt[17], Rhsfmt[21];
1071 if ((in_file = fopen( filename,
"r")) == NULL) {
1072 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
1076 *Valfmt = (
char *)malloc(21*
sizeof(
char));
1077 if ( *Valfmt == NULL )
IOHBTerminate(
"Insufficient memory for Valfmt.");
1078 readHB_header(in_file, Title, Key, Type, M, N, nonzeros, &Nrhs,
1079 Ptrfmt, Indfmt, (*Valfmt), Rhsfmt,
1080 &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
1082 ParseRfmt(*Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
1084 *colptr = (
int *)malloc((*N+1)*
sizeof(int));
1085 if ( *colptr == NULL )
IOHBTerminate(
"Insufficient memory for colptr.\n");
1086 *rowind = (
int *)malloc(*nonzeros*
sizeof(
int));
1087 if ( *rowind == NULL )
IOHBTerminate(
"Insufficient memory for rowind.\n");
1088 if ( Type[0] ==
'C' ) {
1094 *val = (
char *)malloc(*nonzeros*Valwidth*
sizeof(
char)*2);
1095 if ( *val == NULL )
IOHBTerminate(
"Insufficient memory for val.\n");
1097 if ( Type[0] !=
'P' ) {
1099 *val = (
char *)malloc(*nonzeros*Valwidth*
sizeof(
char));
1100 if ( *val == NULL )
IOHBTerminate(
"Insufficient memory for val.\n");
1131 int i,j,
n,maxcol,start,stride,col,last,linel,nvecs,rhsi;
1132 int Nrow, Ncol, Nnzero, Nentries,Nrhs;
1133 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
1134 int Rhsperline, Rhswidth, Rhsprec;
1136 char Title[73], Key[9], Type[4] =
"XXX\0", Rhstype[4];
1137 char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
1141 if ((in_file = fopen( filename,
"r")) == NULL) {
1142 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
1146 readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
1147 Ptrfmt, Indfmt, Valfmt, Rhsfmt,
1148 &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
1152 fprintf(stderr,
"Warn: Attempt to read auxillary vector(s) when none are present.\n");
1155 if (Rhstype[0] !=
'F' )
1157 fprintf(stderr,
"Warn: Attempt to read auxillary vector(s) which are not stored in Full form.\n");
1158 fprintf(stderr,
" Rhs must be specified as full. \n");
1163 if ( Type[0] ==
'C' ) {
1171 if ( Rhstype[1] ==
'G' ) nvecs++;
1172 if ( Rhstype[2] ==
'X' ) nvecs++;
1174 if ( AuxType ==
'G' && Rhstype[1] !=
'G' ) {
1175 fprintf(stderr,
"Warn: Attempt to read auxillary Guess vector(s) when none are present.\n");
1178 if ( AuxType ==
'X' && Rhstype[2] !=
'X' ) {
1179 fprintf(stderr,
"Warn: Attempt to read auxillary eXact solution vector(s) when none are present.\n");
1183 ParseRfmt(Rhsfmt, &Rhsperline, &Rhswidth, &Rhsprec,&Rhsflag);
1184 maxcol = Rhsperline*Rhswidth;
1187 n = Ptrcrd + Indcrd + Valcrd;
1189 for (i = 0; i < n; i++)
1190 fgets(line, BUFSIZ, in_file);
1196 if ( AuxType ==
'F' ) start = 0;
1197 else if ( AuxType ==
'G' ) start = Nentries;
1198 else start = (nvecs-1)*Nentries;
1199 stride = (nvecs-1)*Nentries;
1201 fgets(line, BUFSIZ, in_file);
1202 linel= strchr(line,
'\n')-line;
1203 if ( sscanf(line,
"%*s") < 0 )
1204 IOHBTerminate(
"iohb.c: Null (or blank) line in auxillary vector data region of HB file.\n");
1208 for (i=0;i<start;i++) {
1210 if ( col >= ( maxcol<linel?maxcol:linel ) ) {
1211 fgets(line, BUFSIZ, in_file);
1212 linel= strchr(line,
'\n')-line;
1213 if ( sscanf(line,
"%*s") < 0 )
1214 IOHBTerminate(
"iohb.c: Null (or blank) line in auxillary vector data region of HB file.\n");
1219 if (Rhsflag ==
'D') {
1220 while( strchr(line,
'D') ) *strchr(line,
'D') =
'E';
1225 for (rhsi=0;rhsi<Nrhs;rhsi++) {
1227 for (i=0;i<Nentries;i++) {
1228 if ( col >= ( maxcol<linel?maxcol:linel ) ) {
1229 fgets(line, BUFSIZ, in_file);
1230 linel= strchr(line,
'\n')-line;
1231 if ( sscanf(line,
"%*s") < 0 )
1232 IOHBTerminate(
"iohb.c: Null (or blank) line in auxillary vector data region of HB file.\n");
1233 if (Rhsflag ==
'D') {
1234 while( strchr(line,
'D') ) *strchr(line,
'D') =
'E';
1238 ThisElement = &b[i*Rhswidth];
1239 strncpy(ThisElement,line+col,Rhswidth);
1240 if ( Rhsflag !=
'F' && strchr(ThisElement,
'E') == NULL ) {
1242 last = strlen(ThisElement);
1243 for (j=last+1;j>=0;j--) {
1244 ThisElement[j] = ThisElement[j-1];
1245 if ( ThisElement[j] ==
'+' || ThisElement[j] ==
'-' ) {
1246 ThisElement[j-1] = Rhsflag;
1253 b+=Nentries*Rhswidth;
1257 for (i=0;i<stride;i++) {
1259 if ( col >= ( maxcol<linel?maxcol:linel ) ) {
1260 fgets(line, BUFSIZ, in_file);
1261 linel= strchr(line,
'\n')-line;
1262 if ( sscanf(line,
"%*s") < 0 )
1263 IOHBTerminate(
"iohb.c: Null (or blank) line in auxillary vector data region of HB file.\n");
1278 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
1279 int Nrow,Ncol,Nnzero,Nrhs;
1280 int Rhsperline, Rhswidth, Rhsprec;
1282 char Title[73], Key[9], Type[4] =
"XXX\0", Rhstype[4];
1283 char Ptrfmt[17], Indfmt[17], Valfmt[21];
1285 if ((in_file = fopen( filename,
"r")) == NULL) {
1286 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
1290 *Rhsfmt = (
char *)malloc(21*
sizeof(
char));
1291 if ( *Rhsfmt == NULL )
IOHBTerminate(
"Insufficient memory for Rhsfmt.");
1292 readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
1293 Ptrfmt, Indfmt, Valfmt, (*Rhsfmt),
1294 &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
1297 fprintf(stderr,
"Warn: Requested read of aux vector(s) when none are present.\n");
1300 ParseRfmt(*Rhsfmt,&Rhsperline,&Rhswidth,&Rhsprec,&Rhsflag);
1301 if ( Type[0] ==
'C' ) {
1302 fprintf(stderr,
"Warning: Reading complex aux vector(s) from HB file %s.",filename);
1303 fprintf(stderr,
" Real and imaginary parts will be interlaced in b[].");
1304 *b = (
char *)malloc(Nrow*Nrhs*Rhswidth*
sizeof(
char)*2);
1305 if ( *b == NULL )
IOHBTerminate(
"Insufficient memory for rhs.\n");
1308 *b = (
char *)malloc(Nrow*Nrhs*Rhswidth*
sizeof(
char));
1309 if ( *b == NULL )
IOHBTerminate(
"Insufficient memory for rhs.\n");
1316 int nz,
const int colptr[],
const int rowind[],
1317 const char val[],
int Nrhs,
const char rhs[],
1318 const char guess[],
const char exact[],
1319 const char* Title,
const char* Key,
const char* Type,
1320 char* Ptrfmt,
char* Indfmt,
char* Valfmt,
char* Rhsfmt,
1321 const char* Rhstype)
1333 int i,j,acount,linemod,entry,offset;
1334 int totcrd, ptrcrd, indcrd, valcrd, rhscrd;
1335 int nvalentries, nrhsentries;
1336 int Ptrperline, Ptrwidth, Indperline, Indwidth;
1337 int Rhsperline, Rhswidth, Rhsprec;
1339 int Valperline, Valwidth, Valprec;
1341 char pformat[16],iformat[16],vformat[19],rformat[19];
1343 if ( Type[0] ==
'C' ) {
1351 if ( filename != NULL ) {
1352 if ( (out_file = fopen( filename,
"w")) == NULL ) {
1353 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
1356 }
else out_file = stdout;
1358 if ( Ptrfmt == NULL ) Ptrfmt =
"(8I10)";
1359 ParseIfmt(Ptrfmt,&Ptrperline,&Ptrwidth);
1360 sprintf(pformat,
"%%%dd",Ptrwidth);
1362 if ( Indfmt == NULL ) Indfmt = Ptrfmt;
1363 ParseIfmt(Indfmt,&Indperline,&Indwidth);
1364 sprintf(iformat,
"%%%dd",Indwidth);
1366 if ( Type[0] !=
'P' ) {
1367 if ( Valfmt == NULL ) Valfmt =
"(4E20.13)";
1368 ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
1369 sprintf(vformat,
"%%%ds",Valwidth);
1372 ptrcrd = (N+1)/Ptrperline;
1373 if ( (N+1)%Ptrperline != 0) ptrcrd++;
1375 indcrd = nz/Indperline;
1376 if ( nz%Indperline != 0) indcrd++;
1378 valcrd = nvalentries/Valperline;
1379 if ( nvalentries%Valperline != 0) valcrd++;
1382 if ( Rhsfmt == NULL ) Rhsfmt = Valfmt;
1383 ParseRfmt(Rhsfmt,&Rhsperline,&Rhswidth,&Rhsprec, &Rhsflag);
1384 sprintf(rformat,
"%%%ds",Rhswidth);
1385 rhscrd = nrhsentries/Rhsperline;
1386 if ( nrhsentries%Rhsperline != 0) rhscrd++;
1387 if ( Rhstype[1] ==
'G' ) rhscrd+=rhscrd;
1388 if ( Rhstype[2] ==
'X' ) rhscrd+=rhscrd;
1392 totcrd = 4+ptrcrd+indcrd+valcrd+rhscrd;
1397 fprintf(out_file,
"%-72s%-8s\n%14d%14d%14d%14d%14d\n",Title, Key, totcrd,
1398 ptrcrd, indcrd, valcrd, rhscrd);
1399 fprintf(out_file,
"%3s%11s%14d%14d%14d\n",Type,
" ", M, N, nz);
1400 fprintf(out_file,
"%-16s%-16s%-20s", Ptrfmt, Indfmt, Valfmt);
1404 fprintf(out_file,
"%-20s\n%-14s%d\n",Rhsfmt,Rhstype,Nrhs);
1405 }
else fprintf(out_file,
"\n");
1413 entry = colptr[i]+offset;
1414 fprintf(out_file,pformat,entry);
1415 if ( (i+1)%Ptrperline == 0 ) fprintf(out_file,
"\n");
1418 if ( (N+1) % Ptrperline != 0 ) fprintf(out_file,
"\n");
1423 entry = rowind[i]+offset;
1424 fprintf(out_file,iformat,entry);
1425 if ( (i+1)%Indperline == 0 ) fprintf(out_file,
"\n");
1428 if ( nz % Indperline != 0 ) fprintf(out_file,
"\n");
1432 if ( Type[0] !=
'P' ) {
1433 for (i=0;i<nvalentries;i++)
1435 fprintf(out_file,vformat,val+i*Valwidth);
1436 if ( (i+1)%Valperline == 0 ) fprintf(out_file,
"\n");
1439 if ( nvalentries % Valperline != 0 ) fprintf(out_file,
"\n");
1445 for (j=0;j<Nrhs;j++) {
1446 for (i=0;i<nrhsentries;i++)
1448 fprintf(out_file,rformat,rhs+i*Rhswidth);
1449 if ( acount++%Rhsperline == linemod ) fprintf(out_file,
"\n");
1451 if ( acount%Rhsperline != linemod ) {
1452 fprintf(out_file,
"\n");
1453 linemod = (acount-1)%Rhsperline;
1455 if ( Rhstype[1] ==
'G' ) {
1456 for (i=0;i<nrhsentries;i++)
1458 fprintf(out_file,rformat,guess+i*Rhswidth);
1459 if ( acount++%Rhsperline == linemod ) fprintf(out_file,
"\n");
1461 if ( acount%Rhsperline != linemod ) {
1462 fprintf(out_file,
"\n");
1463 linemod = (acount-1)%Rhsperline;
1466 if ( Rhstype[2] ==
'X' ) {
1467 for (i=0;i<nrhsentries;i++)
1469 fprintf(out_file,rformat,exact+i*Rhswidth);
1470 if ( acount++%Rhsperline == linemod ) fprintf(out_file,
"\n");
1472 if ( acount%Rhsperline != linemod ) {
1473 fprintf(out_file,
"\n");
1474 linemod = (acount-1)%Rhsperline;
1482 if ( fclose(out_file) != 0){
1483 fprintf(stderr,
"Error closing file in writeHB_mat_char().\n");
1497 *perline = 0; *width = 0;
return 0;
1500 tmp = strchr(fmt,
'(');
1501 tmp =
substr(fmt,tmp - fmt + 1, strchr(fmt,
'I') - tmp - 1);
1502 *perline = atoi(tmp);
1503 tmp = strchr(fmt,
'I');
1504 tmp =
substr(fmt,tmp - fmt + 1, strchr(fmt,
')') - tmp - 1);
1505 return *width = atoi(tmp);
1508 int ParseRfmt(
char* fmt,
int* perline,
int* width,
int* prec,
int* flag)
1529 if (strchr(fmt,
'(') != NULL) fmt = strchr(fmt,
'(');
1530 if (strchr(fmt,
')') != NULL) {
1531 tmp2 = strchr(fmt,
')');
1532 while ( strchr(tmp2+1,
')') != NULL ) {
1533 tmp2 = strchr(tmp2+1,
')');
1535 *(tmp2+1) = (
int) NULL;
1537 if (strchr(fmt,
'P') != NULL)
1539 if (strchr(fmt,
'(') != NULL) {
1540 tmp = strchr(fmt,
'P');
1541 if ( *(++tmp) ==
',' ) tmp++;
1542 tmp3 = strchr(fmt,
'(')+1;
1545 while ( *(tmp2+len) != (
int) NULL ) {
1549 *(strchr(fmt,
')')+1) = (
int) NULL;
1552 if (strchr(fmt,
'E') != NULL) {
1554 }
else if (strchr(fmt,
'D') != NULL) {
1556 }
else if (strchr(fmt,
'F') != NULL) {
1559 fprintf(stderr,
"Real format %s in H/B file not supported.\n",fmt);
1562 tmp = strchr(fmt,
'(');
1563 tmp =
substr(fmt,tmp - fmt + 1, strchr(fmt,*flag) - tmp - 1);
1564 *perline = atoi(tmp);
1565 tmp = strchr(fmt,*flag);
1566 if ( strchr(fmt,
'.') ) {
1567 *prec = atoi(
substr( fmt, strchr(fmt,
'.') - fmt + 1, strchr(fmt,
')') - strchr(fmt,
'.')-1) );
1568 tmp =
substr(fmt,tmp - fmt + 1, strchr(fmt,
'.') - tmp - 1);
1570 tmp =
substr(fmt,tmp - fmt + 1, strchr(fmt,
')') - tmp - 1);
1572 return *width = atoi(tmp);
1575 char*
substr(
const char* S,
const int pos,
const int len)
1579 if ( pos+len <= strlen(S)) {
1580 SubS = (
char *)malloc(len+1);
1581 if ( SubS == NULL )
IOHBTerminate(
"Insufficient memory for SubS.");
1582 for (i=0;i<len;i++) SubS[i] = S[pos+i];
1583 SubS[len] = (char) NULL;
1596 for (i=0;i< len;i++)
1597 S[i] = toupper(S[i]);
1602 fprintf(stderr,message);
char * substr(const char *S, const int pos, const int len)
int ParseRfmt(char *fmt, int *perline, int *width, int *prec, int *flag)
int writeHB_mat_char(const char *filename, int M, int N, int nz, const int colptr[], const int rowind[], const char val[], int Nrhs, const char rhs[], const char guess[], const char exact[], const char *Title, const char *Key, const char *Type, char *Ptrfmt, char *Indfmt, char *Valfmt, char *Rhsfmt, const char *Rhstype)
void IOHBTerminate(char *message)
int readHB_header(FILE *in_file, char *Title, char *Key, char *Type, int *Nrow, int *Ncol, int *Nnzero, int *Nrhs, char *Ptrfmt, char *Indfmt, char *Valfmt, char *Rhsfmt, int *Ptrcrd, int *Indcrd, int *Valcrd, int *Rhscrd, char *Rhstype)
int readHB_mat_char(const char *filename, int colptr[], int rowind[], char val[], char *Valfmt)
int readHB_mat_double(const char *filename, int colptr[], int rowind[], double val[])
int readHB_aux_char(const char *filename, const char AuxType, char b[])
int readHB_newmat_double(const char *filename, int *M, int *N, int *nonzeros, int **colptr, int **rowind, double **val)
int readHB_newaux_char(const char *filename, const char AuxType, char **b, char **Rhsfmt)
int readHB_newmat_char(const char *filename, int *M, int *N, int *nonzeros, int **colptr, int **rowind, char **val, char **Valfmt)
int readHB_aux_double(const char *filename, const char AuxType, double b[])
int readHB_info(const char *filename, int *M, int *N, int *nz, char **Type, int *Nrhs)
int writeHB_mat_double(const char *filename, int M, int N, int nz, const int colptr[], const int rowind[], const double val[], int Nrhs, const double rhs[], const double guess[], const double exact[], const char *Title, const char *Key, const char *Type, char *Ptrfmt, char *Indfmt, char *Valfmt, char *Rhsfmt, const char *Rhstype)
int ParseIfmt(char *fmt, int *perline, int *width)
int readHB_newaux_double(const char *filename, const char AuxType, double **b)