ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum.c
(Generate patch)

Comparing ray/src/gen/mkillum.c (file contents):
Revision 2.11 by greg, Sat Feb 22 02:07:24 2003 UTC vs.
Revision 2.25 by greg, Thu May 26 14:46:21 2005 UTC

# Line 1 | Line 1
1   #ifndef lint
2 < static const char       RCSid[] = "$Id$";
2 > static const char RCSid[] = "$Id$";
3   #endif
4   /*
5   * Make illum sources for optimizing rendering process
6   */
7  
8 #include  "mkillum.h"
9
8   #include  <signal.h>
11
9   #include  <ctype.h>
10  
11 + #include  "platform.h"
12 + #include  "mkillum.h"
13 + #include  "random.h"
14 +
15                                  /* default parameters */
16   #define  SAMPDENS       48              /* points per projected steradian */
17   #define  NSAMPS         32              /* samples per point */
# Line 24 | Line 25 | static const char      RCSid[] = "$Id$";
25  
26                                  /* rtrace command and defaults */
27   char  *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-dv-",
28 <                "-ab", "2", "-ad", "256", "-as", "128", "-aa", ".15", };
28 >                "-ab", "2", "-ad", "1024", "-as", "512", "-aa", ".1", };
29   int  rtargc = 14;
30                                  /* overriding rtrace options */
31   char  *myrtopts[] = { "-I-", "-i-", "-ld-", "-ov", "-h-",
32                          "-fff", "-y", "0", NULL };
33  
34 < struct rtproc   rt;             /* our rtrace process */
34 > struct rtproc   rt0;            /* head of rtrace process list */
35  
36   struct illum_args  thisillum = {        /* our illum and default values */
37                  0,
# Line 48 | Line 49 | int    matselect = S_ALL;      /* selection criterion */
49  
50   FUN     ofun[NUMOTYPE] = INIT_OTYPE;    /* object types */
51  
52 + char    persistfn[] = "pfXXXXXX";       /* persist file name */
53 +
54   int     gargc;                  /* global argc */
55   char    **gargv;                /* global argv */
56   #define  progname       gargv[0]
# Line 57 | Line 60 | int    doneheader = 0;         /* printed header yet? */
60  
61   int     warnings = 1;           /* print warnings? */
62  
63 + int done_rprocs(struct rtproc *rtp);
64 + void init(int np);
65 + void filter(register FILE       *infp, char     *name);
66 + void xoptions(char      *s, char        *nm);
67 + void printopts(void);
68 + void printhead(register int  ac, register char  **av);
69 + void xobject(FILE  *fp, char  *nm);
70  
71 < main(argc, argv)                /* compute illum distributions using rtrace */
72 < int     argc;
73 < char    *argv[];
71 >
72 > int
73 > main(           /* compute illum distributions using rtrace */
74 >        int     argc,
75 >        char    *argv[]
76 > )
77   {
78 +        int     nprocs = 1;
79          char    *rtpath;
80          FILE    *fp;
81          register int    i;
82                                  /* set global arguments */
83          gargv = argv;
84 +                                /* check for -n option */
85 +        if (!strcmp(argv[1], "-n")) {
86 +                nprocs = atoi(argv[2]);
87 +                if (nprocs <= 0)
88 +                        error(USER, "illegal number of processes");
89 +                i = 3;
90 +        } else
91 +                i = 1;
92                                  /* set up rtrace command */
93 <        for (i = 1; i < argc; i++) {
93 >        for ( ; i < argc; i++) {
94                  if (argv[i][0] == '<' && argv[i][1] == '\0')
95                          break;
96                  rtargv[rtargc++] = argv[i];
# Line 92 | Line 114 | char   *argv[];
114                          }
115          }
116          gargc = i;
117 <        rtargc--;
117 >        if (!strcmp(rtargv[--rtargc], "-defaults"))
118 >                nprocs = 0;
119 >        if (nprocs > 1) {       /* add persist file if parallel invocation */
120 >                rtargv[rtargc++] = "-PP";
121 >                rtargv[rtargc++] = mktemp(persistfn);
122 >        }
123 >                                /* add "mandatory" rtrace options */
124          for (i = 0; myrtopts[i] != NULL; i++)
125                  rtargv[rtargc++] = myrtopts[i];
126 +                                /* finally, put back final argument */
127          rtargv[rtargc++] = argv[gargc-1];
128          rtargv[rtargc] = NULL;
129 <                                /* just asking for defaults? */
101 <        if (!strcmp(argv[gargc-1], "-defaults")) {
129 >        if (!nprocs) {          /* just asking for defaults? */
130                  printopts(); fflush(stdout);
131                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
132                  if (rtpath == NULL) {
# Line 107 | Line 135 | char   *argv[];
135                          exit(1);
136                  }
137                  execv(rtpath, rtargv);
138 <                perror(rtpath);
138 >                perror(rtpath); /* execv() should not return */
139                  exit(1);
140          }
141          if (gargc < 2 || argv[gargc-1][0] == '-')
142                  error(USER, "missing octree argument");
143                                  /* else initialize and run our calculation */
144 <        init();
144 >        init(nprocs);
145          if (gargc+1 < argc)
146                  for (i = gargc+1; i < argc; i++) {
147                          if ((fp = fopen(argv[i], "r")) == NULL) {
# Line 129 | Line 157 | char   *argv[];
157          quit(0);
158   }
159  
160 + static void
161 + killpersist(void)                       /* kill persistent rtrace process */
162 + {
163 +        FILE    *fp = fopen(persistfn, "r");
164 +        int     pid;
165  
166 +        if (fp == NULL)
167 +                return;
168 +        if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0)
169 +                unlink(persistfn);
170 +        fclose(fp);
171 + }
172 +
173 + int
174 + done_rprocs(struct rtproc *rtp)
175 + {
176 +        int     st0, st1 = 0;
177 +
178 +        if (rtp->next != NULL) {        /* close last opened first! */
179 +                st1 = done_rprocs(rtp->next);
180 +                free((void *)rtp->next);
181 +                rtp->next = NULL;
182 +        }
183 +        st0 = close_process(&rtp->pd);
184 +        if (st0 < 0)
185 +                error(WARNING, "unknown return status from rtrace process");
186 +        else if (st0 > 0)
187 +                return(st0);
188 +        return(st1);
189 + }
190 +
191   void
192 < quit(status)                    /* exit with status */
135 < int  status;
192 > quit(int status)                        /* exit with status */
193   {
194          int     rtstat;
195  
196 <        rtstat = close_process(rt.pd);
196 >        if (rt0.next != NULL)           /* terminate persistent rtrace */
197 >                killpersist();
198 >                                        /* clean up rtrace process(es) */
199 >        rtstat = done_rprocs(&rt0);
200          if (status == 0)
201 <                if (rtstat < 0)
142 <                        error(WARNING,
143 <                        "unknown return status from rtrace process");
144 <                else
145 <                        status = rtstat;
201 >                status = rtstat;
202          exit(status);
203   }
204  
205 <
206 < init()                          /* start rtrace and set up buffers */
205 > void
206 > init(int np)                            /* start rtrace and set up buffers */
207   {
208 <        extern int  o_face(), o_sphere(), o_ring();
208 >        struct rtproc   *rtp;
209 >        int     i;
210          int     maxbytes;
211                                          /* set up object functions */
212          ofun[OBJ_FACE].funp = o_face;
213          ofun[OBJ_SPHERE].funp = o_sphere;
214          ofun[OBJ_RING].funp = o_ring;
215                                          /* set up signal handling */
216 +        signal(SIGINT, quit);
217 + #ifdef SIGHUP
218 +        signal(SIGHUP, quit);
219 + #endif
220 + #ifdef SIGTERM
221 +        signal(SIGTERM, quit);
222 + #endif
223 + #ifdef SIGPIPE
224          signal(SIGPIPE, quit);
225 <                                        /* start rtrace process */
226 <        errno = 0;
227 <        maxbytes = open_process(rt.pd, rtargv);
228 <        if (maxbytes == 0) {
229 <                eputs(rtargv[0]);
230 <                eputs(": command not found\n");
231 <                exit(1);
225 > #endif
226 >        rtp = &rt0;                     /* start rtrace process(es) */
227 >        for (i = 0; i++ < np; ) {
228 >                errno = 0;
229 >                maxbytes = open_process(&rtp->pd, rtargv);
230 >                if (maxbytes == 0) {
231 >                        eputs(rtargv[0]);
232 >                        eputs(": command not found\n");
233 >                        exit(1);
234 >                }
235 >                if (maxbytes < 0)
236 >                        error(SYSTEM, "cannot start rtrace process");
237 >                rtp->bsiz = maxbytes/(6*sizeof(float));
238 >                rtp->buf = (float *)malloc(6*sizeof(float)*rtp->bsiz--);
239 >                rtp->dest = (float **)calloc(rtp->bsiz, sizeof(float *));
240 >                if (rtp->buf == NULL || rtp->dest == NULL)
241 >                        error(SYSTEM, "out of memory in init");
242 >                rtp->nrays = 0;
243 >                if (i == np)            /* last process? */
244 >                        break;
245 >                if (np > 1)
246 >                        sleep(2);       /* wait for persist file */
247 >                rtp->next = (struct rtproc *)malloc(sizeof(struct rtproc));
248 >                if (rtp->next == NULL)
249 >                        error(SYSTEM, "out of memory in init");
250 >                rtp = rtp->next;
251          }
252 <        if (maxbytes < 0)
169 <                error(SYSTEM, "cannot start rtrace process");
170 <        rt.bsiz = maxbytes/(6*sizeof(float));
171 <        rt.buf = (float *)malloc(6*sizeof(float)*rt.bsiz--);
172 <        rt.dest = (float **)malloc(sizeof(float *)*rt.bsiz);
173 <        if (rt.buf == NULL || rt.dest == NULL)
174 <                error(SYSTEM, "out of memory in init");
175 <        rt.nrays = 0;
252 >        rtp->next = NULL;
253                                          /* set up urand */
254          initurand(16384);
255   }
256  
257  
258   void
259 < eputs(s)                                /* put string to stderr */
260 < register char  *s;
259 > eputs(                          /* put string to stderr */
260 >        register char  *s
261 > )
262   {
263          static int  midline = 0;
264  
# Line 203 | Line 281 | char  *s;
281   }
282  
283  
284 < filter(infp, name)              /* process stream */
285 < register FILE   *infp;
286 < char    *name;
284 > void
285 > filter(         /* process stream */
286 >        register FILE   *infp,
287 >        char    *name
288 > )
289   {
290          char    buf[512];
291          FILE    *pfp;
# Line 235 | Line 315 | char   *name;
315   }
316  
317  
318 < xoptions(s, nm)                 /* process options in string s */
319 < char    *s;
320 < char    *nm;
318 > void
319 > xoptions(                       /* process options in string s */
320 >        char    *s,
321 >        char    *nm
322 > )
323   {
324          extern FILE     *freopen();
325          char    buf[64];
# Line 396 | Line 478 | char   *nm;
478          printf("# %s", s+2);
479   }
480  
481 <
482 < printopts()                     /* print out option default values */
481 > void
482 > printopts(void)                 /* print out option default values */
483   {
484          printf("m=%-15s\t\t# material name\n", thisillum.matname);
485          printf("f=%-15s\t\t# data file name\n", thisillum.datafile);
# Line 418 | Line 500 | printopts()                    /* print out option default values */
500   }
501  
502  
503 < printhead(ac, av)                       /* print out header */
504 < register int  ac;
505 < register char  **av;
503 > void
504 > printhead(                      /* print out header */
505 >        register int  ac,
506 >        register char  **av
507 > )
508   {
509          putchar('#');
510          while (ac-- > 0) {
# Line 431 | Line 515 | register char  **av;
515   }
516  
517  
518 < xobject(fp, nm)                         /* translate an object from fp */
519 < FILE  *fp;
520 < char  *nm;
518 > void
519 > xobject(                                /* translate an object from fp */
520 >        FILE  *fp,
521 >        char  *nm
522 > )
523   {
524          OBJREC  thisobj;
525          char  str[MAXSTR];
# Line 444 | Line 530 | char  *nm;
530          if (fgetword(str, MAXSTR, fp) == NULL)
531                  goto readerr;
532                                          /* is it an alias? */
533 <        if (!strcmp(str, ALIASID)) {
533 >        if (!strcmp(str, ALIASKEY)) {
534                  if (fgetword(str, MAXSTR, fp) == NULL)
535                          goto readerr;
536 <                printf("\n%s %s %s", thisillum.altmat, ALIASID, str);
536 >                printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str);
537                  if (fgetword(str, MAXSTR, fp) == NULL)
538                          goto readerr;
539                  printf("\t%s\n", str);
# Line 484 | Line 570 | char  *nm;
570          checkhead();
571                                                  /* process object */
572          if (doit)
573 <                (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt, nm);
573 >                (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt0, nm);
574          else
575                  printobj(thisillum.altmat, &thisobj);
576                                                  /* free arguments */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines