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.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 */
# 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 138 | Line 159 | int            xres = 0;               /* horiz. output resolution */
159   int             yres = 0;               /* vert. output resolution */
160  
161   int             account;                /* current accumulation count */
162 < unsigned long   raysleft;               /* number of rays left to trace */
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 169 | Line 190 | void put_contrib(const DCOLOR cnt, FILE *fout);
190   void add_contrib(const char *modn);
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 340 | 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 378 | 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;
383                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");
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 536 | 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          if ((account = accumulate) > 0)
605                  raysleft *= accumulate;
606 <        waitflush = xres;
606 >        waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
607          if (!recover)
608                  return;
609                                          /* recover previous values */
# Line 580 | 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 588 | 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 */
598 <                bincnt = 1;
599 <        else if (bincnt > 1)
664 >        if (bincnt > 1)
665                  mp = growmodifier(mp, bincnt);
666          lep->data = (char *)mp;
667                                          /* allocate output streams */
# Line 659 | 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 691 | 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 725 | Line 801 | printresolu(FILE *fout, int xr, int yr)
801   {
802          if ((xr > 0) & (yr > 0))        /* resolution string */
803                  fprtresolu(xr, yr, fout);
728        if (xres > 0)                   /* global flush flag */
729                fflush(fout);
804   }
805  
806   /* Get output stream pointer (open and write header if new and noopen==0) */
# Line 747 | 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 788 | 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 815 | 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 853 | 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 982 | Line 1061 | done_contrib(int navg)
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 1064 | 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 1095 | 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 1148 | Line 1233 | trace_contribs(FILE *fin)
1233                                  "dummy ray(s) ignored during accumulation\n");
1234                          continue;
1235                  }
1236 <                if (!iblen ||                   /* need reset? */
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)
1157 <                                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 1288 | Line 1372 | reload_output(void)
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);
# Line 1484 | 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 * accumulate;
1574 >        lastray = lastdone = (RNUMBER)lastout * accumulate;
1575          if (raysleft)
1576                  raysleft -= lastray;
1577   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines