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.39 by schorsch, Thu Jun 26 00:58:11 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines