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.40 by greg, Sun Feb 5 22:22:21 2006 UTC vs.
Revision 1.67 by greg, Tue Apr 10 05:16:50 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, computing
13 +        bins and the associated buffer growth, which can be crazy
14 +        (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 summed bins.  These will
17 +        be queued and sorted by the parent for ordered output.
18 + XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
19 +
20   #include  "standard.h"
21   #include  <ctype.h>
22   #include  <signal.h>
# Line 16 | Line 28 | static const char RCSid[] = "$Id$";
28   #include  "lookup.h"
29   #include  "calcomp.h"
30  
31 + #ifdef _WIN32
32 + typedef int     ssize_t;
33 + #endif
34 +
35   #ifndef MAXMODLIST
36   #define MAXMODLIST      1024            /* maximum modifiers we'll track */
37   #endif
38  
39 < int     treebufsiz = BUFSIZ;            /* current tree buffer size */
39 > ssize_t treebufsiz = BUFSIZ;            /* current tree buffer size */
40  
41   typedef double  DCOLOR[3];              /* double-precision color */
42  
43   /*
44   * The MODCONT structure is used to accumulate ray contributions
45   * for a particular modifier, which may be subdivided into bins
46 < * if binv is non-zero.  If outspec contains a %s in it, this will
46 > * if binv evaluates > 0.  If outspec contains a %s in it, this will
47   * be replaced with the modifier name.  If outspec contains a %d in it,
48   * this will be used to create one output file per bin, otherwise all bins
49   * will be written to the same file, in order.  If the global outfmt
# Line 46 | Line 62 | static void mcfree(void *p) { epfree((*(MODCONT *)p).b
62  
63   LUTAB   modconttab = LU_SINIT(NULL,mcfree);     /* modifier lookup table */
64  
49 #define CNT_UNKNOWN     0               /* unknown record length */
50 #define CNT_PIPE        (-1)            /* output to a pipe */
65   /*
66   * The STREAMOUT structure holds an open FILE pointer and a count of
67 < * the number of RGB triplets per record, or CNT_UNKNOWN (0) if
54 < * unknown or CNT_PIPE (-1) if writing to a command.
67 > * the number of RGB triplets per record, or 0 if unknown.
68   */
69   typedef struct {
70          FILE            *ofp;           /* output file pointer */
71 +        int             outpipe;        /* output is to a pipe */
72          int             reclen;         /* triplets/record */
73 +        int             xr, yr;         /* output resolution for picture */
74   } STREAMOUT;
75  
76   /* close output stream and free record */
# Line 64 | Line 79 | closestream(void *p)
79   {
80          STREAMOUT       *sop = (STREAMOUT *)p;
81          int             status;
82 <        if (sop->reclen == CNT_PIPE)
82 >        if (sop->outpipe)
83                  status = pclose(sop->ofp);
84          else
85                  status = fclose(sop->ofp);
# Line 81 | Line 96 | LUTAB  ofiletab = LU_SINIT(free,closestream);  /* output
96   STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int noopen);
97   int ofname(char *oname, const char *ospec, const char *mname, int bn);
98   void printheader(FILE *fout, const char *info);
99 < void printresolu(FILE *fout);
99 > void printresolu(FILE *fout, int xr, int yr);
100  
101   /*
102   * The rcont structure is used to manage i/o with a particular
# Line 93 | Line 108 | struct rtproc {
108          struct rtproc   *next;          /* next in list of processes */
109          SUBPROC         pd;             /* rtrace pipe descriptors */
110          unsigned long   raynum;         /* ray number for this tree */
111 <        int             bsiz;           /* ray tree buffer length */
111 >        size_t          bsiz;           /* ray tree buffer length */
112          char            *buf;           /* ray tree buffer */
113 <        int             nbr;            /* number of bytes from rtrace */
113 >        size_t          nbr;            /* number of bytes from rtrace */
114   };                              /* rtrace process buffer */
115  
116                                          /* rtrace command and defaults */
117   char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
118 <                                "-dj", ".5", "-dr", "3",
119 <                                "-ab", "1", "-ad", "128", };
118 >                                "-dj", ".9", "-dr", "3",
119 >                                "-ab", "1", "-ad", "350", };
120  
121   int  rtargc = 9;
122                                          /* overriding rtrace options */
123   char            *myrtopts[] = { "-h-", "-x", "1", "-y", "0",
124                                  "-dt", "0", "-as", "0", "-aa", "0", NULL };
125  
126 < #define RTCOEFF         "-o~~TmWdp"     /* compute coefficients only */
127 < #define RTCONTRIB       "-o~~TmVdp"     /* compute ray contributions */
126 > #define RTCOEFF         "-o~~~TmWdp"    /* compute coefficients only */
127 > #define RTCONTRIB       "-o~~~TmVdp"    /* compute ray contributions */
128  
129   struct rtproc   rt0;                    /* head of rtrace process list */
130  
131   struct rtproc   *rt_unproc = NULL;      /* unprocessed ray trees */
132  
133 < char    persistfn[] = "pfXXXXXX";       /* persist file name */
133 > #define PERSIST_NONE    0               /* no persist file */
134 > #define PERSIST_SINGLE  1               /* user set -P persist */
135 > #define PERSIST_PARALL  2               /* user set -PP persist */
136 > #define PERSIST_OURS    3               /* -PP persist belongs to us */
137 > int     persist_state = PERSIST_NONE;   /* persist file state */
138 > char    persistfn[] = "pfXXXXXX";       /* our persist file name, if set */
139  
140   int             gargc;                  /* global argc */
141   char            **gargv;                /* global argv */
# Line 128 | Line 148 | int            outfmt = 'a';           /* output format */
148  
149   int             header = 1;             /* output header? */
150   int             force_open = 0;         /* truncate existing output? */
151 + int             recover = 0;            /* recover previous output? */
152 + int             accumulate = 1;         /* input rays per output record */
153   int             xres = 0;               /* horiz. output resolution */
154   int             yres = 0;               /* vert. output resolution */
155  
156 + int             account;                /* current accumulation count */
157   unsigned long   raysleft;               /* number of rays left to trace */
158   long            waitflush;              /* how long until next flush */
159  
# Line 150 | Line 173 | void addmodfile(char *fname, char *outf, char *binv, i
173  
174   void init(int np);
175   int done_rprocs(struct rtproc *rtp);
176 + void reload_output(void);
177   void recover_output(FILE *fin);
178   void trace_contribs(FILE *fin);
179   struct rtproc *wait_rproc(void);
# Line 159 | Line 183 | void process_queue(void);
183  
184   void put_contrib(const DCOLOR cnt, FILE *fout);
185   void add_contrib(const char *modn);
186 < void done_contrib(void);
186 > void done_contrib(int navg);
187  
188 + #ifdef getc_unlocked                    /* avoid nasty overheads */
189 + #undef getc
190 + #define getc    getc_unlocked
191 + #undef putc
192 + #define putc    putc_unlocked
193 + #undef ferror
194 + #define ferror  ferror_unlocked
195 + static int
196 + fread_unl(void *ptr, int size, int nitems, FILE *fp)
197 + {
198 +        char    *p = (char *)ptr;
199 +        int     len = size*nitems;
200 +        while (len-- > 0) {
201 +                int     c = getc_unlocked(fp);
202 +                if (c == EOF)
203 +                        return((p - (char *)ptr)/size);
204 +                *p++ = c;
205 +        }
206 +        return(nitems);
207 + }
208 + #undef fread
209 + #define fread   fread_unl
210 + static int
211 + fwrite_unl(const void *ptr, int size, int nitems, FILE *fp)
212 + {
213 +        const char      *p = (const char *)ptr;
214 +        int             len = size*nitems;
215 +        while (len-- > 0)
216 +                putc_unlocked(*p++, fp);
217 +        if (ferror_unlocked(fp))
218 +                return(0);
219 +        return(nitems);
220 + }
221 + #undef fwrite
222 + #define fwrite  fwrite_unl
223 + #endif
224 +
225   /* return number of open rtrace processes */
226   static int
227   nrtprocs(void)
# Line 214 | Line 275 | main(int argc, char *argv[])
275   {
276          int     contrib = 0;
277          int     nprocs = 1;
217        int     recover = 0;
278          char    *curout = NULL;
279          char    *binval = NULL;
280          int     bincnt = 0;
# Line 239 | Line 299 | main(int argc, char *argv[])
299                  while ((j = expandarg(&argc, &argv, i)) > 0)
300                          ;
301                  if (j < 0) {
302 <                        fprintf(stderr, "%s: cannot expand '%s'",
302 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
303                                          argv[0], argv[i]);
304                          exit(1);
305                  }
# Line 268 | Line 328 | main(int argc, char *argv[])
328                                          continue;
329                                  }
330                                  break;
331 +                        case 'c':               /* input rays per output */
332 +                                if (argv[i][2] || i >= argc-2) break;
333 +                                accumulate = atoi(argv[++i]);
334 +                                continue;
335                          case 'r':               /* recover output */
336                                  if (argv[i][2]) break;
337 <                                recover++;
337 >                                recover = 1;
338                                  continue;
339                          case 'h':               /* output header? */
340                                  switch (argv[i][2]) {
# Line 305 | Line 369 | main(int argc, char *argv[])
369                                          continue;
370                                  }
371                                  if (argv[i][2] == 'o') {
372 <                                        force_open++;
372 >                                        force_open = 1;
373                                          continue;
374                                  }
375                                  setformat(argv[i]+2);
# Line 329 | Line 393 | main(int argc, char *argv[])
393                          case 'b':               /* bin expression/count */
394                                  if (i >= argc-2) break;
395                                  if (argv[i][2] == 'n') {
396 <                                        bincnt = atoi(argv[++i]);
396 >                                        bincnt = (int)(eval(argv[++i]) + .5);
397                                          continue;
398                                  }
399                                  if (argv[i][2]) break;
# Line 348 | Line 412 | main(int argc, char *argv[])
412                                  addmodfile(argv[i], curout, binval, bincnt);
413                                  continue;
414                          case 'P':               /* persist file */
415 <                                error(USER, "persist file is automatic");
416 <                                break;
415 >                                if (i >= argc-2) break;
416 >                                persist_state = (argv[i][2] == 'P') ?
417 >                                                PERSIST_PARALL : PERSIST_SINGLE;
418 >                                rtargv[rtargc++] = argv[i];
419 >                                rtargv[rtargc++] = argv[++i];
420 >                                continue;
421                          }
422                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
423          }
424 +        if (accumulate <= 0)    /* no output flushing for single record */
425 +                xres = yres = 0;
426                                  /* set global argument list */
427          gargc = argc; gargv = argv;
428                                  /* add "mandatory" rtrace settings */
# Line 361 | Line 431 | main(int argc, char *argv[])
431          rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
432                                  /* just asking for defaults? */
433          if (!strcmp(argv[i], "-defaults")) {
434 <                char    sxres[16], syres[16];
434 >                char    nps[8], sxres[16], syres[16];
435                  char    *rtpath;
436 <                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
436 >                printf("-c %-5d\t\t\t# accumulated rays per record\n",
437 >                                accumulate);
438                  printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
439                                  contrib ? "contributions" : "coefficients");
440                  fflush(stdout);                 /* report OUR options */
441 +                rtargv[rtargc++] = "-n";
442 +                sprintf(nps, "%d", nprocs);
443 +                rtargv[rtargc++] = nps;
444                  rtargv[rtargc++] = header ? "-h+" : "-h-";
445                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
446                  rtargv[rtargc++] = fmt;
# Line 389 | Line 463 | main(int argc, char *argv[])
463                  exit(1);
464          }
465          if (nprocs > 1) {       /* add persist file if parallel */
466 <                rtargv[rtargc++] = "-PP";
467 <                rtargv[rtargc++] = mktemp(persistfn);
466 >                if (persist_state == PERSIST_SINGLE)
467 >                        error(USER, "use -PP option for multiple processes");
468 >                if (persist_state == PERSIST_NONE) {
469 >                        rtargv[rtargc++] = "-PP";
470 >                        rtargv[rtargc++] = mktemp(persistfn);
471 >                        persist_state = PERSIST_OURS;
472 >                }
473          }
474                                  /* add format string */
475          sprintf(fmt, "-f%cf", inpfmt);
# Line 400 | Line 479 | main(int argc, char *argv[])
479                  error(USER, "missing octree argument");
480          rtargv[rtargc++] = octree = argv[i];
481          rtargv[rtargc] = NULL;
482 <                                /* start rtrace */
482 >                                /* start rtrace & recover if requested */
483          init(nprocs);
484 <        if (recover)            /* perform recovery if requested */
485 <                recover_output(stdin);
486 <        trace_contribs(stdin);  /* compute contributions */
484 >                                /* compute contributions */
485 >        trace_contribs(stdin);
486 >                                /* clean up */
487          quit(0);
488   }
489  
# Line 450 | Line 529 | quit(int status)
529   {
530          int     rtstat;
531  
532 <        if (rt0.next != NULL)           /* terminate persistent rtrace */
532 >        if (persist_state == PERSIST_OURS)  /* terminate waiting rtrace */
533                  killpersist();
534                                          /* clean up rtrace process(es) */
535          rtstat = done_rprocs(&rt0);
# Line 517 | Line 596 | init(int np)
596                          raysleft = yres;
597          } else
598                  raysleft = 0;
599 <        waitflush = xres;
599 >        if ((account = accumulate) > 0)
600 >                raysleft *= accumulate;
601 >        waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
602 >        if (!recover)
603 >                return;
604 >                                        /* recover previous values */
605 >        if (accumulate <= 0)
606 >                reload_output();
607 >        else
608 >                recover_output(stdin);
609   }
610  
611   /* grow modifier to accommodate more bins */
# Line 547 | Line 635 | addmodifier(char *modn, char *outf, char *binv, int bi
635                  error(USER, errmsg);
636          }
637          if (nmods >= MAXMODLIST)
638 <                error(USER, "too many modifiers");
638 >                error(INTERNAL, "too many modifiers");
639          modname[nmods++] = modn;        /* XXX assumes static string */
640          lep->key = modn;                /* XXX assumes static string */
641          mp = (MODCONT *)malloc(sizeof(MODCONT));
# Line 555 | Line 643 | addmodifier(char *modn, char *outf, char *binv, int bi
643                  error(SYSTEM, "out of memory in addmodifier");
644          mp->outspec = outf;             /* XXX assumes static string */
645          mp->modname = modn;             /* XXX assumes static string */
646 <        if (binv != NULL)
647 <                mp->binv = eparse(binv);
648 <        else
649 <                mp->binv = eparse("0");
650 <        mp->nbins = 1;
646 >        if (binv == NULL)
647 >                binv = "0";             /* use single bin if unspecified */
648 >        mp->binv = eparse(binv);
649 >        if (mp->binv->type == NUM) {    /* check value if constant */
650 >                bincnt = (int)(evalue(mp->binv) + 1.5);
651 >                if (bincnt != 1) {
652 >                        sprintf(errmsg, "illegal non-zero constant for bin (%s)",
653 >                                        binv);
654 >                        error(USER, errmsg);
655 >                }
656 >        }
657 >        mp->nbins = 1;                  /* initialize results holder */
658          setcolor(mp->cbin[0], 0., 0., 0.);
659 <        if (mp->binv->type == NUM)      /* assume one bin if constant */
565 <                bincnt = 1;
566 <        else if (bincnt > 1)
659 >        if (bincnt > 1)
660                  mp = growmodifier(mp, bincnt);
661          lep->data = (char *)mp;
662                                          /* allocate output streams */
663 <        for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i--; )
663 >        for (i = bincnt; i-- > 0; )
664                  getostream(mp->outspec, mp->modname, i, 1);
665          return mp;
666   }
# Line 626 | Line 719 | ofname(char *oname, const char *ospec, const char *mna
719                                  mnp = cp;
720                                  break;
721                          case 'd':
722 +                        case 'i':
723 +                        case 'o':
724 +                        case 'x':
725 +                        case 'X':
726                                  if (bnp != NULL)
727                                          return -1;
728                                  bnp = cp;
# Line 658 | Line 755 | void
755   printheader(FILE *fout, const char *info)
756   {
757          extern char     VersionID[];
758 <        FILE            *fin = fopen(octree, "r");
759 <        
760 <        if (fin == NULL)
761 <                quit(1);
762 <        checkheader(fin, "ignore", fout);       /* copy octree header */
763 <        fclose(fin);
758 >                                                /* copy octree header */
759 >        if (octree[0] == '!') {
760 >                newheader("RADIANCE", fout);
761 >                fputs(octree+1, fout);
762 >                if (octree[strlen(octree)-1] != '\n')
763 >                        fputc('\n', fout);
764 >        } else {
765 >                FILE    *fin = fopen(octree, "r");
766 >                if (fin == NULL)
767 >                        quit(1);
768 >                checkheader(fin, "ignore", fout);
769 >                fclose(fin);
770 >        }
771          printargs(gargc-1, gargv, fout);        /* add our command */
772          fprintf(fout, "SOFTWARE= %s\n", VersionID);
773          fputnow(fout);
# Line 688 | Line 792 | printheader(FILE *fout, const char *info)
792  
793   /* write resolution string to given output stream */
794   void
795 < printresolu(FILE *fout)
795 > printresolu(FILE *fout, int xr, int yr)
796   {
797 <        if (xres > 0) {
798 <                if (yres > 0)                   /* resolution string */
695 <                        fprtresolu(xres, yres, fout);
696 <                fflush(fout);
697 <        }
797 >        if ((xr > 0) & (yr > 0))        /* resolution string */
798 >                fprtresolu(xr, yr, fout);
799   }
800  
801   /* Get output stream pointer (open and write header if new and noopen==0) */
802   STREAMOUT *
803   getostream(const char *ospec, const char *mname, int bn, int noopen)
804   {
805 +        static const DCOLOR     nocontrib = BLKCOLOR;
806          static STREAMOUT        stdos;
807          int                     ofl;
808          char                    oname[1024];
# Line 709 | Line 811 | getostream(const char *ospec, const char *mname, int b
811          
812          if (ospec == NULL) {                    /* use stdout? */
813                  if (!noopen && !using_stdout) {
712                        stdos.reclen = 0;
814                          if (outfmt != 'a')
815                                  SET_FILE_BINARY(stdout);
816                          if (header)
817                                  printheader(stdout, NULL);
818 <                        printresolu(stdout);
818 >                        printresolu(stdout, xres, yres);
819 >                        if (waitflush > 0)
820 >                                fflush(stdout);
821 >                        stdos.xr = xres; stdos.yr = yres;
822                          using_stdout = 1;
823                  }
824                  stdos.ofp = stdout;
# Line 734 | Line 838 | getostream(const char *ospec, const char *mname, int b
838                  sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
839                  if (sop == NULL)
840                          error(SYSTEM, "out of memory in getostream");
841 <                sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN;
841 >                sop->outpipe = oname[0] == '!';
842 >                sop->reclen = 0;
843                  sop->ofp = NULL;                /* open iff noopen==0 */
844 +                sop->xr = xres; sop->yr = yres;
845                  lep->data = (char *)sop;
846 +                if (!sop->outpipe & !force_open & !recover &&
847 +                                access(oname, F_OK) == 0) {
848 +                        errno = EEXIST;         /* file exists */
849 +                        goto openerr;
850 +                }
851          }
852          if (!noopen && sop->ofp == NULL) {      /* open output stream */
853                  long            i;
854                  if (oname[0] == '!')            /* output to command */
855                          sop->ofp = popen(oname+1, "w");
856 <                else if (!force_open && access(oname, F_OK) == 0)
746 <                        errno = EEXIST;         /* file exists */
747 <                else                            /* else open it */
856 >                else                            /* else open file */
857                          sop->ofp = fopen(oname, "w");
858 <                if (sop->ofp == NULL) {
859 <                        sprintf(errmsg, "cannot open '%s' for writing", oname);
751 <                        error(SYSTEM, errmsg);
752 <                }
858 >                if (sop->ofp == NULL)
859 >                        goto openerr;
860                  if (outfmt != 'a')
861                          SET_FILE_BINARY(sop->ofp);
862 + #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
863 +                flockfile(sop->ofp);
864 + #endif
865                  if (header) {
866                          char    info[512];
867                          char    *cp = info;
# Line 766 | Line 876 | getostream(const char *ospec, const char *mname, int b
876                          *cp = '\0';
877                          printheader(sop->ofp, info);
878                  }
879 <                printresolu(sop->ofp);
879 >                if (accumulate > 0) {           /* global resolution */
880 >                        sop->xr = xres; sop->yr = yres;
881 >                }
882 >                printresolu(sop->ofp, sop->xr, sop->yr);
883                                                  /* play catch-up */
884 <                for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone;
885 <                                                                i--; ) {
886 <                        static const DCOLOR     nocontrib = BLKCOLOR;
887 <                        put_contrib(nocontrib, sop->ofp);
884 >                for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) {
885 >                        int     j = sop->reclen;
886 >                        if (j <= 0) j = 1;
887 >                        while (j--)
888 >                                put_contrib(nocontrib, sop->ofp);
889                          if (outfmt == 'a')
890                                  putc('\n', sop->ofp);
891                  }
892 <                if (xres > 0)
892 >                if (waitflush > 0)
893                          fflush(sop->ofp);
894          }
895 <        if (sop->reclen != CNT_PIPE)            /* add to length if noopen */
896 <                sop->reclen += noopen;
897 <        return sop;                             /* return open stream */
895 >        sop->reclen += noopen;                  /* add to length if noopen */
896 >        return sop;                             /* return output stream */
897 > openerr:
898 >        sprintf(errmsg, "cannot open '%s' for writing", oname);
899 >        error(SYSTEM, errmsg);
900 >        return NULL;    /* pro forma return */
901   }
902  
903   /* read input ray into buffer */
904   int
905   getinp(char *buf, FILE *fp)
906   {
907 <        double  dv[3];
908 <        float   fv[3];
907 >        double  dv[3], *dvp;
908 >        float   *fvp;
909          char    *cp;
910          int     i;
911  
# Line 810 | Line 927 | getinp(char *buf, FILE *fp)
927                          return 0;       /* dummy ray */
928                  return strlen(buf);
929          case 'f':
930 <                if (fread(buf, sizeof(float), 6, fp) < 6)
930 >                if (fread(buf, sizeof(float), 6, fp) != 6)
931                          return -1;
932 <                memcpy(fv, buf+3*sizeof(float), 3*sizeof(float));
933 <                if (DOT(fv,fv) <= FTINY*FTINY)
932 >                fvp = (float *)buf + 3;
933 >                if (DOT(fvp,fvp) <= FTINY*FTINY)
934                          return 0;       /* dummy ray */
935                  return sizeof(float)*6;
936          case 'd':
937 <                if (fread(buf, sizeof(double), 6, fp) < 6)
937 >                if (fread(buf, sizeof(double), 6, fp) != 6)
938                          return -1;
939 <                memcpy(dv, buf+3*sizeof(double), 3*sizeof(double));
940 <                if (DOT(dv,dv) <= FTINY*FTINY)
939 >                dvp = (double *)buf + 3;
940 >                if (DOT(dvp,dvp) <= FTINY*FTINY)
941                          return 0;       /* dummy ray */
942                  return sizeof(double)*6;
943          }
# Line 889 | Line 1006 | put_contrib(const DCOLOR cnt, FILE *fout)
1006                  fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
1007                  break;
1008          case 'f':
1009 <                fv[0] = cnt[0];
893 <                fv[1] = cnt[1];
894 <                fv[2] = cnt[2];
1009 >                copycolor(fv, cnt);
1010                  fwrite(fv, sizeof(float), 3, fout);
1011                  break;
1012          case 'd':
# Line 906 | Line 1021 | put_contrib(const DCOLOR cnt, FILE *fout)
1021          }
1022   }
1023  
1024 < /* output ray tallies and clear for next primary */
1024 > /* output ray tallies and clear for next accumulation */
1025   void
1026 < done_contrib(void)
1026 > done_contrib(int navg)
1027   {
1028 +        double          sf = 1.;
1029          int             i, j;
1030          MODCONT         *mp;
1031          STREAMOUT       *sop;
1032 +                                                /* set average scaling */
1033 +        if (navg > 1)
1034 +                sf = 1. / (double)navg;
1035                                                  /* output modifiers in order */
1036          for (i = 0; i < nmods; i++) {
1037                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
1038 +                if (navg > 1)                   /* average scaling */
1039 +                        for (j = mp->nbins; j--; )
1040 +                                scalecolor(mp->cbin[j], sf);
1041                  sop = getostream(mp->outspec, mp->modname, 0,0);
1042                  put_contrib(mp->cbin[0], sop->ofp);
1043                  if (mp->nbins > 3 &&            /* minor optimization */
# Line 926 | Line 1048 | done_contrib(void)
1048                          for (j = 1; j < mp->nbins; j++)
1049                                  put_contrib(mp->cbin[j],
1050                                      getostream(mp->outspec,mp->modname,j,0)->ofp);
1051 <                                                /* clear for next ray tree */
1051 >                                                /* clear for next time */
1052                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
1053          }
1054          --waitflush;                            /* terminate records */
# Line 934 | Line 1056 | done_contrib(void)
1056          if (using_stdout & (outfmt == 'a'))
1057                  putc('\n', stdout);
1058          if (!waitflush) {
1059 <                waitflush = xres;
1059 >                waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
1060                  if (using_stdout)
1061                          fflush(stdout);
1062          }
# Line 999 | Line 1121 | process_queue(void)
1121                          cp += sizeof(float)*9; n -= sizeof(float)*9;
1122                          add_contrib(modname);
1123                  }
1124 <                done_contrib();         /* sum up contributions & output */
1124 >                                        /* time to produce record? */
1125 >                if (account > 0 && !--account)
1126 >                        done_contrib(account = accumulate);
1127                  lastdone = rtp->raynum;
1128                  if (rtp->buf != NULL)   /* free up buffer space */
1129                          free(rtp->buf);
# Line 1014 | Line 1138 | wait_rproc(void)
1138   {
1139          struct rtproc   *rtfree = NULL;
1140          fd_set          readset, errset;
1141 <        int             nr;
1141 >        ssize_t         nr;
1142          struct rtproc   *rt;
1143          int             n;
1144          
# Line 1045 | Line 1169 | wait_rproc(void)
1169                                  continue;
1170                          if (rt->buf == NULL) {
1171                                  rt->bsiz = treebufsiz;
1172 <                                rt->buf = (char *)malloc(treebufsiz);
1172 >                                rt->buf = (char *)malloc(rt->bsiz);
1173                          } else if (rt->nbr + BUFSIZ > rt->bsiz) {
1174                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
1175                                          rt->bsiz = treebufsiz;
1176 <                                else
1177 <                                        treebufsiz = rt->bsiz += BUFSIZ;
1176 >                                else if ((treebufsiz = rt->bsiz += BUFSIZ) < 0)
1177 >                                        error(INTERNAL,
1178 >                                            "ray buffer does not fit memory");
1179                                  rt->buf = (char *)realloc(rt->buf, rt->bsiz);
1180                          }
1181                          if (rt->buf == NULL)
1182                                  error(SYSTEM, "out of memory in wait_rproc");
1183 <                        nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr);
1184 <                        if (nr <= 0)
1183 >                        nr = rt->bsiz - rt->nbr;
1184 >                        if (nr & ~0x7fffffff)   /* avoid 32-bit OS issues */
1185 >                                nr = 0x7fffffff;
1186 >                        nr = read(rt->pd.r, rt->buf+rt->nbr, nr);
1187 >                        if (nr < 0)
1188 >                                error(SYSTEM, "read error from rtrace");
1189 >                        if (!nr)
1190                                  error(USER, "rtrace process died");
1191                          rt->nbr += nr;          /* advance & check */
1192 <                        if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
1193 <                                                        "~\t~\t", 4)) {
1194 <                                rt->nbr -= 4;   /* elide terminator */
1192 >                        if (rt->nbr >= 6 && !memcmp(rt->buf+rt->nbr-6,
1193 >                                                        "~\t~\t~\t", 6)) {
1194 >                                rt->nbr -= 6;   /* elide terminator */
1195                                  queue_raytree(rt);
1196                                  rtfree = rt;    /* ready for next ray */
1197                          }
# Line 1086 | Line 1216 | get_rproc(void)
1216   void
1217   trace_contribs(FILE *fin)
1218   {
1219 +        static int      ignore_warning_given = 0;
1220          char            inpbuf[128];
1221          int             iblen;
1222          struct rtproc   *rtp;
1223                                                  /* loop over input */
1224          while ((iblen = getinp(inpbuf, fin)) >= 0) {
1225 <                if (!iblen ||                   /* need reset? */
1225 >                if (!iblen && accumulate != 1) {
1226 >                        if (!ignore_warning_given++)
1227 >                                error(WARNING,
1228 >                                "dummy ray(s) ignored during accumulation\n");
1229 >                        continue;
1230 >                }
1231 >                if (!iblen ||                   /* need flush/reset? */
1232                                  queue_length() > 10*nrtprocs() ||
1233                                  lastray+1 < lastray) {
1234                          while (wait_rproc() != NULL)
1235                                  process_queue();
1236 <                        if (lastray+1 < lastray)
1100 <                                lastdone = lastray = 0;
1236 >                        lastdone = lastray = 0;
1237                  }
1238                  rtp = get_rproc();              /* get avail. rtrace process */
1239                  rtp->raynum = ++lastray;        /* assign ray */
1240                  if (iblen) {                    /* trace ray if valid */
1241                          writebuf(rtp->pd.w, inpbuf, iblen);
1242                  } else {                        /* else bypass dummy ray */
1243 <                        queue_raytree(rtp);     /* empty tree */
1244 <                        if ((yres <= 0) | (waitflush > 1))
1245 <                                waitflush = 1;  /* flush after this */
1243 >                        queue_raytree(rtp);     /* queue empty ray/record */
1244 >                        if ((yres <= 0) | (xres <= 0))
1245 >                                waitflush = 1;  /* flush right after */
1246                  }
1247                  process_queue();                /* catch up with results */
1248                  if (raysleft && !--raysleft)
# Line 1114 | Line 1250 | trace_contribs(FILE *fin)
1250          }
1251          while (wait_rproc() != NULL)            /* process outstanding rays */
1252                  process_queue();
1253 +        if (accumulate <= 0)
1254 +                done_contrib(0);                /* output tallies */
1255 +        else if (account < accumulate) {
1256 +                error(WARNING, "partial accumulation in final record");
1257 +                done_contrib(accumulate - account);
1258 +        }
1259          if (raysleft)
1260                  error(USER, "unexpected EOF on input");
1261          lu_done(&ofiletab);                     /* close output files */
1262   }
1263  
1264 + /* get ray contribution from previous file */
1265 + static int
1266 + get_contrib(DCOLOR cnt, FILE *finp)
1267 + {
1268 +        COLOR   fv;
1269 +        COLR    cv;
1270 +
1271 +        switch (outfmt) {
1272 +        case 'a':
1273 +                return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
1274 +        case 'f':
1275 +                if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
1276 +                        return(0);
1277 +                copycolor(cnt, fv);
1278 +                return(1);
1279 +        case 'd':
1280 +                return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
1281 +        case 'c':
1282 +                if (fread(cv, sizeof(cv), 1, finp) != 1)
1283 +                        return(0);
1284 +                colr_color(fv, cv);
1285 +                copycolor(cnt, fv);
1286 +                return(1);
1287 +        default:
1288 +                error(INTERNAL, "botched output format");
1289 +        }
1290 +        return(0);      /* pro forma return */
1291 + }
1292 +
1293 + /* close output file opened for input */
1294 + static int
1295 + myclose(const LUENT *e, void *p)
1296 + {
1297 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1298 +        
1299 +        if (sop->ofp == NULL)
1300 +                return(0);
1301 +        fclose(sop->ofp);
1302 +        sop->ofp = NULL;
1303 +        return(0);
1304 + }
1305 +
1306 + /* load previously accumulated values */
1307 + void
1308 + reload_output(void)
1309 + {
1310 +        int             i, j;
1311 +        MODCONT         *mp;
1312 +        int             ofl;
1313 +        char            oname[1024];
1314 +        char            *fmode = "rb";
1315 +        char            *outvfmt;
1316 +        LUENT           *ment, *oent;
1317 +        int             xr, yr;
1318 +        STREAMOUT       sout;
1319 +        DCOLOR          rgbv;
1320 +
1321 +        switch (outfmt) {
1322 +        case 'a':
1323 +                outvfmt = "ascii";
1324 +                fmode = "r";
1325 +                break;
1326 +        case 'f':
1327 +                outvfmt = "float";
1328 +                break;
1329 +        case 'd':
1330 +                outvfmt = "double";
1331 +                break;
1332 +        case 'c':
1333 +                outvfmt = COLRFMT;
1334 +                break;
1335 +        default:
1336 +                error(INTERNAL, "botched output format");
1337 +                return;
1338 +        }
1339 +                                                /* reload modifier values */
1340 +        for (i = 0; i < nmods; i++) {
1341 +                ment = lu_find(&modconttab,modname[i]);
1342 +                mp = (MODCONT *)ment->data;
1343 +                if (mp->outspec == NULL)
1344 +                        error(USER, "cannot reload from stdout");
1345 +                if (mp->outspec[0] == '!')
1346 +                        error(USER, "cannot reload from command");
1347 +                for (j = 0; ; j++) {            /* load each modifier bin */
1348 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1349 +                        if (ofl < 0)
1350 +                                error(USER, "bad output file specification");
1351 +                        oent = lu_find(&ofiletab, oname);
1352 +                        if (oent->data != NULL) {
1353 +                                sout = *(STREAMOUT *)oent->data;
1354 +                        } else {
1355 +                                sout.reclen = 0;
1356 +                                sout.outpipe = 0;
1357 +                                sout.xr = xres; sout.yr = yres;
1358 +                                sout.ofp = NULL;
1359 +                        }
1360 +                        if (sout.ofp == NULL) { /* open output as input */
1361 +                                sout.ofp = fopen(oname, fmode);
1362 +                                if (sout.ofp == NULL) {
1363 +                                        if (j)
1364 +                                                break;  /* assume end of modifier */
1365 +                                        sprintf(errmsg, "missing reload file '%s'",
1366 +                                                        oname);
1367 +                                        error(WARNING, errmsg);
1368 +                                        break;
1369 +                                }
1370 + #ifdef getc_unlocked                                    /* avoid lock/unlock overhead */
1371 +                                flockfile(sout.ofp);
1372 + #endif
1373 +                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1374 +                                        sprintf(errmsg, "format mismatch for '%s'",
1375 +                                                        oname);
1376 +                                        error(USER, errmsg);
1377 +                                }
1378 +                                if ((sout.xr > 0) & (sout.yr > 0) &&
1379 +                                                (!fscnresolu(&xr, &yr, sout.ofp) ||
1380 +                                                        (xr != sout.xr) |
1381 +                                                        (yr != sout.yr))) {
1382 +                                        sprintf(errmsg, "resolution mismatch for '%s'",
1383 +                                                        oname);
1384 +                                        error(USER, errmsg);
1385 +                                }
1386 +                        }
1387 +                                                        /* read in RGB value */
1388 +                        if (!get_contrib(rgbv, sout.ofp)) {
1389 +                                if (!j) {
1390 +                                        fclose(sout.ofp);
1391 +                                        break;          /* ignore empty file */
1392 +                                }
1393 +                                if (j < mp->nbins) {
1394 +                                        sprintf(errmsg, "missing data in '%s'",
1395 +                                                        oname);
1396 +                                        error(USER, errmsg);
1397 +                                }
1398 +                                break;
1399 +                        }
1400 +                        if (j >= mp->nbins)             /* grow modifier size */
1401 +                                ment->data = (char *)(mp = growmodifier(mp, j+1));
1402 +                        copycolor(mp->cbin[j], rgbv);
1403 +                        if (oent->key == NULL)          /* new file entry */
1404 +                                oent->key = strcpy((char *)
1405 +                                                malloc(strlen(oname)+1), oname);
1406 +                        if (oent->data == NULL)
1407 +                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1408 +                        *(STREAMOUT *)oent->data = sout;
1409 +                }
1410 +        }
1411 +        lu_doall(&ofiletab, myclose, NULL);     /* close all files */
1412 + }
1413 +
1414   /* seek on the given output file */
1415   static int
1416   myseeko(const LUENT *e, void *p)
# Line 1132 | Line 1424 | myseeko(const LUENT *e, void *p)
1424                  sprintf(errmsg, "seek error on file '%s'", e->key);
1425                  error(SYSTEM, errmsg);
1426          }
1427 +        return 0;
1428   }
1429  
1430   /* recover output if possible */
# Line 1186 | Line 1479 | recover_output(FILE *fin)
1479                          if (oent->data != NULL) {
1480                                  sout = *(STREAMOUT *)oent->data;
1481                          } else {
1482 <                                sout.reclen = CNT_UNKNOWN;
1482 >                                sout.reclen = 0;
1483 >                                sout.outpipe = 0;
1484                                  sout.ofp = NULL;
1485                          }
1486                          if (sout.ofp != NULL) { /* already open? */
# Line 1201 | Line 1495 | recover_output(FILE *fin)
1495                                          break;  /* assume end of modifier */
1496                                  sprintf(errmsg, "missing recover file '%s'",
1497                                                  oname);
1498 <                                error(USER, errmsg);
1498 >                                error(WARNING, errmsg);
1499 >                                break;
1500                          }
1501                          nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
1502                          if (nvals <= 0) {
# Line 1209 | Line 1504 | recover_output(FILE *fin)
1504                                  fclose(sout.ofp);
1505                                  break;
1506                          }
1507 <                        if (sout.reclen == CNT_UNKNOWN) {
1507 >                        if (!sout.reclen) {
1508                                  if (!(ofl & OF_BIN)) {
1509                                          sprintf(errmsg,
1510                                                  "need -bn to recover file '%s'",
# Line 1226 | Line 1521 | recover_output(FILE *fin)
1521                                                  oname);
1522                                  error(USER, errmsg);
1523                          }
1524 <                        if ((xres > 0) & (yres > 0) &&
1524 >                        sout.xr = xres; sout.yr = yres;
1525 >                        if ((sout.xr > 0) & (sout.yr > 0) &&
1526                                          (!fscnresolu(&xr, &yr, sout.ofp) ||
1527 <                                                xr != xres ||
1528 <                                                yr != yres)) {
1527 >                                                (xr != sout.xr) |
1528 >                                                (yr != sout.yr))) {
1529                                  sprintf(errmsg, "resolution mismatch for '%s'",
1530                                                  oname);
1531                                  error(USER, errmsg);
# Line 1258 | Line 1554 | recover_output(FILE *fin)
1554                  error(WARNING, "no output files to recover");
1555                  return;
1556          }
1557 <        if (raysleft && lastout >= raysleft) {
1557 >        if (raysleft && lastout >= raysleft/accumulate) {
1558                  error(WARNING, "output appears to be complete");
1559                  /* XXX should read & discard input? */
1560                  quit(0);
# Line 1270 | Line 1566 | recover_output(FILE *fin)
1566          for (nvals = 0; nvals < lastout; nvals++)
1567                  if (getinp(oname, fin) < 0)
1568                          error(USER, "unexpected EOF on input");
1569 <        lastray = lastdone = (unsigned long)lastout;
1569 >        lastray = lastdone = (unsigned long)lastout * accumulate;
1570          if (raysleft)
1571                  raysleft -= lastray;
1572   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines