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.34 by gregl, Wed May 28 17:22:52 1997 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  
80   extern long  lseek(), ftell();
81  
# Line 54 | Line 91 | char  *argv[];
91          
92          progname = argv[0];
93          for (i = 1; i < argc; i++) {
94 +                                                /* expand arguments */
95 +                while (rval = expandarg(&argc, &argv, i))
96 +                        if (rval < 0) {
97 +                                fprintf(stderr, "%s: cannot expand '%s'",
98 +                                                argv[0], argv[i]);
99 +                                exit(1);
100 +                        }
101                  if (argv[i][0] == '-')
102                          switch (argv[i][1]) {
103                          case 'v':
# Line 78 | Line 122 | char  *argv[];
122                                  }
123                                  break;
124                          case 'p':               /* pixel aspect ratio? */
125 <                                if (argv[i][2] == 'a' && !argv[i][3])
126 <                                        pixaspect = atof(argv[i+1]);
125 >                                if (argv[i][2] != 'a' || argv[i][3])
126 >                                        break;
127 >                                pixaspect = atof(argv[i+1]);
128 >                                continue;
129 >                        case 'T':               /* time limit (hours) */
130 >                                if (argv[i][2])
131 >                                        break;
132 >                                timelim = atof(argv[++i])*3600. + .5;
133                                  break;
134 <                        case 'x':               /* piece x resolution */
135 <                                if (!argv[i][2])
136 <                                        hres = atoi(argv[i+1]);
137 <                                break;
138 <                        case 'y':               /* piece y resolution */
139 <                                if (!argv[i][2])
140 <                                        vres = atoi(argv[i+1]);
141 <                                break;
134 >                        case 'x':               /* overall x resolution */
135 >                                if (argv[i][2])
136 >                                        break;
137 >                                hres = atoi(argv[++i]);
138 >                                continue;
139 >                        case 'y':               /* overall y resolution */
140 >                                if (argv[i][2])
141 >                                        break;
142 >                                vres = atoi(argv[++i]);
143 >                                continue;
144                          case 'X':               /* horizontal multiplier */
145                                  if (argv[i][2])
146                                          break;
# Line 99 | Line 151 | char  *argv[];
151                                          break;
152                                  vmult = atoi(argv[++i]);
153                                  continue;
154 +                        case 'R':               /* recover */
155 +                                if (argv[i][2])
156 +                                        break;
157 +                                rvrlim = 0;
158 +                        /* fall through */
159                          case 'F':               /* syncronization file */
160                                  if (argv[i][2])
161                                          break;
162 <                                if ((syncfd = open(argv[++i], O_RDWR)) < 0) {
162 >                                if ((syncfp =
163 >                fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) {
164                                          fprintf(stderr, "%s: cannot open\n",
165                                                          argv[i]);
166                                          exit(1);
167                                  }
168                                  continue;
169 +                        case 'z':               /* z-file ist verbotten */
170 +                                fprintf(stderr, "%s: -z option not allowed\n",
171 +                                                argv[0]);
172 +                                exit(1);
173                          case 'o':               /* output file */
174                                  if (argv[i][2])
175                                          break;
176                                  outfile = argv[++i];
177                                  continue;
178 <                        }
178 >                        } else if (i >= argc-1)
179 >                                break;
180                  rpargv[rpargc++] = argv[i];
181          }
182 <        rpargv[rpargc] = NULL;
182 >        if (i >= argc) {
183 >                fprintf(stderr, "%s: missing octree argument\n", argv[0]);
184 >                exit(1);
185 >        }
186          if (outfile == NULL) {
187                  fprintf(stderr, "%s: missing output file\n", argv[0]);
188                  exit(1);
189          }
190          init(argc, argv);
191          rpiece();
192 <        rval = cleanup(0);
127 <        exit(rval);
192 >        exit(cleanup(0));
193   }
194  
195  
196 + dolock(fd, ltyp)                /* lock or unlock a file */
197 + int  fd;
198 + int  ltyp;
199 + {
200 +        static struct flock  fls;       /* static so initialized to zeroes */
201 +
202 +        fls.l_type = ltyp;
203 +        if (fcntl(fd, F_SETLKW, &fls) < 0) {
204 +                fprintf(stderr, "%s: cannot lock/unlock file: %s\n",
205 +                                progname, sys_errlist[errno]);
206 +                exit(1);
207 +        }
208 + }
209 +
210 +
211   init(ac, av)                    /* set up output file and start rpict */
212   int  ac;
213   char  **av;
214   {
215 +        static char  hrbuf[16], vrbuf[16];
216          extern char  VersionID[];
217          char  *err;
218          FILE  *fp;
# Line 141 | Line 222 | char  **av;
222                  fprintf(stderr, "%s: %s\n", progname, err);
223                  exit(1);
224          }
225 <        if (syncfd != -1) {
226 <                char  buf[32];
227 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
228 <                sscanf(buf, "%d %d", &hmult, &vmult);
225 >        if (syncfp != NULL) {
226 >                sflock(F_RDLCK);
227 >                fscanf(syncfp, "%d %d", &hmult, &vmult);
228 >                sflock(F_UNLCK);
229          }
230 +                                        /* compute piece size */
231 +        hres /= hmult;
232 +        vres /= vmult;
233          normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres);
234 +        sprintf(hrbuf, "%d", hres);
235 +        rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf;
236 +        sprintf(vrbuf, "%d", vres);
237 +        rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf;
238 +        rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0";
239 +        rpargv[rpargc++] = av[ac-1];
240 +        rpargv[rpargc] = NULL;
241                                          /* open output file */
242          if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
243 +                dolock(outfd, F_WRLCK);
244                  if ((fp = fdopen(dup(outfd), "w")) == NULL)
245                          goto filerr;
246 <                printargs(ac, av, fp);          /* write header */
246 >                newheader("RADIANCE", fp);      /* create header */
247 >                printargs(ac, av, fp);
248                  fprintf(fp, "SOFTWARE= %s\n", VersionID);
249                  fputs(VIEWSTR, fp);
250                  fprintview(&ourview, fp);
# Line 162 | Line 255 | char  **av;
255                  putc('\n', fp);
256                  fprtresolu(hres*hmult, vres*vmult, fp);
257          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
258 +                dolock(outfd, F_RDLCK);
259                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
260                          goto filerr;
261 <                getheader(fp, NULL);            /* skip header */
262 <                if (fscnresolu(&hr, &vr, fp) < 0 ||     /* check resolution */
261 >                getheader(fp, NULL, NULL);      /* skip header */
262 >                if (!fscnresolu(&hr, &vr, fp) ||        /* check resolution */
263                                  hr != hres*hmult || vr != vres*vmult) {
264                          fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n",
265                                          progname, outfile);
# Line 179 | Line 273 | char  **av;
273          scanorig = ftell(fp);           /* record position of first scanline */
274          if (fclose(fp) == -1)           /* done with stream i/o */
275                  goto filerr;
276 <        sync();                         /* flush NFS buffers */
276 >        dolock(outfd, F_UNLCK);
277                                          /* start rpict process */
278          if (open_process(rpd, rpargv) <= 0) {
279                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
# Line 191 | Line 285 | char  **av;
285                                  progname, rpargv[0]);
286                  exit(1);
287          }
288 <        if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) {
288 >        if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) {
289                  fprintf(stderr, "%s: out of memory\n", progname);
290                  exit(1);
291          }
292          signal(SIGALRM, onalrm);
293 +        if (timelim)
294 +                alarm(timelim);
295          return;
296   filerr:
297          fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
# Line 207 | Line 303 | int
303   nextpiece(xp, yp)               /* get next piece assignment */
304   int  *xp, *yp;
305   {
210        extern char  *fgets();
211        struct flock  fls;
212        char  buf[64];
213
306          if (gotalrm)                    /* someone wants us to quit */
307                  return(0);
308 <        if (syncfd != -1) {             /* use sync file */
309 <                fls.l_type = F_WRLCK;           /* gain exclusive access */
310 <                fls.l_whence = 0;
311 <                fls.l_start = 0L;
312 <                fls.l_len = 0L;
313 <                fcntl(syncfd, F_SETLKW, &fls);
314 <                lseek(syncfd, 0L, 0);
315 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
316 <                if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
308 >        if (syncfp != NULL) {           /* use sync file */
309 >                /*
310 >                 * So we don't necessarily have to lock and unlock the file
311 >                 * multiple times (very slow), we establish an exclusive
312 >                 * lock at the beginning on our synchronization file and
313 >                 * maintain it in the subroutine rvrpiece().
314 >                 */
315 >                sflock(F_WRLCK);
316 >                fseek(syncfp, 0L, 0);           /* read position */
317 >                if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) {
318                          *xp = hmult-1;
319                          *yp = vmult;
320                  }
321 +                if (rvrlim == 0)                /* initialize recovery limit */
322 +                        rvrlim = *xp*vmult + *yp;
323 +                if (rvrpiece(xp, yp)) {         /* do stragglers first */
324 +                        sflock(F_UNLCK);
325 +                        return(1);
326 +                }
327                  if (--(*yp) < 0) {              /* decrement position */
328                          *yp = vmult-1;
329 <                        if (--(*xp) < 0) {      /* all done! */
330 <                                close(syncfd);
329 >                        if (--(*xp) < 0) {      /* all done */
330 >                                sflock(F_UNLCK);
331                                  return(0);
332                          }
333                  }
334 <                sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp);
335 <                lseek(syncfd, 0L, 0);           /* write new position */
336 <                write(syncfd, buf, strlen(buf));
337 <                fls.l_type = F_UNLCK;           /* release sync file */
239 <                fcntl(syncfd, F_SETLKW, &fls);
334 >                fseek(syncfp, 0L, 0);           /* write new position */
335 >                fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp);
336 >                fflush(syncfp);
337 >                sflock(F_UNLCK);                /* release sync file */
338                  return(1);
339          }
340 <        if (fgets(buf, sizeof(buf), stdin) == NULL)     /* use stdin */
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));
340 >        return(scanf("%d %d", xp, yp) == 2);    /* use stdin */
341   }
342  
343  
344   int
345 + rvrpiece(xp, yp)                /* check for recoverable pieces */
346 + register int  *xp, *yp;
347 + {
348 +        static char  *pdone = NULL;     /* which pieces are done */
349 +        static long  readpos = -1;      /* how far we've read */
350 +        register int  i;
351 +        /*
352 +         * This routine is called by nextpiece() with an
353 +         * exclusive lock on syncfp and the file pointer at the
354 +         * appropriate position to read in the finished pieces.
355 +         */
356 +        if (rvrlim < 0)
357 +                return(0);              /* only check if asked */
358 +        if (pdone == NULL)              /* first call */
359 +                pdone = calloc(hmult*vmult, sizeof(char));
360 +        if (pdone == NULL) {
361 +                fprintf(stderr, "%s: out of memory\n", progname);
362 +                exit(1);
363 +        }
364 +        if (readpos != -1)              /* mark what's been done */
365 +                fseek(syncfp, readpos, 0);
366 +        while (fscanf(syncfp, "%d %d", xp, yp) == 2)
367 +                pdone[*xp*vmult+*yp] = 1;
368 +        if (!feof(syncfp)) {
369 +                fprintf(stderr, "%s: format error in sync file\n", progname);
370 +                exit(1);
371 +        }
372 +        readpos = ftell(syncfp);
373 +        i = hmult*vmult;                /* find an unaccounted for piece */
374 +        while (i-- > rvrlim)
375 +                if (!pdone[i]) {
376 +                        *xp = i / vmult;
377 +                        *yp = i % vmult;
378 +                        pdone[i] = 1;   /* consider it done */
379 +                        return(1);
380 +                }
381 +        rvrlim = -1;                    /* nothing left to recover */
382 +        free(pdone);
383 +        pdone = NULL;
384 +        return(0);
385 + }
386 +
387 +
388 + int
389   cleanup(rstat)                  /* close rpict process and clean up */
390   int  rstat;
391   {
392          int  status;
393  
394 <        free((char *)pbuf);
394 >        bfree((char *)pbuf, hres*vres*sizeof(COLR));
395          fclose(torp);
396          fclose(fromrp);
397          while (wait(&status) != -1)
# Line 282 | Line 419 | rpiece()                       /* render picture piece by piece */
419                  pview.horiz = ourview.horiz / hmult;
420                  pview.vert = ourview.vert / vmult;
421                  break;
422 +        case VT_CYL:
423 +                pview.horiz = ourview.horiz / hmult;
424 +                pview.vert = 2.*180./PI*atan(
425 +                                tan(PI/180./2.*ourview.vert)/vmult );
426 +                break;
427          case VT_HEM:
428                  pview.horiz = 2.*180./PI*asin(
429                                  sin(PI/180./2.*ourview.horiz)/hmult );
# Line 295 | Line 437 | rpiece()                       /* render picture piece by piece */
437          }
438                                          /* render each piece */
439          while (nextpiece(&xorg, &yorg)) {
440 <                pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1);
441 <                pview.voff = ourview.voff + yorg - 0.5*(vmult-1);
440 >                pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1);
441 >                pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1);
442                  fputs(VIEWSTR, torp);
443                  fprintview(&pview, torp);
444                  putc('\n', torp);
445                  fflush(torp);                   /* assigns piece to rpict */
446                  putpiece(xorg, yorg);           /* place piece in output */
305                if (verbose) {                  /* notify caller */
306                        printf("%d %d done\n", xorg, yorg);
307                        fflush(stdout);
308                }
447          }
448   }
449  
# Line 325 | Line 463 | int  xpos, ypos;
463                  exit(cleanup(1));
464          }
465                                  /* check header from rpict */
466 <        getheader(fromrp, NULL);
467 <        if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres | vr != vres) {
466 >        guard_io();
467 >        getheader(fromrp, NULL, NULL);
468 >        if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) {
469                  fprintf(stderr, "%s: resolution mismatch from %s\n",
470                                  progname, rpargv[0]);
471                  exit(cleanup(1));
472          }
473 +        if (verbose) {                          /* notify caller */
474 +                printf("%d %d begun\n", xpos, ypos);
475 +                fflush(stdout);
476 +        }
477 +        unguard();
478                                  /* load new piece into buffer */
479 <        for (y = 0; y < vr; y++)
479 >        for (y = 0; y < vr; y++) {
480 >                guard_io();
481                  if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
482                          fprintf(stderr, "%s: read error from %s\n",
483                                          progname, rpargv[0]);
484                          exit(cleanup(1));
485                  }
486 +                unguard();
487 +        }
488   #if MAXFORK
489                                  /* fork so we don't slow rpict down */
490          if ((pid = fork()) > 0) {
# Line 352 | Line 499 | int  xpos, ypos;
499   #else
500          pid = -1;               /* no forking */
501   #endif
502 +        fls.l_start = scanorig +
503 +                ((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR);
504 + #if NFS
505 +        fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR);
506                                  /* lock file section so NFS doesn't mess up */
507          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;
508          fls.l_type = F_WRLCK;
509 <        fcntl(outfd, F_SETLKW, &fls);
509 >        if (fcntl(outfd, F_SETLKW, &fls) < 0)
510 >                filerr("lock");
511 > #endif
512                                  /* write new piece to file */
513 <        if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1)
514 <                goto seekerr;
513 >        if (lseek(outfd, fls.l_start, 0) == -1)
514 >                filerr("seek");
515          if (hmult == 1) {
516                  if (writebuf(outfd, (char *)pbuf,
517                                  vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
518 <                        goto writerr;
518 >                        filerr("write");
519          } else
520                  for (y = 0; y < vr; y++) {
521                          if (writebuf(outfd, (char *)(pbuf+y*hr),
522                                          hr*sizeof(COLR)) != hr*sizeof(COLR))
523 <                                goto writerr;
523 >                                filerr("write");
524                          if (y < vr-1 && lseek(outfd,
525                                          (long)(hmult-1)*hr*sizeof(COLR),
526                                          1) == -1)
527 <                                goto seekerr;
527 >                                filerr("seek");
528                  }
529 <        if (pid == -1) {        /* fork failed */
530 <                fls.l_type = F_UNLCK;           /* release lock */
531 <                fcntl(outfd, F_SETLKW, &fls);
532 <                return(0);
529 > #if NFS
530 >        fls.l_type = F_UNLCK;           /* release lock */
531 >        if (fcntl(outfd, F_SETLKW, &fls) < 0)
532 >                filerr("lock");
533 > #endif
534 >        if (verbose) {                          /* notify caller */
535 >                printf("%d %d done\n", xpos, ypos);
536 >                fflush(stdout);
537          }
538 <        _exit(0);               /* else exit child process (releasing lock) */
539 < seekerr:
540 <        fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
538 >        if (syncfp != NULL) {                   /* record what's been done */
539 >                sflock(F_WRLCK);
540 >                fseek(syncfp, 0L, 2);           /* append index */
541 >                fprintf(syncfp, "%4d %4d\n", xpos, ypos);
542 >                fflush(syncfp);
543 >                                /*** Unlock not necessary, since
544 >                sflock(F_UNLCK);        _exit() or nextpiece() is next ***/
545 >        }
546 >        if (pid == -1)          /* didn't fork or fork failed */
547 >                return(0);
548 >        _exit(0);               /* else exit child process (releasing locks) */
549 > }
550 >
551 >
552 > filerr(t)                       /* report file error and exit */
553 > char  *t;
554 > {
555 >        fprintf(stderr, "%s: %s error on file \"%s\": %s\n",
556 >                        progname, t, outfile, sys_errlist[errno]);
557          _exit(1);
387 writerr:
388        fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile);
389        _exit(1);
558   }
559 +
560 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines