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.8 by gregl, Mon Nov 10 18:05:24 1997 UTC vs.
Revision 3.21 by gregl, Fri Dec 12 18:12:40 1997 UTC

# Line 9 | Line 9 | static char SCCSid[] = "$SunId$ SGI";
9   */
10  
11   #include "rholo.h"
12 + #include "random.h"
13   #include "paths.h"
14 + #include <signal.h>
15   #include <sys/types.h>
16 + #include <sys/stat.h>
17  
18                          /* the following must be consistent with rholo.h */
19   int     NVARS = NRHVARS;                /* total number of variables */
# Line 23 | Line 26 | char   froot[MAXPATH];         /* root file name */
26  
27   int     nowarn = 0;             /* turn warnings off? */
28  
26 double  expval = 1.;            /* global exposure value */
27
29   int     ncprocs = 0;            /* desired number of compute processes */
30  
31   char    *outdev = NULL;         /* output device name */
32  
33 + int     readinp = 0;            /* read commands from stdin */
34 +
35 + int     force = 0;              /* allow overwrite of holodeck */
36 +
37   time_t  starttime;              /* time we got started */
38   time_t  endtime;                /* time we should end by */
39   time_t  reporttime;             /* time for next report */
# Line 38 | Line 43 | long   maxdisk;                /* maximum file space (bytes) */
43   int     rtargc = 1;             /* rtrace command */
44   char    *rtargv[128] = {"rtrace", NULL};
45  
46 + int     orig_mode = -1;         /* original file mode (-1 if unchanged) */
47 +
48   long    nraysdone = 0L;         /* number of rays done */
49   long    npacksdone = 0L;        /* number of packets done */
50  
51   PACKET  *freepacks;             /* available packets */
52  
53 + char  *sigerr[NSIG];            /* signal error messages */
54 +
55   extern time_t   time();
56  
57  
# Line 52 | Line 61 | char   *argv[];
61   {
62          HDGRID  hdg;
63          int     i;
55        int     force = 0;
64                                                  /* mark start time */
65          starttime = time(NULL);
66 +        initurand(16384);                       /* initialize urand */
67          progname = argv[0];                     /* get arguments */
68          for (i = 1; i < argc && argv[i][0] == '-'; i++)
69                  switch (argv[i][1]) {
# Line 64 | Line 73 | char   *argv[];
73                  case 'f':                       /* force overwrite */
74                          force++;
75                          break;
76 +                case 'i':                       /* read input from stdin */
77 +                        readinp++;
78 +                        break;
79                  case 'n':                       /* compute processes */
80                          if (i >= argc-2)
81                                  goto userr;
# Line 107 | Line 119 | char   *argv[];
119                  creatholo(&hdg);
120          } else {                                /* else load holodeck */
121                  loadholo();
122 <                if (vdef(RIF))                          /* load RIF if any */
122 >                                                        /* check settings */
123 >                checkvalues();
124 >                                                        /* load RIF if any */
125 >                if (vdef(RIF))
126                          getradfile(vval(RIF));
127 +                                                        /* set defaults */
128 +                setdefaults(NULL);
129          }
130                                                  /* initialize */
131          initrholo();
132 <                                                /* run */
132 >                                                /* main loop */
133          while (rholo())
134                  ;
135                                                  /* done */
# Line 125 | Line 142 | userr:
142   }
143  
144  
145 + onsig(signo)                            /* fatal signal */
146 + int  signo;
147 + {
148 +        static int  gotsig = 0;
149 +
150 +        if (gotsig++)                   /* two signals and we're gone! */
151 +                _exit(signo);
152 +
153 +        alarm(30);                      /* allow 30 seconds to clean up */
154 +        signal(SIGALRM, SIG_DFL);       /* make certain we do die */
155 +        eputs("signal - ");
156 +        eputs(sigerr[signo]);
157 +        eputs("\n");
158 +        quit(3);
159 + }
160 +
161 +
162 + sigdie(signo, msg)                      /* set fatal signal */
163 + int  signo;
164 + char  *msg;
165 + {
166 +        if (signal(signo, onsig) == SIG_IGN)
167 +                signal(signo, SIG_IGN);
168 +        sigerr[signo] = msg;
169 + }
170 +
171 +
172 + int
173 + resfmode(fd, mod)               /* restrict open file access mode */
174 + int     fd, mod;
175 + {
176 +        struct stat     stbuf;
177 +                                        /* get original mode */
178 +        if (fstat(fd, &stbuf) < 0)
179 +                error(SYSTEM, "cannot stat open holodeck file");
180 +        mod &= stbuf.st_mode;           /* always more restrictive */
181 +        if (mod == stbuf.st_mode)
182 +                return(-1);             /* already set */
183 +                                        /* else change it */
184 +        if (fchmod(fd, mod) < 0) {
185 +                error(WARNING, "cannot change holodeck file access mode");
186 +                return(-1);
187 +        }
188 +        return(stbuf.st_mode);          /* return original mode */
189 + }
190 +
191 +
192   initrholo()                     /* get our holodeck running */
193   {
194          extern int      global_packet();
195          register int    i;
196 +                                                /* close holodeck on exec() */
197 +        fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC);
198  
199          if (outdev != NULL)                     /* open output device */
200                  disp_open(outdev);
# Line 138 | Line 204 | initrholo()                    /* get our holodeck running */
204          if (!vdef(DISKSPACE))
205                  maxdisk = 0;
206          else
207 <                maxdisk = 1024.*1024.*vflt(DISKSPACE));
207 >                maxdisk = 1024.*1024.*vflt(DISKSPACE);
208                                                  /* record end time */
209          if (!vdef(TIME) || vflt(TIME) <= FTINY)
210                  endtime = 0;
# Line 159 | Line 225 | initrholo()                    /* get our holodeck running */
225          if (ncprocs > 0) {
226                  i = start_rtrace();
227                  if (i < 1)
228 <                        error(USER, "cannot start rtrace process");
228 >                        error(USER, "cannot start rtrace process(es)");
229                  if (vdef(REPORT)) {             /* make first report */
230                          printargs(rtargc, rtargv, stderr);
231                          report(0);
# Line 184 | Line 250 | initrholo()                    /* get our holodeck running */
250                          freepacks[i].next = &freepacks[i+1];
251                  }
252          }
253 +                                        /* set up signal handling */
254 +        sigdie(SIGINT, "Interrupt");
255 +        sigdie(SIGHUP, "Hangup");
256 +        sigdie(SIGTERM, "Terminate");
257 +        sigdie(SIGPIPE, "Broken pipe");
258 +        sigdie(SIGALRM, "Alarm clock");
259 + #ifdef  SIGXCPU
260 +        sigdie(SIGXCPU, "CPU limit exceeded");
261 +        sigdie(SIGXFSZ, "File size exceeded");
262 + #endif
263 +                                                /* protect holodeck file */
264 +        orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
265          return;
266   memerr:
267          error(SYSTEM, "out of memory in initrholo");
# Line 202 | Line 280 | rholo()                                /* holodeck main loop */
280                  if (!disp_check(idle))
281                          return(0);
282                                          /* display only? */
283 <        if (ncprocs <= 0)
284 <                return(1);
283 >        if (nprocs <= 0)
284 >                return(outdev != NULL);
285                                          /* check file size */
286 <        if (maxdisk > 0 && hdfiluse(hdlist[0]->fd,0)+hdmemuse(0) >= maxdisk) {
286 >        if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
287                  error(WARNING, "file limit exceeded");
288 <                return(0);
288 >                done_rtrace();
289 >                idle = 1;
290 >                return(1);      /* comes back */
291          }
292                                          /* check time */
293          if (endtime > 0 || reporttime > 0)
294                  t = time(NULL);
295          if (endtime > 0 && t >= endtime) {
296                  error(WARNING, "time limit exceeded");
297 <                return(0);
297 >                done_rtrace();
298 >                idle = 1;
299 >                return(1);      /* comes back */
300          }
301          if (reporttime > 0 && t >= reporttime)
302                  report(t);
# Line 223 | Line 305 | rholo()                                /* holodeck main loop */
305                  p = freepacks; freepacks = p->next; p->next = NULL;
306                  if (!next_packet(p)) {
307                          p->next = freepacks; freepacks = p;
308 +                        idle = 1;
309                          break;
310                  }
311                  if (pl == NULL) pl = p;
312                  else plend->next = p;
313                  plend = p;
314          }
315 <        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 */
315 >                                        /* process packets */
316          done_packets(do_packets(pl));
317          return(1);                      /* and continue */
318   }
# Line 242 | Line 321 | rholo()                                /* holodeck main loop */
321   report(t)                       /* report progress so far */
322   time_t  t;
323   {
324 <        if (t == 0)
324 >        static time_t   seconds2go = 1000000;
325 >
326 >        if (t == 0L)
327                  t = time(NULL);
328 <        fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n",
329 <                        progname, npacksdone, nraysdone, (t-starttime)/3600.);
330 <        fflush(stderr);
331 <        if (vdef(REPORT))
332 <                reporttime = t + (time_t)(vflt(REPORT)*60.+.5);
328 >        sprintf(errmsg, "%ld packets (%ld rays) done after %.2f hours\n",
329 >                        npacksdone, nraysdone, (t-starttime)/3600.);
330 >        eputs(errmsg);
331 >        if (seconds2go == 1000000)
332 >                seconds2go = vdef(REPORT) ? (long)(vflt(REPORT)*60. + .5) : 0L;
333 >        if (seconds2go)
334 >                reporttime = t + seconds2go;
335   }
336  
337  
# Line 268 | Line 351 | register HDGRID        *gp;
351                  sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
352                  error(WARNING, errmsg);
353          }
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);
354          if (!vdef(OCTREE)) {
355                  if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
356                          error(SYSTEM, "out of memory");
# Line 304 | Line 361 | register HDGRID        *gp;
361                  vval(OBSTRUCTIONS) = "T";
362                  vdef(OBSTRUCTIONS)++;
363          }
364 +        if (!vdef(VDIST)) {
365 +                vval(VDIST) = "F";
366 +                vdef(VDIST)++;
367 +        }
368          if (!vdef(OCCUPANCY)) {
369                  vval(OCCUPANCY) = "U";
370                  vdef(OCCUPANCY)++;
# Line 311 | Line 372 | register HDGRID        *gp;
372                                  /* append rendering options */
373          if (vdef(RENDER))
374                  rtargc += wordstring(rtargv+rtargc, vval(RENDER));
375 +        
376 +        if (gp == NULL)         /* already initialized? */
377 +                return;
378 +                                /* set grid parameters */
379 +        gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
380 +        if (sscanf(vval(SECTION),
381 +                "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
382 +                        &gp->orig[0], &gp->orig[1], &gp->orig[2],
383 +                        &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
384 +                        &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
385 +                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
386 +                        &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
387 +                badvalue(SECTION);
388 +        maxlen = 0.;
389 +        for (i = 0; i < 3; i++)
390 +                if ((len[i] = VLEN(gp->xv[i])) > maxlen)
391 +                        maxlen = len[i];
392 +        if (!vdef(GRID)) {
393 +                sprintf(buf, "%.4f", maxlen/8.);
394 +                vval(GRID) = savqstr(buf);
395 +                vdef(GRID)++;
396 +        }
397 +        if ((d = vflt(GRID)) <= FTINY)
398 +                badvalue(GRID);
399 +        for (i = 0; i < 3; i++)
400 +                if (gp->grid[i] <= 0)
401 +                        gp->grid[i] = len[i]/d + (1.-FTINY);
402   }
403  
404  
405   creatholo(gp)                   /* create a holodeck output file */
406   HDGRID  *gp;
407   {
408 +        extern char     VersionID[];
409          long    endloc = 0;
410 +        int     fd;
411          FILE    *fp;
412                                          /* open & truncate file */
413          if ((fp = fopen(hdkfile, "w+")) == NULL) {
# Line 326 | Line 416 | HDGRID *gp;
416          }
417                                          /* write information header */
418          newheader("RADIANCE", fp);
419 +        fprintf(fp, "SOFTWARE= %s\n", VersionID);
420          printvars(fp);
421          fputformat(HOLOFMT, fp);
422          fputc('\n', fp);
423          putw(HOLOMAGIC, fp);            /* put magic number & terminus */
424          fwrite(&endloc, sizeof(long), 1, fp);
425 <        fflush(fp);                     /* flush buffer */
426 <        hdinit(fileno(fp), gp);         /* allocate and initialize index */
427 <                        /* we're dropping fp here.... */
425 >        fd = dup(fileno(fp));
426 >        fclose(fp);                     /* flush and close stdio stream */
427 >        hdinit(fd, gp);                 /* allocate and initialize index */
428   }
429  
430  
# Line 363 | Line 454 | char   *s;
454  
455   loadholo()                      /* start loading a holodeck from fname */
456   {
457 +        extern long     ftell();
458          FILE    *fp;
459 <        long    endloc;
459 >        int     fd;
460 >        long    fpos;
461                                          /* open holodeck file */
462          if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
463                  sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
# Line 379 | Line 472 | loadholo()                     /* start loading a holodeck from fname */
472                                  hdkfile);
473                  error(USER, errmsg);
474          }
475 <        fread(&endloc, sizeof(long), 1, fp);
476 <        if (endloc != 0)
475 >        fread(&fpos, sizeof(long), 1, fp);
476 >        if (fpos != 0)
477                  error(WARNING, "ignoring multiple sections in holodeck file");
478 <        fseek(fp, 0L, 1);                       /* align system file pointer */
479 <        hdinit(fileno(fp), NULL);               /* allocate and load index */
480 <                        /* we're dropping fp here.... */
478 >        fpos = ftell(fp);                       /* get stdio position */
479 >        fd = dup(fileno(fp));
480 >        fclose(fp);                             /* done with stdio */
481 >        lseek(fd, fpos, 0);                     /* align system file pointer */
482 >        hdinit(fd, NULL);                       /* allocate and load index */
483   }
484  
485  
486   done_packets(pl)                /* handle finished packets */
487   PACKET  *pl;
488   {
489 <        static int      nunflushed = 0;
489 >        static int      n2flush = 0;
490          register PACKET *p;
491  
492          while (pl != NULL) {
# Line 402 | Line 497 | PACKET *pl;
497                                  p->nr*sizeof(RAYVAL));
498                          if (outdev != NULL)     /* display it */
499                                  disp_packet((PACKHEAD *)p);
500 <                        else
501 <                                nunflushed += p->nr;
500 >                        if (hdcachesize <= 0)   /* manual flushing */
501 >                                n2flush += p->nr;
502                          nraysdone += p->nr;
503                          npacksdone++;
504                  }
# Line 411 | Line 506 | PACKET *pl;
506                  p->next = freepacks;
507                  freepacks = p;
508          }
509 <        if (nunflushed >= 256*RPACKSIZ) {
509 >        if (n2flush > 512*RPACKSIZ*nprocs) {
510                  hdflush(NULL);                  /* flush holodeck buffers */
511 <                nunflushed = 0;
511 >                n2flush = 0;
512          }
513   }
514  
515  
516 + int
517 + done_rtrace()                   /* clean up and close rtrace calculation */
518 + {
519 +        int     status;
520 +                                        /* already closed? */
521 +        if (!nprocs)
522 +                return;
523 +                                        /* report activity */
524 +        wputs("closing rtrace process...\n");
525 +                                        /* flush beam queue */
526 +        done_packets(flush_queue());
527 +                                        /* close rtrace */
528 +        if ((status = end_rtrace()))
529 +                error(WARNING, "bad exit status from rtrace");
530 +        if (vdef(REPORT))               /* report time */
531 +                report(0);
532 +        return(status);                 /* return status */
533 + }
534 +
535 +
536 + new_rtrace()                    /* restart rtrace calculation */
537 + {
538 +        char    combuf[128];
539 +
540 +        if (nprocs > 0)                 /* already running? */
541 +                return;
542 +        wputs("restarting rtrace process...\n");
543 +        if (vdef(RIF)) {                /* rerun rad to update octree */
544 +                sprintf(combuf, "rad -v 0 -s -w %s", vval(RIF));
545 +                if (system(combuf))
546 +                        error(WARNING, "error running rad");
547 +        }
548 +        if (start_rtrace() < 1)         /* start rtrace */
549 +                error(WARNING, "cannot restart rtrace");
550 +        else if (vdef(REPORT))
551 +                report(0);
552 + }
553 +
554 +
555   getradfile(rfargs)              /* run rad and get needed variables */
556   char    *rfargs;
557   {
558 <        static short    mvar[] = {VIEW,OCTREE,EXPOSURE,-1};
558 >        static short    mvar[] = {OCTREE,-1};
559          static char     tf1[] = TEMPLATE;
560          char    tf2[64];
561          char    combuf[256];
# Line 451 | Line 585 | char   *rfargs;
585          else
586                  sprintf(cp, ")[ \t]*=' > %s", tf2);
587          if (system(combuf)) {
454                error(SYSTEM, "cannot execute rad command");
588                  unlink(tf2);                    /* clean up */
589                  unlink(tf1);
590 <                quit(1);
590 >                error(SYSTEM, "cannot execute rad command");
591          }
592          if (pippt == NULL) {
593                  loadvars(tf2);                  /* load variables */
# Line 520 | Line 653 | int    ec;
653   {
654          int     status = 0;
655  
523        if (outdev != NULL)             /* close display */
524                disp_close();
656          if (hdlist[0] != NULL) {        /* flush holodeck */
657 <                if (ncprocs > 0) {
658 <                        done_packets(flush_queue());
659 <                        status = end_rtrace();  /* close rtrace */
660 <                        hdflush(NULL);
661 <                        if (vdef(REPORT)) {
662 <                                long    fsiz, fuse;
663 <                                report(0);
664 <                                fsiz = lseek(hdlist[0]->fd, 0L, 2);
534 <                                fuse = hdfiluse(hdlist[0]->fd, 1);
535 <                                fprintf(stderr,
657 >                if (nprocs > 0)
658 >                        status = done_rtrace();
659 >                hdflush(NULL);
660 >                if (ncprocs > 0 && vdef(REPORT)) {
661 >                        long    fsiz, fuse;
662 >                        fsiz = hdfilen(hdlist[0]->fd);
663 >                        fuse = hdfiluse(hdlist[0]->fd, 1);
664 >                        fprintf(stderr,
665                          "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
666 <                                                hdkfile, fsiz/(1024.*1024.),
667 <                                                100.*(fsiz-fuse)/fsiz);
668 <                        }
540 <                } else
541 <                        hdflush(NULL);
666 >                                        hdkfile, fsiz/(1024.*1024.),
667 >                                        100.*(fsiz-fuse)/fsiz);
668 >                }
669          }
670 +        if (orig_mode >= 0)             /* reset holodeck access mode */
671 +                fchmod(hdlist[0]->fd, orig_mode);
672 +        if (outdev != NULL)             /* close display */
673 +                disp_close();
674          exit(ec ? ec : status);         /* exit */
675   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines