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.52 by greg, Sat Feb 7 05:40:47 2009 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  
# Line 92 | 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 */
# Line 107 | Line 123 | int  rtargc = 9;
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  
# Line 169 | Line 185 | void put_contrib(const DCOLOR cnt, FILE *fout);
185   void add_contrib(const char *modn);
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 340 | 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 378 | 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;
383                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 543 | Line 598 | init(int np)
598                  raysleft = 0;
599          if ((account = accumulate) > 0)
600                  raysleft *= accumulate;
601 <        waitflush = xres;
601 >        waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
602          if (!recover)
603                  return;
604                                          /* recover previous values */
# Line 580 | 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 588 | 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 */
598 <                bincnt = 1;
599 <        else if (bincnt > 1)
659 >        if (bincnt > 1)
660                  mp = growmodifier(mp, bincnt);
661          lep->data = (char *)mp;
662                                          /* allocate output streams */
# Line 659 | 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 691 | 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 725 | Line 796 | printresolu(FILE *fout, int xr, int yr)
796   {
797          if ((xr > 0) & (yr > 0))        /* resolution string */
798                  fprtresolu(xr, yr, fout);
728        if (xres > 0)                   /* global flush flag */
729                fflush(fout);
799   }
800  
801   /* Get output stream pointer (open and write header if new and noopen==0) */
# Line 747 | Line 816 | getostream(const char *ospec, const char *mname, int b
816                          if (header)
817                                  printheader(stdout, NULL);
818                          printresolu(stdout, xres, yres);
819 +                        if (waitflush > 0)
820 +                                fflush(stdout);
821                          stdos.xr = xres; stdos.yr = yres;
822                          using_stdout = 1;
823                  }
# Line 788 | Line 859 | getostream(const char *ospec, const char *mname, int b
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 815 | Line 889 | getostream(const char *ospec, const char *mname, int b
889                          if (outfmt == 'a')
890                                  putc('\n', sop->ofp);
891                  }
892 <                if (xres > 0)
892 >                if (waitflush > 0)
893                          fflush(sop->ofp);
894          }
895          sop->reclen += noopen;                  /* add to length if noopen */
# Line 853 | 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                  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                  dvp = (double *)buf + 3;
940                  if (DOT(dvp,dvp) <= FTINY*FTINY)
# Line 982 | Line 1056 | done_contrib(int navg)
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 1064 | 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 1095 | 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 1148 | Line 1228 | trace_contribs(FILE *fin)
1228                                  "dummy ray(s) ignored during accumulation\n");
1229                          continue;
1230                  }
1231 <                if (!iblen ||                   /* need reset? */
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)
1157 <                                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 1288 | Line 1367 | reload_output(void)
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);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines