230 char*
substr(
const char*
S,
const int pos,
const int len);
234 int readHB_info(
const char* filename,
int* M,
int* N,
int* nz,
char** Type,
255 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
256 int Nrow, Ncol, Nnzero;
258 char Title[73], Key[9], Rhstype[4];
259 char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
261 mat_type = (
char *) malloc(4);
263 if ( mat_type == NULL )
IOHBTerminate(
"Insufficient memory for mat_typen");
265 if ( (in_file = fopen( filename,
"r")) == NULL ) {
266 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
270 readHB_header(in_file, Title, Key, mat_type, &Nrow, &Ncol, &Nnzero, Nrhs,
271 Ptrfmt, Indfmt, Valfmt, Rhsfmt,
272 &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
275 *(*Type+3) = (
char) NULL;
279 if (Rhscrd == 0) {*Nrhs = 0;}
301 int* Nrow,
int* Ncol,
int* Nnzero,
int* Nrhs,
302 char* Ptrfmt,
char* Indfmt,
char* Valfmt,
char* Rhsfmt,
303 int* Ptrcrd,
int* Indcrd,
int* Valcrd,
int* Rhscrd,
309 int Totcrd,Neltvl,Nrhsix;
313 fgets(line, BUFSIZ, in_file);
314 if ( sscanf(line,
"%*s") < 0 )
315 IOHBTerminate(
"iohb.c: Null (or blank) first line of HB file.\n");
316 (void) sscanf(line,
"%72c%8[^\n]", Title, Key);
317 *(Key+8) = (
char) NULL;
318 *(Title+72) = (
char) NULL;
321 fgets(line, BUFSIZ, in_file);
322 if ( sscanf(line,
"%*s") < 0 )
323 IOHBTerminate(
"iohb.c: Null (or blank) second line of HB file.\n");
324 if ( sscanf(line,
"%i",&Totcrd) != 1) Totcrd = 0;
325 if ( sscanf(line,
"%*i%i",Ptrcrd) != 1) *Ptrcrd = 0;
326 if ( sscanf(line,
"%*i%*i%i",Indcrd) != 1) *Indcrd = 0;
327 if ( sscanf(line,
"%*i%*i%*i%i",Valcrd) != 1) *Valcrd = 0;
328 if ( sscanf(line,
"%*i%*i%*i%*i%i",Rhscrd) != 1) *Rhscrd = 0;
331 fgets(line, BUFSIZ, in_file);
332 if ( sscanf(line,
"%*s") < 0 )
333 IOHBTerminate(
"iohb.c: Null (or blank) third line of HB file.\n");
334 if ( sscanf(line,
"%3c", Type) != 1)
335 IOHBTerminate(
"iohb.c: Invalid Type info, line 3 of Harwell-Boeing file.\n");
337 if ( sscanf(line,
"%*3c%i",Nrow) != 1) *Nrow = 0 ;
338 if ( sscanf(line,
"%*3c%*i%i",Ncol) != 1) *Ncol = 0 ;
339 if ( sscanf(line,
"%*3c%*i%*i%i",Nnzero) != 1) *Nnzero = 0 ;
340 if ( sscanf(line,
"%*3c%*i%*i%*i%i",&Neltvl) != 1) Neltvl = 0 ;
343 fgets(line, BUFSIZ, in_file);
344 if ( sscanf(line,
"%*s") < 0 )
345 IOHBTerminate(
"iohb.c: Null (or blank) fourth line of HB file.\n");
346 if ( sscanf(line,
"%16c",Ptrfmt) != 1)
347 IOHBTerminate(
"iohb.c: Invalid format info, line 4 of Harwell-Boeing file.\n");
348 if ( sscanf(line,
"%*16c%16c",Indfmt) != 1)
349 IOHBTerminate(
"iohb.c: Invalid format info, line 4 of Harwell-Boeing file.\n");
350 if ( sscanf(line,
"%*16c%*16c%20c",Valfmt) != 1)
351 IOHBTerminate(
"iohb.c: Invalid format info, line 4 of Harwell-Boeing file.\n");
352 sscanf(line,
"%*16c%*16c%*20c%20c",Rhsfmt);
353 *(Ptrfmt+16) = (
char) NULL;
354 *(Indfmt+16) = (
char) NULL;
355 *(Valfmt+20) = (
char) NULL;
356 *(Rhsfmt+20) = (
char) NULL;
361 fgets(line, BUFSIZ, in_file);
362 if ( sscanf(line,
"%*s") < 0 )
363 IOHBTerminate(
"iohb.c: Null (or blank) fifth line of HB file.\n");
364 if ( sscanf(line,
"%3c", Rhstype) != 1)
365 IOHBTerminate(
"iohb.c: Invalid RHS type information, line 5 of Harwell-Boeing file.\n");
366 if ( sscanf(line,
"%*3c%i", Nrhs) != 1) *Nrhs = 0;
367 if ( sscanf(line,
"%*3c%*i%i", &Nrhsix) != 1) Nrhsix = 0;
395 int i,j,ind,col,offset,count,last,Nrhs;
396 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
397 int Nrow, Ncol, Nnzero, Nentries;
398 int Ptrperline, Ptrwidth, Indperline, Indwidth;
399 int Valperline, Valwidth, Valprec;
401 char* ThisElement = NULL;
402 char Title[73], Key[8], Rhstype[4];
403 char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
406 char Type[4] = {
'\0',
'\0',
'\0',
'\0'};
408 if ( (in_file = fopen( filename,
"r")) == NULL ) {
409 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
413 readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
414 Ptrfmt, Indfmt, Valfmt, Rhsfmt,
415 &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
420 if ( Type[0] !=
'P' ) {
421 ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
429 ThisElement = (
char *) malloc(Ptrwidth+1);
430 if ( ThisElement == NULL )
IOHBTerminate(
"Insufficient memory for ThisElement.");
431 *(ThisElement+Ptrwidth) = (
char) NULL;
433 for (i=0;i<Ptrcrd;i++)
435 fgets(line, BUFSIZ, in_file);
436 if ( sscanf(line,
"%*s") < 0 )
437 IOHBTerminate(
"iohb.c: Null (or blank) line in pointer data region of HB file.\n");
439 for (ind = 0;ind<Ptrperline;ind++)
441 if (count > Ncol)
break;
442 strncpy(ThisElement,line+col,Ptrwidth);
444 colptr[count] = atoi(ThisElement)-offset;
445 count++; col += Ptrwidth;
452 ThisElement = (
char *) malloc(Indwidth+1);
453 if ( ThisElement == NULL )
IOHBTerminate(
"Insufficient memory for ThisElement.");
454 *(ThisElement+Indwidth) = (
char) NULL;
456 for (i=0;i<Indcrd;i++)
458 fgets(line, BUFSIZ, in_file);
459 if ( sscanf(line,
"%*s") < 0 )
460 IOHBTerminate(
"iohb.c: Null (or blank) line in index data region of HB file.\n");
462 for (ind = 0;ind<Indperline;ind++)
464 if (count == Nnzero)
break;
465 strncpy(ThisElement,line+col,Indwidth);
467 rowind[count] = atoi(ThisElement)-offset;
468 count++; col += Indwidth;
475 if ( Type[0] !=
'P' ) {
477 if ( Type[0] ==
'C' ) Nentries = 2*Nnzero;
478 else Nentries = Nnzero;
480 ThisElement = (
char *) malloc(Valwidth+1);
481 if ( ThisElement == NULL )
IOHBTerminate(
"Insufficient memory for ThisElement.");
482 *(ThisElement+Valwidth) = (
char) NULL;
484 for (i=0;i<Valcrd;i++)
486 fgets(line, BUFSIZ, in_file);
487 if ( sscanf(line,
"%*s") < 0 )
488 IOHBTerminate(
"iohb.c: Null (or blank) line in value data region of HB file.\n");
489 if (Valflag ==
'D') {
490 while( strchr(line,
'D') ) *strchr(line,
'D') =
'E';
494 for (ind = 0;ind<Valperline;ind++)
496 if (count == Nentries)
break;
497 strncpy(ThisElement,line+col,Valwidth);
499 if ( Valflag !=
'F' && strchr(ThisElement,
'E') == NULL ) {
501 last = strlen(ThisElement);
502 for (j=last+1;j>=0;j--) {
503 ThisElement[j] = ThisElement[j-1];
504 if ( ThisElement[j] ==
'+' || ThisElement[j] ==
'-' ) {
505 ThisElement[j-1] = Valflag;
510 val[count] = atof(ThisElement);
511 count++; col += Valwidth;
522 int** colptr,
int** rowind,
double** val)
527 readHB_info(filename, M, N, nonzeros, &Type, &Nrhs);
529 *colptr = (
int *)malloc((*N+1)*
sizeof(int));
530 if ( *colptr == NULL )
IOHBTerminate(
"Insufficient memory for colptr.\n");
531 *rowind = (
int *)malloc(*nonzeros*
sizeof(
int));
532 if ( *rowind == NULL )
IOHBTerminate(
"Insufficient memory for rowind.\n");
533 if ( Type[0] ==
'C' ) {
539 *val = (
double *)malloc(*nonzeros*
sizeof(
double)*2);
540 if ( *val == NULL )
IOHBTerminate(
"Insufficient memory for val.\n");
542 if ( Type[0] !=
'P' ) {
544 *val = (
double *)malloc(*nonzeros*
sizeof(
double));
545 if ( *val == NULL )
IOHBTerminate(
"Insufficient memory for val.\n");
579 int i,j,n,maxcol,start,stride,col,last,linel;
580 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
581 int Nrow, Ncol, Nnzero, Nentries;
582 int Nrhs, nvecs, rhsi;
583 int Rhsperline, Rhswidth, Rhsprec;
586 char Title[73], Key[9], Type[4], Rhstype[4];
587 char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
590 if ((in_file = fopen( filename,
"r")) == NULL) {
591 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
595 readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
596 Ptrfmt, Indfmt, Valfmt, Rhsfmt,
597 &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
601 fprintf(stderr,
"Warn: Attempt to read auxillary vector(s) when none are present.\n");
604 if (Rhstype[0] !=
'F' )
606 fprintf(stderr,
"Warn: Attempt to read auxillary vector(s) which are not stored in Full form.\n");
607 fprintf(stderr,
" Rhs must be specified as full. \n");
612 if ( Type[0] ==
'C' ) {
620 if ( Rhstype[1] ==
'G' ) nvecs++;
621 if ( Rhstype[2] ==
'X' ) nvecs++;
623 if ( AuxType ==
'G' && Rhstype[1] !=
'G' ) {
624 fprintf(stderr,
"Warn: Attempt to read auxillary Guess vector(s) when none are present.\n");
627 if ( AuxType ==
'X' && Rhstype[2] !=
'X' ) {
628 fprintf(stderr,
"Warn: Attempt to read auxillary eXact solution vector(s) when none are present.\n");
632 ParseRfmt(Rhsfmt, &Rhsperline, &Rhswidth, &Rhsprec,&Rhsflag);
633 maxcol = Rhsperline*Rhswidth;
636 n = Ptrcrd + Indcrd + Valcrd;
638 for (i = 0; i < n; i++)
639 fgets(line, BUFSIZ, in_file);
645 if ( AuxType ==
'F' ) start = 0;
646 else if ( AuxType ==
'G' ) start = Nentries;
647 else start = (nvecs-1)*Nentries;
648 stride = (nvecs-1)*Nentries;
650 fgets(line, BUFSIZ, in_file);
651 linel= strchr(line,
'\n')-line;
655 for (i=0;i<start;i++) {
656 if ( col >= ( maxcol<linel?maxcol:linel ) ) {
657 fgets(line, BUFSIZ, in_file);
658 linel= strchr(line,
'\n')-line;
663 if (Rhsflag ==
'D') {
664 while( strchr(line,
'D') ) *strchr(line,
'D') =
'E';
670 ThisElement = (
char *) malloc(Rhswidth+1);
671 if ( ThisElement == NULL )
IOHBTerminate(
"Insufficient memory for ThisElement.");
672 *(ThisElement+Rhswidth) = (
char) NULL;
673 for (rhsi=0;rhsi<Nrhs;rhsi++) {
675 for (i=0;i<Nentries;i++) {
676 if ( col >= ( maxcol<linel?maxcol:linel ) ) {
677 fgets(line, BUFSIZ, in_file);
678 linel= strchr(line,
'\n')-line;
679 if (Rhsflag ==
'D') {
680 while( strchr(line,
'D') ) *strchr(line,
'D') =
'E';
684 strncpy(ThisElement,line+col,Rhswidth);
686 if ( Rhsflag !=
'F' && strchr(ThisElement,
'E') == NULL ) {
688 last = strlen(ThisElement);
689 for (j=last+1;j>=0;j--) {
690 ThisElement[j] = ThisElement[j-1];
691 if ( ThisElement[j] ==
'+' || ThisElement[j] ==
'-' ) {
692 ThisElement[j-1] = Rhsflag;
697 b[i] = atof(ThisElement);
703 for (i=0;i<stride;i++) {
704 if ( col >= ( maxcol<linel?maxcol:linel ) ) {
705 fgets(line, BUFSIZ, in_file);
706 linel= strchr(line,
'\n')-line;
722 int Nrhs,M,N,nonzeros;
725 readHB_info(filename, &M, &N, &nonzeros, &Type, &Nrhs);
727 fprintf(stderr,
"Warn: Requested read of aux vector(s) when none are present.\n");
730 if ( Type[0] ==
'C' ) {
731 fprintf(stderr,
"Warning: Reading complex aux vector(s) from HB file %s.",filename);
732 fprintf(stderr,
" Real and imaginary parts will be interlaced in b[].");
733 *b = (
double *)malloc(M*Nrhs*
sizeof(
double)*2);
734 if ( *b == NULL )
IOHBTerminate(
"Insufficient memory for rhs.\n");
737 *b = (
double *)malloc(M*Nrhs*
sizeof(
double));
738 if ( *b == NULL )
IOHBTerminate(
"Insufficient memory for rhs.\n");
745 int nz,
const int colptr[],
const int rowind[],
746 const double val[],
int Nrhs,
const double rhs[],
747 const double guess[],
const double exact[],
748 const char* Title,
const char* Key,
const char* Type,
749 char* Ptrfmt,
char* Indfmt,
char* Valfmt,
char* Rhsfmt,
762 int i,j,entry,offset,acount,linemod;
763 int totcrd, ptrcrd, indcrd, valcrd, rhscrd;
764 int nvalentries, nrhsentries;
765 int Ptrperline, Ptrwidth, Indperline, Indwidth;
766 int Rhsperline, Rhswidth, Rhsprec;
768 int Valperline, Valwidth, Valprec;
770 char pformat[16],iformat[16],vformat[19],rformat[19];
772 if ( Type[0] ==
'C' ) {
780 if ( filename != NULL ) {
781 if ( (out_file = fopen( filename,
"w")) == NULL ) {
782 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
785 }
else out_file = stdout;
787 if ( Ptrfmt == NULL ) Ptrfmt =
"(8I10)";
789 sprintf(pformat,
"%%%dd",Ptrwidth);
790 ptrcrd = (N+1)/Ptrperline;
791 if ( (N+1)%Ptrperline != 0) ptrcrd++;
793 if ( Indfmt == NULL ) Indfmt = Ptrfmt;
795 sprintf(iformat,
"%%%dd",Indwidth);
796 indcrd = nz/Indperline;
797 if ( nz%Indperline != 0) indcrd++;
799 if ( Type[0] !=
'P' ) {
800 if ( Valfmt == NULL ) Valfmt =
"(4E20.13)";
801 ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
802 if (Valflag ==
'D') *strchr(Valfmt,
'D') =
'E';
804 sprintf(vformat,
"%% %d.%df",Valwidth,Valprec);
806 sprintf(vformat,
"%% %d.%dE",Valwidth,Valprec);
807 valcrd = nvalentries/Valperline;
808 if ( nvalentries%Valperline != 0) valcrd++;
812 if ( Rhsfmt == NULL ) Rhsfmt = Valfmt;
813 ParseRfmt(Rhsfmt,&Rhsperline,&Rhswidth,&Rhsprec, &Rhsflag);
815 sprintf(rformat,
"%% %d.%df",Rhswidth,Rhsprec);
817 sprintf(rformat,
"%% %d.%dE",Rhswidth,Rhsprec);
818 if (Rhsflag ==
'D') *strchr(Rhsfmt,
'D') =
'E';
819 rhscrd = nrhsentries/Rhsperline;
820 if ( nrhsentries%Rhsperline != 0) rhscrd++;
821 if ( Rhstype[1] ==
'G' ) rhscrd+=rhscrd;
822 if ( Rhstype[2] ==
'X' ) rhscrd+=rhscrd;
826 totcrd = 4+ptrcrd+indcrd+valcrd+rhscrd;
831 fprintf(out_file,
"%-72s%-8s\n%14d%14d%14d%14d%14d\n",Title, Key, totcrd,
832 ptrcrd, indcrd, valcrd, rhscrd);
833 fprintf(out_file,
"%3s%11s%14d%14d%14d\n",Type,
" ", M, N, nz);
834 fprintf(out_file,
"%-16s%-16s%-20s", Ptrfmt, Indfmt, Valfmt);
838 fprintf(out_file,
"%-20s\n%-14s%d\n",Rhsfmt,Rhstype,Nrhs);
839 }
else fprintf(out_file,
"\n");
847 entry = colptr[i]+offset;
848 fprintf(out_file,pformat,entry);
849 if ( (i+1)%Ptrperline == 0 ) fprintf(out_file,
"\n");
852 if ( (N+1) % Ptrperline != 0 ) fprintf(out_file,
"\n");
857 entry = rowind[i]+offset;
858 fprintf(out_file,iformat,entry);
859 if ( (i+1)%Indperline == 0 ) fprintf(out_file,
"\n");
862 if ( nz % Indperline != 0 ) fprintf(out_file,
"\n");
866 if ( Type[0] !=
'P' ) {
868 for (i=0;i<nvalentries;i++)
870 fprintf(out_file,vformat,val[i]);
871 if ( (i+1)%Valperline == 0 ) fprintf(out_file,
"\n");
874 if ( nvalentries % Valperline != 0 ) fprintf(out_file,
"\n");
883 for ( j=0;j<nrhsentries;j++ ) {
884 fprintf(out_file,rformat,rhs[j]);
885 if ( acount++%Rhsperline == linemod ) fprintf(out_file,
"\n");
887 if ( acount%Rhsperline != linemod ) {
888 fprintf(out_file,
"\n");
889 linemod = (acount-1)%Rhsperline;
892 if ( Rhstype[1] ==
'G' ) {
893 for ( j=0;j<nrhsentries;j++ ) {
894 fprintf(out_file,rformat,guess[j]);
895 if ( acount++%Rhsperline == linemod ) fprintf(out_file,
"\n");
897 if ( acount%Rhsperline != linemod ) {
898 fprintf(out_file,
"\n");
899 linemod = (acount-1)%Rhsperline;
901 guess += nrhsentries;
903 if ( Rhstype[2] ==
'X' ) {
904 for ( j=0;j<nrhsentries;j++ ) {
905 fprintf(out_file,rformat,exact[j]);
906 if ( acount++%Rhsperline == linemod ) fprintf(out_file,
"\n");
908 if ( acount%Rhsperline != linemod ) {
909 fprintf(out_file,
"\n");
910 linemod = (acount-1)%Rhsperline;
912 exact += nrhsentries;
919 if ( fclose(out_file) != 0){
920 fprintf(stderr,
"Error closing file in writeHB_mat_double().\n");
927 char val[],
char* Valfmt)
948 int i,j,ind,col,offset,count,last;
949 int Nrow,Ncol,Nnzero,Nentries,Nrhs;
950 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
951 int Ptrperline, Ptrwidth, Indperline, Indwidth;
952 int Valperline, Valwidth, Valprec;
956 char Title[73], Key[8], Type[4], Rhstype[4];
957 char Ptrfmt[17], Indfmt[17], Rhsfmt[21];
959 if ( (in_file = fopen( filename,
"r")) == NULL ) {
960 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
964 readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
965 Ptrfmt, Indfmt, Valfmt, Rhsfmt,
966 &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
971 if ( Type[0] !=
'P' ) {
972 ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
973 if (Valflag ==
'D') {
974 *strchr(Valfmt,
'D') =
'E';
983 ThisElement = (
char *) malloc(Ptrwidth+1);
984 if ( ThisElement == NULL )
IOHBTerminate(
"Insufficient memory for ThisElement.");
985 *(ThisElement+Ptrwidth) = (
char) NULL;
987 for (i=0;i<Ptrcrd;i++)
989 fgets(line, BUFSIZ, in_file);
990 if ( sscanf(line,
"%*s") < 0 )
991 IOHBTerminate(
"iohb.c: Null (or blank) line in pointer data region of HB file.\n");
993 for (ind = 0;ind<Ptrperline;ind++)
995 if (count > Ncol)
break;
996 strncpy(ThisElement,line+col,Ptrwidth);
998 colptr[count] = atoi(ThisElement)-offset;
999 count++; col += Ptrwidth;
1006 ThisElement = (
char *) malloc(Indwidth+1);
1007 if ( ThisElement == NULL )
IOHBTerminate(
"Insufficient memory for ThisElement.");
1008 *(ThisElement+Indwidth) = (
char) NULL;
1010 for (i=0;i<Indcrd;i++)
1012 fgets(line, BUFSIZ, in_file);
1013 if ( sscanf(line,
"%*s") < 0 )
1014 IOHBTerminate(
"iohb.c: Null (or blank) line in index data region of HB file.\n");
1016 for (ind = 0;ind<Indperline;ind++)
1018 if (count == Nnzero)
break;
1019 strncpy(ThisElement,line+col,Indwidth);
1021 rowind[count] = atoi(ThisElement)-offset;
1022 count++; col += Indwidth;
1029 if ( Type[0] !=
'P' ) {
1031 if ( Type[0] ==
'C' ) Nentries = 2*Nnzero;
1032 else Nentries = Nnzero;
1034 ThisElement = (
char *) malloc(Valwidth+1);
1035 if ( ThisElement == NULL )
IOHBTerminate(
"Insufficient memory for ThisElement.");
1036 *(ThisElement+Valwidth) = (
char) NULL;
1038 for (i=0;i<Valcrd;i++)
1040 fgets(line, BUFSIZ, in_file);
1041 if ( sscanf(line,
"%*s") < 0 )
1042 IOHBTerminate(
"iohb.c: Null (or blank) line in value data region of HB file.\n");
1043 if (Valflag ==
'D') {
1044 while( strchr(line,
'D') ) *strchr(line,
'D') =
'E';
1047 for (ind = 0;ind<Valperline;ind++)
1049 if (count == Nentries)
break;
1050 ThisElement = &val[count*Valwidth];
1051 strncpy(ThisElement,line+col,Valwidth);
1053 if ( Valflag !=
'F' && strchr(ThisElement,
'E') == NULL ) {
1055 last = strlen(ThisElement);
1056 for (j=last+1;j>=0;j--) {
1057 ThisElement[j] = ThisElement[j-1];
1058 if ( ThisElement[j] ==
'+' || ThisElement[j] ==
'-' ) {
1059 ThisElement[j-1] = Valflag;
1064 count++; col += Valwidth;
1073 int** rowind,
char** val,
char** Valfmt)
1077 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
1078 int Valperline, Valwidth, Valprec;
1080 char Title[73], Key[9], Type[4], Rhstype[4];
1081 char Ptrfmt[17], Indfmt[17], Rhsfmt[21];
1083 if ((in_file = fopen( filename,
"r")) == NULL) {
1084 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
1088 *Valfmt = (
char *)malloc(21*
sizeof(
char));
1089 if ( *Valfmt == NULL )
IOHBTerminate(
"Insufficient memory for Valfmt.");
1090 readHB_header(in_file, Title, Key, Type, M, N, nonzeros, &Nrhs,
1091 Ptrfmt, Indfmt, (*Valfmt), Rhsfmt,
1092 &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
1094 ParseRfmt(*Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
1096 *colptr = (
int *)malloc((*N+1)*
sizeof(int));
1097 if ( *colptr == NULL )
IOHBTerminate(
"Insufficient memory for colptr.\n");
1098 *rowind = (
int *)malloc(*nonzeros*
sizeof(
int));
1099 if ( *rowind == NULL )
IOHBTerminate(
"Insufficient memory for rowind.\n");
1100 if ( Type[0] ==
'C' ) {
1106 *val = (
char *)malloc(*nonzeros*Valwidth*
sizeof(
char)*2);
1107 if ( *val == NULL )
IOHBTerminate(
"Insufficient memory for val.\n");
1109 if ( Type[0] !=
'P' ) {
1111 *val = (
char *)malloc(*nonzeros*Valwidth*
sizeof(
char));
1112 if ( *val == NULL )
IOHBTerminate(
"Insufficient memory for val.\n");
1143 int i,j,n,maxcol,start,stride,col,last,linel,nvecs,rhsi;
1144 int Nrow, Ncol, Nnzero, Nentries,Nrhs;
1145 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
1146 int Rhsperline, Rhswidth, Rhsprec;
1148 char Title[73], Key[9], Type[4], Rhstype[4];
1149 char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
1153 if ((in_file = fopen( filename,
"r")) == NULL) {
1154 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
1158 readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
1159 Ptrfmt, Indfmt, Valfmt, Rhsfmt,
1160 &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
1164 fprintf(stderr,
"Warn: Attempt to read auxillary vector(s) when none are present.\n");
1167 if (Rhstype[0] !=
'F' )
1169 fprintf(stderr,
"Warn: Attempt to read auxillary vector(s) which are not stored in Full form.\n");
1170 fprintf(stderr,
" Rhs must be specified as full. \n");
1175 if ( Type[0] ==
'C' ) {
1183 if ( Rhstype[1] ==
'G' ) nvecs++;
1184 if ( Rhstype[2] ==
'X' ) nvecs++;
1186 if ( AuxType ==
'G' && Rhstype[1] !=
'G' ) {
1187 fprintf(stderr,
"Warn: Attempt to read auxillary Guess vector(s) when none are present.\n");
1190 if ( AuxType ==
'X' && Rhstype[2] !=
'X' ) {
1191 fprintf(stderr,
"Warn: Attempt to read auxillary eXact solution vector(s) when none are present.\n");
1195 ParseRfmt(Rhsfmt, &Rhsperline, &Rhswidth, &Rhsprec,&Rhsflag);
1196 maxcol = Rhsperline*Rhswidth;
1199 n = Ptrcrd + Indcrd + Valcrd;
1201 for (i = 0; i < n; i++)
1202 fgets(line, BUFSIZ, in_file);
1208 if ( AuxType ==
'F' ) start = 0;
1209 else if ( AuxType ==
'G' ) start = Nentries;
1210 else start = (nvecs-1)*Nentries;
1211 stride = (nvecs-1)*Nentries;
1213 fgets(line, BUFSIZ, in_file);
1214 linel= strchr(line,
'\n')-line;
1215 if ( sscanf(line,
"%*s") < 0 )
1216 IOHBTerminate(
"iohb.c: Null (or blank) line in auxillary vector data region of HB file.\n");
1220 for (i=0;i<start;i++) {
1222 if ( col >= ( maxcol<linel?maxcol:linel ) ) {
1223 fgets(line, BUFSIZ, in_file);
1224 linel= strchr(line,
'\n')-line;
1225 if ( sscanf(line,
"%*s") < 0 )
1226 IOHBTerminate(
"iohb.c: Null (or blank) line in auxillary vector data region of HB file.\n");
1231 if (Rhsflag ==
'D') {
1232 while( strchr(line,
'D') ) *strchr(line,
'D') =
'E';
1237 for (rhsi=0;rhsi<Nrhs;rhsi++) {
1239 for (i=0;i<Nentries;i++) {
1240 if ( col >= ( maxcol<linel?maxcol:linel ) ) {
1241 fgets(line, BUFSIZ, in_file);
1242 linel= strchr(line,
'\n')-line;
1243 if ( sscanf(line,
"%*s") < 0 )
1244 IOHBTerminate(
"iohb.c: Null (or blank) line in auxillary vector data region of HB file.\n");
1245 if (Rhsflag ==
'D') {
1246 while( strchr(line,
'D') ) *strchr(line,
'D') =
'E';
1250 ThisElement = &b[i*Rhswidth];
1251 strncpy(ThisElement,line+col,Rhswidth);
1252 if ( Rhsflag !=
'F' && strchr(ThisElement,
'E') == NULL ) {
1254 last = strlen(ThisElement);
1255 for (j=last+1;j>=0;j--) {
1256 ThisElement[j] = ThisElement[j-1];
1257 if ( ThisElement[j] ==
'+' || ThisElement[j] ==
'-' ) {
1258 ThisElement[j-1] = Rhsflag;
1265 b+=Nentries*Rhswidth;
1269 for (i=0;i<stride;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 auxillary vector data region of HB file.\n");
1290 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
1291 int Nrow,Ncol,Nnzero,Nrhs;
1292 int Rhsperline, Rhswidth, Rhsprec;
1294 char Title[73], Key[9], Type[4], Rhstype[4];
1295 char Ptrfmt[17], Indfmt[17], Valfmt[21];
1297 if ((in_file = fopen( filename,
"r")) == NULL) {
1298 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
1302 *Rhsfmt = (
char *)malloc(21*
sizeof(
char));
1303 if ( *Rhsfmt == NULL )
IOHBTerminate(
"Insufficient memory for Rhsfmt.");
1304 readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
1305 Ptrfmt, Indfmt, Valfmt, (*Rhsfmt),
1306 &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
1309 fprintf(stderr,
"Warn: Requested read of aux vector(s) when none are present.\n");
1312 ParseRfmt(*Rhsfmt,&Rhsperline,&Rhswidth,&Rhsprec,&Rhsflag);
1313 if ( Type[0] ==
'C' ) {
1314 fprintf(stderr,
"Warning: Reading complex aux vector(s) from HB file %s.",filename);
1315 fprintf(stderr,
" Real and imaginary parts will be interlaced in b[].");
1316 *b = (
char *)malloc(Nrow*Nrhs*Rhswidth*
sizeof(
char)*2);
1317 if ( *b == NULL )
IOHBTerminate(
"Insufficient memory for rhs.\n");
1320 *b = (
char *)malloc(Nrow*Nrhs*Rhswidth*
sizeof(
char));
1321 if ( *b == NULL )
IOHBTerminate(
"Insufficient memory for rhs.\n");
1328 int nz,
const int colptr[],
const int rowind[],
1329 const char val[],
int Nrhs,
const char rhs[],
1330 const char guess[],
const char exact[],
1331 const char* Title,
const char* Key,
const char* Type,
1332 char* Ptrfmt,
char* Indfmt,
char* Valfmt,
char* Rhsfmt,
1333 const char* Rhstype)
1345 int i,j,acount,linemod,entry,offset;
1346 int totcrd, ptrcrd, indcrd, valcrd, rhscrd;
1347 int nvalentries, nrhsentries;
1348 int Ptrperline, Ptrwidth, Indperline, Indwidth;
1349 int Rhsperline, Rhswidth, Rhsprec;
1351 int Valperline, Valwidth, Valprec;
1353 char pformat[16],iformat[16],vformat[19],rformat[19];
1355 if ( Type[0] ==
'C' ) {
1363 if ( filename != NULL ) {
1364 if ( (out_file = fopen( filename,
"w")) == NULL ) {
1365 fprintf(stderr,
"Error: Cannot open file: %s\n",filename);
1368 }
else out_file = stdout;
1370 if ( Ptrfmt == NULL ) Ptrfmt =
"(8I10)";
1371 ParseIfmt(Ptrfmt,&Ptrperline,&Ptrwidth);
1372 sprintf(pformat,
"%%%dd",Ptrwidth);
1374 if ( Indfmt == NULL ) Indfmt = Ptrfmt;
1375 ParseIfmt(Indfmt,&Indperline,&Indwidth);
1376 sprintf(iformat,
"%%%dd",Indwidth);
1378 if ( Type[0] !=
'P' ) {
1379 if ( Valfmt == NULL ) Valfmt =
"(4E20.13)";
1380 ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
1381 sprintf(vformat,
"%%%ds",Valwidth);
1384 ptrcrd = (N+1)/Ptrperline;
1385 if ( (N+1)%Ptrperline != 0) ptrcrd++;
1387 indcrd = nz/Indperline;
1388 if ( nz%Indperline != 0) indcrd++;
1390 valcrd = nvalentries/Valperline;
1391 if ( nvalentries%Valperline != 0) valcrd++;
1394 if ( Rhsfmt == NULL ) Rhsfmt = Valfmt;
1395 ParseRfmt(Rhsfmt,&Rhsperline,&Rhswidth,&Rhsprec, &Rhsflag);
1396 sprintf(rformat,
"%%%ds",Rhswidth);
1397 rhscrd = nrhsentries/Rhsperline;
1398 if ( nrhsentries%Rhsperline != 0) rhscrd++;
1399 if ( Rhstype[1] ==
'G' ) rhscrd+=rhscrd;
1400 if ( Rhstype[2] ==
'X' ) rhscrd+=rhscrd;
1404 totcrd = 4+ptrcrd+indcrd+valcrd+rhscrd;
1409 fprintf(out_file,
"%-72s%-8s\n%14d%14d%14d%14d%14d\n",Title, Key, totcrd,
1410 ptrcrd, indcrd, valcrd, rhscrd);
1411 fprintf(out_file,
"%3s%11s%14d%14d%14d\n",Type,
" ", M, N, nz);
1412 fprintf(out_file,
"%-16s%-16s%-20s", Ptrfmt, Indfmt, Valfmt);
1416 fprintf(out_file,
"%-20s\n%-14s%d\n",Rhsfmt,Rhstype,Nrhs);
1417 }
else fprintf(out_file,
"\n");
1425 entry = colptr[i]+offset;
1426 fprintf(out_file,pformat,entry);
1427 if ( (i+1)%Ptrperline == 0 ) fprintf(out_file,
"\n");
1430 if ( (N+1) % Ptrperline != 0 ) fprintf(out_file,
"\n");
1435 entry = rowind[i]+offset;
1436 fprintf(out_file,iformat,entry);
1437 if ( (i+1)%Indperline == 0 ) fprintf(out_file,
"\n");
1440 if ( nz % Indperline != 0 ) fprintf(out_file,
"\n");
1444 if ( Type[0] !=
'P' ) {
1445 for (i=0;i<nvalentries;i++)
1447 fprintf(out_file,vformat,val+i*Valwidth);
1448 if ( (i+1)%Valperline == 0 ) fprintf(out_file,
"\n");
1451 if ( nvalentries % Valperline != 0 ) fprintf(out_file,
"\n");
1457 for (j=0;j<Nrhs;j++) {
1458 for (i=0;i<nrhsentries;i++)
1460 fprintf(out_file,rformat,rhs+i*Rhswidth);
1461 if ( acount++%Rhsperline == linemod ) fprintf(out_file,
"\n");
1463 if ( acount%Rhsperline != linemod ) {
1464 fprintf(out_file,
"\n");
1465 linemod = (acount-1)%Rhsperline;
1467 if ( Rhstype[1] ==
'G' ) {
1468 for (i=0;i<nrhsentries;i++)
1470 fprintf(out_file,rformat,guess+i*Rhswidth);
1471 if ( acount++%Rhsperline == linemod ) fprintf(out_file,
"\n");
1473 if ( acount%Rhsperline != linemod ) {
1474 fprintf(out_file,
"\n");
1475 linemod = (acount-1)%Rhsperline;
1478 if ( Rhstype[2] ==
'X' ) {
1479 for (i=0;i<nrhsentries;i++)
1481 fprintf(out_file,rformat,exact+i*Rhswidth);
1482 if ( acount++%Rhsperline == linemod ) fprintf(out_file,
"\n");
1484 if ( acount%Rhsperline != linemod ) {
1485 fprintf(out_file,
"\n");
1486 linemod = (acount-1)%Rhsperline;
1494 if ( fclose(out_file) != 0){
1495 fprintf(stderr,
"Error closing file in writeHB_mat_char().\n");
1509 *perline = 0; *width = 0;
return 0;
1512 tmp = strchr(fmt,
'(');
1513 tmp =
substr(fmt,tmp - fmt + 1, strchr(fmt,
'I') - tmp - 1);
1514 *perline = atoi(tmp);
1517 tmp = strchr(fmt,
'I');
1518 tmp =
substr(fmt,tmp - fmt + 1, strchr(fmt,
')') - tmp - 1);
1525 int ParseRfmt(
char* fmt,
int* perline,
int* width,
int* prec,
int* flag)
1546 if (strchr(fmt,
'(') != NULL) fmt = strchr(fmt,
'(');
1547 if (strchr(fmt,
')') != NULL) {
1548 tmp2 = strchr(fmt,
')');
1549 while ( strchr(tmp2+1,
')') != NULL ) {
1550 tmp2 = strchr(tmp2+1,
')');
1552 *(tmp2+1) = (
int) NULL;
1554 if (strchr(fmt,
'P') != NULL)
1556 if (strchr(fmt,
'(') != NULL) {
1557 tmp = strchr(fmt,
'P');
1558 if ( *(++tmp) ==
',' ) tmp++;
1559 tmp3 = strchr(fmt,
'(')+1;
1562 while ( *(tmp2+len) != (
int) NULL ) {
1566 *(strchr(fmt,
')')+1) = (
int) NULL;
1569 if (strchr(fmt,
'E') != NULL) {
1571 }
else if (strchr(fmt,
'D') != NULL) {
1573 }
else if (strchr(fmt,
'F') != NULL) {
1576 fprintf(stderr,
"Real format %s in H/B file not supported.\n",fmt);
1579 tmp = strchr(fmt,
'(');
1580 tmp =
substr(fmt,tmp - fmt + 1, strchr(fmt,*flag) - tmp - 1);
1581 *perline = atoi(tmp);
1584 if(tmp != NULL) free(tmp);
1586 tmp = strchr(fmt,*flag);
1587 if ( strchr(fmt,
'.') ) {
1591 char * tmp2 =
substr( fmt, strchr(fmt,
'.') - fmt + 1, strchr(fmt,
')') - strchr(fmt,
'.')-1);
1592 *prec = atoi( tmp2 );
1593 if(tmp2 != NULL) free(tmp2);
1595 tmp =
substr(fmt,tmp - fmt + 1, strchr(fmt,
'.') - tmp - 1);
1597 tmp =
substr(fmt,tmp - fmt + 1, strchr(fmt,
')') - tmp - 1);
1605 if(tmp != NULL) free(tmp);
1612 char*
substr(
const char*
S,
const int pos,
const int len)
1616 if ( pos+len <= strlen(S)) {
1617 SubS = (
char *)malloc(len+1);
1618 if ( SubS == NULL )
IOHBTerminate(
"Insufficient memory for SubS.");
1619 for (i=0;i<len;i++) SubS[i] = S[pos+i];
1620 SubS[len] = (char) NULL;
1633 for (i=0;i< len;i++)
1634 S[i] = toupper(S[i]);
1639 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)