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.38 by schorsch, Sun Jun 8 12:03:11 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 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"
9 < #include <fcntl.h>
9 >
10 > #ifndef F_SETLKW
11 >
12 > main(argc, argv)
13 > int argc;
14 > char *argv[];
15 > {
16 >        fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]);
17 >        exit(1);
18 > }
19 >
20 > #else
21 >
22   #include <signal.h>
23   #include "color.h"
24   #include "view.h"
16 #include "resolu.h"
25  
26 + #ifndef NFS
27 + #define  NFS                    1
28 + #endif
29                                  /* set the following to 0 to forgo forking */
30   #ifndef MAXFORK
31 + #if NFS
32   #define  MAXFORK                3       /* allotment of duped processes */
33 + #else
34 + #define  MAXFORK                0
35   #endif
36 + #endif
37 +                                        /* protection from SYSV signals(!) */
38 + #if defined(sgi)
39 + #define guard_io()      sighold(SIGALRM)
40 + #define unguard()       sigrelse(SIGALRM)
41 + #endif
42 + #ifndef guard_io
43 + #define guard_io()      0
44 + #define unguard()       0
45 + #endif
46  
47 + extern char  *strerror();
48 +
49                                  /* rpict command */
50 < char  *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"};
51 < int  rpargc = 9;
50 > char  *rpargv[128] = {"rpict", "-S", "1"};
51 > int  rpargc = 3;
52   int  rpd[3];
53   FILE  *torp, *fromrp;
54   COLR  *pbuf;
55                                  /* our view parameters */
56   VIEW  ourview = STDVIEW;
57   double  pixaspect = 1.0;
58 < int  hres = 512, vres = 512, hmult = 2, vmult = 2;
58 > int  hres = 1024, vres = 1024, hmult = 4, vmult = 4;
59                                  /* output file */
60   char  *outfile = NULL;
61   int  outfd;
62   long  scanorig;
63 < int  syncfd = -1;               /* lock file descriptor */
63 > FILE  *syncfp = NULL;           /* synchronization file pointer */
64 > int  synclst = F_UNLCK;         /* synchronization file lock status */
65   int  nforked = 0;
66  
67 + #define  sflock(t)      if ((t)!=synclst) dolock(fileno(syncfp),synclst=t)
68 +
69   char  *progname;
70   int  verbose = 0;
71 + unsigned  timelim = 0;
72 + int  rvrlim = -1;
73  
43 extern long  lseek(), ftell();
44
74   int  gotalrm = 0;
75 < int  onalrm() { gotalrm++; }
75 > void  onalrm(int i) { gotalrm++; }
76  
77  
78   main(argc, argv)
# Line 54 | Line 83 | char  *argv[];
83          
84          progname = argv[0];
85          for (i = 1; i < argc; i++) {
86 +                                                /* expand arguments */
87 +                while ((rval = expandarg(&argc, &argv, i)) > 0)
88 +                        ;
89 +                if (rval < 0) {
90 +                        fprintf(stderr, "%s: cannot expand '%s'",
91 +                                        argv[0], argv[i]);
92 +                        exit(1);
93 +                }
94                  if (argv[i][0] == '-')
95                          switch (argv[i][1]) {
96                          case 'v':
# Line 78 | Line 115 | char  *argv[];
115                                  }
116                                  break;
117                          case 'p':               /* pixel aspect ratio? */
118 <                                if (argv[i][2] == 'a' && !argv[i][3])
119 <                                        pixaspect = atof(argv[i+1]);
118 >                                if (argv[i][2] != 'a' || argv[i][3])
119 >                                        break;
120 >                                pixaspect = atof(argv[++i]);
121 >                                continue;
122 >                        case 'T':               /* time limit (hours) */
123 >                                if (argv[i][2])
124 >                                        break;
125 >                                timelim = atof(argv[++i])*3600. + .5;
126                                  break;
127 <                        case 'x':               /* piece x resolution */
128 <                                if (!argv[i][2])
129 <                                        hres = atoi(argv[i+1]);
130 <                                break;
131 <                        case 'y':               /* piece y resolution */
132 <                                if (!argv[i][2])
133 <                                        vres = atoi(argv[i+1]);
134 <                                break;
127 >                        case 'x':               /* overall x resolution */
128 >                                if (argv[i][2])
129 >                                        break;
130 >                                hres = atoi(argv[++i]);
131 >                                continue;
132 >                        case 'y':               /* overall y resolution */
133 >                                if (argv[i][2])
134 >                                        break;
135 >                                vres = atoi(argv[++i]);
136 >                                continue;
137                          case 'X':               /* horizontal multiplier */
138                                  if (argv[i][2])
139                                          break;
# Line 99 | Line 144 | char  *argv[];
144                                          break;
145                                  vmult = atoi(argv[++i]);
146                                  continue;
147 +                        case 'R':               /* recover */
148 +                                if (argv[i][2])
149 +                                        break;
150 +                                rvrlim = 0;
151 +                        /* fall through */
152                          case 'F':               /* syncronization file */
153                                  if (argv[i][2])
154                                          break;
155 <                                if ((syncfd = open(argv[++i], O_RDWR)) < 0) {
155 >                                if ((syncfp =
156 >                fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) {
157                                          fprintf(stderr, "%s: cannot open\n",
158                                                          argv[i]);
159                                          exit(1);
160                                  }
161                                  continue;
162 +                        case 'z':               /* z-file ist verbotten */
163 +                                fprintf(stderr, "%s: -z option not allowed\n",
164 +                                                argv[0]);
165 +                                exit(1);
166                          case 'o':               /* output file */
167                                  if (argv[i][2])
168                                          break;
169                                  outfile = argv[++i];
170                                  continue;
171 <                        }
171 >                        } else if (i >= argc-1)
172 >                                break;
173                  rpargv[rpargc++] = argv[i];
174          }
175 <        rpargv[rpargc] = NULL;
175 >        if (i >= argc) {
176 >                fprintf(stderr, "%s: missing octree argument\n", argv[0]);
177 >                exit(1);
178 >        }
179          if (outfile == NULL) {
180                  fprintf(stderr, "%s: missing output file\n", argv[0]);
181                  exit(1);
182          }
183          init(argc, argv);
184          rpiece();
185 <        rval = cleanup(0);
127 <        exit(rval);
185 >        exit(cleanup(0));
186   }
187  
188  
189 + dolock(fd, ltyp)                /* lock or unlock a file */
190 + int  fd;
191 + int  ltyp;
192 + {
193 +        static struct flock  fls;       /* static so initialized to zeroes */
194 +
195 +        fls.l_type = ltyp;
196 +        if (fcntl(fd, F_SETLKW, &fls) < 0) {
197 +                fprintf(stderr, "%s: cannot lock/unlock file: %s\n",
198 +                                progname, strerror(errno));
199 +                exit(1);
200 +        }
201 + }
202 +
203 +
204   init(ac, av)                    /* set up output file and start rpict */
205   int  ac;
206   char  **av;
207   {
208 +        static char  hrbuf[16], vrbuf[16];
209          extern char  VersionID[];
210          char  *err;
211          FILE  *fp;
# Line 141 | Line 215 | char  **av;
215                  fprintf(stderr, "%s: %s\n", progname, err);
216                  exit(1);
217          }
218 <        if (syncfd != -1) {
219 <                char  buf[32];
220 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
221 <                sscanf(buf, "%d %d", &hmult, &vmult);
218 >        if (syncfp != NULL) {
219 >                sflock(F_RDLCK);
220 >                fscanf(syncfp, "%d %d", &hmult, &vmult);
221 >                sflock(F_UNLCK);
222          }
223 +                                        /* compute piece size */
224 +        hres /= hmult;
225 +        vres /= vmult;
226          normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres);
227 +        sprintf(hrbuf, "%d", hres);
228 +        rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf;
229 +        sprintf(vrbuf, "%d", vres);
230 +        rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf;
231 +        rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0";
232 +        rpargv[rpargc++] = av[ac-1];
233 +        rpargv[rpargc] = NULL;
234                                          /* open output file */
235          if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
236 +                dolock(outfd, F_WRLCK);
237                  if ((fp = fdopen(dup(outfd), "w")) == NULL)
238                          goto filerr;
239 <                printargs(ac, av, fp);          /* write header */
239 >                newheader("RADIANCE", fp);      /* create header */
240 >                printargs(ac, av, fp);
241                  fprintf(fp, "SOFTWARE= %s\n", VersionID);
242                  fputs(VIEWSTR, fp);
243                  fprintview(&ourview, fp);
# Line 162 | Line 248 | char  **av;
248                  putc('\n', fp);
249                  fprtresolu(hres*hmult, vres*vmult, fp);
250          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
251 +                dolock(outfd, F_RDLCK);
252                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
253                          goto filerr;
254 <                getheader(fp, NULL);            /* skip header */
255 <                if (fscnresolu(&hr, &vr, fp) < 0 ||     /* check resolution */
254 >                getheader(fp, NULL, NULL);      /* skip header */
255 >                if (!fscnresolu(&hr, &vr, fp) ||        /* check resolution */
256                                  hr != hres*hmult || vr != vres*vmult) {
257                          fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n",
258                                          progname, outfile);
# Line 179 | Line 266 | char  **av;
266          scanorig = ftell(fp);           /* record position of first scanline */
267          if (fclose(fp) == -1)           /* done with stream i/o */
268                  goto filerr;
269 <        sync();                         /* flush NFS buffers */
269 >        dolock(outfd, F_UNLCK);
270                                          /* start rpict process */
271          if (open_process(rpd, rpargv) <= 0) {
272                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
# Line 191 | Line 278 | char  **av;
278                                  progname, rpargv[0]);
279                  exit(1);
280          }
281 <        if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) {
281 >        if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) {
282                  fprintf(stderr, "%s: out of memory\n", progname);
283                  exit(1);
284          }
285          signal(SIGALRM, onalrm);
286 +        if (timelim)
287 +                alarm(timelim);
288          return;
289   filerr:
290          fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
# Line 207 | Line 296 | int
296   nextpiece(xp, yp)               /* get next piece assignment */
297   int  *xp, *yp;
298   {
210        extern char  *fgets();
211        struct flock  fls;
212        char  buf[64];
213
299          if (gotalrm)                    /* someone wants us to quit */
300                  return(0);
301 <        if (syncfd != -1) {             /* use sync file */
302 <                fls.l_type = F_WRLCK;           /* gain exclusive access */
303 <                fls.l_whence = 0;
304 <                fls.l_start = 0L;
305 <                fls.l_len = 0L;
306 <                fcntl(syncfd, F_SETLKW, &fls);
307 <                lseek(syncfd, 0L, 0);
308 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
309 <                if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
301 >        if (syncfp != NULL) {           /* use sync file */
302 >                /*
303 >                 * So we don't necessarily have to lock and unlock the file
304 >                 * multiple times (very slow), we establish an exclusive
305 >                 * lock at the beginning on our synchronization file and
306 >                 * maintain it in the subroutine rvrpiece().
307 >                 */
308 >                sflock(F_WRLCK);
309 >                fseek(syncfp, 0L, 0);           /* read position */
310 >                if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) {
311                          *xp = hmult-1;
312                          *yp = vmult;
313                  }
314 +                if (rvrlim == 0)                /* initialize recovery limit */
315 +                        rvrlim = *xp*vmult + *yp;
316 +                if (rvrpiece(xp, yp)) {         /* do stragglers first */
317 +                        sflock(F_UNLCK);
318 +                        return(1);
319 +                }
320                  if (--(*yp) < 0) {              /* decrement position */
321                          *yp = vmult-1;
322 <                        if (--(*xp) < 0) {      /* all done! */
323 <                                close(syncfd);
322 >                        if (--(*xp) < 0) {      /* all done */
323 >                                sflock(F_UNLCK);
324                                  return(0);
325                          }
326                  }
327 <                sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp);
328 <                lseek(syncfd, 0L, 0);           /* write new position */
329 <                write(syncfd, buf, strlen(buf));
330 <                fls.l_type = F_UNLCK;           /* release sync file */
239 <                fcntl(syncfd, F_SETLKW, &fls);
327 >                fseek(syncfp, 0L, 0);           /* write new position */
328 >                fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp);
329 >                fflush(syncfp);
330 >                sflock(F_UNLCK);                /* release sync file */
331                  return(1);
332          }
333 <        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));
333 >        return(scanf("%d %d", xp, yp) == 2);    /* use stdin */
334   }
335  
336  
337   int
338 + rvrpiece(xp, yp)                /* check for recoverable pieces */
339 + register int  *xp, *yp;
340 + {
341 +        static char  *pdone = NULL;     /* which pieces are done */
342 +        static long  readpos = -1;      /* how far we've read */
343 +        register int  i;
344 +        /*
345 +         * This routine is called by nextpiece() with an
346 +         * exclusive lock on syncfp and the file pointer at the
347 +         * appropriate position to read in the finished pieces.
348 +         */
349 +        if (rvrlim < 0)
350 +                return(0);              /* only check if asked */
351 +        if (pdone == NULL)              /* first call */
352 +                pdone = calloc(hmult*vmult, sizeof(char));
353 +        if (pdone == NULL) {
354 +                fprintf(stderr, "%s: out of memory\n", progname);
355 +                exit(1);
356 +        }
357 +        if (readpos != -1)              /* mark what's been done */
358 +                fseek(syncfp, readpos, 0);
359 +        while (fscanf(syncfp, "%d %d", xp, yp) == 2)
360 +                pdone[*xp*vmult+*yp] = 1;
361 +        if (!feof(syncfp)) {
362 +                fprintf(stderr, "%s: format error in sync file\n", progname);
363 +                exit(1);
364 +        }
365 +        readpos = ftell(syncfp);
366 +        i = hmult*vmult;                /* find an unaccounted for piece */
367 +        while (i-- > rvrlim)
368 +                if (!pdone[i]) {
369 +                        *xp = i / vmult;
370 +                        *yp = i % vmult;
371 +                        pdone[i] = 1;   /* consider it done */
372 +                        return(1);
373 +                }
374 +        rvrlim = -1;                    /* nothing left to recover */
375 +        free(pdone);
376 +        pdone = NULL;
377 +        return(0);
378 + }
379 +
380 +
381 + int
382   cleanup(rstat)                  /* close rpict process and clean up */
383   int  rstat;
384   {
385          int  status;
386  
387 <        free((char *)pbuf);
387 >        bfree((char *)pbuf, hres*vres*sizeof(COLR));
388          fclose(torp);
389          fclose(fromrp);
390          while (wait(&status) != -1)
# Line 282 | Line 412 | rpiece()                       /* render picture piece by piece */
412                  pview.horiz = ourview.horiz / hmult;
413                  pview.vert = ourview.vert / vmult;
414                  break;
415 +        case VT_CYL:
416 +                pview.horiz = ourview.horiz / hmult;
417 +                pview.vert = 2.*180./PI*atan(
418 +                                tan(PI/180./2.*ourview.vert)/vmult );
419 +                break;
420          case VT_HEM:
421                  pview.horiz = 2.*180./PI*asin(
422                                  sin(PI/180./2.*ourview.horiz)/hmult );
# Line 295 | Line 430 | rpiece()                       /* render picture piece by piece */
430          }
431                                          /* render each piece */
432          while (nextpiece(&xorg, &yorg)) {
433 <                pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1);
434 <                pview.voff = ourview.voff + yorg - 0.5*(vmult-1);
433 >                pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1);
434 >                pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1);
435                  fputs(VIEWSTR, torp);
436                  fprintview(&pview, torp);
437                  putc('\n', torp);
438                  fflush(torp);                   /* assigns piece to rpict */
439                  putpiece(xorg, yorg);           /* place piece in output */
305                if (verbose) {                  /* notify caller */
306                        printf("%d %d done\n", xorg, yorg);
307                        fflush(stdout);
308                }
440          }
441   }
442  
# Line 325 | Line 456 | int  xpos, ypos;
456                  exit(cleanup(1));
457          }
458                                  /* check header from rpict */
459 <        getheader(fromrp, NULL);
460 <        if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres | vr != vres) {
459 >        guard_io();
460 >        getheader(fromrp, NULL, NULL);
461 >        if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) {
462                  fprintf(stderr, "%s: resolution mismatch from %s\n",
463                                  progname, rpargv[0]);
464                  exit(cleanup(1));
465          }
466 +        if (verbose) {                          /* notify caller */
467 +                printf("%d %d begun\n", xpos, ypos);
468 +                fflush(stdout);
469 +        }
470 +        unguard();
471                                  /* load new piece into buffer */
472 <        for (y = 0; y < vr; y++)
472 >        for (y = 0; y < vr; y++) {
473 >                guard_io();
474                  if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
475                          fprintf(stderr, "%s: read error from %s\n",
476                                          progname, rpargv[0]);
477                          exit(cleanup(1));
478                  }
479 +                unguard();
480 +        }
481   #if MAXFORK
482                                  /* fork so we don't slow rpict down */
483          if ((pid = fork()) > 0) {
# Line 352 | Line 492 | int  xpos, ypos;
492   #else
493          pid = -1;               /* no forking */
494   #endif
495 +        fls.l_start = scanorig +
496 +                ((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR);
497 + #if NFS
498 +        fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR);
499                                  /* lock file section so NFS doesn't mess up */
500          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;
501          fls.l_type = F_WRLCK;
502 <        fcntl(outfd, F_SETLKW, &fls);
502 >        if (fcntl(outfd, F_SETLKW, &fls) < 0)
503 >                filerr("lock");
504 > #endif
505                                  /* write new piece to file */
506 <        if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1)
507 <                goto seekerr;
506 >        if (lseek(outfd, (off_t)fls.l_start, 0) < 0)
507 >                filerr("seek");
508          if (hmult == 1) {
509                  if (writebuf(outfd, (char *)pbuf,
510                                  vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
511 <                        goto writerr;
511 >                        filerr("write");
512          } else
513                  for (y = 0; y < vr; y++) {
514                          if (writebuf(outfd, (char *)(pbuf+y*hr),
515                                          hr*sizeof(COLR)) != hr*sizeof(COLR))
516 <                                goto writerr;
516 >                                filerr("write");
517                          if (y < vr-1 && lseek(outfd,
518 <                                        (long)(hmult-1)*hr*sizeof(COLR),
519 <                                        1) == -1)
520 <                                goto seekerr;
518 >                                        (off_t)(hmult-1)*hr*sizeof(COLR),
519 >                                        1) < 0)
520 >                                filerr("seek");
521                  }
522 <        if (pid == -1) {        /* fork failed */
523 <                fls.l_type = F_UNLCK;           /* release lock */
524 <                fcntl(outfd, F_SETLKW, &fls);
525 <                return(0);
522 > #if NFS
523 >        fls.l_type = F_UNLCK;           /* release lock */
524 >        if (fcntl(outfd, F_SETLKW, &fls) < 0)
525 >                filerr("lock");
526 > #endif
527 >        if (verbose) {                          /* notify caller */
528 >                printf("%d %d done\n", xpos, ypos);
529 >                fflush(stdout);
530          }
531 <        _exit(0);               /* else exit child process (releasing lock) */
532 < seekerr:
533 <        fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
531 >        if (syncfp != NULL) {                   /* record what's been done */
532 >                sflock(F_WRLCK);
533 >                fseek(syncfp, 0L, 2);           /* append index */
534 >                fprintf(syncfp, "%4d %4d\n", xpos, ypos);
535 >                fflush(syncfp);
536 >                                /*** Unlock not necessary, since
537 >                sflock(F_UNLCK);        _exit() or nextpiece() is next ***/
538 >        }
539 >        if (pid == -1)          /* didn't fork or fork failed */
540 >                return(0);
541 >        _exit(0);               /* else exit child process (releasing locks) */
542 > }
543 >
544 >
545 > filerr(t)                       /* report file error and exit */
546 > char  *t;
547 > {
548 >        fprintf(stderr, "%s: %s error on file \"%s\": %s\n",
549 >                        progname, t, outfile, strerror(errno));
550          _exit(1);
387 writerr:
388        fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile);
389        _exit(1);
551   }
552 +
553 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines