| 3 |
|
#endif |
| 4 |
|
/* |
| 5 |
|
* General component matrix combiner, operating on a row at a time. |
| 6 |
+ |
* |
| 7 |
+ |
* Multi-processing mode under Unix creates children that each work |
| 8 |
+ |
* on one input row at a time, fed by the original process. Final conversion |
| 9 |
+ |
* and output to stdout is sorted by last child while its siblings send it |
| 10 |
+ |
* their record calculations. |
| 11 |
|
*/ |
| 12 |
|
|
| 8 |
– |
#include <errno.h> |
| 13 |
|
#include <math.h> |
| 14 |
|
#include "platform.h" |
| 15 |
+ |
#include "rtprocess.h" |
| 16 |
|
#include "rtio.h" |
| 12 |
– |
#include "resolu.h" |
| 17 |
|
#include "rmatrix.h" |
| 18 |
|
#include "calcomp.h" |
| 15 |
– |
#include "paths.h" |
| 19 |
|
|
| 20 |
|
#ifndef M_PI |
| 21 |
|
#define M_PI 3.14159265358979323846 |
| 48 |
|
RMATRIX *mcat = NULL; /* final concatenation */ |
| 49 |
|
int mcat_last = 0; /* goes after trailing ops? */ |
| 50 |
|
|
| 51 |
< |
int in_nrows; /* input row count */ |
| 52 |
< |
#define in_ncols (mop[0].rmp->ncols) /* input column count */ |
| 51 |
> |
int in_nrows; /* number of input rows (or 0) */ |
| 52 |
> |
#define in_ncols (mop[0].rmp->ncols) /* number of input columns */ |
| 53 |
|
#define in_ncomp (mop[0].rmp->ncomp) /* input #components */ |
| 54 |
|
|
| 55 |
|
extern int nowarn; /* turn off warnings? */ |
| 58 |
|
int cur_col; /* current input/output column */ |
| 59 |
|
int cur_chan; /* if we're looping channels */ |
| 60 |
|
|
| 61 |
< |
static int checksymbolic(ROPMAT *rop); |
| 61 |
> |
SUBPROC *cproc = NULL; /* child process array */ |
| 62 |
> |
int nchildren = 0; /* # of child processes */ |
| 63 |
> |
int inchild = -1; /* our child ID (-1: parent) */ |
| 64 |
|
|
| 65 |
< |
static int |
| 65 |
> |
extern int checksymbolic(ROPMAT *rop); |
| 66 |
> |
|
| 67 |
> |
int |
| 68 |
|
split_input(ROPMAT *rop) |
| 69 |
|
{ |
| 70 |
|
if (rop->rmp == &rop->imx && !(rop->rmp = rmx_copy(&rop->imx))) { |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
/* Check/set transform based on a reference input file */ |
| 79 |
< |
static int |
| 79 |
> |
int |
| 80 |
|
checkreffile(ROPMAT *rop) |
| 81 |
|
{ |
| 82 |
|
static const char *curRF = NULL; |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
/* Compute conversion row from spectrum to one channel of RGB */ |
| 142 |
< |
static void |
| 142 |
> |
void |
| 143 |
|
rgbrow(ROPMAT *rop, int r, int p) |
| 144 |
|
{ |
| 145 |
|
const int nc = rop->imx.ncomp; |
| 156 |
|
} |
| 157 |
|
|
| 158 |
|
/* Compute conversion row from spectrum to one channel of XYZ */ |
| 159 |
< |
static void |
| 159 |
> |
void |
| 160 |
|
xyzrow(ROPMAT *rop, int r, int p) |
| 161 |
|
{ |
| 162 |
|
const int nc = rop->imx.ncomp; |
| 173 |
|
} |
| 174 |
|
|
| 175 |
|
/* Use the spectral sensitivity function to compute matrix coefficients */ |
| 176 |
< |
static void |
| 176 |
> |
void |
| 177 |
|
sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, int ncs, const float wlpt[4])) |
| 178 |
|
{ |
| 179 |
|
const int nc = rop->imx.ncomp; |
| 188 |
|
} |
| 189 |
|
|
| 190 |
|
/* Check/set symbolic transform */ |
| 191 |
< |
static int |
| 191 |
> |
int |
| 192 |
|
checksymbolic(ROPMAT *rop) |
| 193 |
|
{ |
| 194 |
|
const int nc = rop->imx.ncomp; |
| 195 |
|
const int dt = rop->imx.dtype; |
| 196 |
+ |
double cf = 1; |
| 197 |
|
int i, j; |
| 198 |
|
/* check suffix => reference file */ |
| 199 |
|
if (strchr(rop->preop.csym, '.') > rop->preop.csym) |
| 214 |
|
int comp = 0; |
| 215 |
|
switch (rop->preop.csym[j]) { |
| 216 |
|
case 'B': |
| 217 |
+ |
case 'b': |
| 218 |
|
++comp; |
| 219 |
|
/* fall through */ |
| 220 |
|
case 'G': |
| 221 |
+ |
case 'g': |
| 222 |
|
++comp; |
| 223 |
|
/* fall through */ |
| 224 |
|
case 'R': |
| 225 |
+ |
case 'r': |
| 226 |
+ |
if (rop->preop.csym[j] <= 'Z') |
| 227 |
+ |
cf = 1./WHTEFFICACY; |
| 228 |
|
if (dt == DTxyze) { |
| 229 |
|
for (i = 3; i--; ) |
| 230 |
< |
rop->preop.cmat[j*nc+i] = 1./WHTEFFICACY * |
| 218 |
< |
xyz2rgbmat[comp][i]; |
| 230 |
> |
rop->preop.cmat[j*nc+i] = cf*xyz2rgbmat[comp][i]; |
| 231 |
|
} else if (nc == 3) |
| 232 |
|
rop->preop.cmat[j*nc+comp] = 1.; |
| 233 |
|
else |
| 234 |
|
rgbrow(rop, j, comp); |
| 235 |
|
break; |
| 236 |
|
case 'Z': |
| 237 |
+ |
case 'z': |
| 238 |
|
++comp; |
| 239 |
|
/* fall through */ |
| 240 |
|
case 'Y': |
| 241 |
+ |
case 'y': |
| 242 |
|
++comp; |
| 243 |
|
/* fall through */ |
| 244 |
|
case 'X': |
| 245 |
+ |
case 'x': |
| 246 |
+ |
if ((rop->preop.csym[j] <= 'Z') & (dt != DTxyze)) |
| 247 |
+ |
cf = WHTEFFICACY; |
| 248 |
|
if (dt == DTxyze) { |
| 249 |
|
rop->preop.cmat[j*nc+comp] = 1.; |
| 250 |
|
} else if (nc == 3) { |
| 256 |
|
else |
| 257 |
|
xyzrow(rop, j, comp); |
| 258 |
|
|
| 259 |
< |
for (i = nc*(dt != DTxyze); i--; ) |
| 260 |
< |
rop->preop.cmat[j*nc+i] *= WHTEFFICACY; |
| 259 |
> |
for (i = nc*(cf != 1); i--; ) |
| 260 |
> |
rop->preop.cmat[j*nc+i] *= cf; |
| 261 |
|
break; |
| 262 |
|
case 'S': /* scotopic (il)luminance */ |
| 263 |
+ |
cf = WHTSCOTOPIC; |
| 264 |
+ |
/* fall through */ |
| 265 |
+ |
case 's': |
| 266 |
|
sensrow(rop, j, scolor2scotopic); |
| 267 |
< |
for (i = nc; i--; ) |
| 268 |
< |
rop->preop.cmat[j*nc+i] *= WHTSCOTOPIC; |
| 267 |
> |
for (i = nc*(cf != 1); i--; ) |
| 268 |
> |
rop->preop.cmat[j*nc+i] *= cf; |
| 269 |
|
break; |
| 270 |
|
case 'M': /* melanopic (il)luminance */ |
| 271 |
+ |
cf = WHTMELANOPIC; |
| 272 |
+ |
/* fall through */ |
| 273 |
+ |
case 'm': |
| 274 |
|
sensrow(rop, j, scolor2melanopic); |
| 275 |
< |
for (i = nc; i--; ) |
| 276 |
< |
rop->preop.cmat[j*nc+i] *= WHTMELANOPIC; |
| 275 |
> |
for (i = nc*(cf != 1); i--; ) |
| 276 |
> |
rop->preop.cmat[j*nc+i] *= cf; |
| 277 |
|
break; |
| 278 |
|
case 'A': /* average component */ |
| 279 |
+ |
case 'a': |
| 280 |
|
for (i = nc; i--; ) |
| 281 |
|
rop->preop.cmat[j*nc+i] = 1./(double)nc; |
| 282 |
|
break; |
| 291 |
|
memcpy(rop->rmp->wlpart, WLPART, sizeof(rop->rmp->wlpart)); |
| 292 |
|
rop->rmp->ncomp = rop->preop.clen / nc; |
| 293 |
|
/* decide on output type */ |
| 294 |
< |
if (!strcmp(rop->preop.csym, "XYZ")) { |
| 294 |
> |
if (!strcasecmp(rop->preop.csym, "XYZ")) { |
| 295 |
|
if (dt <= DTspec) |
| 296 |
|
rop->rmp->dtype = DTxyze; |
| 297 |
< |
} else if (!strcmp(rop->preop.csym, "RGB")) { |
| 297 |
> |
} else if (!strcasecmp(rop->preop.csym, "RGB")) { |
| 298 |
|
if (dt <= DTspec) |
| 299 |
|
rop->rmp->dtype = DTrgbe; |
| 300 |
|
} else if (rop->rmp->dtype == DTspec) |
| 302 |
|
return(1); |
| 303 |
|
} |
| 304 |
|
|
| 305 |
< |
static int |
| 305 |
> |
int |
| 306 |
|
get_component_xfm(ROPMAT *rop) |
| 307 |
|
{ |
| 308 |
|
int i, j; |
| 385 |
|
return(1); |
| 386 |
|
} |
| 387 |
|
|
| 388 |
< |
static int |
| 388 |
> |
int |
| 389 |
|
apply_op(RMATRIX *dst, const RMATRIX *src, const RUNARYOP *ro) |
| 390 |
|
{ |
| 391 |
|
if (ro->clen > 0) { |
| 398 |
|
return(0); |
| 399 |
|
rmx_free(res); |
| 400 |
|
} else if (dst != src) |
| 401 |
< |
memcpy(dst->mtx, src->mtx, |
| 378 |
< |
sizeof(double)*dst->ncomp*dst->ncols*dst->nrows); |
| 401 |
> |
memcpy(dst->mtx, src->mtx, rmx_array_size(dst)); |
| 402 |
|
if (ro->nsf == dst->ncomp) |
| 403 |
|
rmx_scale(dst, ro->sca); |
| 404 |
|
return(1); |
| 405 |
|
} |
| 406 |
|
|
| 407 |
< |
static int |
| 407 |
> |
int |
| 408 |
|
open_input(ROPMAT *rop) |
| 409 |
|
{ |
| 410 |
|
int outtype; |
| 426 |
|
} |
| 427 |
|
|
| 428 |
|
/* Return nominal wavelength associated with input component (return nm) */ |
| 429 |
< |
static double |
| 429 |
> |
double |
| 430 |
|
l_wavelength(char *nam) |
| 431 |
|
{ |
| 432 |
|
double comp = argument(1); |
| 450 |
|
} |
| 451 |
|
|
| 452 |
|
/* Return ith input with optional channel selector */ |
| 453 |
< |
static double |
| 453 |
> |
double |
| 454 |
|
l_chanin(char *nam) |
| 455 |
|
{ |
| 456 |
|
double inp = argument(1); |
| 475 |
|
return(mop[mi].rmp->mtx[cur_col*in_ncomp + chan]); |
| 476 |
|
} |
| 477 |
|
|
| 478 |
< |
static int |
| 478 |
> |
int |
| 479 |
|
initialize(RMATRIX *imp) |
| 480 |
|
{ |
| 481 |
|
int i; |
| 492 |
|
fprintf(stderr, "%s: warning - data type mismatch\n", |
| 493 |
|
mop[i].inspec); |
| 494 |
|
if (!i) { |
| 472 |
– |
imp->nrows = in_nrows = mop[0].rmp->nrows; |
| 495 |
|
imp->ncols = mop[0].rmp->ncols; |
| 496 |
|
imp->ncomp = mop[0].rmp->ncomp; |
| 497 |
|
memcpy(imp->wlpart, mop[0].rmp->wlpart, sizeof(imp->wlpart)); |
| 498 |
< |
} else if ((mop[i].rmp->nrows != imp->nrows) | |
| 499 |
< |
(mop[i].rmp->ncols != imp->ncols) | |
| 500 |
< |
(mop[i].rmp->ncomp != imp->ncomp)) { |
| 498 |
> |
} else if ((mop[i].rmp->ncols != imp->ncols) | |
| 499 |
> |
(mop[i].rmp->ncomp != imp->ncomp) | |
| 500 |
> |
((in_nrows > 0) & (mop[i].rmp->nrows > 0) & |
| 501 |
> |
(mop[i].rmp->nrows != in_nrows))) { |
| 502 |
|
fprintf(stderr, "%s: mismatch in size or #components\n", |
| 503 |
|
mop[i].inspec); |
| 504 |
|
return(0); |
| 505 |
|
} /* XXX should check wlpart? */ |
| 506 |
+ |
if (in_nrows <= 0) |
| 507 |
+ |
in_nrows = imp->nrows = mop[i].rmp->nrows; |
| 508 |
|
} /* set up .cal environment */ |
| 509 |
|
esupport |= E_VARIABLE|E_FUNCTION|E_RCONST; |
| 510 |
|
esupport &= ~(E_OUTCHAN|E_INCHAN); |
| 522 |
|
return(1); |
| 523 |
|
} |
| 524 |
|
|
| 525 |
< |
static void |
| 525 |
> |
void |
| 526 |
|
output_headinfo(FILE *fp) |
| 527 |
|
{ |
| 528 |
|
int i; |
| 545 |
|
} |
| 546 |
|
} |
| 547 |
|
|
| 548 |
< |
static int |
| 549 |
< |
combine_input(ROPMAT *res, FILE *fout) |
| 548 |
> |
int |
| 549 |
> |
spawned_children(int np) |
| 550 |
|
{ |
| 551 |
< |
int set_r, set_c; |
| 552 |
< |
RMATRIX *tmp = NULL; |
| 553 |
< |
int co_set; |
| 554 |
< |
int i; |
| 555 |
< |
/* allocate input row buffers */ |
| 551 |
> |
int i, rv; |
| 552 |
> |
|
| 553 |
> |
#if defined(_WIN32) || defined(_WIN64) |
| 554 |
> |
if (np > 1) { |
| 555 |
> |
fputs("Warning: only one process under Windows\n", stderr); |
| 556 |
> |
np = 1; |
| 557 |
> |
} else |
| 558 |
> |
#endif |
| 559 |
> |
if ((in_nrows > 0) & (np*4 > in_nrows)) |
| 560 |
> |
np = in_nrows/4; |
| 561 |
> |
/* we'll be doing a row at a time */ |
| 562 |
|
for (i = 0; i < nmats; i++) { |
| 563 |
< |
mop[i].imx.nrows = 1; /* we'll be doing a row at a time */ |
| 563 |
> |
mop[i].imx.nrows = 1; |
| 564 |
|
if (!rmx_prepare(&mop[i].imx)) |
| 565 |
|
goto memerror; |
| 566 |
|
if (mop[i].rmp != &mop[i].imx) { |
| 569 |
|
goto memerror; |
| 570 |
|
} |
| 571 |
|
} |
| 572 |
< |
/* prep output row buffers */ |
| 573 |
< |
if (mcat || res->preop.clen > 0) { |
| 574 |
< |
if (!split_input(res)) /* need separate buffer */ |
| 572 |
> |
/* prep output row buffer(s) */ |
| 573 |
> |
if (mcat || mop[nmats].preop.clen > 0) { |
| 574 |
> |
if (!split_input(&mop[nmats])) /* need separate buffer */ |
| 575 |
|
return(0); |
| 576 |
< |
if (res->preop.clen > 0) |
| 577 |
< |
res->rmp->ncomp = res->preop.clen / res->imx.ncomp; |
| 578 |
< |
res->rmp->nrows = 1; |
| 579 |
< |
if (!mcat | !mcat_last && !rmx_prepare(res->rmp)) |
| 576 |
> |
if (mop[nmats].preop.clen > 0) |
| 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)) |
| 582 |
> |
goto memerror; |
| 583 |
> |
if (mop[nmats].rmp != &mop[nmats].imx) { |
| 584 |
> |
mop[nmats].rmp->nrows = 1; |
| 585 |
> |
if (!rmx_prepare(mop[nmats].rmp)) |
| 586 |
|
goto memerror; |
| 587 |
|
} |
| 588 |
+ |
if (np <= 1) { /* single process return */ |
| 589 |
+ |
#ifdef getc_unlocked |
| 590 |
+ |
for (i = 0; i < nmats; i++) |
| 591 |
+ |
flockfile(mop[i].infp); |
| 592 |
+ |
flockfile(stdout); |
| 593 |
+ |
#endif |
| 594 |
+ |
return(0); |
| 595 |
+ |
} |
| 596 |
+ |
fflush(stdout); /* flush header & spawn children */ |
| 597 |
+ |
nchildren = np + 1; /* extra child to sequence output */ |
| 598 |
+ |
cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*nchildren); |
| 599 |
+ |
if (!cproc) |
| 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 |
+ |
} |
| 608 |
+ |
if (rv < 0) { |
| 609 |
+ |
perror("fork"); |
| 610 |
+ |
close_processes(cproc, i); |
| 611 |
+ |
exit(1); |
| 612 |
+ |
} |
| 613 |
+ |
if (rv) { /* are we the parent? */ |
| 614 |
+ |
i = nchildren-1; /* last child is sole reader */ |
| 615 |
+ |
while (i-- > 0) { |
| 616 |
+ |
close(cproc[i].r); |
| 617 |
+ |
cproc[i].r = -1; |
| 618 |
+ |
} |
| 619 |
+ |
return(1); /* parent return value */ |
| 620 |
+ |
} |
| 621 |
+ |
inchild = i; /* our child index */ |
| 622 |
+ |
while (i-- > 0) /* only parent writes siblings */ |
| 623 |
+ |
close(cproc[i].w); |
| 624 |
+ |
|
| 625 |
+ |
if (inchild == nchildren-1) |
| 626 |
+ |
return(-1); /* output process return value */ |
| 627 |
+ |
|
| 628 |
+ |
i = inchild; /* won't read from siblings */ |
| 629 |
+ |
while (i-- > 0) |
| 630 |
+ |
close(cproc[i].r); |
| 631 |
+ |
i = nmats; /* redirect input matrix streams */ |
| 632 |
+ |
while (i-- > 0) { |
| 633 |
+ |
if (mop[i].infp != stdin) |
| 634 |
+ |
fclose(mop[i].infp); /* ! pclose() */ |
| 635 |
+ |
mop[i].infp = stdin; |
| 636 |
+ |
mop[i].imx.dtype = DTrmx_native; |
| 637 |
+ |
mop[i].imx.pflags &= ~RMF_SWAPIN; |
| 638 |
+ |
} |
| 639 |
+ |
fpurge(stdin); /* discard any previous matrix input */ |
| 640 |
+ |
#ifdef getc_unlocked |
| 641 |
+ |
flockfile(stdin); |
| 642 |
+ |
#endif |
| 643 |
+ |
mop[nmats].rmp->dtype = DTrmx_native; |
| 644 |
+ |
return(0); /* worker child return value */ |
| 645 |
+ |
memerror: |
| 646 |
+ |
fputs("Out of memory in spawned_children()\n", stderr); |
| 647 |
+ |
exit(1); |
| 648 |
+ |
} |
| 649 |
+ |
|
| 650 |
+ |
int |
| 651 |
+ |
parent_loop(void) |
| 652 |
+ |
{ |
| 653 |
+ |
int i; |
| 654 |
+ |
|
| 655 |
+ |
rmx_reset(&mop[nmats].imx); /* not touching output side */ |
| 656 |
+ |
if (mop[nmats].rmp != &mop[nmats].imx) { |
| 657 |
+ |
rmx_free(mop[nmats].rmp); |
| 658 |
+ |
mop[nmats].rmp = &mop[nmats].imx; |
| 659 |
+ |
} |
| 660 |
+ |
#ifdef getc_unlocked |
| 661 |
+ |
for (i = 0; i < nmats; i++) /* we handle matrix inputs */ |
| 662 |
+ |
flockfile(mop[i].infp); |
| 663 |
+ |
#endif |
| 664 |
+ |
/* load & send rows to kids */ |
| 665 |
+ |
for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) { |
| 666 |
+ |
int wfd = cproc[cur_row % (nchildren-1)].w; |
| 667 |
+ |
for (i = 0; i < nmats; i++) |
| 668 |
+ |
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
| 669 |
+ |
if (cur_row > in_nrows) /* unknown #input rows? */ |
| 670 |
+ |
break; |
| 671 |
+ |
fprintf(stderr, "%s: parent read error at row %d\n", |
| 672 |
+ |
mop[i].inspec, cur_row); |
| 673 |
+ |
return(0); |
| 674 |
+ |
} |
| 675 |
+ |
if (i < nmats) |
| 676 |
+ |
break; |
| 677 |
+ |
for (i = 0; i < nmats; i++) |
| 678 |
+ |
if (writebuf(wfd, mop[i].imx.mtx, rmx_array_size(&mop[i].imx)) |
| 679 |
+ |
!= rmx_array_size(&mop[i].imx)) |
| 680 |
+ |
return(0); |
| 681 |
+ |
} |
| 682 |
+ |
i = close_processes(cproc, nchildren); |
| 683 |
+ |
free(cproc); cproc = NULL; nchildren = 0; |
| 684 |
+ |
if (i < 0) { |
| 685 |
+ |
fputs("Warning: lost child in parent_loop()\n", stderr); |
| 686 |
+ |
return(1); |
| 687 |
+ |
} |
| 688 |
+ |
if (i > 0) { |
| 689 |
+ |
fprintf(stderr, "Child exited with status %d\n", i); |
| 690 |
+ |
return(0); |
| 691 |
+ |
} |
| 692 |
+ |
return(1); /* return success! */ |
| 693 |
+ |
memerror: |
| 694 |
+ |
fputs("Out of memory in parent_loop()\n", stderr); |
| 695 |
+ |
exit(1); |
| 696 |
+ |
} |
| 697 |
+ |
|
| 698 |
+ |
int |
| 699 |
+ |
combine_input(void) |
| 700 |
+ |
{ |
| 701 |
+ |
const int row0 = (inchild >= 0)*inchild; |
| 702 |
+ |
const int rstep = nchildren ? nchildren-1 : 1; |
| 703 |
+ |
ROPMAT *res = &mop[nmats]; |
| 704 |
+ |
int set_r, set_c; |
| 705 |
+ |
RMATRIX *tmp = NULL; |
| 706 |
+ |
int co_set; |
| 707 |
+ |
int i; |
| 708 |
+ |
|
| 709 |
|
if (mcat && mcat_last && |
| 710 |
|
!(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp))) |
| 711 |
|
goto memerror; |
| 554 |
– |
res->imx.nrows = 1; |
| 555 |
– |
if (!rmx_prepare(&res->imx)) |
| 556 |
– |
goto memerror; |
| 712 |
|
/* figure out what the user set */ |
| 713 |
|
co_set = fundefined("co"); |
| 714 |
|
if (!co_set) |
| 724 |
|
} else /* save a little time */ |
| 725 |
|
set_r = set_c = 0; |
| 726 |
|
/* read/process row-by-row */ |
| 727 |
< |
for (cur_row = 0; cur_row < in_nrows; cur_row++) { |
| 727 |
> |
for (cur_row = row0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row += rstep) { |
| 728 |
|
RMATRIX *mres = NULL; |
| 729 |
< |
for (i = 0; i < nmats; i++) { |
| 729 |
> |
for (i = 0; i < nmats; i++) |
| 730 |
|
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
| 731 |
+ |
if (cur_row > in_nrows) /* unknown #input rows? */ |
| 732 |
+ |
break; |
| 733 |
|
fprintf(stderr, "%s: read error at row %d\n", |
| 734 |
|
mop[i].inspec, cur_row); |
| 735 |
|
return(0); |
| 736 |
|
} |
| 737 |
+ |
if (i < nmats) |
| 738 |
+ |
break; |
| 739 |
+ |
for (i = 0; i < nmats; i++) |
| 740 |
|
if (!apply_op(mop[i].rmp, &mop[i].imx, &mop[i].preop)) |
| 741 |
|
return(0); |
| 582 |
– |
} |
| 742 |
|
if (set_r) varset("r", '=', cur_row); |
| 743 |
|
for (cur_col = 0; cur_col < in_ncols; cur_col++) { |
| 744 |
|
if (set_c) varset("c", '=', cur_col); |
| 776 |
|
} |
| 777 |
|
rmx_free(mres); mres = NULL; |
| 778 |
|
if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp, |
| 779 |
< |
res->rmp->ncols, res->rmp->dtype, fout)) |
| 779 |
> |
res->rmp->ncols, res->rmp->dtype, stdout)) |
| 780 |
|
return(0); |
| 781 |
+ |
if (inchild >= 0 && fflush(stdout) == EOF) |
| 782 |
+ |
return(0); |
| 783 |
|
} |
| 784 |
< |
#if 0 /* we're about to exit, so who cares? */ |
| 624 |
< |
rmx_free(tmp); /* clean up */ |
| 625 |
< |
rmx_reset(res->rmp); |
| 626 |
< |
rmx_reset(&res->imx); |
| 627 |
< |
for (i = 0; i < nmats; i++) { |
| 628 |
< |
rmx_reset(mop[i].rmp); |
| 629 |
< |
rmx_reset(&mop[i].imx); |
| 630 |
< |
if (mop[i].inspec[0] == '!') |
| 631 |
< |
pclose(mop[i].infp); |
| 632 |
< |
else if (mop[i].inspec != stdin_name) |
| 633 |
< |
fclose(mop[i].infp); |
| 634 |
< |
mop[i].infp = NULL; |
| 635 |
< |
} |
| 636 |
< |
#endif |
| 637 |
< |
return(fflush(fout) != EOF); |
| 784 |
> |
return(inchild >= 0 || fflush(stdout) != EOF); |
| 785 |
|
memerror: |
| 786 |
|
fputs("Out of buffer space in combine_input()\n", stderr); |
| 787 |
|
return(0); |
| 790 |
|
return(0); |
| 791 |
|
} |
| 792 |
|
|
| 793 |
< |
static int |
| 793 |
> |
int |
| 794 |
> |
output_loop(void) |
| 795 |
> |
{ |
| 796 |
> |
const size_t row_size = rmx_array_size(mop[nmats].rmp); |
| 797 |
> |
int i = nmats; |
| 798 |
> |
int cur_child = 0; |
| 799 |
> |
|
| 800 |
> |
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; |
| 805 |
> |
rmx_reset(&mop[i].imx); |
| 806 |
> |
if (mop[i].rmp != &mop[i].imx) { |
| 807 |
> |
rmx_free(mop[i].rmp); |
| 808 |
> |
mop[i].rmp = &mop[i].imx; |
| 809 |
> |
} |
| 810 |
> |
} |
| 811 |
> |
#ifdef getc_unlocked |
| 812 |
> |
flockfile(stdout); /* we own this, now */ |
| 813 |
> |
#endif |
| 814 |
> |
for ( ; ; ) { /* loop until no more */ |
| 815 |
> |
ssize_t rv; |
| 816 |
> |
rv = readbuf(cproc[cur_child].r, mop[nmats].rmp->mtx, row_size); |
| 817 |
> |
if (!rv) /* out of rows? */ |
| 818 |
> |
break; |
| 819 |
> |
if (rv != row_size) { |
| 820 |
> |
fputs("Read error in output loop\n", stderr); |
| 821 |
> |
return(0); |
| 822 |
> |
} /* do final conversion */ |
| 823 |
> |
if (!rmx_write_data(mop[nmats].rmp->mtx, mop[nmats].rmp->ncomp, |
| 824 |
> |
mop[nmats].rmp->ncols, mop[nmats].rmp->dtype, stdout)) { |
| 825 |
> |
fputs("Conversion/write error in output loop\n", stderr); |
| 826 |
> |
return(0); |
| 827 |
> |
} |
| 828 |
> |
cur_child++; |
| 829 |
> |
cur_child *= (cur_child < inchild); |
| 830 |
> |
} |
| 831 |
> |
return(fflush(stdout) != EOF); |
| 832 |
> |
} |
| 833 |
> |
|
| 834 |
> |
int |
| 835 |
|
get_factors(double da[], int n, char *av[]) |
| 836 |
|
{ |
| 837 |
|
int ac; |
| 841 |
|
return(ac); |
| 842 |
|
} |
| 843 |
|
|
| 844 |
< |
static void |
| 844 |
> |
void |
| 845 |
|
resize_inparr(int n2alloc) |
| 846 |
|
{ |
| 847 |
|
int i; |
| 848 |
|
|
| 849 |
< |
for (i = nmats; i > n2alloc; i--) { |
| 849 |
> |
if (n2alloc == nall) |
| 850 |
> |
return; |
| 851 |
> |
for (i = nall; i > n2alloc; i--) { |
| 852 |
|
rmx_reset(&mop[i].imx); |
| 853 |
|
if (mop[i].rmp != &mop[i].imx) |
| 854 |
|
rmx_free(mop[i].rmp); |
| 858 |
|
fputs("Out of memory in resize_inparr()\n", stderr); |
| 859 |
|
exit(1); |
| 860 |
|
} |
| 861 |
< |
if (n2alloc > nmats) |
| 862 |
< |
memset(mop+nmats, 0, (n2alloc-nmats)*sizeof(ROPMAT)); |
| 861 |
> |
if (n2alloc > nall) |
| 862 |
> |
memset(mop+nall, 0, (n2alloc-nall)*sizeof(ROPMAT)); |
| 863 |
|
nall = n2alloc; |
| 864 |
|
} |
| 865 |
|
|
| 872 |
|
const char *defCsym = NULL; |
| 873 |
|
int echoheader = 1; |
| 874 |
|
int stdin_used = 0; |
| 875 |
+ |
int nproc = 1; |
| 876 |
|
const char *mcat_spec = NULL; |
| 877 |
|
int n2comp = 0; |
| 878 |
|
uby8 comp_ndx[128]; |
| 900 |
|
case 'h': |
| 901 |
|
echoheader = !echoheader; |
| 902 |
|
break; |
| 903 |
+ |
case 'n': |
| 904 |
+ |
nproc = atoi(argv[++i]); |
| 905 |
+ |
if (nproc <= 0) |
| 906 |
+ |
goto userr; |
| 907 |
+ |
break; |
| 908 |
|
case 'e': |
| 909 |
|
if (!n) goto userr; |
| 910 |
|
comp_ndx[n2comp++] = i++; |
| 953 |
|
if (n && !isflt(argv[i+1])) { |
| 954 |
|
mop[nmats].preop.csym = argv[++i]; |
| 955 |
|
mop[nmats].preop.clen = 0; |
| 956 |
+ |
mcat_last = 0; |
| 957 |
|
break; |
| 958 |
|
} |
| 959 |
|
if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP; |
| 1043 |
|
fprintf(stderr, "%s: unsupported output format\n", argv[0]); |
| 1044 |
|
return(1); |
| 1045 |
|
} |
| 1046 |
< |
/* process & write rows */ |
| 1047 |
< |
return(combine_input(&mop[nmats], stdout) ? 0 : 1); |
| 1046 |
> |
doptimize(1); /* optimize definitions */ |
| 1047 |
> |
i = spawned_children(nproc); /* create multiple processes if requested */ |
| 1048 |
> |
if (i > 0) /* running in parent process? */ |
| 1049 |
> |
return(parent_loop() ? 0 : 1); |
| 1050 |
> |
if (i < 0) /* running in output process? */ |
| 1051 |
> |
return(output_loop() ? 0 : 1); |
| 1052 |
> |
/* else we are a worker process */ |
| 1053 |
> |
return(combine_input() ? 0 : 1); |
| 1054 |
|
stdin_error: |
| 1055 |
|
fprintf(stderr, "%s: %s used for more than one input\n", |
| 1056 |
|
argv[0], stdin_name); |
| 1057 |
|
return(1); |
| 1058 |
|
userr: |
| 1059 |
|
fprintf(stderr, |
| 1060 |
< |
"Usage: %s [-h][-f{adfc}][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n", |
| 1060 |
> |
"Usage: %s [-h][-f{adfc}][-n nproc][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n", |
| 1061 |
|
argv[0]); |
| 1062 |
|
return(1); |
| 1063 |
|
} |