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.21 by greg, Fri Jun 4 17:03:29 1993 UTC vs.
Revision 2.28 by greg, Tue Oct 19 16:02:39 1993 UTC

# 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  
# Line 40 | Line 39 | char *argv[];
39   #endif
40   #endif
41                                          /* protection from SYSV signals(!) */
42 < #if defined(sgi) || defined(hpux)
42 > #if defined(sgi)
43   #define guard_io()      sighold(SIGALRM)
44   #define unguard()       sigrelse(SIGALRM)
45   #endif
# Line 49 | Line 48 | char *argv[];
48   #define unguard()       0
49   #endif
50                                  /* rpict command */
51 < char  *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"};
52 < int  rpargc = 9;
51 > char  *rpargv[128] = {"rpict", "-S", "1"};
52 > int  rpargc = 3;
53   int  rpd[3];
54   FILE  *torp, *fromrp;
55   COLR  *pbuf;
56                                  /* our view parameters */
57   VIEW  ourview = STDVIEW;
58   double  pixaspect = 1.0;
59 < int  hres = 512, vres = 512, hmult = 2, vmult = 2;
59 > int  hres = 1024, vres = 1024, hmult = 4, vmult = 4;
60                                  /* output file */
61   char  *outfile = NULL;
62   int  outfd;
63   long  scanorig;
64 < int  syncfd = -1;               /* lock file descriptor */
64 > FILE  *syncfp = NULL;           /* synchronization file pointer */
65 > int  synclst = F_UNLCK;         /* synchronization file lock status */
66   int  nforked = 0;
67  
68 + #define  sflock(t)      if ((t)!=synclst) dolock(fileno(syncfp),synclst=t)
69 +
70   char  *progname;
71   int  verbose = 0;
72 + int  rvrlim = -1;
73  
74   extern long  lseek(), ftell();
75  
# Line 82 | 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 106 | 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 127 | 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 ((syncfd = open(argv[++i],
152 <                                                O_RDWR|O_CREAT, 0666)) < 0) {
151 >                                if ((syncfp =
152 >                fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) {
153                                          fprintf(stderr, "%s: cannot open\n",
154                                                          argv[i]);
155                                          exit(1);
# Line 146 | Line 164 | char  *argv[];
164                                          break;
165                                  outfile = argv[++i];
166                                  continue;
167 <                        }
167 >                        } else if (i >= argc-1)
168 >                                break;
169                  rpargv[rpargc++] = argv[i];
170          }
171 <        rpargv[rpargc] = NULL;
171 >        if (i >= argc) {
172 >                fprintf(stderr, "%s: missing octree argument\n", argv[0]);
173 >                exit(1);
174 >        }
175          if (outfile == NULL) {
176                  fprintf(stderr, "%s: missing output file\n", argv[0]);
177                  exit(1);
# Line 160 | Line 182 | char  *argv[];
182   }
183  
184  
185 + dolock(fd, ltyp)                /* lock or unlock a file */
186 + int  fd;
187 + int  ltyp;
188 + {
189 +        static struct flock  fls;       /* static so initialized to zeroes */
190 +        extern char  *sys_errlist[];
191 +
192 +        fls.l_type = ltyp;
193 +        if (fcntl(fd, F_SETLKW, &fls) < 0) {
194 +                fprintf(stderr, "%s: cannot lock/unlock file: %s\n",
195 +                                progname, sys_errlist[errno]);
196 +                exit(1);
197 +        }
198 + }
199 +
200 +
201   init(ac, av)                    /* set up output file and start rpict */
202   int  ac;
203   char  **av;
204   {
205 +        static char  hrbuf[16], vrbuf[16];
206          extern char  VersionID[];
207          char  *err;
208          FILE  *fp;
# Line 173 | Line 212 | char  **av;
212                  fprintf(stderr, "%s: %s\n", progname, err);
213                  exit(1);
214          }
215 <        if (syncfd != -1) {
216 <                char  buf[32];
217 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
218 <                sscanf(buf, "%d %d", &hmult, &vmult);
215 >        if (syncfp != NULL) {
216 >                sflock(F_RDLCK);
217 >                fscanf(syncfp, "%d %d", &hmult, &vmult);
218 >                sflock(F_UNLCK);
219          }
220 +                                        /* compute piece size */
221 +        hres /= hmult;
222 +        vres /= vmult;
223          normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres);
224 +        sprintf(hrbuf, "%d", hres);
225 +        rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf;
226 +        sprintf(vrbuf, "%d", vres);
227 +        rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf;
228 +        rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0";
229 +        rpargv[rpargc++] = av[ac-1];
230 +        rpargv[rpargc] = NULL;
231                                          /* open output file */
232          if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
233 +                dolock(outfd, F_WRLCK);
234                  if ((fp = fdopen(dup(outfd), "w")) == NULL)
235                          goto filerr;
236                  printargs(ac, av, fp);          /* write header */
# Line 194 | Line 244 | char  **av;
244                  putc('\n', fp);
245                  fprtresolu(hres*hmult, vres*vmult, fp);
246          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
247 +                dolock(outfd, F_RDLCK);
248                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
249                          goto filerr;
250                  getheader(fp, NULL, NULL);      /* skip header */
# Line 211 | Line 262 | char  **av;
262          scanorig = ftell(fp);           /* record position of first scanline */
263          if (fclose(fp) == -1)           /* done with stream i/o */
264                  goto filerr;
265 < #if NFS
215 <        sync();                         /* flush NFS buffers */
216 < #endif
265 >        dolock(outfd, F_UNLCK);
266                                          /* start rpict process */
267          if (open_process(rpd, rpargv) <= 0) {
268                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
# Line 225 | Line 274 | char  **av;
274                                  progname, rpargv[0]);
275                  exit(1);
276          }
277 <        if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) {
277 >        if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) {
278                  fprintf(stderr, "%s: out of memory\n", progname);
279                  exit(1);
280          }
# Line 241 | Line 290 | int
290   nextpiece(xp, yp)               /* get next piece assignment */
291   int  *xp, *yp;
292   {
244        struct flock  fls;
245        char  buf[64];
246
293          if (gotalrm)                    /* someone wants us to quit */
294                  return(0);
295 <        if (syncfd != -1) {             /* use sync file */
296 <                fls.l_type = F_WRLCK;           /* gain exclusive access */
297 <                fls.l_whence = 0;
298 <                fls.l_start = 0L;
299 <                fls.l_len = 0L;
300 <                fcntl(syncfd, F_SETLKW, &fls);
301 <                lseek(syncfd, 0L, 0);
302 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
303 <                if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
295 >        if (syncfp != NULL) {           /* use sync file */
296 >                /*
297 >                 * So we don't necessarily have to lock and unlock the file
298 >                 * multiple times (very slow), we establish an exclusive
299 >                 * lock at the beginning on our synchronization file and
300 >                 * maintain it in the subroutine rvrpiece().
301 >                 */
302 >                sflock(F_WRLCK);
303 >                fseek(syncfp, 0L, 0);           /* read position */
304 >                if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) {
305                          *xp = hmult-1;
306                          *yp = vmult;
307                  }
308 +                if (rvrlim == 0)                /* initialize recovery limit */
309 +                        rvrlim = *xp*vmult + *yp;
310 +                if (rvrpiece(xp, yp)) {         /* do stragglers first */
311 +                        sflock(F_UNLCK);
312 +                        return(1);
313 +                }
314                  if (--(*yp) < 0) {              /* decrement position */
315                          *yp = vmult-1;
316 <                        if (--(*xp) < 0) {      /* all done! */
317 <                                close(syncfd);
316 >                        if (--(*xp) < 0) {      /* all done */
317 >                                sflock(F_UNLCK);
318                                  return(0);
319                          }
320                  }
321 <                sprintf(buf, "%4d %4d\n%4d %4d\n", hmult, vmult, *xp, *yp);
322 <                lseek(syncfd, 0L, 0);           /* write new position */
323 <                write(syncfd, buf, strlen(buf));
324 <                fls.l_type = F_UNLCK;           /* release sync file */
272 <                fcntl(syncfd, F_SETLKW, &fls);
321 >                fseek(syncfp, 0L, 0);           /* write new position */
322 >                fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp);
323 >                fflush(syncfp);
324 >                sflock(F_UNLCK);                /* release sync file */
325                  return(1);
326          }
327 <        if (fgets(buf, sizeof(buf), stdin) == NULL)     /* use stdin */
276 <                return(0);
277 <        if (sscanf(buf, "%d %d", xp, yp) == 2)
278 <                return(1);
279 <        fprintf(stderr, "%s: input format error\n", progname);
280 <        exit(cleanup(1));
327 >        return(scanf("%d %d", xp, yp) == 2);    /* use stdin */
328   }
329  
330  
331   int
332 + rvrpiece(xp, yp)                /* check for recoverable pieces */
333 + register int  *xp, *yp;
334 + {
335 +        static char  *pdone = NULL;     /* which pieces are done */
336 +        static long  readpos = -1;      /* how far we've read */
337 +        register int  i;
338 +        /*
339 +         * This routine is called by nextpiece() with an
340 +         * exclusive lock on syncfp and the file pointer at the
341 +         * appropriate position to read in the finished pieces.
342 +         */
343 +        if (rvrlim < 0)
344 +                return(0);              /* only check if asked */
345 +        if (pdone == NULL)              /* first call */
346 +                pdone = calloc(hmult*vmult, sizeof(char));
347 +        if (readpos != -1)              /* mark what's been done */
348 +                fseek(syncfp, readpos, 0);
349 +        while (fscanf(syncfp, "%d %d", xp, yp) == 2)
350 +                pdone[*xp*vmult+*yp] = 1;
351 +        if (!feof(syncfp)) {
352 +                fprintf(stderr, "%s: format error in sync file\n", progname);
353 +                exit(1);
354 +        }
355 +        readpos = ftell(syncfp);
356 +        i = hmult*vmult;                /* find an unaccounted for piece */
357 +        while (i-- > rvrlim)
358 +                if (!pdone[i]) {
359 +                        *xp = i / vmult;
360 +                        *yp = i % vmult;
361 +                        pdone[i] = 1;   /* consider it done */
362 +                        return(1);
363 +                }
364 +        rvrlim = -1;                    /* nothing left to recover */
365 +        free(pdone);
366 +        pdone = NULL;
367 +        return(0);
368 + }
369 +
370 +
371 + int
372   cleanup(rstat)                  /* close rpict process and clean up */
373   int  rstat;
374   {
375          int  status;
376  
377 <        free((char *)pbuf);
377 >        bfree((char *)pbuf, hres*vres*sizeof(COLR));
378          fclose(torp);
379          fclose(fromrp);
380          while (wait(&status) != -1)
# Line 361 | Line 448 | int  xpos, ypos;
448                                  progname, rpargv[0]);
449                  exit(cleanup(1));
450          }
451 +        if (verbose) {                          /* notify caller */
452 +                printf("%d %d begun\n", xpos, ypos);
453 +                fflush(stdout);
454 +        }
455          unguard();
456                                  /* load new piece into buffer */
457          for (y = 0; y < vr; y++) {
# Line 393 | Line 484 | int  xpos, ypos;
484                                  /* lock file section so NFS doesn't mess up */
485          fls.l_whence = 0;
486          fls.l_type = F_WRLCK;
487 <        fcntl(outfd, F_SETLKW, &fls);
487 >        if (fcntl(outfd, F_SETLKW, &fls) < 0)
488 >                filerr("lock");
489   #endif
490                                  /* write new piece to file */
491          if (lseek(outfd, fls.l_start, 0) == -1)
492 <                goto seekerr;
492 >                filerr("seek");
493          if (hmult == 1) {
494                  if (writebuf(outfd, (char *)pbuf,
495                                  vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
496 <                        goto writerr;
496 >                        filerr("write");
497          } else
498                  for (y = 0; y < vr; y++) {
499                          if (writebuf(outfd, (char *)(pbuf+y*hr),
500                                          hr*sizeof(COLR)) != hr*sizeof(COLR))
501 <                                goto writerr;
501 >                                filerr("write");
502                          if (y < vr-1 && lseek(outfd,
503                                          (long)(hmult-1)*hr*sizeof(COLR),
504                                          1) == -1)
505 <                                goto seekerr;
505 >                                filerr("seek");
506                  }
507 + #if NFS
508 +        fls.l_type = F_UNLCK;           /* release lock */
509 +        if (fcntl(outfd, F_SETLKW, &fls) < 0)
510 +                filerr("lock");
511 + #endif
512 +        if (syncfp != NULL) {                   /* record what's been done */
513 +                sflock(F_WRLCK);
514 +                fseek(syncfp, 0L, 2);           /* append index */
515 +                fprintf(syncfp, "%4d %4d\n", xpos, ypos);
516 +                fflush(syncfp);
517 +                                /*** Unlock not necessary, since
518 +                sflock(F_UNLCK);        _exit() or nextpiece() is next ***/
519 +        }
520          if (verbose) {                          /* notify caller */
521                  printf("%d %d done\n", xpos, ypos);
522                  fflush(stdout);
523          }
524 <        if (pid == -1) {        /* didn't fork or fork failed */
420 < #if NFS
421 <                fls.l_type = F_UNLCK;           /* release lock */
422 <                fcntl(outfd, F_SETLKW, &fls);
423 < #endif
524 >        if (pid == -1)          /* didn't fork or fork failed */
525                  return(0);
526 <        }
527 <        _exit(0);               /* else exit child process (releasing lock) */
528 < seekerr:
529 <        fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
530 <        _exit(1);
531 < writerr:
532 <        fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile);
526 >        _exit(0);               /* else exit child process (releasing locks) */
527 > }
528 >
529 >
530 > filerr(t)                       /* report file error and exit */
531 > char  *t;
532 > {
533 >        extern char  *sys_errlist[];
534 >
535 >        fprintf(stderr, "%s: %s error on file \"%s\": %s\n",
536 >                        progname, t, outfile, sys_errlist[errno]);
537          _exit(1);
538   }
539  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines