--- ray/src/util/rad.c 2010/05/18 00:01:02 2.93 +++ ray/src/util/rad.c 2013/12/04 20:07:07 2.110 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rad.c,v 2.93 2010/05/18 00:01:02 greg Exp $"; +static const char RCSid[] = "$Id: rad.c,v 2.110 2013/12/04 20:07:07 greg Exp $"; #endif /* * Executive program for oconv, rpict and pfilt @@ -165,7 +165,7 @@ static int touch(char *fn); static int runcom(char *cs); static int rmfile(char *fn); static int mvfile(char *fold, char *fnew); -static int next_process(void); +static int next_process(int reserve); static void wait_process(int all); static void finish_process(void); static void badvalue(int vc); @@ -264,8 +264,8 @@ userr: static void rootname( /* remove tail from end of fn */ - register char *rn, - register char *fn + char *rn, + char *fn ) { char *tp, *dp; @@ -282,7 +282,7 @@ rootname( /* remove tail from end of fn */ static time_t checklast( /* check files and find most recent */ - register char *fnames + char *fnames ) { char thisfile[PATH_MAX]; @@ -312,8 +312,8 @@ newfname( /* create modified file name */ int pred ) { - register char *cp; - register int n; + char *cp; + int n; int suffix; n = 0; cp = orig; suffix = -1; /* suffix position, length */ @@ -378,7 +378,7 @@ getoctcube( /* get octree bounding cube */ double min[3], max[3]; char buf[1024]; FILE *fp; - register int i; + int i; if (osiz <= FTINY) { if (!nprocs && fdate(oct1name) < @@ -558,6 +558,7 @@ oconv(void) /* run oconv and mkillum if necessary * unlink(illumtmp); quit(1); } + rmfile(oct0name); /* make octree1 (frozen) */ if (octreedate) sprintf(combuf, "%s%s -f -i %s %s > %s", c_oconv, @@ -586,8 +587,8 @@ oconv(void) /* run oconv and mkillum if necessary * static char * addarg( /* append argument and advance pointer */ -register char *op, -register char *arg +char *op, +char *arg ) { while (*op) @@ -601,7 +602,7 @@ register char *arg static void oconvopts( /* get oconv options */ - register char *oo + char *oo ) { /* BEWARE: This may be called via setdefaults(), so no assumptions */ @@ -710,7 +711,7 @@ renderopts( /* set rendering options */ static void lowqopts( /* low quality rendering options */ - register char *op, + char *op, char *po ) { @@ -751,7 +752,7 @@ lowqopts( /* low quality rendering options */ op = addarg(op, "-ds .4"); else op = addarg(op, "-ds 0"); - op = addarg(op, "-dt .2 -dc .25 -dr 0 -sj 0 -st .5"); + op = addarg(op, "-dt .2 -dc .25 -dr 0 -ss 0 -st .5"); if (vdef(AMBFILE)) { sprintf(op, " -af %s", vval(AMBFILE)); op += strlen(op); @@ -772,13 +773,13 @@ lowqopts( /* low quality rendering options */ d = ambval(); sprintf(op, " -av %.2g %.2g %.2g", d, d, d); op += strlen(op); - op = addarg(op, "-lr 6 -lw .01"); + op = addarg(op, "-lr 6 -lw .003"); } static void medqopts( /* medium quality rendering options */ - register char *op, + char *op, char *po ) { @@ -826,7 +827,7 @@ medqopts( /* medium quality rendering options */ op = addarg(op, "-ds .2 -dj .9"); else op = addarg(op, "-ds .3"); - op = addarg(op, "-dt .1 -dc .5 -dr 1 -sj .7 -st .1"); + op = addarg(op, "-dt .1 -dc .5 -dr 1 -ss 1 -st .1"); if ( (overture = vint(INDIRECT)) ) { sprintf(op, " -ab %d", overture); op += strlen(op); @@ -850,13 +851,13 @@ medqopts( /* medium quality rendering options */ d = ambval(); sprintf(op, " -av %.2g %.2g %.2g", d, d, d); op += strlen(op); - op = addarg(op, "-lr 8 -lw .002"); + op = addarg(op, "-lr 8 -lw 1e-4"); } static void hiqopts( /* high quality rendering options */ - register char *op, + char *op, char *po ) { @@ -904,7 +905,7 @@ hiqopts( /* high quality rendering options */ op = addarg(op, "-ds .1 -dj .9"); else op = addarg(op, "-ds .2"); - op = addarg(op, "-dt .05 -dc .75 -dr 3 -sj 1 -st .01"); + op = addarg(op, "-dt .05 -dc .75 -dr 3 -ss 16 -st .01"); sprintf(op, " -ab %d", overture=vint(INDIRECT)+1); op += strlen(op); if (vdef(AMBFILE)) { @@ -926,17 +927,40 @@ hiqopts( /* high quality rendering options */ d = ambval(); sprintf(op, " -av %.2g %.2g %.2g", d, d, d); op += strlen(op); - op = addarg(op, "-lr 12 -lw .0005"); + op = addarg(op, "-lr 12 -lw 1e-5"); } +#ifdef _WIN32 static void +setenv( /* set an environment variable */ + char *vname, + char *value +) +{ + char *evp; + + evp = bmalloc(strlen(vname)+strlen(value)+2); + if (evp == NULL) + syserr(progname); + sprintf(evp, "%s=%s", vname, value); + if (putenv(evp) != 0) { + fprintf(stderr, "%s: out of environment space\n", progname); + quit(1); + } + if (!silent) + printf("set %s\n", evp); +} +#endif + + +static void xferopts( /* transfer options if indicated */ char *ro ) { int fd, n; - register char *cp; + char *cp; n = strlen(ro); if (n < 2) @@ -965,7 +989,7 @@ xferopts( /* transfer options if indicated */ static void pfiltopts( /* get pfilt options */ - register char *po + char *po ) { *po = '\0'; @@ -992,8 +1016,8 @@ pfiltopts( /* get pfilt options */ static int matchword( /* match white-delimited words */ - register char *s1, - register char *s2 + char *s1, + char *s2 ) { while (isspace(*s1)) s1++; @@ -1007,15 +1031,15 @@ matchword( /* match white-delimited words */ static char * specview( /* get proper view spec from vs */ - register char *vs + char *vs ) { static char vup[7][12] = {"-vu 0 0 -1","-vu 0 -1 0","-vu -1 0 0", "-vu 0 0 1", "-vu 1 0 0","-vu 0 1 0","-vu 0 0 1"}; static char viewopts[128]; - register char *cp; + char *cp; int xpos, ypos, zpos, viewtype, upax; - register int i; + int i; double cent[3], dim[3], mult, d; if (vs == NULL || *vs == '-') @@ -1075,7 +1099,7 @@ specview( /* get proper view spec from vs */ cent[i] += .5*dim[i]; } mult = vlet(ZONE)=='E' ? 2. : .45 ; - sprintf(cp, " -vp %.2g %.2g %.2g -vd %.2g %.2g %.2g", + sprintf(cp, " -vp %.3g %.3g %.3g -vd %.3g %.3g %.3g", cent[0]+xpos*mult*dim[0], cent[1]+ypos*mult*dim[1], cent[2]+zpos*mult*dim[2], @@ -1107,7 +1131,7 @@ specview( /* get proper view spec from vs */ break; case VT_PAR: d = sqrt(dim[0]*dim[0]+dim[1]*dim[1]+dim[2]*dim[2]); - sprintf(cp, " -vh %.2g -vv %.2g", d, d); + sprintf(cp, " -vh %.3g -vv %.3g", d, d); cp += strlen(cp); break; case VT_ANG: @@ -1147,7 +1171,7 @@ getview( /* get view n, or NULL if none */ char *vn /* returned view name */ ) { - register char *mv; + char *mv; if (viewselect != NULL) { /* command-line selected */ if (n) /* only do one */ @@ -1179,7 +1203,7 @@ numview: mv = nvalue(VIEWS, n); /* use view n */ if ((vn != NULL) & (mv != NULL)) if (*mv != '-') { - register char *mv2 = mv; + char *mv2 = mv; while (*mv2 && !isspace(*mv2)) *vn++ = *mv2++; *vn = '\0'; @@ -1192,13 +1216,13 @@ numview: static int myprintview( /* print out selected view */ - register char *vopts, + char *vopts, FILE *fp ) { VIEW vwr; char buf[128]; - register char *cp; + char *cp; #ifdef _WIN32 /* XXX Should we allow something like this for all platforms? */ /* XXX Or is it still required at all? */ @@ -1254,6 +1278,32 @@ rvu( /* run rvu with first view */ } +static int +syncf_done( /* check if an rpiece sync file is complete */ + char *sfname +) +{ + FILE *fp = fopen(sfname, "r"); + int todo = 1; + int x, y; + + if (fp == NULL) + return(0); + if (fscanf(fp, "%d %d", &x, &y) != 2) + goto checked; + todo = x*y; /* total number of tiles */ + if (fscanf(fp, "%d %d", &x, &y) != 2 || (x != 0) | (y != 0)) + goto checked; + /* XXX assume no redundant tiles */ + while (fscanf(fp, "%d %d", &x, &y) == 2) + if (!--todo) + break; +checked: + fclose(fp); + return(!todo); +} + + static void rpict( /* run rpict and pfilt for each view */ char *opts, @@ -1261,15 +1311,18 @@ rpict( /* run rpict and pfilt for each view */ ) { #define do_rpiece (sfile[0]!='\0') - char combuf[4*PATH_MAX+512]; + char combuf[5*PATH_MAX+512]; char rawfile[PATH_MAX], picfile[PATH_MAX]; char zopt[PATH_MAX+4], rep[PATH_MAX+16], res[32]; - char rppopt[128], sfile[64], *pfile = NULL; + char rppopt[32], sfile[PATH_MAX], *pfile = NULL; char pfopts[128]; char vs[32], *vw; int vn, mult; FILE *fp; time_t rfdt, pfdt; + int xres, yres; + double aspect; + int n; /* get pfilt options */ pfiltopts(pfopts); /* get resolution, reporting */ @@ -1284,24 +1337,19 @@ rpict( /* run rpict and pfilt for each view */ mult = 3; break; } - { - int xres, yres; - double aspect; - int n; - n = sscanf(vval(RESOLUTION), "%d %d %lf", &xres, &yres, &aspect); - if (n == 3) - sprintf(res, "-x %d -y %d -pa %.3f", - mult*xres, mult*yres, aspect); - else if (n) { - if (n == 1) yres = xres; - sprintf(res, "-x %d -y %d", mult*xres, mult*yres); - } else - badvalue(RESOLUTION); - } + n = sscanf(vval(RESOLUTION), "%d %d %lf", &xres, &yres, &aspect); + if (n == 3) + sprintf(res, "-x %d -y %d -pa %.3f", + mult*xres, mult*yres, aspect); + else if (n) { + aspect = 1.; + if (n == 1) yres = xres; + sprintf(res, "-x %d -y %d", mult*xres, mult*yres); + } else + badvalue(RESOLUTION); rep[0] = '\0'; if (vdef(REPORT)) { double minutes; - int n; n = sscanf(vval(REPORT), "%lf %s", &minutes, rawfile); if (n == 2) sprintf(rep, " -t %d -e %s", (int)(minutes*60), rawfile); @@ -1316,18 +1364,21 @@ rpict( /* run rpict and pfilt for each view */ getview(0, vs) != NULL) { if (!strcmp(c_rpict, DEF_RPICT_PATH) && getview(1, NULL) == NULL) { - sprintf(sfile, "rpiece_%s.txt", vs); + sprintf(sfile, "%s_%s_rpsync.txt", + vdef(RAWFILE) ? vval(RAWFILE) : vval(PICTURE), + vs); strcpy(rppopt, "-PP pfXXXXXX"); } else { strcpy(rppopt, "-S 1 -PP pfXXXXXX"); } pfile = rppopt + strlen(rppopt) - 8; - if (mktemp(pfile) == NULL) + if (mktemp(pfile) == NULL) { if (do_rpiece) { fprintf(stderr, "%s: cannot create\n", pfile); quit(1); - } else - pfile = NULL; + } + pfile = NULL; + } } vn = 0; /* do each view */ while ((vw = getview(vn++, vs)) != NULL) { @@ -1356,17 +1407,29 @@ rpict( /* run rpict and pfilt for each view */ } /* parallel running? */ if (do_rpiece) { - if (rfdt < oct1date || fdate(sfile) < oct1date) { - rfdt = 0; /* start fresh */ + if (rfdt < oct1date || !fdate(sfile)) { + int xdiv = 8+nprocs/3, ydiv = 8+nprocs/3; + if (rfdt >= oct1date) { + fprintf(stderr, + "%s: partial output not created with %s\n", rawfile, c_rpiece); + quit(1); + } + if (rfdt) { /* start fresh */ + rmfile(rawfile); + rfdt = 0; + } + if (!silent) + printf("\techo %d %d > %s\n", + xdiv, ydiv, sfile); if ((fp = fopen(sfile, "w")) == NULL) { fprintf(stderr, "%s: cannot create\n", sfile); quit(1); } - fprintf(fp, "%d %d\n", 8+nprocs/3, 8+nprocs/3); + fprintf(fp, "%d %d\n", xdiv, ydiv); fclose(fp); } - } else if (next_process()) { + } else if (next_process(0)) { if (pfile != NULL) sleep(10); continue; @@ -1379,8 +1442,7 @@ rpict( /* run rpict and pfilt for each view */ sprintf(combuf, "%s -R %s %s%s %s %s%s%s -o %s %s", c_rpiece, sfile, rppopt, rep, vw, res, opts, po, rawfile, oct1name); - while (children_running < nprocs-1 && - next_process()) { + while (next_process(1)) { sleep(10); combuf[strlen(c_rpiece)+2] = 'F'; } @@ -1392,25 +1454,27 @@ rpict( /* run rpict and pfilt for each view */ } else { if (overture) { /* run overture calculation */ sprintf(combuf, - "%s%s %s%s -x 64 -y 64 -ps 1 %s > %s", + "%s%s %s%s -x 64 -y 64 -ps 1 %s > %s", c_rpict, rep, vw, opts, oct1name, overfile); - if (runcom(combuf)) { - fprintf(stderr, + if (!do_rpiece || !next_process(0)) { + if (runcom(combuf)) { + fprintf(stderr, "%s: error in overture for view %s\n", - progname, vs); - quit(1); - } + progname, vs); + quit(1); + } #ifndef NULL_DEVICE - rmfile(overfile); + rmfile(overfile); #endif + } else if (do_rpiece) + sleep(20); } if (do_rpiece) { sprintf(combuf, "%s -F %s %s%s %s %s%s%s -o %s %s", c_rpiece, sfile, rppopt, rep, vw, res, opts, po, rawfile, oct1name); - while (children_running < nprocs-1 && - next_process()) + while (next_process(1)) sleep(10); } else { sprintf(combuf, "%s%s %s %s%s%s%s %s > %s", @@ -1436,11 +1500,21 @@ rpict( /* run rpict and pfilt for each view */ if (do_rpiece) { /* need to finish raw, first */ finish_process(); wait_process(1); - /* XXX should check sync file to see if really done? */ + if (!syncf_done(sfile)) { + fprintf(stderr, + "%s: %s did not complete rendering of view %s\n", + progname, c_rpiece, vs); + quit(1); + } } if (!vdef(RAWFILE) || strcmp(vval(RAWFILE),vval(PICTURE))) { /* build pfilt command */ - if (mult > 1) + if (do_rpiece) + sprintf(combuf, + "%s%s -x %d -y %d -p %.3f %s > %s", + c_pfilt, pfopts, xres, yres, aspect, + rawfile, picfile); + else if (mult > 1) sprintf(combuf, "%s%s -x /%d -y /%d %s > %s", c_pfilt, pfopts, mult, mult, rawfile, picfile); @@ -1467,7 +1541,7 @@ rpict( /* run rpict and pfilt for each view */ finish_process(); /* exit if child */ } wait_process(1); /* wait for children to finish */ - if (pfile != NULL) { /* clean up rpict persistent mode */ + if (pfile != NULL) { /* clean up persistent rpict */ RT_PID pid; fp = fopen(pfile, "r"); if (fp != NULL) { @@ -1494,11 +1568,6 @@ touch( /* update a file */ printf("\ttouch %s\n", fn); if (!nprocs) return(0); -#ifdef notused - if (access(fn, F_OK) == -1) /* create it */ - if (close(open(fn, O_WRONLY|O_CREAT, 0666)) == -1) - return(-1); -#endif return(setfdate(fn, time((time_t *)NULL))); } @@ -1546,7 +1615,7 @@ mvfile( /* move a file */ #ifdef RHAS_FORK_EXEC static int -next_process(void) /* fork the next process (max. nprocs) */ +next_process(int reserve) /* fork the next process */ { RT_PID child_pid; @@ -1557,12 +1626,15 @@ next_process(void) /* fork the next process (max. np progname); quit(1); } + if (reserve > 0 && children_running >= nprocs-reserve) + return(0); /* caller holding back process(es) */ if (children_running >= nprocs) wait_process(0); /* wait for someone to finish */ fflush(NULL); /* flush output */ child_pid = fork(); /* split process */ if (child_pid == 0) { /* we're the child */ children_running = -1; + nprocs = 1; return(0); } if (child_pid > 0) { /* we're the parent */ @@ -1604,7 +1676,7 @@ wait_process( /* wait for process(es) to finish */ } #else /* ! RHAS_FORK_EXEC */ static int -next_process(void) +next_process(int reserve) { return(0); /* cannot start new process */ } @@ -1630,25 +1702,6 @@ finish_process(void) /* exit a child process */ return; /* in parent -- noop */ exit(0); } - -#ifdef _WIN32 -setenv(vname, value) /* set an environment variable */ -char *vname, *value; -{ - register char *evp; - - evp = bmalloc(strlen(vname)+strlen(value)+2); - if (evp == NULL) - syserr(progname); - sprintf(evp, "%s=%s", vname, value); - if (putenv(evp) != 0) { - fprintf(stderr, "%s: out of environment space\n", progname); - quit(1); - } - if (!silent) - printf("set %s\n", evp); -} -#endif static void