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

Comparing ray/src/hd/rholo.c (file contents):
Revision 3.10 by gregl, Wed Nov 19 17:04:56 1997 UTC vs.
Revision 3.22 by gregl, Fri Dec 12 18:23:08 1997 UTC

# Line 9 | Line 9 | static char SCCSid[] = "$SunId$ SGI";
9   */
10  
11   #include "rholo.h"
12 < #include "paths.h"
12 > #include "random.h"
13 > #include <signal.h>
14   #include <sys/types.h>
15 + #include <sys/stat.h>
16  
17                          /* the following must be consistent with rholo.h */
18   int     NVARS = NRHVARS;                /* total number of variables */
# Line 19 | Line 21 | VARIABLE       vv[] = RHVINIT;         /* variable-value pairs */
21  
22   char    *progname;              /* our program name */
23   char    *hdkfile;               /* holodeck file name */
24 < char    froot[MAXPATH];         /* root file name */
24 > char    froot[256];             /* root file name */
25  
26   int     nowarn = 0;             /* turn warnings off? */
27  
26 double  expval = 1.;            /* global exposure value */
27
28   int     ncprocs = 0;            /* desired number of compute processes */
29  
30   char    *outdev = NULL;         /* output device name */
31  
32 + int     readinp = 0;            /* read commands from stdin */
33 +
34 + int     force = 0;              /* allow overwrite of holodeck */
35 +
36   time_t  starttime;              /* time we got started */
37   time_t  endtime;                /* time we should end by */
38   time_t  reporttime;             /* time for next report */
# Line 38 | Line 42 | long   maxdisk;                /* maximum file space (bytes) */
42   int     rtargc = 1;             /* rtrace command */
43   char    *rtargv[128] = {"rtrace", NULL};
44  
45 + int     orig_mode = -1;         /* original file mode (-1 if unchanged) */
46 +
47   long    nraysdone = 0L;         /* number of rays done */
48   long    npacksdone = 0L;        /* number of packets done */
49  
50   PACKET  *freepacks;             /* available packets */
51  
52 + char  *sigerr[NSIG];            /* signal error messages */
53 +
54   extern time_t   time();
55  
56  
# Line 52 | Line 60 | char   *argv[];
60   {
61          HDGRID  hdg;
62          int     i;
55        int     force = 0;
63                                                  /* mark start time */
64          starttime = time(NULL);
65 +        initurand(16384);                       /* initialize urand */
66          progname = argv[0];                     /* get arguments */
67          for (i = 1; i < argc && argv[i][0] == '-'; i++)
68                  switch (argv[i][1]) {
# Line 64 | Line 72 | char   *argv[];
72                  case 'f':                       /* force overwrite */
73                          force++;
74                          break;
75 +                case 'i':                       /* read input from stdin */
76 +                        readinp++;
77 +                        break;
78                  case 'n':                       /* compute processes */
79                          if (i >= argc-2)
80                                  goto userr;
# Line 95 | Line 106 | char   *argv[];
106                                                          /* check settings */
107                  checkvalues();
108                                                          /* load RIF if any */
109 <                if (vdef(RIF))
99 <                        getradfile(vval(RIF));
109 >                getradfile();
110                                                          /* set defaults */
111                  setdefaults(&hdg);
112                                                          /* holodeck exists? */
# Line 107 | Line 117 | char   *argv[];
117                  creatholo(&hdg);
118          } else {                                /* else load holodeck */
119                  loadholo();
120 <                if (vdef(RIF))                          /* load RIF if any */
121 <                        getradfile(vval(RIF));
120 >                                                        /* check settings */
121 >                checkvalues();
122 >                                                        /* load RIF if any */
123 >                getradfile();
124 >                                                        /* set defaults */
125 >                setdefaults(NULL);
126          }
127                                                  /* initialize */
128          initrholo();
129 <                                                /* run */
129 >                                                /* main loop */
130          while (rholo())
131                  ;
132                                                  /* done */
# Line 125 | Line 139 | userr:
139   }
140  
141  
142 + onsig(signo)                            /* fatal signal */
143 + int  signo;
144 + {
145 +        static int  gotsig = 0;
146 +
147 +        if (gotsig++)                   /* two signals and we're gone! */
148 +                _exit(signo);
149 +
150 +        alarm(30);                      /* allow 30 seconds to clean up */
151 +        signal(SIGALRM, SIG_DFL);       /* make certain we do die */
152 +        eputs("signal - ");
153 +        eputs(sigerr[signo]);
154 +        eputs("\n");
155 +        quit(3);
156 + }
157 +
158 +
159 + sigdie(signo, msg)                      /* set fatal signal */
160 + int  signo;
161 + char  *msg;
162 + {
163 +        if (signal(signo, onsig) == SIG_IGN)
164 +                signal(signo, SIG_IGN);
165 +        sigerr[signo] = msg;
166 + }
167 +
168 +
169 + int
170 + resfmode(fd, mod)               /* restrict open file access mode */
171 + int     fd, mod;
172 + {
173 +        struct stat     stbuf;
174 +                                        /* get original mode */
175 +        if (fstat(fd, &stbuf) < 0)
176 +                error(SYSTEM, "cannot stat open holodeck file");
177 +        mod &= stbuf.st_mode;           /* always more restrictive */
178 +        if (mod == stbuf.st_mode)
179 +                return(-1);             /* already set */
180 +                                        /* else change it */
181 +        if (fchmod(fd, mod) < 0) {
182 +                error(WARNING, "cannot change holodeck file access mode");
183 +                return(-1);
184 +        }
185 +        return(stbuf.st_mode);          /* return original mode */
186 + }
187 +
188 +
189   initrholo()                     /* get our holodeck running */
190   {
191          extern int      global_packet();
192          register int    i;
193 +                                                /* close holodeck on exec() */
194 +        fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC);
195  
196          if (outdev != NULL)                     /* open output device */
197                  disp_open(outdev);
# Line 159 | Line 222 | initrholo()                    /* get our holodeck running */
222          if (ncprocs > 0) {
223                  i = start_rtrace();
224                  if (i < 1)
225 <                        error(USER, "cannot start rtrace process");
225 >                        error(USER, "cannot start rtrace process(es)");
226                  if (vdef(REPORT)) {             /* make first report */
227                          printargs(rtargc, rtargv, stderr);
228                          report(0);
# Line 184 | Line 247 | initrholo()                    /* get our holodeck running */
247                          freepacks[i].next = &freepacks[i+1];
248                  }
249          }
250 +                                        /* set up signal handling */
251 +        sigdie(SIGINT, "Interrupt");
252 +        sigdie(SIGHUP, "Hangup");
253 +        sigdie(SIGTERM, "Terminate");
254 +        sigdie(SIGPIPE, "Broken pipe");
255 +        sigdie(SIGALRM, "Alarm clock");
256 + #ifdef  SIGXCPU
257 +        sigdie(SIGXCPU, "CPU limit exceeded");
258 +        sigdie(SIGXFSZ, "File size exceeded");
259 + #endif
260 +                                                /* protect holodeck file */
261 +        orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
262          return;
263   memerr:
264          error(SYSTEM, "out of memory in initrholo");
# Line 202 | Line 277 | rholo()                                /* holodeck main loop */
277                  if (!disp_check(idle))
278                          return(0);
279                                          /* display only? */
280 <        if (ncprocs <= 0)
281 <                return(1);
280 >        if (nprocs <= 0)
281 >                return(outdev != NULL);
282                                          /* check file size */
283          if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
284                  error(WARNING, "file limit exceeded");
285 <                return(0);
285 >                done_rtrace();
286 >                idle = 1;
287 >                return(1);      /* comes back */
288          }
289                                          /* check time */
290          if (endtime > 0 || reporttime > 0)
291                  t = time(NULL);
292          if (endtime > 0 && t >= endtime) {
293                  error(WARNING, "time limit exceeded");
294 <                return(0);
294 >                done_rtrace();
295 >                idle = 1;
296 >                return(1);      /* comes back */
297          }
298          if (reporttime > 0 && t >= reporttime)
299                  report(t);
# Line 223 | Line 302 | rholo()                                /* holodeck main loop */
302                  p = freepacks; freepacks = p->next; p->next = NULL;
303                  if (!next_packet(p)) {
304                          p->next = freepacks; freepacks = p;
305 +                        idle = 1;
306                          break;
307                  }
308                  if (pl == NULL) pl = p;
309                  else plend->next = p;
310                  plend = p;
311          }
312 <        idle = pl == NULL && freepacks != NULL;
233 <                                        /* are we out of stuff to do? */
234 <        if (idle && outdev == NULL)
235 <                return(0);
236 <                                        /* else process packets */
312 >                                        /* process packets */
313          done_packets(do_packets(pl));
314          return(1);                      /* and continue */
315   }
# Line 242 | Line 318 | rholo()                                /* holodeck main loop */
318   report(t)                       /* report progress so far */
319   time_t  t;
320   {
321 <        if (t == 0)
321 >        static time_t   seconds2go = 1000000;
322 >
323 >        if (t == 0L)
324                  t = time(NULL);
325 <        fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n",
326 <                        progname, npacksdone, nraysdone, (t-starttime)/3600.);
327 <        fflush(stderr);
328 <        if (vdef(REPORT))
329 <                reporttime = t + (time_t)(vflt(REPORT)*60.+.5);
325 >        sprintf(errmsg, "%ld packets (%ld rays) done after %.2f hours\n",
326 >                        npacksdone, nraysdone, (t-starttime)/3600.);
327 >        eputs(errmsg);
328 >        if (seconds2go == 1000000)
329 >                seconds2go = vdef(REPORT) ? (long)(vflt(REPORT)*60. + .5) : 0L;
330 >        if (seconds2go)
331 >                reporttime = t + seconds2go;
332   }
333  
334  
# Line 268 | Line 348 | register HDGRID        *gp;
348                  sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
349                  error(WARNING, errmsg);
350          }
271        if (sscanf(vval(SECTION),
272                        "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
273                        &gp->orig[0], &gp->orig[1], &gp->orig[2],
274                        &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
275                        &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
276                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2]) != 12)
277                badvalue(SECTION);
278        maxlen = 0.;
279        for (i = 0; i < 3; i++)
280                if ((len[i] = VLEN(gp->xv[i])) > maxlen)
281                        maxlen = len[i];
282        if (!vdef(GRID)) {
283                sprintf(buf, "%.4f", maxlen/8.);
284                vval(GRID) = savqstr(buf);
285                vdef(GRID)++;
286        }
287        if ((d = vflt(GRID)) <= FTINY)
288                badvalue(GRID);
289        for (i = 0; i < 3; i++)
290                gp->grid[i] = len[i]/d + (1.-FTINY);
291        if (!vdef(EXPOSURE)) {
292                sprintf(errmsg, "%s must be defined", vnam(EXPOSURE));
293                error(USER, errmsg);
294        }
295        expval = vval(EXPOSURE)[0] == '-' || vval(EXPOSURE)[0] == '+' ?
296                        pow(2., vflt(EXPOSURE)) : vflt(EXPOSURE);
351          if (!vdef(OCTREE)) {
352                  if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
353                          error(SYSTEM, "out of memory");
# Line 304 | Line 358 | register HDGRID        *gp;
358                  vval(OBSTRUCTIONS) = "T";
359                  vdef(OBSTRUCTIONS)++;
360          }
361 +        if (!vdef(VDIST)) {
362 +                vval(VDIST) = "F";
363 +                vdef(VDIST)++;
364 +        }
365          if (!vdef(OCCUPANCY)) {
366                  vval(OCCUPANCY) = "U";
367                  vdef(OCCUPANCY)++;
# Line 311 | Line 369 | register HDGRID        *gp;
369                                  /* append rendering options */
370          if (vdef(RENDER))
371                  rtargc += wordstring(rtargv+rtargc, vval(RENDER));
372 +        
373 +        if (gp == NULL)         /* already initialized? */
374 +                return;
375 +                                /* set grid parameters */
376 +        gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
377 +        if (sscanf(vval(SECTION),
378 +                "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
379 +                        &gp->orig[0], &gp->orig[1], &gp->orig[2],
380 +                        &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
381 +                        &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
382 +                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
383 +                        &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
384 +                badvalue(SECTION);
385 +        maxlen = 0.;
386 +        for (i = 0; i < 3; i++)
387 +                if ((len[i] = VLEN(gp->xv[i])) > maxlen)
388 +                        maxlen = len[i];
389 +        if (!vdef(GRID)) {
390 +                sprintf(buf, "%.4f", maxlen/8.);
391 +                vval(GRID) = savqstr(buf);
392 +                vdef(GRID)++;
393 +        }
394 +        if ((d = vflt(GRID)) <= FTINY)
395 +                badvalue(GRID);
396 +        for (i = 0; i < 3; i++)
397 +                if (gp->grid[i] <= 0)
398 +                        gp->grid[i] = len[i]/d + (1.-FTINY);
399   }
400  
401  
402   creatholo(gp)                   /* create a holodeck output file */
403   HDGRID  *gp;
404   {
405 +        extern char     VersionID[];
406          long    endloc = 0;
407          int     fd;
408          FILE    *fp;
# Line 327 | Line 413 | HDGRID *gp;
413          }
414                                          /* write information header */
415          newheader("RADIANCE", fp);
416 +        fprintf(fp, "SOFTWARE= %s\n", VersionID);
417          printvars(fp);
418          fputformat(HOLOFMT, fp);
419          fputc('\n', fp);
# Line 396 | Line 483 | loadholo()                     /* start loading a holodeck from fname */
483   done_packets(pl)                /* handle finished packets */
484   PACKET  *pl;
485   {
486 <        static int      nunflushed = 0;
486 >        static int      n2flush = 0;
487          register PACKET *p;
488  
489          while (pl != NULL) {
# Line 407 | Line 494 | PACKET *pl;
494                                  p->nr*sizeof(RAYVAL));
495                          if (outdev != NULL)     /* display it */
496                                  disp_packet((PACKHEAD *)p);
497 <                        else
498 <                                nunflushed += p->nr;
497 >                        if (hdcachesize <= 0)   /* manual flushing */
498 >                                n2flush += p->nr;
499                          nraysdone += p->nr;
500                          npacksdone++;
501                  }
# Line 416 | Line 503 | PACKET *pl;
503                  p->next = freepacks;
504                  freepacks = p;
505          }
506 <        if (nunflushed >= 256*RPACKSIZ) {
506 >        if (n2flush > 512*RPACKSIZ*nprocs) {
507                  hdflush(NULL);                  /* flush holodeck buffers */
508 <                nunflushed = 0;
508 >                n2flush = 0;
509          }
510   }
511  
512  
426 getradfile(rfargs)              /* run rad and get needed variables */
427 char    *rfargs;
428 {
429        static short    mvar[] = {OCTREE,EXPOSURE,-1};
430        static char     tf1[] = TEMPLATE;
431        char    tf2[64];
432        char    combuf[256];
433        char    *pippt;
434        register int    i;
435        register char   *cp;
436                                        /* create rad command */
437        mktemp(tf1);
438        sprintf(tf2, "%s.rif", tf1);
439        sprintf(combuf,
440                "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
441                        rfargs, tf1);
442        cp = combuf;
443        while (*cp){
444                if (*cp == '|') pippt = cp;
445                cp++;
446        }                               /* match unset variables */
447        for (i = 0; mvar[i] >= 0; i++)
448                if (!vdef(mvar[i])) {
449                        *cp++ = '|';
450                        strcpy(cp, vnam(mvar[i]));
451                        while (*cp) cp++;
452                        pippt = NULL;
453                }
454        if (pippt != NULL)
455                strcpy(pippt, "> /dev/null");   /* nothing to match */
456        else
457                sprintf(cp, ")[ \t]*=' > %s", tf2);
458        if (system(combuf)) {
459                error(SYSTEM, "cannot execute rad command");
460                unlink(tf2);                    /* clean up */
461                unlink(tf1);
462                quit(1);
463        }
464        if (pippt == NULL) {
465                loadvars(tf2);                  /* load variables */
466                unlink(tf2);
467        }
468        rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */
469        unlink(tf1);                    /* clean up */
470 }
471
472
513   rootname(rn, fn)                /* remove tail from end of fn */
514   register char   *rn, *fn;
515   {
516          char    *tp, *dp;
517  
518          for (tp = NULL, dp = rn; *rn = *fn++; rn++)
519 <                if (ISDIRSEP(*rn))
519 >                if (*rn == '/')
520                          dp = rn;
521                  else if (*rn == '.')
522                          tp = rn;
# Line 525 | Line 565 | int    ec;
565   {
566          int     status = 0;
567  
528        if (outdev != NULL)             /* close display */
529                disp_close();
568          if (hdlist[0] != NULL) {        /* flush holodeck */
569 <                if (ncprocs > 0) {
570 <                        done_packets(flush_queue());
571 <                        status = end_rtrace();  /* close rtrace */
572 <                        hdflush(NULL);
573 <                        if (vdef(REPORT)) {
574 <                                long    fsiz, fuse;
575 <                                report(0);
576 <                                fsiz = hdfilen(hdlist[0]->fd);
539 <                                fuse = hdfiluse(hdlist[0]->fd, 1);
540 <                                fprintf(stderr,
569 >                if (nprocs > 0)
570 >                        status = done_rtrace();
571 >                hdflush(NULL);
572 >                if (ncprocs > 0 && vdef(REPORT)) {
573 >                        long    fsiz, fuse;
574 >                        fsiz = hdfilen(hdlist[0]->fd);
575 >                        fuse = hdfiluse(hdlist[0]->fd, 1);
576 >                        fprintf(stderr,
577                          "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
578 <                                                hdkfile, fsiz/(1024.*1024.),
579 <                                                100.*(fsiz-fuse)/fsiz);
580 <                        }
545 <                } else
546 <                        hdflush(NULL);
578 >                                        hdkfile, fsiz/(1024.*1024.),
579 >                                        100.*(fsiz-fuse)/fsiz);
580 >                }
581          }
582 +        if (orig_mode >= 0)             /* reset holodeck access mode */
583 +                fchmod(hdlist[0]->fd, orig_mode);
584 +        if (outdev != NULL)             /* close display */
585 +                disp_close();
586          exit(ec ? ec : status);         /* exit */
587   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines