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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines