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.34 by gregl, Wed May 28 17:22:52 1997 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  
80   extern long  lseek(), ftell();
81  
# Line 117 | Line 126 | char  *argv[];
126                                          break;
127                                  pixaspect = atof(argv[i+1]);
128                                  continue;
129 +                        case 'T':               /* time limit (hours) */
130 +                                if (argv[i][2])
131 +                                        break;
132 +                                timelim = atof(argv[++i])*3600. + .5;
133 +                                break;
134                          case 'x':               /* overall x resolution */
135                                  if (argv[i][2])
136                                          break;
# Line 137 | Line 151 | char  *argv[];
151                                          break;
152                                  vmult = atoi(argv[++i]);
153                                  continue;
154 +                        case 'R':               /* recover */
155 +                                if (argv[i][2])
156 +                                        break;
157 +                                rvrlim = 0;
158 +                        /* fall through */
159                          case 'F':               /* syncronization file */
160                                  if (argv[i][2])
161                                          break;
162 <                                if ((syncfd = open(argv[++i],
163 <                                                O_RDWR|O_CREAT, 0666)) < 0) {
162 >                                if ((syncfp =
163 >                fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) {
164                                          fprintf(stderr, "%s: cannot open\n",
165                                                          argv[i]);
166                                          exit(1);
# Line 174 | Line 193 | char  *argv[];
193   }
194  
195  
196 + dolock(fd, ltyp)                /* lock or unlock a file */
197 + int  fd;
198 + int  ltyp;
199 + {
200 +        static struct flock  fls;       /* static so initialized to zeroes */
201 +
202 +        fls.l_type = ltyp;
203 +        if (fcntl(fd, F_SETLKW, &fls) < 0) {
204 +                fprintf(stderr, "%s: cannot lock/unlock file: %s\n",
205 +                                progname, sys_errlist[errno]);
206 +                exit(1);
207 +        }
208 + }
209 +
210 +
211   init(ac, av)                    /* set up output file and start rpict */
212   int  ac;
213   char  **av;
# Line 188 | Line 222 | char  **av;
222                  fprintf(stderr, "%s: %s\n", progname, err);
223                  exit(1);
224          }
225 <        if (syncfd != -1) {
226 <                char  buf[32];
227 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
228 <                sscanf(buf, "%d %d", &hmult, &vmult);
225 >        if (syncfp != NULL) {
226 >                sflock(F_RDLCK);
227 >                fscanf(syncfp, "%d %d", &hmult, &vmult);
228 >                sflock(F_UNLCK);
229          }
230                                          /* compute piece size */
231          hres /= hmult;
# Line 206 | Line 240 | char  **av;
240          rpargv[rpargc] = NULL;
241                                          /* open output file */
242          if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
243 +                dolock(outfd, F_WRLCK);
244                  if ((fp = fdopen(dup(outfd), "w")) == NULL)
245                          goto filerr;
246 <                printargs(ac, av, fp);          /* write header */
246 >                newheader("RADIANCE", fp);      /* create header */
247 >                printargs(ac, av, fp);
248                  fprintf(fp, "SOFTWARE= %s\n", VersionID);
249                  fputs(VIEWSTR, fp);
250                  fprintview(&ourview, fp);
# Line 219 | Line 255 | char  **av;
255                  putc('\n', fp);
256                  fprtresolu(hres*hmult, vres*vmult, fp);
257          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
258 +                dolock(outfd, F_RDLCK);
259                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
260                          goto filerr;
261                  getheader(fp, NULL, NULL);      /* skip header */
# Line 236 | Line 273 | char  **av;
273          scanorig = ftell(fp);           /* record position of first scanline */
274          if (fclose(fp) == -1)           /* done with stream i/o */
275                  goto filerr;
276 < #if NFS
240 <        sync();                         /* flush NFS buffers */
241 < #endif
276 >        dolock(outfd, F_UNLCK);
277                                          /* start rpict process */
278          if (open_process(rpd, rpargv) <= 0) {
279                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
# Line 255 | Line 290 | char  **av;
290                  exit(1);
291          }
292          signal(SIGALRM, onalrm);
293 +        if (timelim)
294 +                alarm(timelim);
295          return;
296   filerr:
297          fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
# Line 266 | Line 303 | int
303   nextpiece(xp, yp)               /* get next piece assignment */
304   int  *xp, *yp;
305   {
269        struct flock  fls;
270        char  buf[64];
271
306          if (gotalrm)                    /* someone wants us to quit */
307                  return(0);
308 <        if (syncfd != -1) {             /* use sync file */
309 <                fls.l_type = F_WRLCK;           /* gain exclusive access */
310 <                fls.l_whence = 0;
311 <                fls.l_start = 0L;
312 <                fls.l_len = 0L;
313 <                fcntl(syncfd, F_SETLKW, &fls);
314 <                lseek(syncfd, 0L, 0);
315 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
316 <                if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
308 >        if (syncfp != NULL) {           /* use sync file */
309 >                /*
310 >                 * So we don't necessarily have to lock and unlock the file
311 >                 * multiple times (very slow), we establish an exclusive
312 >                 * lock at the beginning on our synchronization file and
313 >                 * maintain it in the subroutine rvrpiece().
314 >                 */
315 >                sflock(F_WRLCK);
316 >                fseek(syncfp, 0L, 0);           /* read position */
317 >                if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) {
318                          *xp = hmult-1;
319                          *yp = vmult;
320                  }
321 +                if (rvrlim == 0)                /* initialize recovery limit */
322 +                        rvrlim = *xp*vmult + *yp;
323 +                if (rvrpiece(xp, yp)) {         /* do stragglers first */
324 +                        sflock(F_UNLCK);
325 +                        return(1);
326 +                }
327                  if (--(*yp) < 0) {              /* decrement position */
328                          *yp = vmult-1;
329 <                        if (--(*xp) < 0) {      /* all done! */
330 <                                close(syncfd);
329 >                        if (--(*xp) < 0) {      /* all done */
330 >                                sflock(F_UNLCK);
331                                  return(0);
332                          }
333                  }
334 <                sprintf(buf, "%4d %4d\n%4d %4d\n", hmult, vmult, *xp, *yp);
335 <                lseek(syncfd, 0L, 0);           /* write new position */
336 <                write(syncfd, buf, strlen(buf));
337 <                fls.l_type = F_UNLCK;           /* release sync file */
297 <                fcntl(syncfd, F_SETLKW, &fls);
334 >                fseek(syncfp, 0L, 0);           /* write new position */
335 >                fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp);
336 >                fflush(syncfp);
337 >                sflock(F_UNLCK);                /* release sync file */
338                  return(1);
339          }
340 <        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));
340 >        return(scanf("%d %d", xp, yp) == 2);    /* use stdin */
341   }
342  
343  
344   int
345 + rvrpiece(xp, yp)                /* check for recoverable pieces */
346 + register int  *xp, *yp;
347 + {
348 +        static char  *pdone = NULL;     /* which pieces are done */
349 +        static long  readpos = -1;      /* how far we've read */
350 +        register int  i;
351 +        /*
352 +         * This routine is called by nextpiece() with an
353 +         * exclusive lock on syncfp and the file pointer at the
354 +         * appropriate position to read in the finished pieces.
355 +         */
356 +        if (rvrlim < 0)
357 +                return(0);              /* only check if asked */
358 +        if (pdone == NULL)              /* first call */
359 +                pdone = calloc(hmult*vmult, sizeof(char));
360 +        if (pdone == NULL) {
361 +                fprintf(stderr, "%s: out of memory\n", progname);
362 +                exit(1);
363 +        }
364 +        if (readpos != -1)              /* mark what's been done */
365 +                fseek(syncfp, readpos, 0);
366 +        while (fscanf(syncfp, "%d %d", xp, yp) == 2)
367 +                pdone[*xp*vmult+*yp] = 1;
368 +        if (!feof(syncfp)) {
369 +                fprintf(stderr, "%s: format error in sync file\n", progname);
370 +                exit(1);
371 +        }
372 +        readpos = ftell(syncfp);
373 +        i = hmult*vmult;                /* find an unaccounted for piece */
374 +        while (i-- > rvrlim)
375 +                if (!pdone[i]) {
376 +                        *xp = i / vmult;
377 +                        *yp = i % vmult;
378 +                        pdone[i] = 1;   /* consider it done */
379 +                        return(1);
380 +                }
381 +        rvrlim = -1;                    /* nothing left to recover */
382 +        free(pdone);
383 +        pdone = NULL;
384 +        return(0);
385 + }
386 +
387 +
388 + int
389   cleanup(rstat)                  /* close rpict process and clean up */
390   int  rstat;
391   {
# Line 340 | Line 419 | rpiece()                       /* render picture piece by piece */
419                  pview.horiz = ourview.horiz / hmult;
420                  pview.vert = ourview.vert / vmult;
421                  break;
422 +        case VT_CYL:
423 +                pview.horiz = ourview.horiz / hmult;
424 +                pview.vert = 2.*180./PI*atan(
425 +                                tan(PI/180./2.*ourview.vert)/vmult );
426 +                break;
427          case VT_HEM:
428                  pview.horiz = 2.*180./PI*asin(
429                                  sin(PI/180./2.*ourview.horiz)/hmult );
# Line 353 | Line 437 | rpiece()                       /* render picture piece by piece */
437          }
438                                          /* render each piece */
439          while (nextpiece(&xorg, &yorg)) {
440 <                pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1);
441 <                pview.voff = ourview.voff + yorg - 0.5*(vmult-1);
440 >                pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1);
441 >                pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1);
442                  fputs(VIEWSTR, torp);
443                  fprintview(&pview, torp);
444                  putc('\n', torp);
# Line 422 | Line 506 | int  xpos, ypos;
506                                  /* lock file section so NFS doesn't mess up */
507          fls.l_whence = 0;
508          fls.l_type = F_WRLCK;
509 <        fcntl(outfd, F_SETLKW, &fls);
509 >        if (fcntl(outfd, F_SETLKW, &fls) < 0)
510 >                filerr("lock");
511   #endif
512                                  /* write new piece to file */
513          if (lseek(outfd, fls.l_start, 0) == -1)
514 <                goto seekerr;
514 >                filerr("seek");
515          if (hmult == 1) {
516                  if (writebuf(outfd, (char *)pbuf,
517                                  vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
518 <                        goto writerr;
518 >                        filerr("write");
519          } else
520                  for (y = 0; y < vr; y++) {
521                          if (writebuf(outfd, (char *)(pbuf+y*hr),
522                                          hr*sizeof(COLR)) != hr*sizeof(COLR))
523 <                                goto writerr;
523 >                                filerr("write");
524                          if (y < vr-1 && lseek(outfd,
525                                          (long)(hmult-1)*hr*sizeof(COLR),
526                                          1) == -1)
527 <                                goto seekerr;
527 >                                filerr("seek");
528                  }
529 + #if NFS
530 +        fls.l_type = F_UNLCK;           /* release lock */
531 +        if (fcntl(outfd, F_SETLKW, &fls) < 0)
532 +                filerr("lock");
533 + #endif
534          if (verbose) {                          /* notify caller */
535                  printf("%d %d done\n", xpos, ypos);
536                  fflush(stdout);
537          }
538 <        if (pid == -1) {        /* didn't fork or fork failed */
539 < #if NFS
540 <                fls.l_type = F_UNLCK;           /* release lock */
541 <                fcntl(outfd, F_SETLKW, &fls);
542 < #endif
543 <                return(0);
538 >        if (syncfp != NULL) {                   /* record what's been done */
539 >                sflock(F_WRLCK);
540 >                fseek(syncfp, 0L, 2);           /* append index */
541 >                fprintf(syncfp, "%4d %4d\n", xpos, ypos);
542 >                fflush(syncfp);
543 >                                /*** Unlock not necessary, since
544 >                sflock(F_UNLCK);        _exit() or nextpiece() is next ***/
545          }
546 <        _exit(0);               /* else exit child process (releasing lock) */
547 < seekerr:
548 <        fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
549 <        _exit(1);
550 < writerr:
551 <        fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile);
546 >        if (pid == -1)          /* didn't fork or fork failed */
547 >                return(0);
548 >        _exit(0);               /* else exit child process (releasing locks) */
549 > }
550 >
551 >
552 > filerr(t)                       /* report file error and exit */
553 > char  *t;
554 > {
555 >        fprintf(stderr, "%s: %s error on file \"%s\": %s\n",
556 >                        progname, t, outfile, sys_errlist[errno]);
557          _exit(1);
558   }
559  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines