ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rtcontrib.c
(Generate patch)

Comparing ray/src/util/rtcontrib.c (file contents):
Revision 1.45 by greg, Sat Nov 17 01:13:51 2007 UTC vs.
Revision 1.68 by greg, Thu Apr 12 01:56:07 2012 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   * Gather rtrace output to compute contributions from particular sources
6   */
7  
8 + /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
9 +        Need to refactor code by forking a subprocess for each
10 +        rtrace call to take output and accumulate it into bins
11 +        for the parent process.  This will avoid our current
12 +        bottleneck around processing output queues.  We'll sum into
13 +        bins and avoid the associated buffer growth, which can be crazy
14 +        now (gigabytes/subprocess).  Each child process will return
15 +        a ray number and a fully computed and ready-to-output
16 +        record of modifiers and their bin totals.  These will
17 +        be queued and sorted by the parent for ordered output or
18 +        accumulated for all rays if -c 0 is in play.
19 + XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
20 +
21   #include  "standard.h"
22   #include  <ctype.h>
23   #include  <signal.h>
# Line 16 | Line 29 | static const char RCSid[] = "$Id$";
29   #include  "lookup.h"
30   #include  "calcomp.h"
31  
32 + #ifdef _WIN32
33 + typedef int     ssize_t;
34 + #endif
35 +
36   #ifndef MAXMODLIST
37   #define MAXMODLIST      1024            /* maximum modifiers we'll track */
38   #endif
39  
40 < int     treebufsiz = BUFSIZ;            /* current tree buffer size */
40 > #ifndef RNUMBER
41 > #define RNUMBER         unsigned long   /* ray counter (>= sizeof pointer) */
42 > #endif
43  
44 + ssize_t treebufsiz = BUFSIZ;            /* current tree buffer size */
45 +
46   typedef double  DCOLOR[3];              /* double-precision color */
47  
48   /*
# Line 91 | Line 112 | void printresolu(FILE *fout, int xr, int yr);
112   struct rtproc {
113          struct rtproc   *next;          /* next in list of processes */
114          SUBPROC         pd;             /* rtrace pipe descriptors */
115 <        unsigned long   raynum;         /* ray number for this tree */
116 <        int             bsiz;           /* ray tree buffer length */
115 >        RNUMBER         raynum;         /* ray number for this tree */
116 >        size_t          bsiz;           /* ray tree buffer length */
117          char            *buf;           /* ray tree buffer */
118 <        int             nbr;            /* number of bytes from rtrace */
118 >        size_t          nbr;            /* number of bytes from rtrace */
119   };                              /* rtrace process buffer */
120  
121                                          /* rtrace command and defaults */
122   char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
123 <                                "-dj", ".5", "-dr", "3",
123 >                                "-dj", ".9", "-dr", "3",
124                                  "-ab", "1", "-ad", "350", };
125  
126   int  rtargc = 9;
# Line 107 | Line 128 | int  rtargc = 9;
128   char            *myrtopts[] = { "-h-", "-x", "1", "-y", "0",
129                                  "-dt", "0", "-as", "0", "-aa", "0", NULL };
130  
131 < #define RTCOEFF         "-o~~TmWdp"     /* compute coefficients only */
132 < #define RTCONTRIB       "-o~~TmVdp"     /* compute ray contributions */
131 > #define RTCOEFF         "-o~~~TmWdp"    /* compute coefficients only */
132 > #define RTCONTRIB       "-o~~~TmVdp"    /* compute ray contributions */
133  
134   struct rtproc   rt0;                    /* head of rtrace process list */
135  
# Line 131 | Line 152 | int            inpfmt = 'a';           /* input format */
152   int             outfmt = 'a';           /* output format */
153  
154   int             header = 1;             /* output header? */
134 int             accumulate = 0;         /* accumulate ray values? */
155   int             force_open = 0;         /* truncate existing output? */
156 + int             recover = 0;            /* recover previous output? */
157 + int             accumulate = 1;         /* input rays per output record */
158   int             xres = 0;               /* horiz. output resolution */
159   int             yres = 0;               /* vert. output resolution */
160  
161 < unsigned long   raysleft;               /* number of rays left to trace */
161 > int             account;                /* current accumulation count */
162 > RNUMBER         raysleft;               /* number of rays left to trace */
163   long            waitflush;              /* how long until next flush */
164  
165 < unsigned long   lastray = 0;            /* last ray number sent */
166 < unsigned long   lastdone = 0;           /* last ray processed */
165 > RNUMBER         lastray = 0;            /* last ray number sent */
166 > RNUMBER         lastdone = 0;           /* last ray processed */
167  
168   int             using_stdout = 0;       /* are we using stdout? */
169  
# Line 165 | Line 188 | void process_queue(void);
188  
189   void put_contrib(const DCOLOR cnt, FILE *fout);
190   void add_contrib(const char *modn);
191 < void done_contrib(void);
191 > void done_contrib(int navg);
192  
193 + #ifdef getc_unlocked                    /* avoid nasty overheads */
194 + #undef getc
195 + #define getc    getc_unlocked
196 + #undef putc
197 + #define putc    putc_unlocked
198 + #undef ferror
199 + #define ferror  ferror_unlocked
200 + static int
201 + fread_unl(void *ptr, int size, int nitems, FILE *fp)
202 + {
203 +        char    *p = (char *)ptr;
204 +        int     len = size*nitems;
205 +        while (len-- > 0) {
206 +                int     c = getc_unlocked(fp);
207 +                if (c == EOF)
208 +                        return((p - (char *)ptr)/size);
209 +                *p++ = c;
210 +        }
211 +        return(nitems);
212 + }
213 + #undef fread
214 + #define fread   fread_unl
215 + static int
216 + fwrite_unl(const void *ptr, int size, int nitems, FILE *fp)
217 + {
218 +        const char      *p = (const char *)ptr;
219 +        int             len = size*nitems;
220 +        while (len-- > 0)
221 +                putc_unlocked(*p++, fp);
222 +        if (ferror_unlocked(fp))
223 +                return(0);
224 +        return(nitems);
225 + }
226 + #undef fwrite
227 + #define fwrite  fwrite_unl
228 + #endif
229 +
230   /* return number of open rtrace processes */
231   static int
232   nrtprocs(void)
# Line 220 | Line 280 | main(int argc, char *argv[])
280   {
281          int     contrib = 0;
282          int     nprocs = 1;
223        int     recover = 0;
283          char    *curout = NULL;
284          char    *binval = NULL;
285          int     bincnt = 0;
# Line 245 | Line 304 | main(int argc, char *argv[])
304                  while ((j = expandarg(&argc, &argv, i)) > 0)
305                          ;
306                  if (j < 0) {
307 <                        fprintf(stderr, "%s: cannot expand '%s'",
307 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
308                                          argv[0], argv[i]);
309                          exit(1);
310                  }
# Line 274 | Line 333 | main(int argc, char *argv[])
333                                          continue;
334                                  }
335                                  break;
336 <                        case 'c':               /* accumulate ray values */
337 <                                switch (argv[i][2]) {
338 <                                case '\0':
339 <                                        accumulate = !accumulate;
281 <                                        continue;
282 <                                case '+': case '1':
283 <                                case 'T': case 't':
284 <                                case 'Y': case 'y':
285 <                                        accumulate = 1;
286 <                                        continue;
287 <                                case '-': case '0':
288 <                                case 'F': case 'f':
289 <                                case 'N': case 'n':
290 <                                        accumulate = 0;
291 <                                        continue;
292 <                                }
293 <                                break;
336 >                        case 'c':               /* input rays per output */
337 >                                if (argv[i][2] || i >= argc-2) break;
338 >                                accumulate = atoi(argv[++i]);
339 >                                continue;
340                          case 'r':               /* recover output */
341                                  if (argv[i][2]) break;
342 <                                recover++;
342 >                                recover = 1;
343                                  continue;
344                          case 'h':               /* output header? */
345                                  switch (argv[i][2]) {
# Line 328 | Line 374 | main(int argc, char *argv[])
374                                          continue;
375                                  }
376                                  if (argv[i][2] == 'o') {
377 <                                        force_open++;
377 >                                        force_open = 1;
378                                          continue;
379                                  }
380                                  setformat(argv[i]+2);
# Line 352 | Line 398 | main(int argc, char *argv[])
398                          case 'b':               /* bin expression/count */
399                                  if (i >= argc-2) break;
400                                  if (argv[i][2] == 'n') {
401 <                                        bincnt = atoi(argv[++i]);
401 >                                        bincnt = (int)(eval(argv[++i]) + .5);
402                                          continue;
403                                  }
404                                  if (argv[i][2]) break;
# Line 380 | Line 426 | main(int argc, char *argv[])
426                          }
427                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
428          }
429 <        if (accumulate)         /* no output flushing for single record */
429 >        if (accumulate <= 0)    /* no output flushing for single record */
430                  xres = yres = 0;
431                                  /* set global argument list */
432          gargc = argc; gargv = argv;
# Line 390 | Line 436 | main(int argc, char *argv[])
436          rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
437                                  /* just asking for defaults? */
438          if (!strcmp(argv[i], "-defaults")) {
439 <                char    sxres[16], syres[16];
439 >                char    nps[8], sxres[16], syres[16];
440                  char    *rtpath;
441 <                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
441 >                printf("-c %-5d\t\t\t# accumulated rays per record\n",
442 >                                accumulate);
443                  printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
444                                  contrib ? "contributions" : "coefficients");
398                printf("-c%c\t\t\t\t# %s\n", accumulate ? '+' : '-',
399                                accumulate ? "accumulate ray values" :
400                                        "one output record per ray");
445                  fflush(stdout);                 /* report OUR options */
446 +                rtargv[rtargc++] = "-n";
447 +                sprintf(nps, "%d", nprocs);
448 +                rtargv[rtargc++] = nps;
449                  rtargv[rtargc++] = header ? "-h+" : "-h-";
450                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
451                  rtargv[rtargc++] = fmt;
# Line 437 | Line 484 | main(int argc, char *argv[])
484                  error(USER, "missing octree argument");
485          rtargv[rtargc++] = octree = argv[i];
486          rtargv[rtargc] = NULL;
487 <                                /* start rtrace */
487 >                                /* start rtrace & recover if requested */
488          init(nprocs);
489 <        if (recover)            /* perform recovery if requested */
490 <                if ((force_open = accumulate))
491 <                        reload_output();
445 <                else
446 <                        recover_output(stdin);
447 <        trace_contribs(stdin);  /* compute contributions */
489 >                                /* compute contributions */
490 >        trace_contribs(stdin);
491 >                                /* clean up */
492          quit(0);
493   }
494  
# Line 552 | Line 596 | init(int np)
596          rtp->next = NULL;               /* terminate list */
597          if (yres > 0) {
598                  if (xres > 0)
599 <                        raysleft = (unsigned long)xres*yres;
599 >                        raysleft = (RNUMBER)xres*yres;
600                  else
601                          raysleft = yres;
602          } else
603                  raysleft = 0;
604 <        waitflush = xres;
604 >        if ((account = accumulate) > 0)
605 >                raysleft *= accumulate;
606 >        waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
607 >        if (!recover)
608 >                return;
609 >                                        /* recover previous values */
610 >        if (accumulate <= 0)
611 >                reload_output();
612 >        else
613 >                recover_output(stdin);
614   }
615  
616   /* grow modifier to accommodate more bins */
# Line 587 | Line 640 | addmodifier(char *modn, char *outf, char *binv, int bi
640                  error(USER, errmsg);
641          }
642          if (nmods >= MAXMODLIST)
643 <                error(USER, "too many modifiers");
643 >                error(INTERNAL, "too many modifiers");
644          modname[nmods++] = modn;        /* XXX assumes static string */
645          lep->key = modn;                /* XXX assumes static string */
646          mp = (MODCONT *)malloc(sizeof(MODCONT));
# Line 595 | Line 648 | addmodifier(char *modn, char *outf, char *binv, int bi
648                  error(SYSTEM, "out of memory in addmodifier");
649          mp->outspec = outf;             /* XXX assumes static string */
650          mp->modname = modn;             /* XXX assumes static string */
651 <        if (binv != NULL)
652 <                mp->binv = eparse(binv);
653 <        else
654 <                mp->binv = eparse("0");
655 <        mp->nbins = 1;
651 >        if (binv == NULL)
652 >                binv = "0";             /* use single bin if unspecified */
653 >        mp->binv = eparse(binv);
654 >        if (mp->binv->type == NUM) {    /* check value if constant */
655 >                bincnt = (int)(evalue(mp->binv) + 1.5);
656 >                if (bincnt != 1) {
657 >                        sprintf(errmsg, "illegal non-zero constant for bin (%s)",
658 >                                        binv);
659 >                        error(USER, errmsg);
660 >                }
661 >        }
662 >        mp->nbins = 1;                  /* initialize results holder */
663          setcolor(mp->cbin[0], 0., 0., 0.);
664 <        if (mp->binv->type == NUM)      /* assume one bin if constant */
605 <                bincnt = 1;
606 <        else if (bincnt > 1)
664 >        if (bincnt > 1)
665                  mp = growmodifier(mp, bincnt);
666          lep->data = (char *)mp;
667                                          /* allocate output streams */
# Line 666 | Line 724 | ofname(char *oname, const char *ospec, const char *mna
724                                  mnp = cp;
725                                  break;
726                          case 'd':
727 +                        case 'i':
728 +                        case 'o':
729 +                        case 'x':
730 +                        case 'X':
731                                  if (bnp != NULL)
732                                          return -1;
733                                  bnp = cp;
# Line 698 | Line 760 | void
760   printheader(FILE *fout, const char *info)
761   {
762          extern char     VersionID[];
763 <        FILE            *fin = fopen(octree, "r");
764 <        
765 <        if (fin == NULL)
766 <                quit(1);
767 <        checkheader(fin, "ignore", fout);       /* copy octree header */
768 <        fclose(fin);
763 >                                                /* copy octree header */
764 >        if (octree[0] == '!') {
765 >                newheader("RADIANCE", fout);
766 >                fputs(octree+1, fout);
767 >                if (octree[strlen(octree)-1] != '\n')
768 >                        fputc('\n', fout);
769 >        } else {
770 >                FILE    *fin = fopen(octree, "r");
771 >                if (fin == NULL)
772 >                        quit(1);
773 >                checkheader(fin, "ignore", fout);
774 >                fclose(fin);
775 >        }
776          printargs(gargc-1, gargv, fout);        /* add our command */
777          fprintf(fout, "SOFTWARE= %s\n", VersionID);
778          fputnow(fout);
# Line 732 | Line 801 | printresolu(FILE *fout, int xr, int yr)
801   {
802          if ((xr > 0) & (yr > 0))        /* resolution string */
803                  fprtresolu(xr, yr, fout);
735        if (xres > 0)                   /* global flush flag */
736                fflush(fout);
804   }
805  
806   /* Get output stream pointer (open and write header if new and noopen==0) */
# Line 754 | Line 821 | getostream(const char *ospec, const char *mname, int b
821                          if (header)
822                                  printheader(stdout, NULL);
823                          printresolu(stdout, xres, yres);
824 +                        if (waitflush > 0)
825 +                                fflush(stdout);
826                          stdos.xr = xres; stdos.yr = yres;
827                          using_stdout = 1;
828                  }
# Line 779 | Line 848 | getostream(const char *ospec, const char *mname, int b
848                  sop->ofp = NULL;                /* open iff noopen==0 */
849                  sop->xr = xres; sop->yr = yres;
850                  lep->data = (char *)sop;
851 <                if (!force_open && access(oname, F_OK) == 0) {
851 >                if (!sop->outpipe & !force_open & !recover &&
852 >                                access(oname, F_OK) == 0) {
853                          errno = EEXIST;         /* file exists */
854                          goto openerr;
855                  }
# Line 794 | Line 864 | getostream(const char *ospec, const char *mname, int b
864                          goto openerr;
865                  if (outfmt != 'a')
866                          SET_FILE_BINARY(sop->ofp);
867 + #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
868 +                flockfile(sop->ofp);
869 + #endif
870                  if (header) {
871                          char    info[512];
872                          char    *cp = info;
# Line 808 | Line 881 | getostream(const char *ospec, const char *mname, int b
881                          *cp = '\0';
882                          printheader(sop->ofp, info);
883                  }
884 +                if (accumulate > 0) {           /* global resolution */
885 +                        sop->xr = xres; sop->yr = yres;
886 +                }
887                  printresolu(sop->ofp, sop->xr, sop->yr);
888                                                  /* play catch-up */
889 <                for (i = accumulate ? 0 : lastdone; i--; ) {
889 >                for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) {
890                          int     j = sop->reclen;
891                          if (j <= 0) j = 1;
892                          while (j--)
# Line 818 | Line 894 | getostream(const char *ospec, const char *mname, int b
894                          if (outfmt == 'a')
895                                  putc('\n', sop->ofp);
896                  }
897 <                if (xres > 0)
897 >                if (waitflush > 0)
898                          fflush(sop->ofp);
899          }
900          sop->reclen += noopen;                  /* add to length if noopen */
# Line 856 | Line 932 | getinp(char *buf, FILE *fp)
932                          return 0;       /* dummy ray */
933                  return strlen(buf);
934          case 'f':
935 <                if (fread(buf, sizeof(float), 6, fp) < 6)
935 >                if (fread(buf, sizeof(float), 6, fp) != 6)
936                          return -1;
937                  fvp = (float *)buf + 3;
938                  if (DOT(fvp,fvp) <= FTINY*FTINY)
939                          return 0;       /* dummy ray */
940                  return sizeof(float)*6;
941          case 'd':
942 <                if (fread(buf, sizeof(double), 6, fp) < 6)
942 >                if (fread(buf, sizeof(double), 6, fp) != 6)
943                          return -1;
944                  dvp = (double *)buf + 3;
945                  if (DOT(dvp,dvp) <= FTINY*FTINY)
# Line 935 | Line 1011 | put_contrib(const DCOLOR cnt, FILE *fout)
1011                  fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
1012                  break;
1013          case 'f':
1014 <                fv[0] = cnt[0];
939 <                fv[1] = cnt[1];
940 <                fv[2] = cnt[2];
1014 >                copycolor(fv, cnt);
1015                  fwrite(fv, sizeof(float), 3, fout);
1016                  break;
1017          case 'd':
# Line 954 | Line 1028 | put_contrib(const DCOLOR cnt, FILE *fout)
1028  
1029   /* output ray tallies and clear for next accumulation */
1030   void
1031 < done_contrib(void)
1031 > done_contrib(int navg)
1032   {
1033 +        double          sf = 1.;
1034          int             i, j;
1035          MODCONT         *mp;
1036          STREAMOUT       *sop;
1037 +                                                /* set average scaling */
1038 +        if (navg > 1)
1039 +                sf = 1. / (double)navg;
1040                                                  /* output modifiers in order */
1041          for (i = 0; i < nmods; i++) {
1042                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
1043 +                if (navg > 1)                   /* average scaling */
1044 +                        for (j = mp->nbins; j--; )
1045 +                                scalecolor(mp->cbin[j], sf);
1046                  sop = getostream(mp->outspec, mp->modname, 0,0);
1047                  put_contrib(mp->cbin[0], sop->ofp);
1048                  if (mp->nbins > 3 &&            /* minor optimization */
# Line 980 | Line 1061 | done_contrib(void)
1061          if (using_stdout & (outfmt == 'a'))
1062                  putc('\n', stdout);
1063          if (!waitflush) {
1064 <                waitflush = xres;
1064 >                waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
1065                  if (using_stdout)
1066                          fflush(stdout);
1067          }
# Line 1045 | Line 1126 | process_queue(void)
1126                          cp += sizeof(float)*9; n -= sizeof(float)*9;
1127                          add_contrib(modname);
1128                  }
1129 <                if (!accumulate)
1130 <                        done_contrib(); /* sum up contributions & output */
1129 >                                        /* time to produce record? */
1130 >                if (account > 0 && !--account)
1131 >                        done_contrib(account = accumulate);
1132                  lastdone = rtp->raynum;
1133                  if (rtp->buf != NULL)   /* free up buffer space */
1134                          free(rtp->buf);
# Line 1061 | Line 1143 | wait_rproc(void)
1143   {
1144          struct rtproc   *rtfree = NULL;
1145          fd_set          readset, errset;
1146 <        int             nr;
1146 >        ssize_t         nr;
1147          struct rtproc   *rt;
1148          int             n;
1149          
# Line 1092 | Line 1174 | wait_rproc(void)
1174                                  continue;
1175                          if (rt->buf == NULL) {
1176                                  rt->bsiz = treebufsiz;
1177 <                                rt->buf = (char *)malloc(treebufsiz);
1177 >                                rt->buf = (char *)malloc(rt->bsiz);
1178                          } else if (rt->nbr + BUFSIZ > rt->bsiz) {
1179                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
1180                                          rt->bsiz = treebufsiz;
1181 <                                else
1182 <                                        treebufsiz = rt->bsiz += BUFSIZ;
1181 >                                else if ((treebufsiz = rt->bsiz += BUFSIZ) < 0)
1182 >                                        error(INTERNAL,
1183 >                                            "ray buffer does not fit memory");
1184                                  rt->buf = (char *)realloc(rt->buf, rt->bsiz);
1185                          }
1186                          if (rt->buf == NULL)
1187                                  error(SYSTEM, "out of memory in wait_rproc");
1188 <                        nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr);
1189 <                        if (nr <= 0)
1188 >                        nr = rt->bsiz - rt->nbr;
1189 >                        if (nr & ~0x7fffffff)   /* avoid 32-bit OS issues */
1190 >                                nr = 0x7fffffff;
1191 >                        nr = read(rt->pd.r, rt->buf+rt->nbr, nr);
1192 >                        if (nr < 0)
1193 >                                error(SYSTEM, "read error from rtrace");
1194 >                        if (!nr)
1195                                  error(USER, "rtrace process died");
1196                          rt->nbr += nr;          /* advance & check */
1197 <                        if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
1198 <                                                        "~\t~\t", 4)) {
1199 <                                rt->nbr -= 4;   /* elide terminator */
1197 >                        if (rt->nbr >= 6 && !memcmp(rt->buf+rt->nbr-6,
1198 >                                                        "~\t~\t~\t", 6)) {
1199 >                                rt->nbr -= 6;   /* elide terminator */
1200                                  queue_raytree(rt);
1201                                  rtfree = rt;    /* ready for next ray */
1202                          }
# Line 1133 | Line 1221 | get_rproc(void)
1221   void
1222   trace_contribs(FILE *fin)
1223   {
1224 +        static int      ignore_warning_given = 0;
1225          char            inpbuf[128];
1226          int             iblen;
1227          struct rtproc   *rtp;
1228                                                  /* loop over input */
1229          while ((iblen = getinp(inpbuf, fin)) >= 0) {
1230 <                if (!iblen ||                   /* need reset? */
1230 >                if (!iblen && accumulate != 1) {
1231 >                        if (!ignore_warning_given++)
1232 >                                error(WARNING,
1233 >                                "dummy ray(s) ignored during accumulation\n");
1234 >                        continue;
1235 >                }
1236 >                if (!iblen ||                   /* need flush/reset? */
1237                                  queue_length() > 10*nrtprocs() ||
1238                                  lastray+1 < lastray) {
1239                          while (wait_rproc() != NULL)
1240                                  process_queue();
1241 <                        if (lastray+1 < lastray)
1147 <                                lastdone = lastray = 0;
1241 >                        lastdone = lastray = 0;
1242                  }
1243                  rtp = get_rproc();              /* get avail. rtrace process */
1244                  rtp->raynum = ++lastray;        /* assign ray */
1245                  if (iblen) {                    /* trace ray if valid */
1246                          writebuf(rtp->pd.w, inpbuf, iblen);
1247                  } else {                        /* else bypass dummy ray */
1248 <                        queue_raytree(rtp);     /* empty tree */
1249 <                        if ((yres <= 0) | (waitflush > 1))
1250 <                                waitflush = 1;  /* flush after this */
1248 >                        queue_raytree(rtp);     /* queue empty ray/record */
1249 >                        if ((yres <= 0) | (xres <= 0))
1250 >                                waitflush = 1;  /* flush right after */
1251                  }
1252                  process_queue();                /* catch up with results */
1253                  if (raysleft && !--raysleft)
# Line 1161 | Line 1255 | trace_contribs(FILE *fin)
1255          }
1256          while (wait_rproc() != NULL)            /* process outstanding rays */
1257                  process_queue();
1258 <        if (accumulate)
1259 <                done_contrib();                 /* output tallies */
1258 >        if (accumulate <= 0)
1259 >                done_contrib(0);                /* output tallies */
1260 >        else if (account < accumulate) {
1261 >                error(WARNING, "partial accumulation in final record");
1262 >                done_contrib(accumulate - account);
1263 >        }
1264          if (raysleft)
1265                  error(USER, "unexpected EOF on input");
1266          lu_done(&ofiletab);                     /* close output files */
1267   }
1268  
1269 < /* seek on the given output file */
1269 > /* get ray contribution from previous file */
1270   static int
1271 < myseeko(const LUENT *e, void *p)
1271 > get_contrib(DCOLOR cnt, FILE *finp)
1272   {
1273 +        COLOR   fv;
1274 +        COLR    cv;
1275 +
1276 +        switch (outfmt) {
1277 +        case 'a':
1278 +                return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
1279 +        case 'f':
1280 +                if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
1281 +                        return(0);
1282 +                copycolor(cnt, fv);
1283 +                return(1);
1284 +        case 'd':
1285 +                return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
1286 +        case 'c':
1287 +                if (fread(cv, sizeof(cv), 1, finp) != 1)
1288 +                        return(0);
1289 +                colr_color(fv, cv);
1290 +                copycolor(cnt, fv);
1291 +                return(1);
1292 +        default:
1293 +                error(INTERNAL, "botched output format");
1294 +        }
1295 +        return(0);      /* pro forma return */
1296 + }
1297 +
1298 + /* close output file opened for input */
1299 + static int
1300 + myclose(const LUENT *e, void *p)
1301 + {
1302          STREAMOUT       *sop = (STREAMOUT *)e->data;
1176        off_t           nbytes = *(off_t *)p;
1303          
1304 <        if (sop->reclen > 1)
1305 <                nbytes = nbytes * sop->reclen;
1306 <        if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1307 <                sprintf(errmsg, "seek error on file '%s'", e->key);
1308 <                error(SYSTEM, errmsg);
1183 <        }
1184 <        return 0;
1304 >        if (sop->ofp == NULL)
1305 >                return(0);
1306 >        fclose(sop->ofp);
1307 >        sop->ofp = NULL;
1308 >        return(0);
1309   }
1310  
1311   /* load previously accumulated values */
# Line 1195 | Line 1319 | reload_output(void)
1319          char            *fmode = "rb";
1320          char            *outvfmt;
1321          LUENT           *ment, *oent;
1322 +        int             xr, yr;
1323          STREAMOUT       sout;
1324 +        DCOLOR          rgbv;
1325  
1200        error(INTERNAL, "-r option not yet implemented with -c");
1201
1326          switch (outfmt) {
1327          case 'a':
1328                  outvfmt = "ascii";
# Line 1217 | Line 1341 | reload_output(void)
1341                  error(INTERNAL, "botched output format");
1342                  return;
1343          }
1344 <                                                /* check modifier outputs */
1344 >                                                /* reload modifier values */
1345          for (i = 0; i < nmods; i++) {
1346                  ment = lu_find(&modconttab,modname[i]);
1347                  mp = (MODCONT *)ment->data;
# Line 1225 | Line 1349 | reload_output(void)
1349                          error(USER, "cannot reload from stdout");
1350                  if (mp->outspec[0] == '!')
1351                          error(USER, "cannot reload from command");
1352 <                for (j = 0; ; j++) {            /* check each bin's file */
1352 >                for (j = 0; ; j++) {            /* load each modifier bin */
1353                          ofl = ofname(oname, mp->outspec, mp->modname, j);
1354                          if (ofl < 0)
1355                                  error(USER, "bad output file specification");
# Line 1234 | Line 1358 | reload_output(void)
1358                                  sout = *(STREAMOUT *)oent->data;
1359                          } else {
1360                                  sout.reclen = 0;
1361 +                                sout.outpipe = 0;
1362 +                                sout.xr = xres; sout.yr = yres;
1363                                  sout.ofp = NULL;
1364                          }
1365 <                        if (sout.ofp != NULL) { /* already open? */
1366 <                                if (ofl & OF_BIN)
1367 <                                        continue;
1368 <                                break;
1365 >                        if (sout.ofp == NULL) { /* open output as input */
1366 >                                sout.ofp = fopen(oname, fmode);
1367 >                                if (sout.ofp == NULL) {
1368 >                                        if (j)
1369 >                                                break;  /* assume end of modifier */
1370 >                                        sprintf(errmsg, "missing reload file '%s'",
1371 >                                                        oname);
1372 >                                        error(WARNING, errmsg);
1373 >                                        break;
1374 >                                }
1375 > #ifdef getc_unlocked                                    /* avoid lock/unlock overhead */
1376 >                                flockfile(sout.ofp);
1377 > #endif
1378 >                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1379 >                                        sprintf(errmsg, "format mismatch for '%s'",
1380 >                                                        oname);
1381 >                                        error(USER, errmsg);
1382 >                                }
1383 >                                if ((sout.xr > 0) & (sout.yr > 0) &&
1384 >                                                (!fscnresolu(&xr, &yr, sout.ofp) ||
1385 >                                                        (xr != sout.xr) |
1386 >                                                        (yr != sout.yr))) {
1387 >                                        sprintf(errmsg, "resolution mismatch for '%s'",
1388 >                                                        oname);
1389 >                                        error(USER, errmsg);
1390 >                                }
1391                          }
1392 <                                                /* open output */
1393 <                        sout.ofp = fopen(oname, fmode);
1394 <                        if (sout.ofp == NULL) {
1395 <                                if (j)
1396 <                                        break;  /* assume end of modifier */
1397 <                                sprintf(errmsg, "missing reload file '%s'",
1398 <                                                oname);
1399 <                                error(WARNING, errmsg);
1392 >                                                        /* read in RGB value */
1393 >                        if (!get_contrib(rgbv, sout.ofp)) {
1394 >                                if (!j) {
1395 >                                        fclose(sout.ofp);
1396 >                                        break;          /* ignore empty file */
1397 >                                }
1398 >                                if (j < mp->nbins) {
1399 >                                        sprintf(errmsg, "missing data in '%s'",
1400 >                                                        oname);
1401 >                                        error(USER, errmsg);
1402 >                                }
1403                                  break;
1404                          }
1405 <                        if (oent->key == NULL)  /* new entry */
1405 >                        if (j >= mp->nbins)             /* grow modifier size */
1406 >                                ment->data = (char *)(mp = growmodifier(mp, j+1));
1407 >                        copycolor(mp->cbin[j], rgbv);
1408 >                        if (oent->key == NULL)          /* new file entry */
1409                                  oent->key = strcpy((char *)
1410                                                  malloc(strlen(oname)+1), oname);
1411                          if (oent->data == NULL)
1412                                  oent->data = (char *)malloc(sizeof(STREAMOUT));
1413                          *(STREAMOUT *)oent->data = sout;
1260                        if (!(ofl & OF_BIN))
1261                                break;          /* no bin separation */
1414                  }
1263                if (j > mp->nbins)              /* reallocate modifier bins */
1264                        ment->data = (char *)(mp = growmodifier(mp, j));
1415          }
1416 <        lu_done(&ofiletab);                     /* close all files */
1416 >        lu_doall(&ofiletab, myclose, NULL);     /* close all files */
1417   }
1418  
1419 + /* seek on the given output file */
1420 + static int
1421 + myseeko(const LUENT *e, void *p)
1422 + {
1423 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1424 +        off_t           nbytes = *(off_t *)p;
1425 +        
1426 +        if (sop->reclen > 1)
1427 +                nbytes = nbytes * sop->reclen;
1428 +        if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1429 +                sprintf(errmsg, "seek error on file '%s'", e->key);
1430 +                error(SYSTEM, errmsg);
1431 +        }
1432 +        return 0;
1433 + }
1434 +
1435   /* recover output if possible */
1436   void
1437   recover_output(FILE *fin)
# Line 1319 | Line 1485 | recover_output(FILE *fin)
1485                                  sout = *(STREAMOUT *)oent->data;
1486                          } else {
1487                                  sout.reclen = 0;
1488 +                                sout.outpipe = 0;
1489                                  sout.ofp = NULL;
1490                          }
1491                          if (sout.ofp != NULL) { /* already open? */
# Line 1359 | Line 1526 | recover_output(FILE *fin)
1526                                                  oname);
1527                                  error(USER, errmsg);
1528                          }
1529 <                        if ((xres > 0) & (yres > 0) &&
1529 >                        sout.xr = xres; sout.yr = yres;
1530 >                        if ((sout.xr > 0) & (sout.yr > 0) &&
1531                                          (!fscnresolu(&xr, &yr, sout.ofp) ||
1532 <                                                xr != xres ||
1533 <                                                yr != yres)) {
1532 >                                                (xr != sout.xr) |
1533 >                                                (yr != sout.yr))) {
1534                                  sprintf(errmsg, "resolution mismatch for '%s'",
1535                                                  oname);
1536                                  error(USER, errmsg);
# Line 1391 | Line 1559 | recover_output(FILE *fin)
1559                  error(WARNING, "no output files to recover");
1560                  return;
1561          }
1562 <        if (raysleft && lastout >= raysleft) {
1562 >        if (raysleft && lastout >= raysleft/accumulate) {
1563                  error(WARNING, "output appears to be complete");
1564                  /* XXX should read & discard input? */
1565                  quit(0);
# Line 1403 | Line 1571 | recover_output(FILE *fin)
1571          for (nvals = 0; nvals < lastout; nvals++)
1572                  if (getinp(oname, fin) < 0)
1573                          error(USER, "unexpected EOF on input");
1574 <        lastray = lastdone = (unsigned long)lastout;
1574 >        lastray = lastdone = (RNUMBER)lastout * accumulate;
1575          if (raysleft)
1576                  raysleft -= lastray;
1577   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines