174 |
|
|
175 |
|
/* Use the spectral sensitivity function to compute matrix coefficients */ |
176 |
|
void |
177 |
< |
sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, int ncs, const float wlpt[4])) |
177 |
> |
sensrow(ROPMAT *rop, int r, double (*sf)(const SCOLOR sc, int ncs, const float wlpt[4])) |
178 |
|
{ |
179 |
|
const int nc = rop->imx.ncomp; |
180 |
|
int i; |
488 |
|
restype = mop[i].rmp->dtype; |
489 |
|
if (!imp->dtype || (restype = rmx_newtype(restype, imp->dtype)) > 0) |
490 |
|
imp->dtype = restype; |
491 |
< |
else |
491 |
> |
else if (!nowarn) |
492 |
|
fprintf(stderr, "%s: warning - data type mismatch\n", |
493 |
|
mop[i].inspec); |
494 |
|
if (!i) { |
552 |
|
|
553 |
|
#if defined(_WIN32) || defined(_WIN64) |
554 |
|
if (np > 1) { |
555 |
< |
fputs("Warning: only one process under Windows\n", stderr); |
555 |
> |
if (!nowarn) |
556 |
> |
fputs("Warning: only one process under Windows\n", stderr); |
557 |
|
np = 1; |
558 |
|
} else |
559 |
|
#endif |
571 |
|
} |
572 |
|
} |
573 |
|
/* prep output row buffer(s) */ |
574 |
< |
if (mcat || mop[nmats].preop.clen > 0) { |
574 |
> |
if (mop[nmats].preop.clen > 0) { |
575 |
|
if (!split_input(&mop[nmats])) /* need separate buffer */ |
576 |
< |
return(0); |
577 |
< |
if (mop[nmats].preop.clen > 0) |
578 |
< |
mop[nmats].rmp->ncomp = mop[nmats].preop.clen / |
578 |
< |
mop[nmats].imx.ncomp; |
576 |
> |
goto memerror; |
577 |
> |
mop[nmats].rmp->ncomp = mop[nmats].preop.clen / |
578 |
> |
mop[nmats].imx.ncomp; |
579 |
|
} |
580 |
|
mop[nmats].imx.nrows = 1; |
581 |
|
if (!rmx_prepare(&mop[nmats].imx)) |
600 |
|
goto memerror; |
601 |
|
for (i = nchildren; i--; ) cproc[i] = sp_inactive; |
602 |
|
cproc[nchildren-1].flags |= PF_FILT_OUT; |
603 |
< |
/* start each child */ |
604 |
< |
for (i = 0; i < nchildren; i++) { |
605 |
< |
rv = open_process(&cproc[i], NULL); |
606 |
< |
if (rv <= 0) break; |
607 |
< |
} |
603 |
> |
/* start each child from parent */ |
604 |
> |
for (i = 0; i < nchildren; i++) |
605 |
> |
if ((rv = open_process(&cproc[i], NULL)) <= 0) |
606 |
> |
break; /* child breaks here */ |
607 |
|
if (rv < 0) { |
608 |
< |
perror("fork"); |
608 |
> |
perror("fork"); /* WTH? */ |
609 |
|
close_processes(cproc, i); |
610 |
|
exit(1); |
611 |
|
} |
612 |
< |
if (rv) { /* are we the parent? */ |
613 |
< |
i = nchildren-1; /* last child is sole reader */ |
614 |
< |
while (i-- > 0) { |
615 |
< |
close(cproc[i].r); |
616 |
< |
cproc[i].r = -1; |
612 |
> |
if (i != nchildren-1) { /* last child is sole reader */ |
613 |
> |
int j = i; |
614 |
> |
while (j-- > 0) { |
615 |
> |
close(cproc[j].r); |
616 |
> |
cproc[j].r = -1; |
617 |
|
} |
619 |
– |
return(1); /* parent return value */ |
618 |
|
} |
619 |
< |
inchild = i; /* our child index */ |
619 |
> |
if (rv > 0) |
620 |
> |
return(1); /* parent return value */ |
621 |
> |
|
622 |
> |
inchild = i; /* else set our child index */ |
623 |
|
while (i-- > 0) /* only parent writes siblings */ |
624 |
|
close(cproc[i].w); |
625 |
|
|
626 |
+ |
i = nmats; /* close matrix streams (carefully) */ |
627 |
+ |
while (i-- > 0) { |
628 |
+ |
if (mop[i].infp != stdin) { |
629 |
+ |
close(fileno(mop[i].infp)); /* avoid lseek() */ |
630 |
+ |
fclose(mop[i].infp); /* ! pclose() */ |
631 |
+ |
} |
632 |
+ |
mop[i].infp = NULL; |
633 |
+ |
} |
634 |
+ |
fpurge(stdin); /* discard previously buffered input */ |
635 |
+ |
|
636 |
|
if (inchild == nchildren-1) |
637 |
|
return(-1); /* output process return value */ |
638 |
|
|
639 |
< |
i = inchild; /* won't read from siblings */ |
629 |
< |
while (i-- > 0) |
630 |
< |
close(cproc[i].r); |
631 |
< |
i = nmats; /* redirect input matrix streams */ |
639 |
> |
i = nmats; /* get matrix rows from parent */ |
640 |
|
while (i-- > 0) { |
633 |
– |
if (mop[i].infp != stdin) |
634 |
– |
fclose(mop[i].infp); /* ! pclose() */ |
641 |
|
mop[i].infp = stdin; |
642 |
|
mop[i].imx.dtype = DTrmx_native; |
643 |
|
mop[i].imx.pflags &= ~RMF_SWAPIN; |
644 |
|
} |
639 |
– |
fpurge(stdin); /* discard any previous matrix input */ |
645 |
|
#ifdef getc_unlocked |
646 |
|
flockfile(stdin); |
647 |
|
#endif |
673 |
|
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
674 |
|
if (cur_row > in_nrows) /* unknown #input rows? */ |
675 |
|
break; |
676 |
< |
fprintf(stderr, "%s: parent read error at row %d\n", |
676 |
> |
fprintf(stderr, "%s: load error at row %d\n", |
677 |
|
mop[i].inspec, cur_row); |
678 |
|
return(0); |
679 |
|
} |
681 |
|
break; |
682 |
|
for (i = 0; i < nmats; i++) |
683 |
|
if (writebuf(wfd, mop[i].imx.mtx, rmx_array_size(&mop[i].imx)) |
684 |
< |
!= rmx_array_size(&mop[i].imx)) |
684 |
> |
!= rmx_array_size(&mop[i].imx)) { |
685 |
> |
fprintf(stderr, "%s: write error at row %d\n", |
686 |
> |
mop[i].inspec, cur_row); |
687 |
|
return(0); |
688 |
+ |
} |
689 |
|
} |
690 |
< |
i = close_processes(cproc, nchildren); |
690 |
> |
i = close_processes(cproc, nchildren); /* collect family */ |
691 |
|
free(cproc); cproc = NULL; nchildren = 0; |
692 |
|
if (i < 0) { |
693 |
< |
fputs("Warning: lost child in parent_loop()\n", stderr); |
693 |
> |
if (!nowarn) |
694 |
> |
fputs("Warning: lost child process\n", stderr); |
695 |
|
return(1); |
696 |
|
} |
697 |
|
if (i > 0) { |
699 |
|
return(0); |
700 |
|
} |
701 |
|
return(1); /* return success! */ |
693 |
– |
memerror: |
694 |
– |
fputs("Out of memory in parent_loop()\n", stderr); |
695 |
– |
exit(1); |
702 |
|
} |
703 |
|
|
704 |
|
int |
712 |
|
int co_set; |
713 |
|
int i; |
714 |
|
|
715 |
< |
if (mcat && mcat_last && |
716 |
< |
!(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp))) |
717 |
< |
goto memerror; |
715 |
> |
if (mcat_last && !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp))) { |
716 |
> |
fputs("Out of buffer space in combine_input()\n", stderr); |
717 |
> |
return(0); |
718 |
> |
} |
719 |
|
/* figure out what the user set */ |
720 |
|
co_set = fundefined("co"); |
721 |
|
if (!co_set) |
737 |
|
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
738 |
|
if (cur_row > in_nrows) /* unknown #input rows? */ |
739 |
|
break; |
740 |
< |
fprintf(stderr, "%s: read error at row %d\n", |
740 |
> |
fprintf(stderr, "%s: load error at row %d\n", |
741 |
|
mop[i].inspec, cur_row); |
742 |
|
return(0); |
743 |
|
} |
783 |
|
} |
784 |
|
rmx_free(mres); mres = NULL; |
785 |
|
if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp, |
786 |
< |
res->rmp->ncols, res->rmp->dtype, stdout)) |
786 |
> |
res->rmp->ncols, res->rmp->dtype, stdout) || |
787 |
> |
(inchild >= 0 && fflush(stdout) == EOF)) { |
788 |
> |
fprintf(stderr, "Conversion/write error at row %d\n", |
789 |
> |
cur_row); |
790 |
|
return(0); |
791 |
< |
if (inchild >= 0 && fflush(stdout) == EOF) |
782 |
< |
return(0); |
791 |
> |
} |
792 |
|
} |
793 |
|
return(inchild >= 0 || fflush(stdout) != EOF); |
785 |
– |
memerror: |
786 |
– |
fputs("Out of buffer space in combine_input()\n", stderr); |
787 |
– |
return(0); |
794 |
|
multerror: |
795 |
< |
fputs("Unexpected matrix multiply error in combine_input()\n", stderr); |
795 |
> |
fputs("Unexpected matrix multiply error\n", stderr); |
796 |
|
return(0); |
797 |
|
} |
798 |
|
|
800 |
|
output_loop(void) |
801 |
|
{ |
802 |
|
const size_t row_size = rmx_array_size(mop[nmats].rmp); |
797 |
– |
int i = nmats; |
803 |
|
int cur_child = 0; |
804 |
+ |
int i = nmats; |
805 |
|
|
806 |
< |
if (mop[nmats].rmp != &mop[nmats].imx) /* output is split? */ |
801 |
< |
rmx_reset(&mop[nmats].imx); |
802 |
< |
while (i-- > 0) { /* close input matrices */ |
803 |
< |
fclose(mop[i].infp); /* ! pclose() */ |
804 |
< |
mop[i].infp = NULL; |
806 |
> |
while (i-- > 0) { /* free input buffers */ |
807 |
|
rmx_reset(&mop[i].imx); |
808 |
|
if (mop[i].rmp != &mop[i].imx) { |
809 |
|
rmx_free(mop[i].rmp); |
810 |
|
mop[i].rmp = &mop[i].imx; |
811 |
|
} |
812 |
|
} |
813 |
+ |
if (mop[nmats].rmp != &mop[nmats].imx) /* output is split? */ |
814 |
+ |
rmx_reset(&mop[nmats].imx); |
815 |
|
#ifdef getc_unlocked |
816 |
|
flockfile(stdout); /* we own this, now */ |
817 |
|
#endif |
821 |
|
if (!rv) /* out of rows? */ |
822 |
|
break; |
823 |
|
if (rv != row_size) { |
824 |
< |
fputs("Read error in output loop\n", stderr); |
824 |
> |
fputs("Read error\n", stderr); |
825 |
|
return(0); |
826 |
|
} /* do final conversion */ |
827 |
|
if (!rmx_write_data(mop[nmats].rmp->mtx, mop[nmats].rmp->ncomp, |
828 |
|
mop[nmats].rmp->ncols, mop[nmats].rmp->dtype, stdout)) { |
829 |
< |
fputs("Conversion/write error in output loop\n", stderr); |
829 |
> |
fputs("Conversion/write error\n", stderr); |
830 |
|
return(0); |
831 |
|
} |
832 |
|
cur_child++; |
833 |
< |
cur_child *= (cur_child < inchild); |
833 |
> |
cur_child *= (cur_child < inchild); /* loop over workers */ |
834 |
|
} |
835 |
|
return(fflush(stdout) != EOF); |
836 |
|
} |
852 |
|
|
853 |
|
if (n2alloc == nall) |
854 |
|
return; |
855 |
< |
for (i = nall; i > n2alloc; i--) { |
855 |
> |
for (i = nall; i-- > n2alloc; ) { |
856 |
|
rmx_reset(&mop[i].imx); |
857 |
|
if (mop[i].rmp != &mop[i].imx) |
858 |
|
rmx_free(mop[i].rmp); |
947 |
|
} |
948 |
|
break; |
949 |
|
case 'C': |
950 |
+ |
mcat_last = 0; |
951 |
|
if (!n || isflt(argv[i+1])) |
952 |
|
goto userr; |
953 |
|
defCsym = mop[nmats].preop.csym = argv[++i]; |
954 |
|
mop[nmats].preop.clen = 0; |
950 |
– |
mcat_last = 0; |
955 |
|
break; |
956 |
|
case 'c': |
957 |
+ |
mcat_last = 0; |
958 |
|
if (n && !isflt(argv[i+1])) { |
959 |
|
mop[nmats].preop.csym = argv[++i]; |
960 |
|
mop[nmats].preop.clen = 0; |
956 |
– |
mcat_last = 0; |
961 |
|
break; |
962 |
|
} |
963 |
|
if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP; |
970 |
|
goto userr; |
971 |
|
} |
972 |
|
mop[nmats].preop.csym = NULL; |
969 |
– |
mcat_last = 0; |
973 |
|
break; |
974 |
|
case 'm': |
975 |
+ |
mcat_last = 1; |
976 |
|
if (!n) goto userr; |
977 |
|
if (argv[++i][0] == '-' && !argv[i][1]) { |
978 |
|
if (stdin_used++) goto stdin_error; |
979 |
|
mcat_spec = stdin_name; |
980 |
|
} else |
981 |
|
mcat_spec = argv[i]; |
978 |
– |
mcat_last = 1; |
982 |
|
break; |
983 |
|
default: |
984 |
|
fprintf(stderr, "%s: unknown option '%s'\n", |