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.13 by greg, Sun Jun 5 19:52: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", };
72 < int  rtargc = 11;
70 > char            *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3",
71 >                                "-ab", "1", "-ad", "128", };
72 > int  rtargc = 9;
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;
167 <                                /* set up calcomp mode */
167 >                                /* initialize calcomp routines */
168          esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
169          esupport &= ~(E_OUTCHAN);
170 +        varset("PI", ':', PI);
171                                  /* get our options */
172          for (i = 1; i < argc-1; i++) {
173                                                  /* expand arguments */
# Line 188 | Line 191 | main(int argc, char *argv[])
191                                  case '\0':
192                                          header = !header;
193                                          continue;
194 <                                case '+': case '1': case 'T': case 't':
194 >                                case '+': case '1':
195 >                                case 'T': case 't':
196 >                                case 'Y': case 'y':
197                                          header = 1;
198                                          continue;
199 <                                case '-': case '0': case 'F': case 'f':
199 >                                case '-': case '0':
200 >                                case 'F': case 'f':
201 >                                case 'N': case 'n':
202                                          header = 0;
203                                          continue;
204                                  }
205                                  break;
206                          case 'f':               /* file or i/o format */
207                                  if (!argv[i][2]) {
208 +                                        char    *fpath;
209                                          if (i >= argc-1) break;
210 <                                        fcompile(argv[++i]);
210 >                                        fpath = getpath(argv[++i],
211 >                                                        getrlibpath(), R_OK);
212 >                                        if (fpath == NULL) {
213 >                                                sprintf(errmsg,
214 >                                                        "cannot find file '%s'",
215 >                                                                argv[i]);
216 >                                                error(USER, errmsg);
217 >                                        }
218 >                                        fcompile(fpath);
219                                          continue;
220                                  }
221                                  setformat(argv[i]+2);
# Line 265 | Line 281 | main(int argc, char *argv[])
281                  execv(rtpath, rtargv);
282                  perror(rtpath); /* execv() should not return */
283                  exit(1);
284 <        } else if (nprocs > 1) {        /* add persist file if parallel */
284 >        }
285 >        if (nprocs > 1) {       /* add persist file if parallel */
286                  rtargv[rtargc++] = "-PP";
287                  rtargv[rtargc++] = mktemp(persistfn);
288          }
# Line 279 | Line 296 | main(int argc, char *argv[])
296          rtargv[rtargc] = NULL;
297                                  /* start rtrace & compute contributions */
298          init(nprocs);
299 <        tracecontribs(stdin);
299 >        trace_contribs(stdin);
300          quit(0);
301   }
302  
# Line 331 | Line 348 | quit(int status)
348          exit(status);                   /* flushes all output streams */
349   }
350  
351 < /* start rtrace and initialize buffers */
351 > /* start rtrace processes and initialize */
352   void
353   init(int np)
354   {
# Line 345 | Line 362 | init(int np)
362          scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
363          scompile("Px=$4;Py=$5;Pz=$6;", NULL, 0);
364                                          /* set up signal handling */
365 < #ifdef SIGPIPE /* not present on Windows */
365 >        signal(SIGINT, quit);
366 > #ifdef SIGHUP
367 >        signal(SIGHUP, quit);
368 > #endif
369 > #ifdef SIGTERM
370 >        signal(SIGTERM, quit);
371 > #endif
372 > #ifdef SIGPIPE
373          signal(SIGPIPE, quit);
374   #endif
375          rtp = &rt0;                     /* start rtrace process(es) */
# Line 477 | Line 501 | getofile(const char *ospec, const char *mname, int bn)
501          LUENT           *lep;
502          
503          if (ospec == NULL) {                    /* use stdout? */
504 <                if (!using_stdout && header)
505 <                        printheader(stdout);
504 >                if (!using_stdout) {
505 >                        if (outfmt != 'a')
506 >                                SET_FILE_BINARY(stdout);
507 >                        if (header)
508 >                                printheader(stdout);
509 >                }
510                  using_stdout = 1;
511                  return stdout;
512          }
# Line 520 | Line 548 | getofile(const char *ospec, const char *mname, int bn)
548          if (lep->key == NULL)                   /* new entry */
549                  lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
550          if (lep->data == NULL) {                /* open output file */
551 <                FILE            *fp = fopen(ofname, "w");
551 >                FILE            *fp;
552                  int             i;
553 +                if (ofname[0] == '!')           /* output to command */
554 +                        fp = popen(ofname+1, "w");
555 +                else
556 +                        fp = fopen(ofname, "w");
557                  if (fp == NULL) {
558                          sprintf(errmsg, "cannot open '%s' for writing", ofname);
559                          error(SYSTEM, errmsg);
560                  }
561 +                if (outfmt != 'a')
562 +                        SET_FILE_BINARY(fp);
563                  if (header)
564                          printheader(fp);
565                                                  /* play catch-up */
# Line 550 | Line 584 | badspec:
584   int
585   getinp(char *buf, FILE *fp)
586   {
587 +        char    *cp;
588 +        int     i;
589 +
590          switch (inpfmt) {
591          case 'a':
592 <                if (fgets(buf, 128, fp) == NULL)
593 <                        return 0;
592 >                cp = buf;               /* make sure we get 6 floats */
593 >                for (i = 0; i < 6; i++) {
594 >                        if (fgetword(cp, buf+127-cp, fp) == NULL)
595 >                                return 0;
596 >                        if ((cp = fskip(cp)) == NULL || *cp)
597 >                                return 0;
598 >                        *cp++ = ' ';
599 >                }
600 >                getc(fp);               /* get/put eol */
601 >                *cp-- = '\0'; *cp = '\n';
602                  return strlen(buf);
603          case 'f':
604                  if (fread(buf, sizeof(float), 6, fp) < 6)
# Line 661 | Line 706 | done_contrib(void)
706          MODCONT *mp;
707                                                  /* output modifiers in order */
708          for (i = 0; i < nmods; i++) {
709 +                FILE    *fp;
710                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
711 <                for (j = 0; j < mp->nbins; j++)
712 <                        putcontrib(mp->cbin[j],
711 >                fp = getofile(mp->outspec, mp->modname, 0);
712 >                putcontrib(mp->cbin[0], fp);
713 >                if (mp->nbins > 3 &&            /* minor optimization */
714 >                                fp == getofile(mp->outspec, mp->modname, 1))
715 >                        for (j = 1; j < mp->nbins; j++)
716 >                                putcontrib(mp->cbin[j], fp);
717 >                else
718 >                        for (j = 1; j < mp->nbins; j++)
719 >                                putcontrib(mp->cbin[j],
720                                          getofile(mp->outspec, mp->modname, j));
721                                                  /* clear for next ray tree */
722                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
# Line 679 | Line 732 | done_contrib(void)
732          }
733   }
734  
735 < /* process (or save) ray tree produced by rtrace process */
735 > /* queue completed ray tree produced by rtrace process */
736   void
737 < process_rays(struct rtproc *rtp)
737 > queue_raytree(struct rtproc *rtp)
738   {
739 <        struct rtproc   *rtu;
740 <                                        /* check if time to process it */
741 <        if (rtp->raynum == lastdone+1) {
739 >        struct rtproc   *rtu, *rtl = NULL;
740 >                                        /* insert following ray order */
741 >        for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
742 >                if (rtp->raynum < rtu->raynum)
743 >                        break;
744 >        rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
745 >        if (rtu == NULL)
746 >                error(SYSTEM, "out of memory in queue_raytree");
747 >        *rtu = *rtp;
748 >        if (rtl == NULL) {
749 >                rtu->next = rt_unproc;
750 >                rt_unproc = rtu;
751 >        } else {
752 >                rtu->next = rtl->next;
753 >                rtl->next = rtu;
754 >        }
755 >        rtp->raynum = 0;                /* clear path for next ray tree */
756 >        rtp->bsiz = 0;
757 >        rtp->buf = NULL;
758 >        rtp->nbr = 0;
759 > }
760 >
761 > /* process completed ray trees from our queue */
762 > void
763 > process_queue(void)
764 > {
765 >        char    modname[128];
766 >                                        /* ray-ordered queue */
767 >        while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
768 >                struct rtproc   *rtp = rt_unproc;
769                  int             n = rtp->nbr;
770                  const char      *cp = rtp->buf;
691                char            modname[128];
771                  while (n > 0) {         /* process rays */
772                          register char   *mnp = modname;
773                                          /* skip leading tabs */
# Line 712 | Line 791 | process_rays(struct rtproc *rtp)
791                  }
792                  done_contrib();         /* sum up contributions & output */
793                  lastdone = rtp->raynum;
794 <                free(rtp->buf);
795 <                                        /* catch up with unprocessed list */
796 <                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 <                }
794 >                free(rtp->buf);         /* free up buffer space */
795 >                rt_unproc = rtp->next;
796 >                free(rtp);              /* done with this ray tree */
797          }
739        rtp->raynum = 0;                /* clear path for next ray tree */
740        rtp->bsiz = 0;
741        rtp->buf = NULL;
742        rtp->nbr = 0;
798   }
799  
800   /* wait for rtrace process to finish with ray tree */
# Line 796 | Line 851 | wait_rproc(void)
851                          if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
852                                                          "~\t~\t", 4)) {
853                                  rt->nbr -= 4;   /* elide terminator */
854 <                                process_rays(rt);
854 >                                queue_raytree(rt);
855                                  rtfree = rt;    /* ready for next ray */
856                          }
857                  }
# Line 818 | Line 873 | get_rproc(void)
873  
874   /* trace ray contributions (main loop) */
875   void
876 < tracecontribs(FILE *fin)
876 > trace_contribs(FILE *fin)
877   {
878          char            inpbuf[128];
879          int             iblen;
# Line 827 | Line 882 | tracecontribs(FILE *fin)
882          while ((iblen = getinp(inpbuf, fin)) > 0) {
883                  if (lastray+1 < lastray) {      /* counter rollover? */
884                          while (wait_rproc() != NULL)
885 <                                ;
885 >                                process_queue();
886                          lastdone = lastray = 0;
887                  }
888                  rtp = get_rproc();              /* get avail. rtrace process */
889 <                rtp->raynum = ++lastray;        /* assign this ray to it */
889 >                rtp->raynum = ++lastray;        /* assign ray to it */
890                  writebuf(rtp->pd.w, inpbuf, iblen);
891                  if (!--raysleft)
892 <                        break;                  /* explicit EOF */
892 >                        break;
893 >                process_queue();                /* catch up with results */
894          }
895          while (wait_rproc() != NULL)            /* process outstanding rays */
896 <                ;
896 >                process_queue();
897          if (raysleft > 0)
898                  error(USER, "unexpected EOF on input");
899   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines