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.2 by greg, Thu May 26 06:55:22 2005 UTC vs.
Revision 1.6 by greg, Sat May 28 22:27:54 2005 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   * Gather rtrace output to compute contributions from particular sources
6   */
7  
8 + #include  "standard.h"
9   #include  <ctype.h>
9 #include  "rtio.h"
10 #include  "rterror.h"
10   #include  "platform.h"
11   #include  "rtprocess.h"
12   #include  "selcall.h"
# Line 64 | Line 63 | struct rtproc {
63          int             bsiz;           /* ray tree buffer length */
64          char            *buf;           /* ray tree buffer */
65          int             nbr;            /* number of bytes from rtrace */
66 < };                              /* rtrace process */
66 > };                              /* rtrace process buffer */
67  
68                                          /* rtrace command and defaults */
69 < char            *rtargv[256] = { "rtrace", "-dt", "0", "-dj", ".5", "-dr", "3",
70 <                                "-ab", "1", "-ad", "512", };
71 < int  rtargc = 11;
69 > char            *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3",
70 >                                "-ab", "1", "-ad", "128", };
71 > int  rtargc = 9;
72                                          /* overriding rtrace options */
73 < char            *myrtopts[] = { "-o~~TmWdp", "-h-",
74 <                                "-x", "1", "-y", "0",
76 <                                "-as", "0", "-aa", "0", NULL };
73 > char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
74 >                                "-dt", "0", "-as", "0", "-aa", "0", NULL };
75  
76   struct rtproc   rt0;                    /* head of rtrace process list */
77  
78   struct rtproc   *rt_unproc = NULL;      /* unprocessed ray trees */
79  
80   char    persistfn[] = "pfXXXXXX";       /* persist file name */
83 char    fmt[8];                         /* rtrace i/o format */
81  
82   int             gargc;                  /* global argc */
83   char            **gargv;                /* global argv */
# Line 108 | Line 105 | int            nmods = 0;              /* number of modifiers */
105  
106   MODCONT *addmodifier(char *modn, char *outf, char *binv);
107  
111 int done_rprocs(struct rtproc *rtp);
108   void init(int np);
109 < void tracecontribs(FILE *fp);
109 > int done_rprocs(struct rtproc *rtp);
110 > void trace_contribs(FILE *fp);
111   struct rtproc *wait_rproc(void);
112   struct rtproc *get_rproc(void);
113 < void process_rays(struct rtproc *rtp);
113 > void queue_raytree(struct rtproc *rtp);
114 > void process_queue(void);
115  
116   void putcontrib(const DCOLOR cnt, FILE *fout);
117   void add_contrib(const char *modn);
# Line 159 | Line 157 | main(int argc, char *argv[])
157          int     nprocs = 1;
158          char    *curout = NULL;
159          char    *binval = NULL;
160 +        char    fmt[8];
161          int     i, j;
162                                  /* global program name */
163          gargv = argv;
# Line 188 | Line 187 | main(int argc, char *argv[])
187                                  case '\0':
188                                          header = !header;
189                                          continue;
190 <                                case '+': case '1': case 'T': case 't':
190 >                                case '+': case '1':
191 >                                case 'T': case 't':
192 >                                case 'Y': case 'y':
193                                          header = 1;
194                                          continue;
195 <                                case '-': case '0': case 'F': case 'f':
195 >                                case '-': case '0':
196 >                                case 'F': case 'f':
197 >                                case 'N': case 'n':
198                                          header = 0;
199                                          continue;
200                                  }
# Line 265 | Line 268 | main(int argc, char *argv[])
268                  execv(rtpath, rtargv);
269                  perror(rtpath); /* execv() should not return */
270                  exit(1);
271 <        } else if (nprocs > 1) {        /* add persist file if parallel */
271 >        }
272 >        if (nprocs > 1) {       /* add persist file if parallel */
273                  rtargv[rtargc++] = "-PP";
274                  rtargv[rtargc++] = mktemp(persistfn);
275          }
# Line 279 | Line 283 | main(int argc, char *argv[])
283          rtargv[rtargc] = NULL;
284                                  /* start rtrace & compute contributions */
285          init(nprocs);
286 <        tracecontribs(stdin);
286 >        trace_contribs(stdin);
287          quit(0);
288   }
289  
# Line 331 | Line 335 | quit(int status)
335          exit(status);                   /* flushes all output streams */
336   }
337  
338 < /* start rtrace and initialize buffers */
338 > /* start rtrace processes and initialize */
339   void
340   init(int np)
341   {
# Line 345 | Line 349 | init(int np)
349          scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
350          scompile("Px=$4;Py=$5;Pz=$6;", NULL, 0);
351                                          /* set up signal handling */
352 < #ifdef SIGPIPE /* not present on Windows */
352 >        signal(SIGINT, quit);
353 > #ifdef SIGHUP
354 >        signal(SIGHUP, quit);
355 > #endif
356 > #ifdef SIGTERM
357 >        signal(SIGTERM, quit);
358 > #endif
359 > #ifdef SIGPIPE
360          signal(SIGPIPE, quit);
361   #endif
362          rtp = &rt0;                     /* start rtrace process(es) */
# Line 550 | Line 561 | badspec:
561   int
562   getinp(char *buf, FILE *fp)
563   {
564 +        char    *cp;
565 +        int     i;
566 +
567          switch (inpfmt) {
568          case 'a':
569 <                if (fgets(buf, 128, fp) == NULL)
570 <                        return 0;
569 >                cp = buf;               /* make sure we get 6 floats */
570 >                for (i = 0; i < 6; i++) {
571 >                        if (fgetword(cp, buf+127-cp, fp) == NULL)
572 >                                return 0;
573 >                        if ((cp = fskip(cp)) == NULL || *cp)
574 >                                return 0;
575 >                        *cp++ = ' ';
576 >                }
577 >                getc(fp);               /* get/put eol */
578 >                *cp-- = '\0'; *cp = '\n';
579                  return strlen(buf);
580          case 'f':
581                  if (fread(buf, sizeof(float), 6, fp) < 6)
# Line 679 | Line 701 | done_contrib(void)
701          }
702   }
703  
704 < /* process (or save) ray tree produced by rtrace process */
704 > /* queue completed ray tree produced by rtrace process */
705   void
706 < process_rays(struct rtproc *rtp)
706 > queue_raytree(struct rtproc *rtp)
707   {
708 <        struct rtproc   *rtu;
709 <                                        /* check if time to process it */
710 <        if (rtp->raynum == lastdone+1) {
708 >        struct rtproc   *rtu, *rtl = NULL;
709 >                                        /* insert following ray order */
710 >        for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
711 >                if (rtp->raynum < rtu->raynum)
712 >                        break;
713 >        rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
714 >        if (rtu == NULL)
715 >                error(SYSTEM, "out of memory in queue_raytree");
716 >        *rtu = *rtp;
717 >        if (rtl == NULL) {
718 >                rtu->next = rt_unproc;
719 >                rt_unproc = rtu;
720 >        } else {
721 >                rtu->next = rtl->next;
722 >                rtl->next = rtu;
723 >        }
724 >        rtp->raynum = 0;                /* clear path for next ray tree */
725 >        rtp->bsiz = 0;
726 >        rtp->buf = NULL;
727 >        rtp->nbr = 0;
728 > }
729 >
730 > /* process completed ray trees from our queue */
731 > void
732 > process_queue(void)
733 > {
734 >        char    modname[128];
735 >                                        /* ray-ordered queue */
736 >        while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
737 >                struct rtproc   *rtp = rt_unproc;
738                  int             n = rtp->nbr;
739                  const char      *cp = rtp->buf;
691                char            modname[128];
740                  while (n > 0) {         /* process rays */
741                          register char   *mnp = modname;
742                                          /* skip leading tabs */
# Line 712 | Line 760 | process_rays(struct rtproc *rtp)
760                  }
761                  done_contrib();         /* sum up contributions & output */
762                  lastdone = rtp->raynum;
763 <                free(rtp->buf);
764 <                                        /* catch up with unprocessed list */
765 <                while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
718 <                        process_rays(rt_unproc);
719 <                        rt_unproc = (rtu=rt_unproc)->next;
720 <                        free(rtu);
721 <                }
722 <        } else {                        /* else insert in unprocessed list */
723 <                struct rtproc   *rtl = NULL;
724 <                for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
725 <                        if (rtp->raynum < rtu->raynum)
726 <                                break;
727 <                rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
728 <                if (rtu == NULL)
729 <                        error(SYSTEM, "out of memory in process_rays");
730 <                *rtu = *rtp;
731 <                if (rtl == NULL) {
732 <                        rtu->next = rt_unproc;
733 <                        rt_unproc = rtu;
734 <                } else {
735 <                        rtu->next = rtl->next;
736 <                        rtl->next = rtu;
737 <                }
763 >                free(rtp->buf);         /* free up buffer space */
764 >                rt_unproc = rtp->next;
765 >                free(rtp);              /* done with this ray tree */
766          }
739        rtp->raynum = 0;                /* clear path for next ray tree */
740        rtp->bsiz = 0;
741        rtp->buf = NULL;
742        rtp->nbr = 0;
767   }
768  
769   /* wait for rtrace process to finish with ray tree */
# Line 796 | Line 820 | wait_rproc(void)
820                          if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
821                                                          "~\t~\t", 4)) {
822                                  rt->nbr -= 4;   /* elide terminator */
823 <                                process_rays(rt);
823 >                                queue_raytree(rt);
824                                  rtfree = rt;    /* ready for next ray */
825                          }
826                  }
# Line 818 | Line 842 | get_rproc(void)
842  
843   /* trace ray contributions (main loop) */
844   void
845 < tracecontribs(FILE *fin)
845 > trace_contribs(FILE *fin)
846   {
847          char            inpbuf[128];
848          int             iblen;
# Line 827 | Line 851 | tracecontribs(FILE *fin)
851          while ((iblen = getinp(inpbuf, fin)) > 0) {
852                  if (lastray+1 < lastray) {      /* counter rollover? */
853                          while (wait_rproc() != NULL)
854 <                                ;
854 >                                process_queue();
855                          lastdone = lastray = 0;
856                  }
857                  rtp = get_rproc();              /* get avail. rtrace process */
858 <                rtp->raynum = ++lastray;        /* assign this ray to it */
858 >                rtp->raynum = ++lastray;        /* assign ray to it */
859                  writebuf(rtp->pd.w, inpbuf, iblen);
860                  if (!--raysleft)
861 <                        break;                  /* explicit EOF */
861 >                        break;
862 >                process_queue();                /* catch up with results */
863          }
864          while (wait_rproc() != NULL)            /* process outstanding rays */
865 <                ;
865 >                process_queue();
866          if (raysleft > 0)
867                  error(USER, "unexpected EOF on input");
868   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines