ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
(Generate patch)

Comparing ray/src/util/rad.c (file contents):
Revision 2.61 by greg, Sat Feb 22 02:07:30 2003 UTC vs.
Revision 2.67 by greg, Thu Jul 3 18:03:58 2003 UTC

# Line 6 | Line 6 | static const char      RCSid[] = "$Id$";
6   */
7  
8   #include "standard.h"
9 +
10 + #include <ctype.h>
11 +
12 + #include "platform.h"
13   #include "view.h"
14   #include "paths.h"
15   #include "vars.h"
12 #include <ctype.h>
16  
17                                  /* variables (alphabetical by name) */
18   #define AMBFILE         0               /* ambient file name */
# Line 71 | Line 74 | VARIABLE       vv[] = {                /* variable-value pairs */
74   };
75  
76                                  /* overture calculation file */
77 < #ifdef NIX
78 < char    overfile[] = "overture.unf";
77 > #ifdef NULL_DEVICE
78 > char    overfile[] = NULL_DEVICE;
79   #else
80 < char    overfile[] = "/dev/null";
80 > char    overfile[] = "overture.unf";
81   #endif
82  
83   extern time_t   time();
# Line 93 | Line 96 | int    nowarn = 0;             /* no warnings */
96   int     explicate = 0;          /* explicate variables */
97   int     silent = 0;             /* do work silently */
98   int     touchonly = 0;          /* touch files only */
99 < int     noaction = 0;           /* don't do anything */
99 > int     nprocs = 1;             /* maximum executing processes */
100   int     sayview = 0;            /* print view out */
101   char    *rvdevice = NULL;       /* rview output device */
102   char    *viewselect = NULL;     /* specific view only */
103  
104   int     overture = 0;           /* overture calculation needed */
105  
106 + int     children_running = 0;   /* set negative in children */
107 +
108   char    *progname;              /* global argv[0] */
109   char    *rifname;               /* global rad input file name */
110  
111 < char    radname[MAXPATH];       /* root Radiance file name */
111 > char    radname[PATH_MAX];      /* root Radiance file name */
112  
113 + #define inchild()       (children_running < 0)
114  
115 +
116   main(argc, argv)
117   int     argc;
118   char    *argv[];
# Line 122 | Line 129 | char   *argv[];
129                          silent++;
130                          break;
131                  case 'n':
132 <                        noaction++;
132 >                        nprocs = 0;
133                          break;
134 +                case 'N':
135 +                        nprocs = atoi(argv[++i]);
136 +                        if (nprocs < 0)
137 +                                nprocs = 0;
138 +                        break;
139                  case 't':
140                          touchonly++;
141                          break;
# Line 148 | Line 160 | char   *argv[];
160          if (i >= argc)
161                  goto userr;
162          rifname = argv[i];
163 +                                /* check command-line options */
164 +        if (nprocs > 1 & viewselect != NULL)
165 +                nprocs = 1;
166                                  /* assign Radiance root file name */
167          rootname(radname, rifname);
168                                  /* load variable values */
# Line 182 | Line 197 | char   *argv[];
197          quit(0);
198   userr:
199          fprintf(stderr,
200 < "Usage: %s [-w][-s][-n][-t][-e][-V][-v view][-o dev] rfile [VAR=value ..]\n",
200 > "Usage: %s [-w][-s][-n|-N npr][-t][-e][-V][-v view][-o dev] rfile [VAR=value ..]\n",
201                          progname);
202          quit(1);
203   }
# Line 207 | Line 222 | time_t
222   checklast(fnames)                       /* check files and find most recent */
223   register char   *fnames;
224   {
225 <        char    thisfile[MAXPATH];
225 >        char    thisfile[PATH_MAX];
226          time_t  thisdate, lastdate = 0;
227  
228          if (fnames == NULL)
229                  return(0);
230 <        while ((fnames = nextword(thisfile, MAXPATH, fnames)) != NULL) {
230 >        while ((fnames = nextword(thisfile, PATH_MAX, fnames)) != NULL) {
231                  if (thisfile[0] == '!' ||
232                                  (thisfile[0] == '\\' && thisfile[1] == '!'))
233                          continue;
# Line 230 | Line 245 | newfname(orig, pred)           /* create modified file name */
245   char    *orig;
246   int     pred;
247   {
233        extern char     *rindex();
248          register char   *cp;
249          register int    n;
250          int     suffix;
# Line 297 | Line 311 | double org[3], *sizp;
311          register int    i;
312  
313          if (osiz <= FTINY)
314 <                if (noaction && fdate(oct1name) <
314 >                if (!nprocs && fdate(oct1name) <
315                                  (scenedate>illumdate?scenedate:illumdate)) {
316                                                          /* run getbbox */
317                          sprintf(buf, "getbbox -w -h %s",
# Line 396 | Line 410 | setdefaults()                  /* set default values for unassigned v
410   oconv()                         /* run oconv and mkillum if necessary */
411   {
412          static char     illumtmp[] = "ilXXXXXX";
413 <        char    combuf[1024], ocopts[64], mkopts[64];
413 >        char    combuf[PATH_MAX], ocopts[64], mkopts[64];
414  
415          oconvopts(ocopts);              /* get options */
416          if (octreedate < scenedate) {   /* check date on original octree */
# Line 821 | Line 835 | char   *ro;
835                          syserr(vval(OPTFILE));
836                  sprintf(ro, " @%s", vval(OPTFILE));
837          }
838 < #ifdef MSDOS
838 > #ifdef _WIN32
839          else if (n > 50) {
840                  setenv("ROPT", ro+1);
841                  strcpy(ro, " $ROPT");
# Line 977 | Line 991 | register char  *vs;
991          if (cp == viewopts)             /* append any additional options */
992                  vs++;           /* skip prefixed space if unneeded */
993          strcpy(cp, vs);
994 < #ifdef MSDOS
994 > #ifdef _WIN32
995          if (strlen(viewopts) > 40) {
996                  setenv("VIEW", viewopts);
997                  return("$VIEW");
# Line 1028 | Line 1042 | char   *vn;            /* returned view name */
1042   }
1043  
1044  
1045 < printview(vopts)                        /* print out selected view */
1045 > int
1046 > myprintview(vopts, fp)                  /* print out selected view */
1047   register char   *vopts;
1048 + FILE    *fp;
1049   {
1034        extern char     *strstr(), *atos(), *getenv();
1050          VIEW    vwr;
1051          char    buf[128];
1052          register char   *cp;
1053   again:
1054          if (vopts == NULL)
1055                  return(-1);
1056 < #ifdef MSDOS
1056 > #ifdef _WIN32
1057          if (vopts[0] == '$') {
1058                  vopts = getenv(vopts+1);
1059                  goto again;
# Line 1051 | Line 1066 | again:
1066                  viewfile(buf, &vwr, NULL);      /* load -vf file */
1067                  sscanview(&vwr, cp);            /* reset tail */
1068          }
1069 <        fputs(VIEWSTR, stdout);
1070 <        fprintview(&vwr, stdout);               /* print full spec. */
1071 <        fputc('\n', stdout);
1069 >        fputs(VIEWSTR, fp);
1070 >        fprintview(&vwr, fp);                   /* print full spec. */
1071 >        fputc('\n', fp);
1072          return(0);
1073   }
1074  
# Line 1062 | Line 1077 | rview(opts, po)                                /* run rview with first view */
1077   char    *opts, *po;
1078   {
1079          char    *vw;
1080 <        char    combuf[512];
1080 >        char    combuf[PATH_MAX];
1081                                          /* build command */
1082          if (touchonly || (vw = getview(0, NULL)) == NULL)
1083                  return;
1084          if (sayview)
1085 <                printview(vw);
1085 >                myprintview(vw, stdout);
1086          sprintf(combuf, "rview %s%s%s -R %s ", vw, po, opts, rifname);
1087          if (rvdevice != NULL)
1088                  sprintf(combuf+strlen(combuf), "-o %s ", rvdevice);
# Line 1084 | Line 1099 | char   *opts, *po;
1099   rpict(opts, po)                         /* run rpict and pfilt for each view */
1100   char    *opts, *po;
1101   {
1102 <        char    combuf[1024];
1103 <        char    rawfile[MAXPATH], picfile[MAXPATH];
1104 <        char    zopt[MAXPATH+4], rep[MAXPATH+16], res[32];
1102 >        char    combuf[PATH_MAX];
1103 >        char    rawfile[PATH_MAX], picfile[PATH_MAX];
1104 >        char    zopt[PATH_MAX+4], rep[PATH_MAX+16], res[32];
1105 >        char    rppopt[128], *pfile = NULL;
1106          char    pfopts[128];
1107          char    vs[32], *vw;
1108          int     vn, mult;
1109 +        FILE    *fp;
1110          time_t  rfdt, pfdt;
1111                                          /* get pfilt options */
1112          pfiltopts(pfopts);
# Line 1131 | Line 1148 | char   *opts, *po;
1148                  else
1149                          badvalue(REPORT);
1150          }
1151 <                                        /* do each view */
1152 <        vn = 0;
1151 >                                        /* set up parallel rendering */
1152 >        if (nprocs > 1 & !vdef(ZFILE)) {
1153 >                strcpy(rppopt, "-S 1 -PP pfXXXXXX");
1154 >                pfile = rppopt+9;
1155 >                if (mktemp(pfile) == NULL)
1156 >                        pfile = NULL;
1157 >        }
1158 >        vn = 0;                                 /* do each view */
1159          while ((vw = getview(vn++, vs)) != NULL) {
1160                  if (sayview)
1161 <                        printview(vw);
1161 >                        myprintview(vw, stdout);
1162                  if (!vs[0])
1163                          sprintf(vs, "%d", vn);
1164                  sprintf(picfile, "%s_%s.pic", vval(PICTURE), vs);
# Line 1159 | Line 1182 | char   *opts, *po;
1182                                  touch(picfile);
1183                          continue;
1184                  }
1185 +                if (next_process()) {           /* parallel running? */
1186 +                        if (pfile != NULL)
1187 +                                sleep(20);
1188 +                        continue;
1189 +                }
1190 +                /* XXX Remember to call finish_process() */
1191                                                  /* build rpict command */
1192 <                if (rfdt >= oct1date)           /* recover */
1192 >                if (rfdt >= oct1date) {         /* recover */
1193                          sprintf(combuf, "rpict%s%s%s%s -ro %s %s",
1194                                  rep, po, opts, zopt, rawfile, oct1name);
1195 <                else {
1195 >                        if (runcom(combuf))     /* run rpict */
1196 >                                goto rperror;
1197 >                } else {
1198                          if (overture) {         /* run overture calculation */
1199                                  sprintf(combuf,
1200                                  "rpict%s %s%s -x 64 -y 64 -ps 1 %s > %s",
# Line 1175 | Line 1206 | char   *opts, *po;
1206                                                  progname, vs);
1207                                          quit(1);
1208                                  }
1209 < #ifdef NIX
1209 > #ifndef NULL_DEVICE
1210                                  rmfile(overfile);
1211   #endif
1212                          }
1213                          sprintf(combuf, "rpict%s %s %s%s%s%s %s > %s",
1214 <                                        rep, vw, res, po, opts, zopt,
1215 <                                        oct1name, rawfile);
1214 >                                        rep, vw, res, po, opts,
1215 >                                        zopt, oct1name, rawfile);
1216 >                        if (pfile != NULL && inchild()) {
1217 >                                                /* rpict persistent mode */
1218 >                                if (!silent)
1219 >                                        printf("\t%s\n", combuf);
1220 >                                sprintf(combuf, "rpict%s %s %s%s%s %s > %s",
1221 >                                                rep, rppopt, res, po, opts,
1222 >                                                oct1name, rawfile);
1223 >                                fflush(stdout);
1224 >                                fp = popen(combuf, "w");
1225 >                                if (fp == NULL)
1226 >                                        goto rperror;
1227 >                                myprintview(vw, fp);
1228 >                                if (pclose(fp))
1229 >                                        goto rperror;
1230 >                        } else {                /* rpict normal mode */
1231 >                                if (runcom(combuf))
1232 >                                        goto rperror;
1233 >                        }
1234                  }
1186                if (runcom(combuf)) {           /* run rpict */
1187                        fprintf(stderr, "%s: error rendering view %s\n",
1188                                        progname, vs);
1189                        quit(1);
1190                }
1235                  if (!vdef(RAWFILE) || strcmp(vval(RAWFILE),vval(PICTURE))) {
1236                                                  /* build pfilt command */
1237                          if (mult > 1)
# Line 1196 | Line 1240 | char   *opts, *po;
1240                          else
1241                                  sprintf(combuf, "pfilt%s %s > %s", pfopts,
1242                                                  rawfile, picfile);
1243 <                        if (runcom(combuf)) {           /* run pfilt */
1243 >                        if (runcom(combuf)) {   /* run pfilt */
1244                                  fprintf(stderr,
1245                                  "%s: error filtering view %s\n\t%s removed\n",
1246                                                  progname, vs, picfile);
# Line 1210 | Line 1254 | char   *opts, *po;
1254                          mvfile(rawfile, combuf);
1255                  } else
1256                          rmfile(rawfile);
1257 +                finish_process();               /* leave if child */
1258          }
1259 +        wait_process(1);                /* wait for children to finish */
1260 +        if (pfile != NULL) {            /* clean up rpict persistent mode */
1261 +                int     pid;
1262 +                fp = fopen(pfile, "r");
1263 +                if (fp != NULL) {
1264 +                        if (fscanf(fp, "%*s %d", &pid) != 1 ||
1265 +                                        kill(pid, 1) == -1)
1266 +                                unlink(pfile);
1267 +                        fclose(fp);
1268 +                }
1269 +        }
1270 +        return;
1271 + rperror:
1272 +        fprintf(stderr, "%s: error rendering view %s\n", progname, vs);
1273 +        quit(1);
1274   }
1275  
1276  
# Line 1219 | Line 1279 | char   *fn;
1279   {
1280          if (!silent)
1281                  printf("\ttouch %s\n", fn);
1282 <        if (noaction)
1282 >        if (!nprocs)
1283                  return(0);
1284   #ifdef notused
1285          if (access(fn, F_OK) == -1)             /* create it */
# Line 1235 | Line 1295 | char   *cs;
1295   {
1296          if (!silent)            /* echo it */
1297                  printf("\t%s\n", cs);
1298 <        if (noaction)
1298 >        if (!nprocs)
1299                  return(0);
1300          fflush(stdout);         /* flush output and pass to shell */
1301          return(system(cs));
# Line 1246 | Line 1306 | rmfile(fn)                     /* remove a file */
1306   char    *fn;
1307   {
1308          if (!silent)
1309 < #ifdef MSDOS
1309 > #ifdef _WIN32
1310                  printf("\tdel %s\n", fn);
1311   #else
1312                  printf("\trm -f %s\n", fn);
1313   #endif
1314 <        if (noaction)
1314 >        if (!nprocs)
1315                  return(0);
1316          return(unlink(fn));
1317   }
# Line 1261 | Line 1321 | mvfile(fold, fnew)             /* move a file */
1321   char    *fold, *fnew;
1322   {
1323          if (!silent)
1324 < #ifdef MSDOS
1324 > #ifdef _WIN32
1325                  printf("\trename %s %s\n", fold, fnew);
1326   #else
1327                  printf("\tmv %s %s\n", fold, fnew);
1328   #endif
1329 <        if (noaction)
1329 >        if (!nprocs)
1330                  return(0);
1331          return(rename(fold, fnew));
1332   }
1333  
1334  
1335 < #ifdef MSDOS
1335 > #ifdef RHAS_FORK_EXEC
1336 > int
1337 > next_process()                  /* fork the next process (max. nprocs) */
1338 > {
1339 >        int     child_pid;
1340 >
1341 >        if (nprocs <= 1)
1342 >                return(0);              /* it's us or no one */
1343 >        if (inchild()) {
1344 >                fprintf(stderr, "%s: internal error 1 in spawn_process()\n",
1345 >                                progname);
1346 >                quit(1);
1347 >        }
1348 >        if (children_running >= nprocs)
1349 >                wait_process(0);        /* wait for someone to finish */
1350 >        fflush(stdout);
1351 >        child_pid = fork();             /* split process */
1352 >        if (child_pid == 0) {           /* we're the child */
1353 >                children_running = -1;
1354 >                return(0);
1355 >        }
1356 >        if (child_pid > 0) {            /* we're the parent */
1357 >                ++children_running;
1358 >                return(1);
1359 >        }
1360 >        fprintf(stderr, "%s: warning -- fork() failed\n", progname);
1361 >        return(0);
1362 > }
1363 >
1364 > wait_process(all)                       /* wait for process(es) to finish */
1365 > int     all;
1366 > {
1367 >        int     ourstatus = 0;
1368 >        int     pid, status;
1369 >
1370 >        if (all)
1371 >                all = children_running;
1372 >        else if (children_running > 0)
1373 >                all = 1;
1374 >        while (all-- > 0) {
1375 >                pid = wait(&status);
1376 >                if (pid < 0)
1377 >                        syserr(progname);
1378 >                status = status>>8 & 0xff;
1379 >                --children_running;
1380 >                if (status != 0) {      /* child's problem is our problem */
1381 >                        if (ourstatus == 0 & children_running > 0)
1382 >                                fprintf(stderr, "%s: waiting for remaining processes\n",
1383 >                                                progname);
1384 >                        ourstatus = status;
1385 >                        all = children_running;
1386 >                }
1387 >        }
1388 >        if (ourstatus != 0)
1389 >                quit(ourstatus);        /* bad status from child */
1390 > }
1391 > #else   /* ! RHAS_FORK_EXEC */
1392 > int
1393 > next_process()
1394 > {
1395 >        return(0);                      /* cannot start new process */
1396 > }
1397 > wait_process(all)
1398 > int     all;
1399 > {
1400 >        (void)all;                      /* no one to wait for */
1401 > }
1402 > #endif  /* ! RHAS_FORK_EXEC */
1403 >
1404 > finish_process()                        /* exit a child process */
1405 > {
1406 >        if (!inchild())
1407 >                return;                 /* in parent -- noop */
1408 >        exit(0);
1409 > }
1410 >
1411 > #ifdef _WIN32
1412   setenv(vname, value)            /* set an environment variable */
1413   char    *vname, *value;
1414   {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines