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.19 by schorsch, Sun Nov 16 10:29:38 2003 UTC vs.
Revision 2.23 by greg, Thu Oct 28 00:50:47 2004 UTC

# Line 9 | Line 9 | static const char RCSid[] = "$Id$";
9   #include  <ctype.h>
10  
11   #include  "platform.h"
12 + #include  "rtprocess.h"
13   #include  "mkillum.h"
14   #include  "random.h"
15  
# Line 31 | Line 32 | int  rtargc = 14;
32   char  *myrtopts[] = { "-I-", "-i-", "-ld-", "-ov", "-h-",
33                          "-fff", "-y", "0", NULL };
34  
35 < struct rtproc   rt;             /* our rtrace process */
35 > struct rtproc   rt0;            /* head of rtrace process list */
36  
37   struct illum_args  thisillum = {        /* our illum and default values */
38                  0,
# Line 49 | Line 50 | int    matselect = S_ALL;      /* selection criterion */
50  
51   FUN     ofun[NUMOTYPE] = INIT_OTYPE;    /* object types */
52  
53 + char    persistfn[] = "pfXXXXXX";       /* persist file name */
54 +
55   int     gargc;                  /* global argc */
56   char    **gargv;                /* global argv */
57   #define  progname       gargv[0]
# Line 58 | Line 61 | int    doneheader = 0;         /* printed header yet? */
61  
62   int     warnings = 1;           /* print warnings? */
63  
64 < void init(void);
64 > int done_rprocs(struct rtproc *rtp);
65 > void init(int np);
66   void filter(register FILE       *infp, char     *name);
67   void xoptions(char      *s, char        *nm);
68   void printopts(void);
# Line 72 | Line 76 | main(          /* compute illum distributions using rtrace */
76          char    *argv[]
77   )
78   {
79 +        int     nprocs = 1;
80          char    *rtpath;
81          FILE    *fp;
82          register int    i;
83                                  /* set global arguments */
84          gargv = argv;
85 +                                /* check for -n option */
86 +        if (!strcmp(argv[1], "-n")) {
87 +                nprocs = atoi(argv[2]);
88 +                if (nprocs <= 0)
89 +                        error(USER, "illegal number of processes");
90 +                i = 3;
91 +        } else
92 +                i = 1;
93                                  /* set up rtrace command */
94 <        for (i = 1; i < argc; i++) {
94 >        for ( ; i < argc; i++) {
95                  if (argv[i][0] == '<' && argv[i][1] == '\0')
96                          break;
97                  rtargv[rtargc++] = argv[i];
# Line 102 | Line 115 | main(          /* compute illum distributions using rtrace */
115                          }
116          }
117          gargc = i;
118 <        rtargc--;
118 >        if (!strcmp(rtargv[--rtargc], "-defaults"))
119 >                nprocs = 0;
120 >        if (nprocs > 1) {       /* add persist file if parallel invocation */
121 >                rtargv[rtargc++] = "-PP";
122 >                rtargv[rtargc++] = mktemp(persistfn);
123 >        }
124 >                                /* add "mandatory" rtrace options */
125          for (i = 0; myrtopts[i] != NULL; i++)
126                  rtargv[rtargc++] = myrtopts[i];
127 +                                /* finally, put back final argument */
128          rtargv[rtargc++] = argv[gargc-1];
129          rtargv[rtargc] = NULL;
130 <                                /* just asking for defaults? */
111 <        if (!strcmp(argv[gargc-1], "-defaults")) {
130 >        if (!nprocs) {          /* just asking for defaults? */
131                  printopts(); fflush(stdout);
132                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
133                  if (rtpath == NULL) {
# Line 117 | Line 136 | main(          /* compute illum distributions using rtrace */
136                          exit(1);
137                  }
138                  execv(rtpath, rtargv);
139 <                perror(rtpath);
139 >                perror(rtpath); /* execv() should not return */
140                  exit(1);
141          }
142          if (gargc < 2 || argv[gargc-1][0] == '-')
143                  error(USER, "missing octree argument");
144                                  /* else initialize and run our calculation */
145 <        init();
145 >        init(nprocs);
146          if (gargc+1 < argc)
147                  for (i = gargc+1; i < argc; i++) {
148                          if ((fp = fopen(argv[i], "r")) == NULL) {
# Line 136 | Line 155 | main(          /* compute illum distributions using rtrace */
155                  }
156          else
157                  filter(stdin, "standard input");
158 <        return 0;
158 >        quit(0);
159   }
160  
161 + static void
162 + killpersist(void)                       /* kill persistent rtrace process */
163 + {
164 +        FILE    *fp = fopen(persistfn, "r");
165 +        int     pid;
166  
167 +        if (fp == NULL)
168 +                return;
169 +        if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0)
170 +                unlink(persistfn);
171 +        fclose(fp);
172 + }
173 +
174 + int
175 + done_rprocs(struct rtproc *rtp)
176 + {
177 +        int     st0, st1 = 0;
178 +
179 +        if (rtp->next != NULL) {        /* close last opened first! */
180 +                st1 = done_rprocs(rtp->next);
181 +                free((void *)rtp->next);
182 +                rtp->next = NULL;
183 +        }
184 +        st0 = close_process(&rtp->pd);
185 +        if (st0 < 0)
186 +                error(WARNING, "unknown return status from rtrace process");
187 +        else if (st0 > 0)
188 +                return(st0);
189 +        return(st1);
190 + }
191 +
192   void
193 < quit(status)                    /* exit with status */
145 < int  status;
193 > quit(int status)                        /* exit with status */
194   {
195          int     rtstat;
196  
197 <        rtstat = close_process(&(rt.pd));
198 <        if (status == 0) {
199 <                if (rtstat < 0)
200 <                        error(WARNING,
201 <                        "unknown return status from rtrace process");
202 <                else
155 <                        status = rtstat;
156 <        }
197 >        if (rt0.next != NULL)           /* terminate persistent rtrace */
198 >                killpersist();
199 >                                        /* clean up rtrace process(es) */
200 >        rtstat = done_rprocs(&rt0);
201 >        if (status == 0)
202 >                status = rtstat;
203          exit(status);
204   }
205  
206   void
207 < init(void)                              /* start rtrace and set up buffers */
207 > init(int np)                            /* start rtrace and set up buffers */
208   {
209 <        extern int  o_face(), o_sphere(), o_ring();
209 >        struct rtproc   *rtp;
210 >        int     i;
211          int     maxbytes;
212                                          /* set up object functions */
213          ofun[OBJ_FACE].funp = o_face;
# Line 170 | Line 217 | init(void)                             /* start rtrace and set up buffers */
217   #ifdef SIGPIPE /* not present on Windows */
218          signal(SIGPIPE, quit);
219   #endif
220 <                                        /* start rtrace process */
221 <        errno = 0;
222 <        maxbytes = open_process(&(rt.pd), rtargv);
223 <        if (maxbytes == 0) {
224 <                eputs(rtargv[0]);
225 <                eputs(": command not found\n");
226 <                exit(1);
220 >        rtp = &rt0;                     /* start rtrace process(es) */
221 >        for (i = 0; i++ < np; ) {
222 >                errno = 0;
223 >                maxbytes = open_process(&rtp->pd, rtargv);
224 >                if (maxbytes == 0) {
225 >                        eputs(rtargv[0]);
226 >                        eputs(": command not found\n");
227 >                        exit(1);
228 >                }
229 >                if (maxbytes < 0)
230 >                        error(SYSTEM, "cannot start rtrace process");
231 >                if (!i && np > 1)
232 >                        sleep(2);       /* wait for persist file */
233 >                rtp->bsiz = maxbytes/(6*sizeof(float));
234 >                rtp->buf = (float *)malloc(6*sizeof(float)*rtp->bsiz--);
235 >                rtp->dest = (float **)calloc(rtp->bsiz, sizeof(float *));
236 >                if (rtp->buf == NULL || rtp->dest == NULL)
237 >                        error(SYSTEM, "out of memory in init");
238 >                rtp->nrays = 0;
239 >                if (i == np)            /* last process? */
240 >                        break;
241 >                rtp->next = (struct rtproc *)malloc(sizeof(struct rtproc));
242 >                if (rtp->next == NULL)
243 >                        error(SYSTEM, "out of memory in init");
244 >                rtp = rtp->next;
245          }
246 <        if (maxbytes < 0)
182 <                error(SYSTEM, "cannot start rtrace process");
183 <        rt.bsiz = maxbytes/(6*sizeof(float));
184 <        rt.buf = (float *)malloc(6*sizeof(float)*rt.bsiz--);
185 <        rt.dest = (float **)malloc(sizeof(float *)*rt.bsiz);
186 <        if (rt.buf == NULL || rt.dest == NULL)
187 <                error(SYSTEM, "out of memory in init");
188 <        rt.nrays = 0;
246 >        rtp->next = NULL;
247                                          /* set up urand */
248          initurand(16384);
249   }
# Line 506 | Line 564 | xobject(                               /* translate an object from fp */
564          checkhead();
565                                                  /* process object */
566          if (doit)
567 <                (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt, nm);
567 >                (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt0, nm);
568          else
569                  printobj(thisillum.altmat, &thisobj);
570                                                  /* free arguments */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines