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.10 by greg, Thu Jun 2 04:47:27 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 124 | Line 123 | static void
123   setformat(const char *fmt)
124   {
125          switch (fmt[0]) {
127        case 'a':
126          case 'f':
127          case 'd':
128 +                SET_FILE_BINARY(stdin);
129 +                /* fall through */
130 +        case 'a':
131                  inpfmt = fmt[0];
132                  break;
133          default:
# Line 159 | Line 160 | main(int argc, char *argv[])
160          int     nprocs = 1;
161          char    *curout = NULL;
162          char    *binval = NULL;
163 +        char    fmt[8];
164          int     i, j;
165                                  /* global program name */
166          gargv = argv;
# Line 188 | Line 190 | main(int argc, char *argv[])
190                                  case '\0':
191                                          header = !header;
192                                          continue;
193 <                                case '+': case '1': case 'T': case 't':
193 >                                case '+': case '1':
194 >                                case 'T': case 't':
195 >                                case 'Y': case 'y':
196                                          header = 1;
197                                          continue;
198 <                                case '-': case '0': case 'F': case 'f':
198 >                                case '-': case '0':
199 >                                case 'F': case 'f':
200 >                                case 'N': case 'n':
201                                          header = 0;
202                                          continue;
203                                  }
204                                  break;
205                          case 'f':               /* file or i/o format */
206                                  if (!argv[i][2]) {
207 +                                        char    *fpath;
208                                          if (i >= argc-1) break;
209 <                                        fcompile(argv[++i]);
209 >                                        fpath = getpath(argv[++i],
210 >                                                        getrlibpath(), R_OK);
211 >                                        if (fpath == NULL) {
212 >                                                sprintf(errmsg,
213 >                                                        "cannot find file '%s'",
214 >                                                                argv[i]);
215 >                                                error(USER, errmsg);
216 >                                        }
217 >                                        fcompile(fpath);
218                                          continue;
219                                  }
220                                  setformat(argv[i]+2);
# Line 265 | Line 280 | main(int argc, char *argv[])
280                  execv(rtpath, rtargv);
281                  perror(rtpath); /* execv() should not return */
282                  exit(1);
283 <        } else if (nprocs > 1) {        /* add persist file if parallel */
283 >        }
284 >        if (nprocs > 1) {       /* add persist file if parallel */
285                  rtargv[rtargc++] = "-PP";
286                  rtargv[rtargc++] = mktemp(persistfn);
287          }
# Line 279 | Line 295 | main(int argc, char *argv[])
295          rtargv[rtargc] = NULL;
296                                  /* start rtrace & compute contributions */
297          init(nprocs);
298 <        tracecontribs(stdin);
298 >        trace_contribs(stdin);
299          quit(0);
300   }
301  
# Line 331 | Line 347 | quit(int status)
347          exit(status);                   /* flushes all output streams */
348   }
349  
350 < /* start rtrace and initialize buffers */
350 > /* start rtrace processes and initialize */
351   void
352   init(int np)
353   {
# Line 345 | Line 361 | init(int np)
361          scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
362          scompile("Px=$4;Py=$5;Pz=$6;", NULL, 0);
363                                          /* set up signal handling */
364 < #ifdef SIGPIPE /* not present on Windows */
364 >        signal(SIGINT, quit);
365 > #ifdef SIGHUP
366 >        signal(SIGHUP, quit);
367 > #endif
368 > #ifdef SIGTERM
369 >        signal(SIGTERM, quit);
370 > #endif
371 > #ifdef SIGPIPE
372          signal(SIGPIPE, quit);
373   #endif
374          rtp = &rt0;                     /* start rtrace process(es) */
# Line 477 | Line 500 | getofile(const char *ospec, const char *mname, int bn)
500          LUENT           *lep;
501          
502          if (ospec == NULL) {                    /* use stdout? */
503 <                if (!using_stdout && header)
504 <                        printheader(stdout);
503 >                if (!using_stdout) {
504 >                        if (outfmt != 'a')
505 >                                SET_FILE_BINARY(stdout);
506 >                        if (header)
507 >                                printheader(stdout);
508 >                }
509                  using_stdout = 1;
510                  return stdout;
511          }
# Line 520 | Line 547 | getofile(const char *ospec, const char *mname, int bn)
547          if (lep->key == NULL)                   /* new entry */
548                  lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
549          if (lep->data == NULL) {                /* open output file */
550 <                FILE            *fp = fopen(ofname, "w");
550 >                FILE            *fp;
551                  int             i;
552 +                if (ofname[0] == '!')           /* output to command */
553 +                        fp = popen(ofname+1, "w");
554 +                else
555 +                        fp = fopen(ofname, "w");
556                  if (fp == NULL) {
557                          sprintf(errmsg, "cannot open '%s' for writing", ofname);
558                          error(SYSTEM, errmsg);
559                  }
560 +                if (outfmt != 'a')
561 +                        SET_FILE_BINARY(fp);
562                  if (header)
563                          printheader(fp);
564                                                  /* play catch-up */
# Line 550 | Line 583 | badspec:
583   int
584   getinp(char *buf, FILE *fp)
585   {
586 +        char    *cp;
587 +        int     i;
588 +
589          switch (inpfmt) {
590          case 'a':
591 <                if (fgets(buf, 128, fp) == NULL)
592 <                        return 0;
591 >                cp = buf;               /* make sure we get 6 floats */
592 >                for (i = 0; i < 6; i++) {
593 >                        if (fgetword(cp, buf+127-cp, fp) == NULL)
594 >                                return 0;
595 >                        if ((cp = fskip(cp)) == NULL || *cp)
596 >                                return 0;
597 >                        *cp++ = ' ';
598 >                }
599 >                getc(fp);               /* get/put eol */
600 >                *cp-- = '\0'; *cp = '\n';
601                  return strlen(buf);
602          case 'f':
603                  if (fread(buf, sizeof(float), 6, fp) < 6)
# Line 679 | Line 723 | done_contrib(void)
723          }
724   }
725  
726 < /* process (or save) ray tree produced by rtrace process */
726 > /* queue completed ray tree produced by rtrace process */
727   void
728 < process_rays(struct rtproc *rtp)
728 > queue_raytree(struct rtproc *rtp)
729   {
730 <        struct rtproc   *rtu;
731 <                                        /* check if time to process it */
732 <        if (rtp->raynum == lastdone+1) {
730 >        struct rtproc   *rtu, *rtl = NULL;
731 >                                        /* insert following ray order */
732 >        for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
733 >                if (rtp->raynum < rtu->raynum)
734 >                        break;
735 >        rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
736 >        if (rtu == NULL)
737 >                error(SYSTEM, "out of memory in queue_raytree");
738 >        *rtu = *rtp;
739 >        if (rtl == NULL) {
740 >                rtu->next = rt_unproc;
741 >                rt_unproc = rtu;
742 >        } else {
743 >                rtu->next = rtl->next;
744 >                rtl->next = rtu;
745 >        }
746 >        rtp->raynum = 0;                /* clear path for next ray tree */
747 >        rtp->bsiz = 0;
748 >        rtp->buf = NULL;
749 >        rtp->nbr = 0;
750 > }
751 >
752 > /* process completed ray trees from our queue */
753 > void
754 > process_queue(void)
755 > {
756 >        char    modname[128];
757 >                                        /* ray-ordered queue */
758 >        while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
759 >                struct rtproc   *rtp = rt_unproc;
760                  int             n = rtp->nbr;
761                  const char      *cp = rtp->buf;
691                char            modname[128];
762                  while (n > 0) {         /* process rays */
763                          register char   *mnp = modname;
764                                          /* skip leading tabs */
# Line 712 | Line 782 | process_rays(struct rtproc *rtp)
782                  }
783                  done_contrib();         /* sum up contributions & output */
784                  lastdone = rtp->raynum;
785 <                free(rtp->buf);
786 <                                        /* catch up with unprocessed list */
787 <                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 <                }
785 >                free(rtp->buf);         /* free up buffer space */
786 >                rt_unproc = rtp->next;
787 >                free(rtp);              /* done with this ray tree */
788          }
739        rtp->raynum = 0;                /* clear path for next ray tree */
740        rtp->bsiz = 0;
741        rtp->buf = NULL;
742        rtp->nbr = 0;
789   }
790  
791   /* wait for rtrace process to finish with ray tree */
# Line 796 | Line 842 | wait_rproc(void)
842                          if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
843                                                          "~\t~\t", 4)) {
844                                  rt->nbr -= 4;   /* elide terminator */
845 <                                process_rays(rt);
845 >                                queue_raytree(rt);
846                                  rtfree = rt;    /* ready for next ray */
847                          }
848                  }
# Line 818 | Line 864 | get_rproc(void)
864  
865   /* trace ray contributions (main loop) */
866   void
867 < tracecontribs(FILE *fin)
867 > trace_contribs(FILE *fin)
868   {
869          char            inpbuf[128];
870          int             iblen;
# Line 827 | Line 873 | tracecontribs(FILE *fin)
873          while ((iblen = getinp(inpbuf, fin)) > 0) {
874                  if (lastray+1 < lastray) {      /* counter rollover? */
875                          while (wait_rproc() != NULL)
876 <                                ;
876 >                                process_queue();
877                          lastdone = lastray = 0;
878                  }
879                  rtp = get_rproc();              /* get avail. rtrace process */
880 <                rtp->raynum = ++lastray;        /* assign this ray to it */
880 >                rtp->raynum = ++lastray;        /* assign ray to it */
881                  writebuf(rtp->pd.w, inpbuf, iblen);
882                  if (!--raysleft)
883 <                        break;                  /* explicit EOF */
883 >                        break;
884 >                process_queue();                /* catch up with results */
885          }
886          while (wait_rproc() != NULL)            /* process outstanding rays */
887 <                ;
887 >                process_queue();
888          if (raysleft > 0)
889                  error(USER, "unexpected EOF on input");
890   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines