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

Comparing ray/src/util/rpiece.c (file contents):
Revision 2.24 by greg, Fri Aug 6 12:58:43 1993 UTC vs.
Revision 2.36 by gwlarson, Thu Jun 18 09:23:49 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1993 Regents of the University of California */
1 > /* Copyright (c) 1995 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 9 | Line 9 | static char SCCSid[] = "$SunId$ LBL";
9   */
10  
11   #include "standard.h"
12 #include <fcntl.h>
12  
13   #ifndef F_SETLKW
14  
# Line 40 | Line 39 | char *argv[];
39   #endif
40   #endif
41                                          /* protection from SYSV signals(!) */
42 < #if defined(sgi) || defined(hpux)
42 > #if defined(sgi)
43   #define guard_io()      sighold(SIGALRM)
44   #define unguard()       sigrelse(SIGALRM)
45   #endif
# Line 48 | Line 47 | char *argv[];
47   #define guard_io()      0
48   #define unguard()       0
49   #endif
50 +
51 + #ifndef linux
52 + extern char  *sys_errlist[];
53 + #endif
54 +
55                                  /* rpict command */
56   char  *rpargv[128] = {"rpict", "-S", "1"};
57   int  rpargc = 3;
# Line 62 | Line 66 | int  hres = 1024, vres = 1024, hmult = 4, vmult = 4;
66   char  *outfile = NULL;
67   int  outfd;
68   long  scanorig;
69 < int  syncfd = -1;               /* lock file descriptor */
69 > FILE  *syncfp = NULL;           /* synchronization file pointer */
70 > int  synclst = F_UNLCK;         /* synchronization file lock status */
71   int  nforked = 0;
72  
73 + #define  sflock(t)      if ((t)!=synclst) dolock(fileno(syncfp),synclst=t)
74 +
75   char  *progname;
76   int  verbose = 0;
77 + unsigned  timelim = 0;
78 + int  rvrlim = -1;
79  
71 extern long  lseek(), ftell();
72
80   int  gotalrm = 0;
81   int  onalrm() { gotalrm++; }
82  
# Line 115 | Line 122 | char  *argv[];
122                          case 'p':               /* pixel aspect ratio? */
123                                  if (argv[i][2] != 'a' || argv[i][3])
124                                          break;
125 <                                pixaspect = atof(argv[i+1]);
125 >                                pixaspect = atof(argv[++i]);
126                                  continue;
127 +                        case 'T':               /* time limit (hours) */
128 +                                if (argv[i][2])
129 +                                        break;
130 +                                timelim = atof(argv[++i])*3600. + .5;
131 +                                break;
132                          case 'x':               /* overall x resolution */
133                                  if (argv[i][2])
134                                          break;
# Line 137 | Line 149 | char  *argv[];
149                                          break;
150                                  vmult = atoi(argv[++i]);
151                                  continue;
152 +                        case 'R':               /* recover */
153 +                                if (argv[i][2])
154 +                                        break;
155 +                                rvrlim = 0;
156 +                        /* fall through */
157                          case 'F':               /* syncronization file */
158                                  if (argv[i][2])
159                                          break;
160 <                                if ((syncfd = open(argv[++i],
161 <                                                O_RDWR|O_CREAT, 0666)) < 0) {
160 >                                if ((syncfp =
161 >                fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) {
162                                          fprintf(stderr, "%s: cannot open\n",
163                                                          argv[i]);
164                                          exit(1);
# Line 174 | Line 191 | char  *argv[];
191   }
192  
193  
194 + dolock(fd, ltyp)                /* lock or unlock a file */
195 + int  fd;
196 + int  ltyp;
197 + {
198 +        static struct flock  fls;       /* static so initialized to zeroes */
199 +
200 +        fls.l_type = ltyp;
201 +        if (fcntl(fd, F_SETLKW, &fls) < 0) {
202 +                fprintf(stderr, "%s: cannot lock/unlock file: %s\n",
203 +                                progname, sys_errlist[errno]);
204 +                exit(1);
205 +        }
206 + }
207 +
208 +
209   init(ac, av)                    /* set up output file and start rpict */
210   int  ac;
211   char  **av;
# Line 188 | Line 220 | char  **av;
220                  fprintf(stderr, "%s: %s\n", progname, err);
221                  exit(1);
222          }
223 <        if (syncfd != -1) {
224 <                char  buf[32];
225 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
226 <                sscanf(buf, "%d %d", &hmult, &vmult);
223 >        if (syncfp != NULL) {
224 >                sflock(F_RDLCK);
225 >                fscanf(syncfp, "%d %d", &hmult, &vmult);
226 >                sflock(F_UNLCK);
227          }
228                                          /* compute piece size */
229          hres /= hmult;
# Line 206 | Line 238 | char  **av;
238          rpargv[rpargc] = NULL;
239                                          /* open output file */
240          if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
241 +                dolock(outfd, F_WRLCK);
242                  if ((fp = fdopen(dup(outfd), "w")) == NULL)
243                          goto filerr;
244 <                printargs(ac, av, fp);          /* write header */
244 >                newheader("RADIANCE", fp);      /* create header */
245 >                printargs(ac, av, fp);
246                  fprintf(fp, "SOFTWARE= %s\n", VersionID);
247                  fputs(VIEWSTR, fp);
248                  fprintview(&ourview, fp);
# Line 219 | Line 253 | char  **av;
253                  putc('\n', fp);
254                  fprtresolu(hres*hmult, vres*vmult, fp);
255          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
256 +                dolock(outfd, F_RDLCK);
257                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
258                          goto filerr;
259                  getheader(fp, NULL, NULL);      /* skip header */
# Line 236 | Line 271 | char  **av;
271          scanorig = ftell(fp);           /* record position of first scanline */
272          if (fclose(fp) == -1)           /* done with stream i/o */
273                  goto filerr;
274 < #if NFS
240 <        sync();                         /* flush NFS buffers */
241 < #endif
274 >        dolock(outfd, F_UNLCK);
275                                          /* start rpict process */
276          if (open_process(rpd, rpargv) <= 0) {
277                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
# Line 255 | Line 288 | char  **av;
288                  exit(1);
289          }
290          signal(SIGALRM, onalrm);
291 +        if (timelim)
292 +                alarm(timelim);
293          return;
294   filerr:
295          fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
# Line 266 | Line 301 | int
301   nextpiece(xp, yp)               /* get next piece assignment */
302   int  *xp, *yp;
303   {
269        struct flock  fls;
270        char  buf[64];
271
304          if (gotalrm)                    /* someone wants us to quit */
305                  return(0);
306 <        if (syncfd != -1) {             /* use sync file */
307 <                fls.l_type = F_WRLCK;           /* gain exclusive access */
308 <                fls.l_whence = 0;
309 <                fls.l_start = 0L;
310 <                fls.l_len = 0L;
311 <                fcntl(syncfd, F_SETLKW, &fls);
312 <                lseek(syncfd, 0L, 0);
313 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
314 <                if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
306 >        if (syncfp != NULL) {           /* use sync file */
307 >                /*
308 >                 * So we don't necessarily have to lock and unlock the file
309 >                 * multiple times (very slow), we establish an exclusive
310 >                 * lock at the beginning on our synchronization file and
311 >                 * maintain it in the subroutine rvrpiece().
312 >                 */
313 >                sflock(F_WRLCK);
314 >                fseek(syncfp, 0L, 0);           /* read position */
315 >                if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) {
316                          *xp = hmult-1;
317                          *yp = vmult;
318                  }
319 +                if (rvrlim == 0)                /* initialize recovery limit */
320 +                        rvrlim = *xp*vmult + *yp;
321 +                if (rvrpiece(xp, yp)) {         /* do stragglers first */
322 +                        sflock(F_UNLCK);
323 +                        return(1);
324 +                }
325                  if (--(*yp) < 0) {              /* decrement position */
326                          *yp = vmult-1;
327 <                        if (--(*xp) < 0) {      /* all done! */
328 <                                close(syncfd);
327 >                        if (--(*xp) < 0) {      /* all done */
328 >                                sflock(F_UNLCK);
329                                  return(0);
330                          }
331                  }
332 <                sprintf(buf, "%4d %4d\n%4d %4d\n", hmult, vmult, *xp, *yp);
333 <                lseek(syncfd, 0L, 0);           /* write new position */
334 <                write(syncfd, buf, strlen(buf));
335 <                fls.l_type = F_UNLCK;           /* release sync file */
297 <                fcntl(syncfd, F_SETLKW, &fls);
332 >                fseek(syncfp, 0L, 0);           /* write new position */
333 >                fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp);
334 >                fflush(syncfp);
335 >                sflock(F_UNLCK);                /* release sync file */
336                  return(1);
337          }
338 <        if (fgets(buf, sizeof(buf), stdin) == NULL)     /* use stdin */
301 <                return(0);
302 <        if (sscanf(buf, "%d %d", xp, yp) == 2)
303 <                return(1);
304 <        fprintf(stderr, "%s: input format error\n", progname);
305 <        exit(cleanup(1));
338 >        return(scanf("%d %d", xp, yp) == 2);    /* use stdin */
339   }
340  
341  
342   int
343 + rvrpiece(xp, yp)                /* check for recoverable pieces */
344 + register int  *xp, *yp;
345 + {
346 +        static char  *pdone = NULL;     /* which pieces are done */
347 +        static long  readpos = -1;      /* how far we've read */
348 +        register int  i;
349 +        /*
350 +         * This routine is called by nextpiece() with an
351 +         * exclusive lock on syncfp and the file pointer at the
352 +         * appropriate position to read in the finished pieces.
353 +         */
354 +        if (rvrlim < 0)
355 +                return(0);              /* only check if asked */
356 +        if (pdone == NULL)              /* first call */
357 +                pdone = calloc(hmult*vmult, sizeof(char));
358 +        if (pdone == NULL) {
359 +                fprintf(stderr, "%s: out of memory\n", progname);
360 +                exit(1);
361 +        }
362 +        if (readpos != -1)              /* mark what's been done */
363 +                fseek(syncfp, readpos, 0);
364 +        while (fscanf(syncfp, "%d %d", xp, yp) == 2)
365 +                pdone[*xp*vmult+*yp] = 1;
366 +        if (!feof(syncfp)) {
367 +                fprintf(stderr, "%s: format error in sync file\n", progname);
368 +                exit(1);
369 +        }
370 +        readpos = ftell(syncfp);
371 +        i = hmult*vmult;                /* find an unaccounted for piece */
372 +        while (i-- > rvrlim)
373 +                if (!pdone[i]) {
374 +                        *xp = i / vmult;
375 +                        *yp = i % vmult;
376 +                        pdone[i] = 1;   /* consider it done */
377 +                        return(1);
378 +                }
379 +        rvrlim = -1;                    /* nothing left to recover */
380 +        free(pdone);
381 +        pdone = NULL;
382 +        return(0);
383 + }
384 +
385 +
386 + int
387   cleanup(rstat)                  /* close rpict process and clean up */
388   int  rstat;
389   {
# Line 340 | Line 417 | rpiece()                       /* render picture piece by piece */
417                  pview.horiz = ourview.horiz / hmult;
418                  pview.vert = ourview.vert / vmult;
419                  break;
420 +        case VT_CYL:
421 +                pview.horiz = ourview.horiz / hmult;
422 +                pview.vert = 2.*180./PI*atan(
423 +                                tan(PI/180./2.*ourview.vert)/vmult );
424 +                break;
425          case VT_HEM:
426                  pview.horiz = 2.*180./PI*asin(
427                                  sin(PI/180./2.*ourview.horiz)/hmult );
# Line 353 | Line 435 | rpiece()                       /* render picture piece by piece */
435          }
436                                          /* render each piece */
437          while (nextpiece(&xorg, &yorg)) {
438 <                pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1);
439 <                pview.voff = ourview.voff + yorg - 0.5*(vmult-1);
438 >                pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1);
439 >                pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1);
440                  fputs(VIEWSTR, torp);
441                  fprintview(&pview, torp);
442                  putc('\n', torp);
# Line 422 | Line 504 | int  xpos, ypos;
504                                  /* lock file section so NFS doesn't mess up */
505          fls.l_whence = 0;
506          fls.l_type = F_WRLCK;
507 <        fcntl(outfd, F_SETLKW, &fls);
507 >        if (fcntl(outfd, F_SETLKW, &fls) < 0)
508 >                filerr("lock");
509   #endif
510                                  /* write new piece to file */
511 <        if (lseek(outfd, fls.l_start, 0) == -1)
512 <                goto seekerr;
511 >        if (lseek(outfd, fls.l_start, 0) < 0)
512 >                filerr("seek");
513          if (hmult == 1) {
514                  if (writebuf(outfd, (char *)pbuf,
515                                  vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
516 <                        goto writerr;
516 >                        filerr("write");
517          } else
518                  for (y = 0; y < vr; y++) {
519                          if (writebuf(outfd, (char *)(pbuf+y*hr),
520                                          hr*sizeof(COLR)) != hr*sizeof(COLR))
521 <                                goto writerr;
521 >                                filerr("write");
522                          if (y < vr-1 && lseek(outfd,
523                                          (long)(hmult-1)*hr*sizeof(COLR),
524 <                                        1) == -1)
525 <                                goto seekerr;
524 >                                        1) < 0)
525 >                                filerr("seek");
526                  }
527 + #if NFS
528 +        fls.l_type = F_UNLCK;           /* release lock */
529 +        if (fcntl(outfd, F_SETLKW, &fls) < 0)
530 +                filerr("lock");
531 + #endif
532          if (verbose) {                          /* notify caller */
533                  printf("%d %d done\n", xpos, ypos);
534                  fflush(stdout);
535          }
536 <        if (pid == -1) {        /* didn't fork or fork failed */
537 < #if NFS
538 <                fls.l_type = F_UNLCK;           /* release lock */
539 <                fcntl(outfd, F_SETLKW, &fls);
540 < #endif
541 <                return(0);
536 >        if (syncfp != NULL) {                   /* record what's been done */
537 >                sflock(F_WRLCK);
538 >                fseek(syncfp, 0L, 2);           /* append index */
539 >                fprintf(syncfp, "%4d %4d\n", xpos, ypos);
540 >                fflush(syncfp);
541 >                                /*** Unlock not necessary, since
542 >                sflock(F_UNLCK);        _exit() or nextpiece() is next ***/
543          }
544 <        _exit(0);               /* else exit child process (releasing lock) */
545 < seekerr:
546 <        fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
547 <        _exit(1);
548 < writerr:
549 <        fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile);
544 >        if (pid == -1)          /* didn't fork or fork failed */
545 >                return(0);
546 >        _exit(0);               /* else exit child process (releasing locks) */
547 > }
548 >
549 >
550 > filerr(t)                       /* report file error and exit */
551 > char  *t;
552 > {
553 >        fprintf(stderr, "%s: %s error on file \"%s\": %s\n",
554 >                        progname, t, outfile, sys_errlist[errno]);
555          _exit(1);
556   }
557  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines