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.11 by greg, Sun Aug 9 23:05:17 1992 UTC vs.
Revision 2.36 by gwlarson, Thu Jun 18 09:23:49 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1992 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 >
15 > main(argc, argv)
16 > int argc;
17 > char *argv[];
18 > {
19 >        fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]);
20 >        exit(1);
21 > }
22 >
23 > #else
24 >
25   #include <signal.h>
26   #include "color.h"
27   #include "view.h"
28   #include "resolu.h"
29  
30 + #ifndef NFS
31 + #define  NFS                    1
32 + #endif
33                                  /* set the following to 0 to forgo forking */
34   #ifndef MAXFORK
35 + #if NFS
36   #define  MAXFORK                3       /* allotment of duped processes */
37 + #else
38 + #define  MAXFORK                0
39   #endif
40 + #endif
41 +                                        /* protection from SYSV signals(!) */
42 + #if defined(sgi)
43 + #define guard_io()      sighold(SIGALRM)
44 + #define unguard()       sigrelse(SIGALRM)
45 + #endif
46 + #ifndef guard_io
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", "-x", "512", "-y", "512", "-pa", "1"};
57 < int  rpargc = 9;
56 > char  *rpargv[128] = {"rpict", "-S", "1"};
57 > int  rpargc = 3;
58   int  rpd[3];
59   FILE  *torp, *fromrp;
60   COLR  *pbuf;
61                                  /* our view parameters */
62   VIEW  ourview = STDVIEW;
63   double  pixaspect = 1.0;
64 < int  hres = 512, vres = 512, hmult = 2, vmult = 2;
64 > int  hres = 1024, vres = 1024, hmult = 4, vmult = 4;
65                                  /* output file */
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  
43 extern long  lseek(), ftell();
44
80   int  gotalrm = 0;
81   int  onalrm() { gotalrm++; }
82  
# Line 54 | Line 89 | char  *argv[];
89          
90          progname = argv[0];
91          for (i = 1; i < argc; i++) {
92 +                                                /* expand arguments */
93 +                while (rval = expandarg(&argc, &argv, i))
94 +                        if (rval < 0) {
95 +                                fprintf(stderr, "%s: cannot expand '%s'",
96 +                                                argv[0], argv[i]);
97 +                                exit(1);
98 +                        }
99                  if (argv[i][0] == '-')
100                          switch (argv[i][1]) {
101                          case 'v':
# Line 78 | Line 120 | char  *argv[];
120                                  }
121                                  break;
122                          case 'p':               /* pixel aspect ratio? */
123 <                                if (argv[i][2] == 'a' && !argv[i][3])
124 <                                        pixaspect = atof(argv[i+1]);
123 >                                if (argv[i][2] != 'a' || argv[i][3])
124 >                                        break;
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':               /* piece x resolution */
133 <                                if (!argv[i][2])
134 <                                        hres = atoi(argv[i+1]);
135 <                                break;
136 <                        case 'y':               /* piece y resolution */
137 <                                if (!argv[i][2])
138 <                                        vres = atoi(argv[i+1]);
139 <                                break;
132 >                        case 'x':               /* overall x resolution */
133 >                                if (argv[i][2])
134 >                                        break;
135 >                                hres = atoi(argv[++i]);
136 >                                continue;
137 >                        case 'y':               /* overall y resolution */
138 >                                if (argv[i][2])
139 >                                        break;
140 >                                vres = atoi(argv[++i]);
141 >                                continue;
142                          case 'X':               /* horizontal multiplier */
143                                  if (argv[i][2])
144                                          break;
# Line 99 | 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], O_RDWR)) < 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);
165                                  }
166                                  continue;
167 +                        case 'z':               /* z-file ist verbotten */
168 +                                fprintf(stderr, "%s: -z option not allowed\n",
169 +                                                argv[0]);
170 +                                exit(1);
171                          case 'o':               /* output file */
172                                  if (argv[i][2])
173                                          break;
174                                  outfile = argv[++i];
175                                  continue;
176 <                        }
176 >                        } else if (i >= argc-1)
177 >                                break;
178                  rpargv[rpargc++] = argv[i];
179          }
180 <        rpargv[rpargc] = NULL;
180 >        if (i >= argc) {
181 >                fprintf(stderr, "%s: missing octree argument\n", argv[0]);
182 >                exit(1);
183 >        }
184          if (outfile == NULL) {
185                  fprintf(stderr, "%s: missing output file\n", argv[0]);
186                  exit(1);
187          }
188          init(argc, argv);
189          rpiece();
190 <        rval = cleanup(0);
127 <        exit(rval);
190 >        exit(cleanup(0));
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;
212   {
213 +        static char  hrbuf[16], vrbuf[16];
214          extern char  VersionID[];
215          char  *err;
216          FILE  *fp;
# Line 141 | 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;
230 +        vres /= vmult;
231          normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres);
232 +        sprintf(hrbuf, "%d", hres);
233 +        rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf;
234 +        sprintf(vrbuf, "%d", vres);
235 +        rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf;
236 +        rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0";
237 +        rpargv[rpargc++] = av[ac-1];
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 162 | 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);            /* skip header */
260 <                if (fscnresolu(&hr, &vr, fp) < 0 ||     /* check resolution */
259 >                getheader(fp, NULL, NULL);      /* skip header */
260 >                if (!fscnresolu(&hr, &vr, fp) ||        /* check resolution */
261                                  hr != hres*hmult || vr != vres*vmult) {
262                          fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n",
263                                          progname, outfile);
# Line 179 | 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 <        sync();                         /* flush NFS buffers */
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 191 | Line 283 | char  **av;
283                                  progname, rpargv[0]);
284                  exit(1);
285          }
286 <        if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) {
286 >        if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) {
287                  fprintf(stderr, "%s: out of memory\n", progname);
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 207 | Line 301 | int
301   nextpiece(xp, yp)               /* get next piece assignment */
302   int  *xp, *yp;
303   {
210        extern char  *fgets();
211        struct flock  fls;
212        char  buf[64];
213
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, "%d %d\n%d %d\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 */
239 <                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 */
243 <                return(0);
244 <        if (sscanf(buf, "%d %d", xp, yp) == 2)
245 <                return(1);
246 <        fprintf(stderr, "%s: input format error\n", progname);
247 <        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   {
390          int  status;
391  
392 <        free((char *)pbuf);
392 >        bfree((char *)pbuf, hres*vres*sizeof(COLR));
393          fclose(torp);
394          fclose(fromrp);
395          while (wait(&status) != -1)
# Line 282 | 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 295 | 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);
443                  fflush(torp);                   /* assigns piece to rpict */
444                  putpiece(xorg, yorg);           /* place piece in output */
305                if (verbose) {                  /* notify caller */
306                        printf("%d %d done\n", xorg, yorg);
307                        fflush(stdout);
308                }
445          }
446   }
447  
# Line 325 | Line 461 | int  xpos, ypos;
461                  exit(cleanup(1));
462          }
463                                  /* check header from rpict */
464 <        getheader(fromrp, NULL);
465 <        if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres | vr != vres) {
464 >        guard_io();
465 >        getheader(fromrp, NULL, NULL);
466 >        if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) {
467                  fprintf(stderr, "%s: resolution mismatch from %s\n",
468                                  progname, rpargv[0]);
469                  exit(cleanup(1));
470          }
471 +        if (verbose) {                          /* notify caller */
472 +                printf("%d %d begun\n", xpos, ypos);
473 +                fflush(stdout);
474 +        }
475 +        unguard();
476                                  /* load new piece into buffer */
477 <        for (y = 0; y < vr; y++)
477 >        for (y = 0; y < vr; y++) {
478 >                guard_io();
479                  if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
480                          fprintf(stderr, "%s: read error from %s\n",
481                                          progname, rpargv[0]);
482                          exit(cleanup(1));
483                  }
484 +                unguard();
485 +        }
486   #if MAXFORK
487                                  /* fork so we don't slow rpict down */
488          if ((pid = fork()) > 0) {
# Line 352 | Line 497 | int  xpos, ypos;
497   #else
498          pid = -1;               /* no forking */
499   #endif
500 +        fls.l_start = scanorig +
501 +                ((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR);
502 + #if NFS
503 +        fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR);
504                                  /* lock file section so NFS doesn't mess up */
505          fls.l_whence = 0;
357        fls.l_len = (long)vres*hmult*hres*sizeof(COLR);
358        fls.l_start = scanorig + (vmult-1-ypos)*fls.l_len;
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+(long)xpos*hres*sizeof(COLR), 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 (pid == -1) {        /* fork failed */
528 <                fls.l_type = F_UNLCK;           /* release lock */
529 <                fcntl(outfd, F_SETLKW, &fls);
530 <                return(0);
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 <        _exit(0);               /* else exit child process (releasing lock) */
537 < seekerr:
538 <        fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
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 >        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);
387 writerr:
388        fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile);
389        _exit(1);
556   }
557 +
558 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines