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.2 by greg, Thu Aug 6 17:16:29 1992 UTC vs.
Revision 2.26 by greg, Sat Aug 7 08:43:32 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1992 Regents of the University of California */
1 > /* Copyright (c) 1993 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) || defined(hpux)
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                                  /* rpict command */
51 < char  *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", };
52 < int  rpargc = 7;
51 > char  *rpargv[128] = {"rpict", "-S", "1"};
52 > int  rpargc = 3;
53   int  rpd[3];
54   FILE  *torp, *fromrp;
55 < COLR  *scanline;
55 > COLR  *pbuf;
56                                  /* our view parameters */
57   VIEW  ourview = STDVIEW;
58 < double  pixaspect = 0.0;
59 < int  hres = 512, vres = 512, hmult = 2, vmult = 2;
58 > double  pixaspect = 1.0;
59 > int  hres = 1024, vres = 1024, hmult = 4, vmult = 4;
60                                  /* output file */
61   char  *outfile = NULL;
62   int  outfd;
63   long  scanorig;
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 + int  rvrlim = -1;
73  
74   extern long  lseek(), ftell();
75  
76 + int  gotalrm = 0;
77 + int  onalrm() { gotalrm++; }
78  
79 +
80   main(argc, argv)
81   int  argc;
82   char  *argv[];
# Line 43 | Line 85 | char  *argv[];
85          
86          progname = argv[0];
87          for (i = 1; i < argc; i++) {
88 +                                                /* expand arguments */
89 +                while (rval = expandarg(&argc, &argv, i))
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 67 | Line 116 | char  *argv[];
116                                  }
117                                  break;
118                          case 'p':               /* pixel aspect ratio? */
119 <                                if (argv[i][2] == 'a' && !argv[i][3])
120 <                                        pixaspect = atof(argv[i+1]);
121 <                                break;
122 <                        case 'x':               /* piece x resolution */
123 <                                if (!argv[i][2])
124 <                                        hres = atoi(argv[i+1]);
125 <                                break;
126 <                        case 'y':               /* piece y resolution */
127 <                                if (!argv[i][2])
128 <                                        vres = atoi(argv[i+1]);
129 <                                break;
119 >                                if (argv[i][2] != 'a' || argv[i][3])
120 >                                        break;
121 >                                pixaspect = atof(argv[i+1]);
122 >                                continue;
123 >                        case 'x':               /* overall x resolution */
124 >                                if (argv[i][2])
125 >                                        break;
126 >                                hres = atoi(argv[++i]);
127 >                                continue;
128 >                        case 'y':               /* overall y resolution */
129 >                                if (argv[i][2])
130 >                                        break;
131 >                                vres = atoi(argv[++i]);
132 >                                continue;
133                          case 'X':               /* horizontal multiplier */
134                                  if (argv[i][2])
135                                          break;
# Line 88 | Line 140 | char  *argv[];
140                                          break;
141                                  vmult = atoi(argv[++i]);
142                                  continue;
143 +                        case 'R':               /* recover */
144 +                                if (argv[i][2])
145 +                                        break;
146 +                                rvrlim = 0;
147 +                        /* fall through */
148 +                        case 'F':               /* syncronization file */
149 +                                if (argv[i][2])
150 +                                        break;
151 +                                if ((syncfp = fopen(argv[++i],"r+")) == NULL) {
152 +                                        fprintf(stderr, "%s: cannot open\n",
153 +                                                        argv[i]);
154 +                                        exit(1);
155 +                                }
156 +                                continue;
157 +                        case 'z':               /* z-file ist verbotten */
158 +                                fprintf(stderr, "%s: -z option not allowed\n",
159 +                                                argv[0]);
160 +                                exit(1);
161                          case 'o':               /* output file */
162                                  if (argv[i][2])
163                                          break;
164                                  outfile = argv[++i];
165                                  continue;
166 <                        }
166 >                        } else if (i >= argc-1)
167 >                                break;
168                  rpargv[rpargc++] = argv[i];
169          }
170 <        rpargv[rpargc] = NULL;
170 >        if (i >= argc) {
171 >                fprintf(stderr, "%s: missing octree argument\n", argv[0]);
172 >                exit(1);
173 >        }
174          if (outfile == NULL) {
175                  fprintf(stderr, "%s: missing output file\n", argv[0]);
176                  exit(1);
177          }
178          init(argc, argv);
179          rpiece();
180 <        rval = cleanup();
107 <        exit(rval);
180 >        exit(cleanup(0));
181   }
182  
183  
184 + dolock(fd, ltyp)                /* lock or unlock a file */
185 + int  fd;
186 + int  ltyp;
187 + {
188 +        static struct flock  fls;       /* static so initialized to zeroes */
189 +
190 +        fls.l_type = ltyp;
191 +        if (fcntl(fd, F_SETLKW, &fls) < 0) {
192 +                fprintf(stderr, "%s: cannot lock/unlock file\n", progname);
193 +                exit(1);
194 +        }
195 + }
196 +
197 +
198   init(ac, av)                    /* set up output file and start rpict */
199   int  ac;
200   char  **av;
201   {
202 +        static char  hrbuf[16], vrbuf[16];
203          extern char  VersionID[];
204          char  *err;
205          FILE  *fp;
# Line 121 | Line 209 | char  **av;
209                  fprintf(stderr, "%s: %s\n", progname, err);
210                  exit(1);
211          }
212 +        if (syncfp != NULL) {
213 +                sflock(F_RDLCK);
214 +                fscanf(syncfp, "%d %d", &hmult, &vmult);
215 +                sflock(F_UNLCK);
216 +        }
217 +                                        /* compute piece size */
218 +        hres /= hmult;
219 +        vres /= vmult;
220          normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres);
221 +        sprintf(hrbuf, "%d", hres);
222 +        rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf;
223 +        sprintf(vrbuf, "%d", vres);
224 +        rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf;
225 +        rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0";
226 +        rpargv[rpargc++] = av[ac-1];
227 +        rpargv[rpargc] = NULL;
228                                          /* open output file */
229          if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
230 +                dolock(outfd, F_WRLCK);
231                  if ((fp = fdopen(dup(outfd), "w")) == NULL)
232                          goto filerr;
233                  printargs(ac, av, fp);          /* write header */
# Line 137 | Line 241 | char  **av;
241                  putc('\n', fp);
242                  fprtresolu(hres*hmult, vres*vmult, fp);
243          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
244 <                sleep(30);                      /* wait for header */
244 >                dolock(outfd, F_RDLCK);
245                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
246                          goto filerr;
247 <                getheader(fp, NULL);            /* skip header */
248 <                if (fscnresolu(&hr, &vr, fp) < 0 ||     /* check resolution */
247 >                getheader(fp, NULL, NULL);      /* skip header */
248 >                if (!fscnresolu(&hr, &vr, fp) ||        /* check resolution */
249                                  hr != hres*hmult || vr != vres*vmult) {
250 <                        fprintf(stderr, "%s: picture resolution mismatch\n",
251 <                                        outfile);
250 >                        fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n",
251 >                                        progname, outfile);
252                          exit(1);
253                  }
254          } else {
255 <                fprintf(stderr, "%s: cannot open\n", outfile);
255 >                fprintf(stderr, "%s: cannot open file \"%s\"\n",
256 >                                progname, outfile);
257                  exit(1);
258          }
259          scanorig = ftell(fp);           /* record position of first scanline */
260          if (fclose(fp) == -1)           /* done with stream i/o */
261                  goto filerr;
262 +        dolock(outfd, F_UNLCK);
263                                          /* start rpict process */
264          if (open_process(rpd, rpargv) <= 0) {
265 <                fprintf(stderr, "%s: cannot start\n", rpargv[0]);
265 >                fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
266                  exit(1);
267          }
268          if ((fromrp = fdopen(rpd[0], "r")) == NULL ||
# Line 165 | Line 271 | char  **av;
271                                  progname, rpargv[0]);
272                  exit(1);
273          }
274 <        if ((scanline = (COLR *)malloc(hres*sizeof(COLR))) == NULL) {
274 >        if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) {
275                  fprintf(stderr, "%s: out of memory\n", progname);
276                  exit(1);
277          }
278 +        signal(SIGALRM, onalrm);
279          return;
280   filerr:
281 <        fprintf(stderr, "%s: file i/o error\n", outfile);
281 >        fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
282          exit(1);
283   }
284  
285  
286   int
287 < cleanup()                       /* close rpict process and clean up */
287 > nextpiece(xp, yp)               /* get next piece assignment */
288 > int  *xp, *yp;
289   {
290 <        register int  rpstat;
290 >        if (gotalrm)                    /* someone wants us to quit */
291 >                return(0);
292 >        if (syncfp != NULL) {           /* use sync file */
293 >                /*
294 >                 * So we don't necessarily have to lock and unlock the file
295 >                 * multiple times (very slow), we establish an exclusive
296 >                 * lock at the beginning on our synchronization file and
297 >                 * maintain it in the subroutine rvrpiece().
298 >                 */
299 >                sflock(F_WRLCK);
300 >                fseek(syncfp, 0L, 0);           /* read position */
301 >                if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) {
302 >                        *xp = hmult-1;
303 >                        *yp = vmult;
304 >                }
305 >                if (rvrlim == 0)                /* initialize recovery limit */
306 >                        rvrlim = *xp*vmult + *yp;
307 >                if (rvrpiece(xp, yp)) {         /* do stragglers first */
308 >                        sflock(F_UNLCK);
309 >                        return(1);
310 >                }
311 >                if (--(*yp) < 0) {              /* decrement position */
312 >                        *yp = vmult-1;
313 >                        if (--(*xp) < 0) {      /* all done */
314 >                                sflock(F_UNLCK);
315 >                                return(0);
316 >                        }
317 >                }
318 >                fseek(syncfp, 0L, 0);           /* write new position */
319 >                fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp);
320 >                fflush(syncfp);
321 >                sflock(F_UNLCK);                /* release sync file */
322 >                return(1);
323 >        }
324 >        return(scanf("%d %d", xp, yp) == 2);    /* use stdin */
325 > }
326  
327 <        free((char *)scanline);
327 >
328 > int
329 > rvrpiece(xp, yp)                /* check for recoverable pieces */
330 > register int  *xp, *yp;
331 > {
332 >        static char  *pdone = NULL;     /* which pieces are done */
333 >        static long  readpos = -1;      /* how far we've read */
334 >        register int  i;
335 >        /*
336 >         * This routine is called by nextpiece() with an
337 >         * exclusive lock on syncfp and the file pointer at the
338 >         * appropriate position to read in the finished pieces.
339 >         */
340 >        if (rvrlim < 0)
341 >                return(0);              /* only check if asked */
342 >        if (pdone == NULL)              /* first call */
343 >                pdone = calloc(hmult*vmult, sizeof(char));
344 >        if (readpos != -1)              /* mark what's been done */
345 >                fseek(syncfp, readpos, 0);
346 >        while (fscanf(syncfp, "%d %d", xp, yp) == 2)
347 >                pdone[*xp*vmult+*yp] = 1;
348 >        if (!feof(syncfp)) {
349 >                fprintf(stderr, "%s: format error in sync file\n", progname);
350 >                exit(1);
351 >        }
352 >        readpos = ftell(syncfp);
353 >        i = hmult*vmult;                /* find an unaccounted for piece */
354 >        while (i-- > rvrlim)
355 >                if (!pdone[i]) {
356 >                        *xp = i / vmult;
357 >                        *yp = i % vmult;
358 >                        pdone[i] = 1;   /* consider it done */
359 >                        return(1);
360 >                }
361 >        rvrlim = -1;                    /* nothing left to recover */
362 >        free(pdone);
363 >        pdone = NULL;
364 >        return(0);
365 > }
366 >
367 >
368 > int
369 > cleanup(rstat)                  /* close rpict process and clean up */
370 > int  rstat;
371 > {
372 >        int  status;
373 >
374 >        bfree((char *)pbuf, hres*vres*sizeof(COLR));
375          fclose(torp);
376          fclose(fromrp);
377 <        rpstat = close_process(rpd);
378 <        if (rpstat == -1)
379 <                rpstat = 0;
380 <        return(rpstat);
377 >        while (wait(&status) != -1)
378 >                if (rstat == 0)
379 >                        rstat = status>>8 & 0xff;
380 >        return(rstat);
381   }
382  
383  
384   rpiece()                        /* render picture piece by piece */
385   {
196        extern char  *gets(), *strcat();
386          VIEW  pview;
198        char  buf[48];
387          int  xorg, yorg;
388 <        
389 <        while (gets(buf) != NULL) {
390 <                if (sscanf(buf, "%d %d", &xorg, &yorg) != 2) {
391 <                        fprintf(stderr, "%s: input format error\n", progname);
392 <                        exit(1);
393 <                }
394 <                copystruct(&pview, &ourview);   /* compute view for piece */
395 <                switch (ourview.type) {
396 <                case VT_PER:
397 <                        pview.horiz = 2.*180./PI*atan(
398 <                                        tan(PI/180./2.*ourview.horiz)/hmult );
399 <                        pview.vert = 2.*180./PI*atan(
400 <                                        tan(PI/180./2.*ourview.vert)/vmult );
401 <                        break;
402 <                case VT_PAR:
403 <                case VT_ANG:
404 <                        pview.horiz = ourview.horiz / hmult;
405 <                        pview.vert = ourview.vert / vmult;
406 <                        break;
407 <                case VT_HEM:
408 <                default:
409 <                        fprintf(stderr, "%s: unknown view type '-vt%c'\n",
410 <                                        progname, ourview.type);
411 <                        exit(1);
412 <                }
413 <                pview.hoff += xorg - 0.5*(hmult-1);
414 <                pview.voff += yorg - 0.5*(vmult-1);
388 >                                        /* compute view parameters */
389 >        copystruct(&pview, &ourview);
390 >        switch (ourview.type) {
391 >        case VT_PER:
392 >                pview.horiz = 2.*180./PI*atan(
393 >                                tan(PI/180./2.*ourview.horiz)/hmult );
394 >                pview.vert = 2.*180./PI*atan(
395 >                                tan(PI/180./2.*ourview.vert)/vmult );
396 >                break;
397 >        case VT_PAR:
398 >        case VT_ANG:
399 >                pview.horiz = ourview.horiz / hmult;
400 >                pview.vert = ourview.vert / vmult;
401 >                break;
402 >        case VT_HEM:
403 >                pview.horiz = 2.*180./PI*asin(
404 >                                sin(PI/180./2.*ourview.horiz)/hmult );
405 >                pview.vert = 2.*180./PI*asin(
406 >                                sin(PI/180./2.*ourview.vert)/vmult );
407 >                break;
408 >        default:
409 >                fprintf(stderr, "%s: unknown view type '-vt%c'\n",
410 >                                progname, ourview.type);
411 >                exit(cleanup(1));
412 >        }
413 >                                        /* render each piece */
414 >        while (nextpiece(&xorg, &yorg)) {
415 >                pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1);
416 >                pview.voff = ourview.voff + yorg - 0.5*(vmult-1);
417                  fputs(VIEWSTR, torp);
418                  fprintview(&pview, torp);
419                  putc('\n', torp);
420 <                fflush(torp);                   /* assign piece to rpict */
420 >                fflush(torp);                   /* assigns piece to rpict */
421                  putpiece(xorg, yorg);           /* place piece in output */
232                if (verbose) {                  /* notify caller */
233                        strcat(buf, " done\n");
234                        writebuf(fileno(stdout), buf, strlen(buf));
235                }
422          }
423   }
424  
425  
426 + int
427   putpiece(xpos, ypos)            /* get next piece from rpict */
428   int  xpos, ypos;
429   {
430 +        struct flock  fls;
431 +        int  pid, status;
432          int  hr, vr;
433 <        int  y;
434 <
435 <        getheader(fromrp, NULL);        /* discard header info. */
436 <        if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */
437 <                        hr != hres || vr != vres) {
438 <                fprintf(stderr, "%s: resolution mismatch from rpict\n",
250 <                                progname);
251 <                exit(1);
433 >        register int  y;
434 >                                /* check bounds */
435 >        if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) {
436 >                fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
437 >                                progname, xpos, ypos);
438 >                exit(cleanup(1));
439          }
440 <        for (y = 0; y < vr; y++) {      /* transfer scanlines */
441 <                if (freadcolrs(scanline, hr, fromrp) < 0) {
442 <                        fprintf(stderr, "%s: read error from rpict\n",
443 <                                        progname);
444 <                        exit(1);
440 >                                /* check header from rpict */
441 >        guard_io();
442 >        getheader(fromrp, NULL, NULL);
443 >        if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) {
444 >                fprintf(stderr, "%s: resolution mismatch from %s\n",
445 >                                progname, rpargv[0]);
446 >                exit(cleanup(1));
447 >        }
448 >        if (verbose) {                          /* notify caller */
449 >                printf("%d %d begun\n", xpos, ypos);
450 >                fflush(stdout);
451 >        }
452 >        unguard();
453 >                                /* load new piece into buffer */
454 >        for (y = 0; y < vr; y++) {
455 >                guard_io();
456 >                if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
457 >                        fprintf(stderr, "%s: read error from %s\n",
458 >                                        progname, rpargv[0]);
459 >                        exit(cleanup(1));
460                  }
461 <                if ((y == 0 || hmult != 1) && lseek(outfd,
462 < scanorig+((long)((vmult-1-ypos)*vres+y)*hmult*hres+xpos*hres)*sizeof(COLR),
463 <                                0) == -1) {
464 <                        fprintf(stderr, "%s: seek error\n", outfile);
465 <                        exit(1);
461 >                unguard();
462 >        }
463 > #if MAXFORK
464 >                                /* fork so we don't slow rpict down */
465 >        if ((pid = fork()) > 0) {
466 >                if (++nforked >= MAXFORK) {
467 >                        wait(&status);          /* reap a child */
468 >                        if (status)
469 >                                exit(cleanup(status>>8 & 0xff));
470 >                        nforked--;
471                  }
472 <                if (writebuf(outfd, (char *)scanline, hr*sizeof(COLR)) !=
473 <                                hr*sizeof(COLR)) {
474 <                        fprintf(stderr, "%s: write error\n", outfile);
475 <                        exit(1);
472 >                return(pid);
473 >        }
474 > #else
475 >        pid = -1;               /* no forking */
476 > #endif
477 >        fls.l_start = scanorig +
478 >                ((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR);
479 > #if NFS
480 >        fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR);
481 >                                /* lock file section so NFS doesn't mess up */
482 >        fls.l_whence = 0;
483 >        fls.l_type = F_WRLCK;
484 >        if (fcntl(outfd, F_SETLKW, &fls) < 0)
485 >                filerr("lock");
486 > #endif
487 >                                /* write new piece to file */
488 >        if (lseek(outfd, fls.l_start, 0) == -1)
489 >                filerr("seek");
490 >        if (hmult == 1) {
491 >                if (writebuf(outfd, (char *)pbuf,
492 >                                vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
493 >                        filerr("write");
494 >        } else
495 >                for (y = 0; y < vr; y++) {
496 >                        if (writebuf(outfd, (char *)(pbuf+y*hr),
497 >                                        hr*sizeof(COLR)) != hr*sizeof(COLR))
498 >                                filerr("write");
499 >                        if (y < vr-1 && lseek(outfd,
500 >                                        (long)(hmult-1)*hr*sizeof(COLR),
501 >                                        1) == -1)
502 >                                filerr("seek");
503                  }
504 + #if NFS
505 +        fls.l_type = F_UNLCK;           /* release lock */
506 +        if (fcntl(outfd, F_SETLKW, &fls) < 0)
507 +                filerr("lock");
508 + #endif
509 +        if (syncfp != NULL) {                   /* record what's been done */
510 +                sflock(F_WRLCK);
511 +                fseek(syncfp, 0L, 2);           /* append index */
512 +                fprintf(syncfp, "%4d %4d\n", xpos, ypos);
513 +                fflush(syncfp);
514 +                                /*** Unlock not necessary, since
515 +                sflock(F_UNLCK);        _exit() or nextpiece() is next ***/
516          }
517 +        if (verbose) {                          /* notify caller */
518 +                printf("%d %d done\n", xpos, ypos);
519 +                fflush(stdout);
520 +        }
521 +        if (pid == -1)          /* didn't fork or fork failed */
522 +                return(0);
523 +        _exit(0);               /* else exit child process (releasing locks) */
524   }
525 +
526 +
527 + filerr(t)                       /* report file error and exit */
528 + char  *t;
529 + {
530 +        extern char  *sys_errlist[];
531 +
532 +        fprintf(stderr, "%s: %s error on file \"%s\": %s\n",
533 +                        progname, t, outfile, sys_errlist[errno]);
534 +        _exit(1);
535 + }
536 +
537 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines