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