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.9 by greg, Wed Jun 1 16:11:01 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                                  }
202                                  break;
203                          case 'f':               /* file or i/o format */
204                                  if (!argv[i][2]) {
205 +                                        char    *fpath;
206                                          if (i >= argc-1) break;
207 <                                        fcompile(argv[++i]);
207 >                                        fpath = getpath(argv[++i],
208 >                                                        getrlibpath(), R_OK);
209 >                                        if (fpath == NULL) {
210 >                                                sprintf(errmsg,
211 >                                                        "cannot find file '%s'",
212 >                                                                argv[i]);
213 >                                                error(USER, errmsg);
214 >                                        }
215 >                                        fcompile(fpath);
216                                          continue;
217                                  }
218                                  setformat(argv[i]+2);
# Line 265 | Line 278 | main(int argc, char *argv[])
278                  execv(rtpath, rtargv);
279                  perror(rtpath); /* execv() should not return */
280                  exit(1);
281 <        } else if (nprocs > 1) {        /* add persist file if parallel */
281 >        }
282 >        if (nprocs > 1) {       /* add persist file if parallel */
283                  rtargv[rtargc++] = "-PP";
284                  rtargv[rtargc++] = mktemp(persistfn);
285          }
# Line 279 | Line 293 | main(int argc, char *argv[])
293          rtargv[rtargc] = NULL;
294                                  /* start rtrace & compute contributions */
295          init(nprocs);
296 <        tracecontribs(stdin);
296 >        trace_contribs(stdin);
297          quit(0);
298   }
299  
# Line 331 | Line 345 | quit(int status)
345          exit(status);                   /* flushes all output streams */
346   }
347  
348 < /* start rtrace and initialize buffers */
348 > /* start rtrace processes and initialize */
349   void
350   init(int np)
351   {
# Line 345 | Line 359 | init(int np)
359          scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
360          scompile("Px=$4;Py=$5;Pz=$6;", NULL, 0);
361                                          /* set up signal handling */
362 < #ifdef SIGPIPE /* not present on Windows */
362 >        signal(SIGINT, quit);
363 > #ifdef SIGHUP
364 >        signal(SIGHUP, quit);
365 > #endif
366 > #ifdef SIGTERM
367 >        signal(SIGTERM, quit);
368 > #endif
369 > #ifdef SIGPIPE
370          signal(SIGPIPE, quit);
371   #endif
372          rtp = &rt0;                     /* start rtrace process(es) */
# Line 550 | Line 571 | badspec:
571   int
572   getinp(char *buf, FILE *fp)
573   {
574 +        char    *cp;
575 +        int     i;
576 +
577          switch (inpfmt) {
578          case 'a':
579 <                if (fgets(buf, 128, fp) == NULL)
580 <                        return 0;
579 >                cp = buf;               /* make sure we get 6 floats */
580 >                for (i = 0; i < 6; i++) {
581 >                        if (fgetword(cp, buf+127-cp, fp) == NULL)
582 >                                return 0;
583 >                        if ((cp = fskip(cp)) == NULL || *cp)
584 >                                return 0;
585 >                        *cp++ = ' ';
586 >                }
587 >                getc(fp);               /* get/put eol */
588 >                *cp-- = '\0'; *cp = '\n';
589                  return strlen(buf);
590          case 'f':
591                  if (fread(buf, sizeof(float), 6, fp) < 6)
# Line 679 | Line 711 | done_contrib(void)
711          }
712   }
713  
714 < /* process (or save) ray tree produced by rtrace process */
714 > /* queue completed ray tree produced by rtrace process */
715   void
716 < process_rays(struct rtproc *rtp)
716 > queue_raytree(struct rtproc *rtp)
717   {
718 <        struct rtproc   *rtu;
719 <                                        /* check if time to process it */
720 <        if (rtp->raynum == lastdone+1) {
718 >        struct rtproc   *rtu, *rtl = NULL;
719 >                                        /* insert following ray order */
720 >        for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
721 >                if (rtp->raynum < rtu->raynum)
722 >                        break;
723 >        rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
724 >        if (rtu == NULL)
725 >                error(SYSTEM, "out of memory in queue_raytree");
726 >        *rtu = *rtp;
727 >        if (rtl == NULL) {
728 >                rtu->next = rt_unproc;
729 >                rt_unproc = rtu;
730 >        } else {
731 >                rtu->next = rtl->next;
732 >                rtl->next = rtu;
733 >        }
734 >        rtp->raynum = 0;                /* clear path for next ray tree */
735 >        rtp->bsiz = 0;
736 >        rtp->buf = NULL;
737 >        rtp->nbr = 0;
738 > }
739 >
740 > /* process completed ray trees from our queue */
741 > void
742 > process_queue(void)
743 > {
744 >        char    modname[128];
745 >                                        /* ray-ordered queue */
746 >        while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
747 >                struct rtproc   *rtp = rt_unproc;
748                  int             n = rtp->nbr;
749                  const char      *cp = rtp->buf;
691                char            modname[128];
750                  while (n > 0) {         /* process rays */
751                          register char   *mnp = modname;
752                                          /* skip leading tabs */
# Line 712 | Line 770 | process_rays(struct rtproc *rtp)
770                  }
771                  done_contrib();         /* sum up contributions & output */
772                  lastdone = rtp->raynum;
773 <                free(rtp->buf);
774 <                                        /* catch up with unprocessed list */
775 <                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 <                }
773 >                free(rtp->buf);         /* free up buffer space */
774 >                rt_unproc = rtp->next;
775 >                free(rtp);              /* done with this ray tree */
776          }
739        rtp->raynum = 0;                /* clear path for next ray tree */
740        rtp->bsiz = 0;
741        rtp->buf = NULL;
742        rtp->nbr = 0;
777   }
778  
779   /* wait for rtrace process to finish with ray tree */
# Line 796 | Line 830 | wait_rproc(void)
830                          if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
831                                                          "~\t~\t", 4)) {
832                                  rt->nbr -= 4;   /* elide terminator */
833 <                                process_rays(rt);
833 >                                queue_raytree(rt);
834                                  rtfree = rt;    /* ready for next ray */
835                          }
836                  }
# Line 818 | Line 852 | get_rproc(void)
852  
853   /* trace ray contributions (main loop) */
854   void
855 < tracecontribs(FILE *fin)
855 > trace_contribs(FILE *fin)
856   {
857          char            inpbuf[128];
858          int             iblen;
# Line 827 | Line 861 | tracecontribs(FILE *fin)
861          while ((iblen = getinp(inpbuf, fin)) > 0) {
862                  if (lastray+1 < lastray) {      /* counter rollover? */
863                          while (wait_rproc() != NULL)
864 <                                ;
864 >                                process_queue();
865                          lastdone = lastray = 0;
866                  }
867                  rtp = get_rproc();              /* get avail. rtrace process */
868 <                rtp->raynum = ++lastray;        /* assign this ray to it */
868 >                rtp->raynum = ++lastray;        /* assign ray to it */
869                  writebuf(rtp->pd.w, inpbuf, iblen);
870                  if (!--raysleft)
871 <                        break;                  /* explicit EOF */
871 >                        break;
872 >                process_queue();                /* catch up with results */
873          }
874          while (wait_rproc() != NULL)            /* process outstanding rays */
875 <                ;
875 >                process_queue();
876          if (raysleft > 0)
877                  error(USER, "unexpected EOF on input");
878   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines