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.65 by schorsch, Mon Jun 30 14:59:13 2003 UTC vs.
Revision 2.69 by schorsch, Mon Jul 14 20:02:30 2003 UTC

# Line 8 | Line 8 | static const char      RCSid[] = "$Id$";
8   #include "standard.h"
9  
10   #include <ctype.h>
11 + #include <time.h>
12  
13   #include "platform.h"
14   #include "view.h"
15   #include "paths.h"
16   #include "vars.h"
17  
18 + #ifdef _WIN32
19 +  #define DELCMD "del"
20 +  #define RENAMECMD "rename"
21 + #else
22 +  #define DELCMD "rm -f"
23 +  #define RENAMECMD "mv"
24 + #endif
25 +
26                                  /* variables (alphabetical by name) */
27   #define AMBFILE         0               /* ambient file name */
28   #define DETAIL          1               /* level of scene detail */
# Line 80 | Line 89 | char   overfile[] = NULL_DEVICE;
89   char    overfile[] = "overture.unf";
90   #endif
91  
83 extern time_t   time();
92  
93   time_t  scenedate;              /* date of latest scene or object file */
94   time_t  octreedate;             /* date of octree */
# Line 96 | Line 104 | int    nowarn = 0;             /* no warnings */
104   int     explicate = 0;          /* explicate variables */
105   int     silent = 0;             /* do work silently */
106   int     touchonly = 0;          /* touch files only */
107 < int     noaction = 0;           /* don't do anything */
107 > int     nprocs = 1;             /* maximum executing processes */
108   int     sayview = 0;            /* print view out */
109   char    *rvdevice = NULL;       /* rview output device */
110   char    *viewselect = NULL;     /* specific view only */
111  
112   int     overture = 0;           /* overture calculation needed */
113  
114 + int     children_running = 0;   /* set negative in children */
115 +
116   char    *progname;              /* global argv[0] */
117   char    *rifname;               /* global rad input file name */
118  
119   char    radname[PATH_MAX];      /* root Radiance file name */
120  
121 + #define inchild()       (children_running < 0)
122  
123 +
124   main(argc, argv)
125   int     argc;
126   char    *argv[];
# Line 125 | Line 137 | char   *argv[];
137                          silent++;
138                          break;
139                  case 'n':
140 <                        noaction++;
140 >                        nprocs = 0;
141                          break;
142 +                case 'N':
143 +                        nprocs = atoi(argv[++i]);
144 +                        if (nprocs < 0)
145 +                                nprocs = 0;
146 +                        break;
147                  case 't':
148                          touchonly++;
149                          break;
# Line 151 | Line 168 | char   *argv[];
168          if (i >= argc)
169                  goto userr;
170          rifname = argv[i];
171 +                                /* check command-line options */
172 +        if (nprocs > 1 & viewselect != NULL)
173 +                nprocs = 1;
174                                  /* assign Radiance root file name */
175          rootname(radname, rifname);
176                                  /* load variable values */
# Line 185 | Line 205 | char   *argv[];
205          quit(0);
206   userr:
207          fprintf(stderr,
208 < "Usage: %s [-w][-s][-n][-t][-e][-V][-v view][-o dev] rfile [VAR=value ..]\n",
208 > "Usage: %s [-w][-s][-n|-N npr][-t][-e][-V][-v view][-o dev] rfile [VAR=value ..]\n",
209                          progname);
210          quit(1);
211   }
# Line 299 | Line 319 | double org[3], *sizp;
319          register int    i;
320  
321          if (osiz <= FTINY)
322 <                if (noaction && fdate(oct1name) <
322 >                if (!nprocs && fdate(oct1name) <
323                                  (scenedate>illumdate?scenedate:illumdate)) {
324                                                          /* run getbbox */
325                          sprintf(buf, "getbbox -w -h %s",
# Line 1030 | Line 1050 | char   *vn;            /* returned view name */
1050   }
1051  
1052  
1053 < printview(vopts)                        /* print out selected view */
1053 > int
1054 > myprintview(vopts, fp)                  /* print out selected view */
1055   register char   *vopts;
1056 + FILE    *fp;
1057   {
1058          VIEW    vwr;
1059          char    buf[128];
# Line 1052 | Line 1074 | again:
1074                  viewfile(buf, &vwr, NULL);      /* load -vf file */
1075                  sscanview(&vwr, cp);            /* reset tail */
1076          }
1077 <        fputs(VIEWSTR, stdout);
1078 <        fprintview(&vwr, stdout);               /* print full spec. */
1079 <        fputc('\n', stdout);
1077 >        fputs(VIEWSTR, fp);
1078 >        fprintview(&vwr, fp);                   /* print full spec. */
1079 >        fputc('\n', fp);
1080          return(0);
1081   }
1082  
# Line 1068 | Line 1090 | char   *opts, *po;
1090          if (touchonly || (vw = getview(0, NULL)) == NULL)
1091                  return;
1092          if (sayview)
1093 <                printview(vw);
1093 >                myprintview(vw, stdout);
1094          sprintf(combuf, "rview %s%s%s -R %s ", vw, po, opts, rifname);
1095          if (rvdevice != NULL)
1096                  sprintf(combuf+strlen(combuf), "-o %s ", rvdevice);
# Line 1088 | Line 1110 | char   *opts, *po;
1110          char    combuf[PATH_MAX];
1111          char    rawfile[PATH_MAX], picfile[PATH_MAX];
1112          char    zopt[PATH_MAX+4], rep[PATH_MAX+16], res[32];
1113 +        char    rppopt[128], *pfile = NULL;
1114          char    pfopts[128];
1115          char    vs[32], *vw;
1116          int     vn, mult;
1117 +        FILE    *fp;
1118          time_t  rfdt, pfdt;
1119                                          /* get pfilt options */
1120          pfiltopts(pfopts);
# Line 1132 | Line 1156 | char   *opts, *po;
1156                  else
1157                          badvalue(REPORT);
1158          }
1159 <                                        /* do each view */
1160 <        vn = 0;
1159 >                                        /* set up parallel rendering */
1160 >        if (nprocs > 1 & !vdef(ZFILE)) {
1161 >                strcpy(rppopt, "-S 1 -PP pfXXXXXX");
1162 >                pfile = rppopt+9;
1163 >                if (mktemp(pfile) == NULL)
1164 >                        pfile = NULL;
1165 >        }
1166 >        vn = 0;                                 /* do each view */
1167          while ((vw = getview(vn++, vs)) != NULL) {
1168                  if (sayview)
1169 <                        printview(vw);
1169 >                        myprintview(vw, stdout);
1170                  if (!vs[0])
1171                          sprintf(vs, "%d", vn);
1172                  sprintf(picfile, "%s_%s.pic", vval(PICTURE), vs);
# Line 1160 | Line 1190 | char   *opts, *po;
1190                                  touch(picfile);
1191                          continue;
1192                  }
1193 +                if (next_process()) {           /* parallel running? */
1194 +                        if (pfile != NULL)
1195 +                                sleep(20);
1196 +                        continue;
1197 +                }
1198 +                /* XXX Remember to call finish_process() */
1199                                                  /* build rpict command */
1200 <                if (rfdt >= oct1date)           /* recover */
1200 >                if (rfdt >= oct1date) {         /* recover */
1201                          sprintf(combuf, "rpict%s%s%s%s -ro %s %s",
1202                                  rep, po, opts, zopt, rawfile, oct1name);
1203 <                else {
1203 >                        if (runcom(combuf))     /* run rpict */
1204 >                                goto rperror;
1205 >                } else {
1206                          if (overture) {         /* run overture calculation */
1207                                  sprintf(combuf,
1208                                  "rpict%s %s%s -x 64 -y 64 -ps 1 %s > %s",
# Line 1181 | Line 1219 | char   *opts, *po;
1219   #endif
1220                          }
1221                          sprintf(combuf, "rpict%s %s %s%s%s%s %s > %s",
1222 <                                        rep, vw, res, po, opts, zopt,
1223 <                                        oct1name, rawfile);
1222 >                                        rep, vw, res, po, opts,
1223 >                                        zopt, oct1name, rawfile);
1224 >                        if (pfile != NULL && inchild()) {
1225 >                                                /* rpict persistent mode */
1226 >                                if (!silent)
1227 >                                        printf("\t%s\n", combuf);
1228 >                                sprintf(combuf, "rpict%s %s %s%s%s %s > %s",
1229 >                                                rep, rppopt, res, po, opts,
1230 >                                                oct1name, rawfile);
1231 >                                fflush(stdout);
1232 >                                fp = popen(combuf, "w");
1233 >                                if (fp == NULL)
1234 >                                        goto rperror;
1235 >                                myprintview(vw, fp);
1236 >                                if (pclose(fp))
1237 >                                        goto rperror;
1238 >                        } else {                /* rpict normal mode */
1239 >                                if (runcom(combuf))
1240 >                                        goto rperror;
1241 >                        }
1242                  }
1187                if (runcom(combuf)) {           /* run rpict */
1188                        fprintf(stderr, "%s: error rendering view %s\n",
1189                                        progname, vs);
1190                        quit(1);
1191                }
1243                  if (!vdef(RAWFILE) || strcmp(vval(RAWFILE),vval(PICTURE))) {
1244                                                  /* build pfilt command */
1245                          if (mult > 1)
# Line 1197 | Line 1248 | char   *opts, *po;
1248                          else
1249                                  sprintf(combuf, "pfilt%s %s > %s", pfopts,
1250                                                  rawfile, picfile);
1251 <                        if (runcom(combuf)) {           /* run pfilt */
1251 >                        if (runcom(combuf)) {   /* run pfilt */
1252                                  fprintf(stderr,
1253                                  "%s: error filtering view %s\n\t%s removed\n",
1254                                                  progname, vs, picfile);
# Line 1211 | Line 1262 | char   *opts, *po;
1262                          mvfile(rawfile, combuf);
1263                  } else
1264                          rmfile(rawfile);
1265 +                finish_process();               /* leave if child */
1266          }
1267 +        wait_process(1);                /* wait for children to finish */
1268 +        if (pfile != NULL) {            /* clean up rpict persistent mode */
1269 +                int     pid;
1270 +                fp = fopen(pfile, "r");
1271 +                if (fp != NULL) {
1272 +                        if (fscanf(fp, "%*s %d", &pid) != 1 ||
1273 +                                        kill(pid, 1) == -1)
1274 +                                unlink(pfile);
1275 +                        fclose(fp);
1276 +                }
1277 +        }
1278 +        return;
1279 + rperror:
1280 +        fprintf(stderr, "%s: error rendering view %s\n", progname, vs);
1281 +        quit(1);
1282   }
1283  
1284  
# Line 1220 | Line 1287 | char   *fn;
1287   {
1288          if (!silent)
1289                  printf("\ttouch %s\n", fn);
1290 <        if (noaction)
1290 >        if (!nprocs)
1291                  return(0);
1292   #ifdef notused
1293          if (access(fn, F_OK) == -1)             /* create it */
# Line 1236 | Line 1303 | char   *cs;
1303   {
1304          if (!silent)            /* echo it */
1305                  printf("\t%s\n", cs);
1306 <        if (noaction)
1306 >        if (!nprocs)
1307                  return(0);
1308          fflush(stdout);         /* flush output and pass to shell */
1309          return(system(cs));
# Line 1247 | Line 1314 | rmfile(fn)                     /* remove a file */
1314   char    *fn;
1315   {
1316          if (!silent)
1317 < #ifdef _WIN32
1318 <                printf("\tdel %s\n", fn);
1252 < #else
1253 <                printf("\trm -f %s\n", fn);
1254 < #endif
1255 <        if (noaction)
1317 >                printf("\t%s %s\n", DELCMD, fn);
1318 >        if (!nprocs)
1319                  return(0);
1320          return(unlink(fn));
1321   }
# Line 1262 | Line 1325 | mvfile(fold, fnew)             /* move a file */
1325   char    *fold, *fnew;
1326   {
1327          if (!silent)
1328 < #ifdef _WIN32
1329 <                printf("\trename %s %s\n", fold, fnew);
1267 < #else
1268 <                printf("\tmv %s %s\n", fold, fnew);
1269 < #endif
1270 <        if (noaction)
1328 >                printf("\t%s %s %s\n", RENAMECMD, fold, fnew);
1329 >        if (!nprocs)
1330                  return(0);
1331          return(rename(fold, fnew));
1332   }
1333  
1334 +
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 + int
1403 + kill(pid, sig) /* win|unix_process.c should also wait and kill */
1404 + int pid, sig;
1405 + {
1406 +        return 0;
1407 + }
1408 + #endif  /* ! RHAS_FORK_EXEC */
1409 +
1410 + finish_process()                        /* exit a child process */
1411 + {
1412 +        if (!inchild())
1413 +                return;                 /* in parent -- noop */
1414 +        exit(0);
1415 + }
1416  
1417   #ifdef _WIN32
1418   setenv(vname, value)            /* set an environment variable */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines