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.15 by greg, Fri Jun 27 22:27:45 2003 UTC vs.
Revision 2.20 by greg, Fri Sep 17 21:43:50 2004 UTC

# Line 8 | Line 8 | static const char RCSid[] = "$Id$";
8   #include  <signal.h>
9   #include  <ctype.h>
10  
11 #include  "mkillum.h"
11   #include  "platform.h"
12 + #include  "mkillum.h"
13 + #include  "random.h"
14  
15                                  /* default parameters */
16   #define  SAMPDENS       48              /* points per projected steradian */
# 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 + void init(int np);
64 + void filter(register FILE       *infp, char     *name);
65 + void xoptions(char      *s, char        *nm);
66 + void printopts(void);
67 + void printhead(register int  ac, register char  **av);
68 + void xobject(FILE  *fp, char  *nm);
69  
70 < main(argc, argv)                /* compute illum distributions using rtrace */
71 < int     argc;
72 < char    *argv[];
70 >
71 > int
72 > main(           /* compute illum distributions using rtrace */
73 >        int     argc,
74 >        char    *argv[]
75 > )
76   {
77 +        int     nprocs = 1;
78          char    *rtpath;
79          FILE    *fp;
80          register int    i;
81                                  /* set global arguments */
82          gargv = argv;
83 +                                /* check for -n option */
84 +        if (!strcmp(argv[1], "-n")) {
85 +                nprocs = atoi(argv[2]);
86 +                if (nprocs <= 0)
87 +                        error(USER, "illegal number of processes");
88 +                i = 3;
89 +        } else
90 +                i = 1;
91                                  /* set up rtrace command */
92 <        for (i = 1; i < argc; i++) {
92 >        for ( ; i < argc; i++) {
93                  if (argv[i][0] == '<' && argv[i][1] == '\0')
94                          break;
95                  rtargv[rtargc++] = argv[i];
# Line 92 | Line 113 | char   *argv[];
113                          }
114          }
115          gargc = i;
116 <        rtargc--;
116 >        if (!strcmp(rtargv[--rtargc], "-defaults"))
117 >                nprocs = 0;
118 >        if (nprocs > 1) {       /* add persist file if parallel invocation */
119 >                rtargv[rtargc++] = "-PP";
120 >                rtargv[rtargc++] = mktemp(persistfn);
121 >        }
122 >                                /* add "mandatory" rtrace options */
123          for (i = 0; myrtopts[i] != NULL; i++)
124                  rtargv[rtargc++] = myrtopts[i];
125 +                                /* finally, put back final argument */
126          rtargv[rtargc++] = argv[gargc-1];
127          rtargv[rtargc] = NULL;
128 <                                /* just asking for defaults? */
101 <        if (!strcmp(argv[gargc-1], "-defaults")) {
128 >        if (!nprocs) {          /* just asking for defaults? */
129                  printopts(); fflush(stdout);
130                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
131                  if (rtpath == NULL) {
# Line 107 | Line 134 | char   *argv[];
134                          exit(1);
135                  }
136                  execv(rtpath, rtargv);
137 <                perror(rtpath);
137 >                perror(rtpath); /* execv() should not return */
138                  exit(1);
139          }
140          if (gargc < 2 || argv[gargc-1][0] == '-')
141                  error(USER, "missing octree argument");
142                                  /* else initialize and run our calculation */
143 <        init();
143 >        init(nprocs);
144          if (gargc+1 < argc)
145                  for (i = gargc+1; i < argc; i++) {
146                          if ((fp = fopen(argv[i], "r")) == NULL) {
# Line 129 | Line 156 | char   *argv[];
156          quit(0);
157   }
158  
159 + static void
160 + killpersist(void)                       /* kill persistent process */
161 + {
162 +        FILE    *fp = fopen(persistfn, "r");
163 +        int     pid;
164  
165 +        if (fp == NULL)
166 +                return;
167 +        if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0)
168 +                unlink(persistfn);
169 +        fclose(fp);
170 + }
171 +
172   void
173 < quit(status)                    /* exit with status */
135 < int  status;
173 > quit(int status)                        /* exit with status */
174   {
175 +        struct rtproc   *rtp;
176          int     rtstat;
177  
178 <        rtstat = close_process(&(rt.pd));
179 <        if (status == 0)
180 <                if (rtstat < 0)
181 <                        error(WARNING,
182 <                        "unknown return status from rtrace process");
183 <                else
184 <                        status = rtstat;
178 >        if (rt0.next != NULL)           /* terminate persistent rtrace */
179 >                killpersist();
180 >                                        /* clean up rtrace process(es) */
181 >        for (rtp = &rt0; rtp != NULL; rtp = rtp->next) {
182 >                rtstat = close_process(&rtp->pd);
183 >                if (status == 0) {
184 >                        if (rtstat < 0)
185 >                                error(WARNING,
186 >                                "unknown return status from rtrace process");
187 >                        else
188 >                                status = rtstat;
189 >                }
190 >        }
191          exit(status);
192   }
193  
194 <
195 < init()                          /* start rtrace and set up buffers */
194 > void
195 > init(int np)                            /* start rtrace and set up buffers */
196   {
197 <        extern int  o_face(), o_sphere(), o_ring();
197 >        struct rtproc   *rtp;
198 >        int     i;
199          int     maxbytes;
200                                          /* set up object functions */
201          ofun[OBJ_FACE].funp = o_face;
# Line 159 | Line 205 | init()                         /* start rtrace and set up buffers */
205   #ifdef SIGPIPE /* not present on Windows */
206          signal(SIGPIPE, quit);
207   #endif
208 <                                        /* start rtrace process */
209 <        errno = 0;
210 <        maxbytes = open_process(&(rt.pd), rtargv);
211 <        if (maxbytes == 0) {
212 <                eputs(rtargv[0]);
213 <                eputs(": command not found\n");
214 <                exit(1);
208 >        rtp = &rt0;                     /* start rtrace process(es) */
209 >        for (i = 0; i++ < np; ) {
210 >                errno = 0;
211 >                maxbytes = open_process(&rtp->pd, rtargv);
212 >                if (maxbytes == 0) {
213 >                        eputs(rtargv[0]);
214 >                        eputs(": command not found\n");
215 >                        exit(1);
216 >                }
217 >                if (maxbytes < 0)
218 >                        error(SYSTEM, "cannot start rtrace process");
219 >                if (!i && np > 1)
220 >                        sleep(2);       /* wait for persist file */
221 >                rtp->bsiz = maxbytes/(6*sizeof(float));
222 >                rtp->buf = (float *)malloc(6*sizeof(float)*rtp->bsiz--);
223 >                rtp->dest = (float **)calloc(rtp->bsiz, sizeof(float *));
224 >                if (rtp->buf == NULL || rtp->dest == NULL)
225 >                        error(SYSTEM, "out of memory in init");
226 >                rtp->nrays = 0;
227 >                if (i == np)            /* last process? */
228 >                        break;
229 >                rtp->next = (struct rtproc *)malloc(sizeof(struct rtproc));
230 >                if (rtp->next == NULL)
231 >                        error(SYSTEM, "out of memory in init");
232 >                rtp = rtp->next;
233          }
234 <        if (maxbytes < 0)
171 <                error(SYSTEM, "cannot start rtrace process");
172 <        rt.bsiz = maxbytes/(6*sizeof(float));
173 <        rt.buf = (float *)malloc(6*sizeof(float)*rt.bsiz--);
174 <        rt.dest = (float **)malloc(sizeof(float *)*rt.bsiz);
175 <        if (rt.buf == NULL || rt.dest == NULL)
176 <                error(SYSTEM, "out of memory in init");
177 <        rt.nrays = 0;
234 >        rtp->next = NULL;
235                                          /* set up urand */
236          initurand(16384);
237   }
238  
239  
240   void
241 < eputs(s)                                /* put string to stderr */
242 < register char  *s;
241 > eputs(                          /* put string to stderr */
242 >        register char  *s
243 > )
244   {
245          static int  midline = 0;
246  
# Line 205 | Line 263 | char  *s;
263   }
264  
265  
266 < filter(infp, name)              /* process stream */
267 < register FILE   *infp;
268 < char    *name;
266 > void
267 > filter(         /* process stream */
268 >        register FILE   *infp,
269 >        char    *name
270 > )
271   {
272          char    buf[512];
273          FILE    *pfp;
# Line 237 | Line 297 | char   *name;
297   }
298  
299  
300 < xoptions(s, nm)                 /* process options in string s */
301 < char    *s;
302 < char    *nm;
300 > void
301 > xoptions(                       /* process options in string s */
302 >        char    *s,
303 >        char    *nm
304 > )
305   {
306          extern FILE     *freopen();
307          char    buf[64];
# Line 398 | Line 460 | char   *nm;
460          printf("# %s", s+2);
461   }
462  
463 <
464 < printopts()                     /* print out option default values */
463 > void
464 > printopts(void)                 /* print out option default values */
465   {
466          printf("m=%-15s\t\t# material name\n", thisillum.matname);
467          printf("f=%-15s\t\t# data file name\n", thisillum.datafile);
# Line 420 | Line 482 | printopts()                    /* print out option default values */
482   }
483  
484  
485 < printhead(ac, av)                       /* print out header */
486 < register int  ac;
487 < register char  **av;
485 > void
486 > printhead(                      /* print out header */
487 >        register int  ac,
488 >        register char  **av
489 > )
490   {
491          putchar('#');
492          while (ac-- > 0) {
# Line 433 | Line 497 | register char  **av;
497   }
498  
499  
500 < xobject(fp, nm)                         /* translate an object from fp */
501 < FILE  *fp;
502 < char  *nm;
500 > void
501 > xobject(                                /* translate an object from fp */
502 >        FILE  *fp,
503 >        char  *nm
504 > )
505   {
506          OBJREC  thisobj;
507          char  str[MAXSTR];
# Line 486 | Line 552 | char  *nm;
552          checkhead();
553                                                  /* process object */
554          if (doit)
555 <                (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt, nm);
555 >                (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt0, nm);
556          else
557                  printobj(thisillum.altmat, &thisobj);
558                                                  /* free arguments */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines