ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapcontrib.c
Revision: 2.15
Committed: Thu Feb 8 19:55:02 2018 UTC (7 years, 3 months ago) by rschregle
Content type: text/plain
Branch: MAIN
Changes since 2.14: +6 -4 lines
Log Message:
Made photon primary incident dirs optional via PMAP_PRIMARYDIR, bumped PMAP_FILEVER

File Contents

# User Rev Content
1 greg 2.9 #ifndef lint
2 rschregle 2.15 static const char RCSid[] = "$Id: pmapcontrib.c,v 2.14 2017/08/14 21:12:10 rschregle Exp $";
3 greg 2.9 #endif
4 rschregle 2.12
5 greg 2.1 /*
6 rschregle 2.12 ======================================================================
7 rschregle 2.14 Photon map for light source contributions
8 greg 2.1
9     Roland Schregle (roland.schregle@{hslu.ch, gmail.com})
10 rschregle 2.4 (c) Lucerne University of Applied Sciences and Arts,
11 rschregle 2.12 supported by the Swiss National Science Foundation (SNSF, #147053)
12     ======================================================================
13 greg 2.1
14 rschregle 2.15 $Id: pmapcontrib.c,v 2.14 2017/08/14 21:12:10 rschregle Exp $
15 greg 2.1 */
16    
17    
18     #include "pmapcontrib.h"
19     #include "pmapmat.h"
20     #include "pmapsrc.h"
21     #include "pmaprand.h"
22     #include "pmapio.h"
23     #include "pmapdiag.h"
24     #include "rcontrib.h"
25     #include "otypes.h"
26 rschregle 2.14 #if NIX
27     #include <sys/mman.h>
28 rschregle 2.15 #include <sys/wait.h>
29 rschregle 2.14 #endif
30 greg 2.1
31    
32 rschregle 2.12 static PhotonPrimaryIdx newPhotonPrimary (PhotonMap *pmap,
33     const RAY *primRay,
34     FILE *primHeap)
35     /* Add primary ray for emitted photon and save light source index, origin on
36     * source, and emitted direction; used by contrib photons. The current
37     * primary is stored in pmap -> lastPrimary. If the previous primary
38     * contributed photons (has srcIdx >= 0), it's appended to primHeap. If
39     * primRay == NULL, the current primary is still flushed, but no new primary
40     * is set. Returns updated primary counter pmap -> numPrimary. */
41 greg 2.1 {
42 rschregle 2.12 if (!pmap || !primHeap)
43     return 0;
44 greg 2.1
45 rschregle 2.12 /* Check if last primary ray has spawned photons (srcIdx >= 0, see
46 rschregle 2.14 * newPhoton()), in which case we save it to the primary heap file
47     * before clobbering it */
48 rschregle 2.12 if (pmap -> lastPrimary.srcIdx >= 0) {
49     if (!fwrite(&pmap -> lastPrimary, sizeof(PhotonPrimary), 1, primHeap))
50     error(SYSTEM, "failed writing photon primary in newPhotonPrimary");
51    
52     pmap -> numPrimary++;
53     if (pmap -> numPrimary > PMAP_MAXPRIMARY)
54     error(INTERNAL, "photon primary overflow in newPhotonPrimary");
55     }
56    
57     /* Mark unused with negative source index until path spawns a photon (see
58     * newPhoton()) */
59     pmap -> lastPrimary.srcIdx = -1;
60    
61     if (primRay) {
62     FVECT dvec;
63 rschregle 2.15
64     #ifdef PMAP_PRIMARYDIR
65 rschregle 2.12 /* Reverse incident direction to point to light source */
66     dvec [0] = -primRay -> rdir [0];
67     dvec [1] = -primRay -> rdir [1];
68     dvec [2] = -primRay -> rdir [2];
69     pmap -> lastPrimary.dir = encodedir(dvec);
70 rschregle 2.15 #endif
71 rschregle 2.12 #ifdef PMAP_PRIMARYPOS
72     VCOPY(pmap -> lastPrimary.pos, primRay -> rop);
73     #endif
74     }
75    
76     return pmap -> numPrimary;
77     }
78    
79 greg 2.1
80    
81 rschregle 2.14 #ifdef DEBUG_PMAP
82 rschregle 2.12 static int checkPrimaryHeap (FILE *file)
83     /* Check heap for ordered primaries */
84     {
85     Photon p, lastp;
86     int i, dup;
87    
88     rewind(file);
89     memset(&lastp, 0, sizeof(lastp));
90 greg 2.1
91 rschregle 2.12 while (fread(&p, sizeof(p), 1, file)) {
92     dup = 1;
93    
94     for (i = 0; i <= 2; i++) {
95     if (p.pos [i] < thescene.cuorg [i] ||
96     p.pos [i] > thescene.cuorg [i] + thescene.cusize) {
97    
98     sprintf(errmsg, "corrupt photon in heap at [%f, %f, %f]\n",
99     p.pos [0], p.pos [1], p.pos [2]);
100     error(WARNING, errmsg);
101     }
102    
103     dup &= p.pos [i] == lastp.pos [i];
104     }
105    
106     if (dup) {
107     sprintf(errmsg,
108     "consecutive duplicate photon in heap at [%f, %f, %f]\n",
109     p.pos [0], p.pos [1], p.pos [2]);
110 greg 2.1 error(WARNING, errmsg);
111 rschregle 2.12 }
112 greg 2.1 }
113    
114 rschregle 2.12 return 0;
115     }
116     #endif
117    
118    
119    
120     static PhotonPrimaryIdx buildPrimaries (PhotonMap *pmap, FILE **primaryHeap,
121 rschregle 2.14 char **primaryHeapFname,
122 rschregle 2.12 PhotonPrimaryIdx *primaryOfs,
123     unsigned numHeaps)
124     /* Consolidate per-subprocess photon primary heaps into the primary array
125     * pmap -> primaries. Returns offset for primary index linearisation in
126     * numPrimary. The heap files in primaryHeap are closed on return. */
127     {
128     PhotonPrimaryIdx heapLen;
129     unsigned heap;
130 greg 2.1
131 rschregle 2.12 if (!pmap || !primaryHeap || !primaryOfs || !numHeaps)
132     return 0;
133 greg 2.1
134 rschregle 2.12 pmap -> numPrimary = 0;
135    
136     for (heap = 0; heap < numHeaps; heap++) {
137     primaryOfs [heap] = pmap -> numPrimary;
138    
139 rschregle 2.14 if (fseek(primaryHeap [heap], 0, SEEK_END) < 0)
140 rschregle 2.12 error(SYSTEM, "failed photon primary seek in buildPrimaries");
141     pmap -> numPrimary += heapLen = ftell(primaryHeap [heap]) /
142     sizeof(PhotonPrimary);
143    
144     pmap -> primaries = realloc(pmap -> primaries,
145     pmap -> numPrimary *
146     sizeof(PhotonPrimary));
147     if (!pmap -> primaries)
148     error(SYSTEM, "failed photon primary alloc in buildPrimaries");
149    
150     rewind(primaryHeap [heap]);
151     if (fread(pmap -> primaries + primaryOfs [heap], sizeof(PhotonPrimary),
152     heapLen, primaryHeap [heap]) != heapLen)
153     error(SYSTEM, "failed reading photon primaries in buildPrimaries");
154 greg 2.1
155 rschregle 2.14 fclose(primaryHeap [heap]);
156     unlink(primaryHeapFname [heap]);
157 rschregle 2.12 }
158    
159     return pmap -> numPrimary;
160     }
161 greg 2.6
162    
163 greg 2.7
164 rschregle 2.12 /* Defs for photon emission counter array passed by sub-processes to parent
165     * via shared memory */
166     typedef unsigned long PhotonContribCnt;
167 greg 2.7
168 rschregle 2.12 /* Indices for photon emission counter array: num photons stored and num
169     * emitted per source */
170     #define PHOTONCNT_NUMPHOT 0
171     #define PHOTONCNT_NUMEMIT(n) (1 + n)
172 greg 2.1
173    
174    
175 rschregle 2.12 void distribPhotonContrib (PhotonMap* pm, unsigned numProc)
176 greg 2.1 {
177 rschregle 2.12 EmissionMap emap;
178 rschregle 2.14 char errmsg2 [128], shmFname [PMAP_TMPFNLEN];
179 rschregle 2.12 unsigned srcIdx, proc;
180     int shmFile, stat, pid;
181     double *srcFlux, /* Emitted flux per light source */
182     srcDistribTarget; /* Target photon count per source */
183     PhotonContribCnt *photonCnt; /* Photon emission counter array */
184 rschregle 2.14 unsigned photonCntSize = sizeof(PhotonContribCnt) *
185 rschregle 2.12 PHOTONCNT_NUMEMIT(nsources);
186 rschregle 2.14 FILE **primaryHeap = NULL;
187     char **primaryHeapFname = NULL;
188     PhotonPrimaryIdx *primaryOfs = NULL;
189 rschregle 2.12
190 greg 2.1 if (!pm)
191 rschregle 2.12 error(USER, "no photon map defined in distribPhotonContrib");
192 greg 2.1
193     if (!nsources)
194 rschregle 2.12 error(USER, "no light sources in distribPhotonContrib");
195    
196     if (nsources > MAXMODLIST)
197     error(USER, "too many light sources in distribPhotonContrib");
198    
199 greg 2.1 /* Allocate photon flux per light source; this differs for every
200     * source as all sources contribute the same number of distributed
201     * photons (srcDistribTarget), hence the number of photons emitted per
202     * source does not correlate with its emitted flux. The resulting flux
203     * per photon is therefore adjusted individually for each source. */
204     if (!(srcFlux = calloc(nsources, sizeof(double))))
205 rschregle 2.12 error(SYSTEM, "can't allocate source flux in distribPhotonContrib");
206 greg 2.1
207 rschregle 2.12 /* ===================================================================
208     * INITIALISATION - Set up emission and scattering funcs
209     * =================================================================== */
210 greg 2.1 emap.samples = NULL;
211     emap.src = NULL;
212     emap.maxPartitions = MAXSPART;
213     emap.partitions = (unsigned char*)malloc(emap.maxPartitions >> 1);
214     if (!emap.partitions)
215 rschregle 2.12 error(USER, "can't allocate source partitions in distribPhotonContrib");
216 greg 2.1
217 rschregle 2.12 /* Initialise contrib photon map */
218 greg 2.1 initPhotonMap(pm, PMAP_TYPE_CONTRIB);
219 rschregle 2.12 initPhotonHeap(pm);
220 greg 2.1 initPhotonEmissionFuncs();
221     initPhotonScatterFuncs();
222    
223 rschregle 2.12 /* Per-subprocess / per-source target counts */
224     pm -> distribTarget /= numProc;
225 rschregle 2.14 srcDistribTarget = nsources ? (double)pm -> distribTarget / nsources : 0;
226    
227     if (!pm -> distribTarget)
228     error(INTERNAL, "no photons to distribute in distribPhotonContrib");
229 rschregle 2.12
230 greg 2.1 /* Get photon ports if specified */
231     if (ambincl == 1)
232     getPhotonPorts();
233    
234     /* Get photon sensor modifiers */
235     getPhotonSensors(photonSensorList);
236 rschregle 2.14
237     #if NIX
238 rschregle 2.12 /* Set up shared mem for photon counters (zeroed by ftruncate) */
239 rschregle 2.14 strcpy(shmFname, PMAP_TMPFNAME);
240 rschregle 2.12 shmFile = mkstemp(shmFname);
241    
242     if (shmFile < 0 || ftruncate(shmFile, photonCntSize) < 0)
243     error(SYSTEM, "failed shared mem init in distribPhotonContrib");
244 greg 2.1
245 rschregle 2.12 photonCnt = mmap(NULL, photonCntSize, PROT_READ | PROT_WRITE,
246     MAP_SHARED, shmFile, 0);
247    
248     if (photonCnt == MAP_FAILED)
249     error(SYSTEM, "failed shared mem mapping in distribPhotonContrib");
250 rschregle 2.14 #else
251     /* Allocate photon counters statically on Windoze */
252     if (!(photonCnt = malloc(photonCntSize)))
253     error(SYSTEM, "failed trivial malloc in distribPhotonContrib");
254    
255     for (srcIdx = 0; srcIdx < PHOTONCNT_NUMEMIT(nsources); srcIdx++)
256     photonCnt [srcIdx] = 0;
257     #endif /* NIX */
258    
259     if (verbose) {
260     sprintf(errmsg, "\nIntegrating flux from %d sources", nsources);
261    
262     if (photonPorts) {
263     sprintf(errmsg2, " via %d ports", numPhotonPorts);
264     strcat(errmsg, errmsg2);
265     }
266    
267     strcat(errmsg, "\n");
268     eputs(errmsg);
269     }
270 rschregle 2.12
271     /* =============================================================
272 rschregle 2.14 * FLUX INTEGRATION - Get total flux emitted from sources/ports
273 rschregle 2.12 * ============================================================= */
274 greg 2.1 for (srcIdx = 0; srcIdx < nsources; srcIdx++) {
275 rschregle 2.14 unsigned portCnt = 0;
276 rschregle 2.12 srcFlux [srcIdx] = 0;
277 greg 2.1 emap.src = source + srcIdx;
278    
279 rschregle 2.12 do { /* Need at least one iteration if no ports! */
280     emap.port = emap.src -> sflags & SDISTANT ? photonPorts + portCnt
281     : NULL;
282 greg 2.1 photonPartition [emap.src -> so -> otype] (&emap);
283 rschregle 2.14
284     if (verbose) {
285     sprintf(errmsg, "\tIntegrating flux from source %s ",
286     source [srcIdx].so -> oname);
287    
288 greg 2.1 if (emap.port) {
289     sprintf(errmsg2, "via port %s ",
290     photonPorts [portCnt].so -> oname);
291     strcat(errmsg, errmsg2);
292     }
293 rschregle 2.14
294     sprintf(errmsg2, "(%lu partitions)\n", emap.numPartitions);
295 greg 2.1 strcat(errmsg, errmsg2);
296     eputs(errmsg);
297 rschregle 2.14 #if NIX
298 greg 2.1 fflush(stderr);
299 rschregle 2.14 #endif
300     }
301 greg 2.1
302 rschregle 2.12 for (emap.partitionCnt = 0; emap.partitionCnt < emap.numPartitions;
303 greg 2.1 emap.partitionCnt++) {
304     initPhotonEmission(&emap, pdfSamples);
305     srcFlux [srcIdx] += colorAvg(emap.partFlux);
306     }
307    
308     portCnt++;
309 rschregle 2.12 } while (portCnt < numPhotonPorts);
310    
311 greg 2.1 if (srcFlux [srcIdx] < FTINY) {
312     sprintf(errmsg, "source %s has zero emission",
313     source [srcIdx].so -> oname);
314     error(WARNING, errmsg);
315     }
316 rschregle 2.12 }
317 rschregle 2.14
318     /* Allocate & init per-subprocess primary heap files */
319     primaryHeap = calloc(numProc, sizeof(FILE*));
320     primaryHeapFname = calloc(numProc, sizeof(char*));
321     primaryOfs = calloc(numProc, sizeof(PhotonPrimaryIdx));
322     if (!primaryHeap || !primaryHeapFname || !primaryOfs)
323     error(SYSTEM, "failed primary heap allocation in "
324     "distribPhotonContrib");
325    
326     for (proc = 0; proc < numProc; proc++) {
327     primaryHeapFname [proc] = malloc(PMAP_TMPFNLEN);
328     if (!primaryHeapFname [proc])
329     error(SYSTEM, "failed primary heap file allocation in "
330     "distribPhotonContrib");
331    
332     mktemp(strcpy(primaryHeapFname [proc], PMAP_TMPFNAME));
333     if (!(primaryHeap [proc] = fopen(primaryHeapFname [proc], "w+b")))
334     error(SYSTEM, "failed opening primary heap file in "
335     "distribPhotonContrib");
336     }
337 rschregle 2.12
338 rschregle 2.14 /* Record start time for progress reports */
339     repStartTime = time(NULL);
340 rschregle 2.12
341 rschregle 2.14 if (verbose) {
342     sprintf(errmsg, "\nPhoton distribution @ %d procs\n", numProc);
343     eputs(errmsg);
344     }
345 rschregle 2.12
346     /* MAIN LOOP */
347     for (proc = 0; proc < numProc; proc++) {
348 rschregle 2.14 #if NIX
349 rschregle 2.12 if (!(pid = fork())) {
350 rschregle 2.14 /* SUBPROCESS ENTERS HERE; opened and mmapped files inherited */
351     #else
352     if (1) {
353     /* No subprocess under Windoze */
354     #endif
355 rschregle 2.12 /* Local photon counters for this subprocess */
356     unsigned long lastNumPhotons = 0, localNumEmitted = 0;
357 rschregle 2.14 double photonFluxSum = 0; /* Accum. photon flux */
358 rschregle 2.12
359     /* Seed RNGs from PID for decorellated photon distribution */
360     pmapSeed(randSeed + proc, partState);
361     pmapSeed(randSeed + proc, emitState);
362     pmapSeed(randSeed + proc, cntState);
363     pmapSeed(randSeed + proc, mediumState);
364     pmapSeed(randSeed + proc, scatterState);
365     pmapSeed(randSeed + proc, rouletteState);
366    
367     /* =============================================================
368 greg 2.1 * 2-PASS PHOTON DISTRIBUTION
369     * Pass 1 (pre): emit fraction of target photon count
370 rschregle 2.12 * Pass 2 (main): based on outcome of pass 1, estimate remaining
371     * number of photons to emit to approximate target
372     * count
373     * ============================================================= */
374     for (srcIdx = 0; srcIdx < nsources; srcIdx++) {
375     unsigned portCnt, passCnt = 0, prePassCnt = 0;
376     float srcPreDistrib = preDistrib;
377     double srcNumEmit = 0; /* # to emit from source */
378     unsigned long srcNumDistrib = pm -> numPhotons; /* # stored */
379    
380     if (srcFlux [srcIdx] < FTINY)
381     continue;
382    
383     while (passCnt < 2) {
384     if (!passCnt) {
385     /* INIT PASS 1 */
386 rschregle 2.14 if (++prePassCnt > maxPreDistrib && !proc) {
387 rschregle 2.12 /* Warn if no photons contributed after sufficient
388 rschregle 2.14 * iterations; only output from subprocess 0 to reduce
389     * console clutter */
390     sprintf(errmsg,
391     "source %s: too many prepasses, skipped",
392     source [srcIdx].so -> oname);
393 rschregle 2.12 error(WARNING, errmsg);
394     break;
395     }
396    
397     /* Num to emit is fraction of target count */
398     srcNumEmit = srcPreDistrib * srcDistribTarget;
399 greg 2.1 }
400 rschregle 2.12 else {
401     /* INIT PASS 2 */
402     double srcPhotonFlux, avgPhotonFlux;
403    
404     /* Based on the outcome of the predistribution we can now
405     * figure out how many more photons we have to emit from
406     * the current source to meet the target count,
407     * srcDistribTarget. This value is clamped to 0 in case
408     * the target has already been exceeded in pass 1.
409     * srcNumEmit and srcNumDistrib is the number of photons
410     * emitted and distributed (stored) from the current
411     * source in pass 1, respectively. */
412     srcNumDistrib = pm -> numPhotons - srcNumDistrib;
413     srcNumEmit *= srcNumDistrib
414     ? max(srcDistribTarget/srcNumDistrib, 1) - 1
415     : 0;
416    
417     if (!srcNumEmit)
418     /* No photons left to distribute in main pass */
419     break;
420 greg 2.1
421 rschregle 2.12 srcPhotonFlux = srcFlux [srcIdx] / srcNumEmit;
422     avgPhotonFlux = photonFluxSum / (srcIdx + 1);
423    
424     if (avgPhotonFlux > 0 &&
425     srcPhotonFlux / avgPhotonFlux < FTINY) {
426     /* Skip source if its photon flux is grossly below the
427 rschregle 2.14 * running average, indicating negligible contributions
428     * at the expense of excessive distribution time; only
429     * output from subproc 0 to reduce console clutter */
430     sprintf(errmsg,
431     "source %s: itsy bitsy photon flux, skipped",
432     source [srcIdx].so -> oname);
433 rschregle 2.12 error(WARNING, errmsg);
434     srcNumEmit = 0;
435 greg 2.1 }
436 rschregle 2.12
437     /* Update sum of photon flux per light source */
438     photonFluxSum += srcPhotonFlux;
439 greg 2.1 }
440 rschregle 2.14
441 rschregle 2.12 portCnt = 0;
442     do { /* Need at least one iteration if no ports! */
443     emap.src = source + srcIdx;
444     emap.port = emap.src -> sflags & SDISTANT
445     ? photonPorts + portCnt : NULL;
446     photonPartition [emap.src -> so -> otype] (&emap);
447 rschregle 2.14
448     if (verbose && !proc) {
449     /* Output from subproc 0 only to avoid race condition
450     * on console I/O */
451 rschregle 2.12 if (!passCnt)
452 rschregle 2.14 sprintf(errmsg, "\tPREPASS %d on source %s ",
453     prePassCnt, source [srcIdx].so -> oname);
454 rschregle 2.12 else
455 rschregle 2.14 sprintf(errmsg, "\tMAIN PASS on source %s ",
456     source [srcIdx].so -> oname);
457    
458 rschregle 2.12 if (emap.port) {
459     sprintf(errmsg2, "via port %s ",
460     photonPorts [portCnt].so -> oname);
461     strcat(errmsg, errmsg2);
462     }
463 rschregle 2.14
464 rschregle 2.12 sprintf(errmsg2, "(%lu partitions)\n",
465     emap.numPartitions);
466 rschregle 2.14 strcat(errmsg, errmsg2);
467 rschregle 2.12 eputs(errmsg);
468 rschregle 2.14 #if NIX
469 rschregle 2.12 fflush(stderr);
470 rschregle 2.14 #endif
471     }
472 greg 2.1
473 rschregle 2.12 for (emap.partitionCnt = 0; emap.partitionCnt < emap.numPartitions;
474     emap.partitionCnt++) {
475     double partNumEmit;
476     unsigned long partEmitCnt;
477 greg 2.1
478 rschregle 2.12 /* Get photon origin within current source partishunn
479     * and build emission map */
480     photonOrigin [emap.src -> so -> otype] (&emap);
481     initPhotonEmission(&emap, pdfSamples);
482    
483     /* Number of photons to emit from ziss partishunn;
484     * scale according to its normalised contribushunn to
485     * the emitted source flux */
486     partNumEmit = srcNumEmit * colorAvg(emap.partFlux) /
487     srcFlux [srcIdx];
488     partEmitCnt = (unsigned long)partNumEmit;
489    
490     /* Probabilistically account for fractional photons */
491     if (pmapRandom(cntState) < partNumEmit - partEmitCnt)
492     partEmitCnt++;
493    
494     /* Update local and shared global emission counter */
495 rschregle 2.14 photonCnt [PHOTONCNT_NUMEMIT(srcIdx)] += partEmitCnt;
496 rschregle 2.12 localNumEmitted += partEmitCnt;
497    
498 rschregle 2.14 /* Integer counter avoids FP rounding errors during
499     * iteration */
500 rschregle 2.12 while (partEmitCnt--) {
501     RAY photonRay;
502 greg 2.1
503 rschregle 2.12 /* Emit photon according to PDF (if any), allocate
504     * associated primary ray, and trace through scene
505     * until absorbed/leaked; emitPhoton() sets the
506     * emitting light source index in photonRay */
507     emitPhoton(&emap, &photonRay);
508 rschregle 2.14 #if 1
509     if (emap.port)
510     /* !!! PHOTON PORT REJECTION SAMPLING HACK: set
511     * !!! photon port as fake hit object for
512     * !!! primary ray to check for intersection in
513     * !!! tracePhoton() */
514     photonRay.ro = emap.port -> so;
515     #endif
516 rschregle 2.12 newPhotonPrimary(pm, &photonRay, primaryHeap[proc]);
517     /* Set subprocess index in photonRay for post-
518     * distrib primary index linearisation; this is
519     * propagated with the primary index in photonRay
520     * and set for photon hits by newPhoton() */
521     PMAP_SETRAYPROC(&photonRay, proc);
522     tracePhoton(&photonRay);
523     }
524 greg 2.1
525 rschregle 2.12 /* Update shared global photon count */
526     photonCnt [PHOTONCNT_NUMPHOT] += pm -> numPhotons -
527     lastNumPhotons;
528     lastNumPhotons = pm -> numPhotons;
529 rschregle 2.14 #if !NIX
530     /* Synchronous progress report on Windoze */
531     if (!proc && photonRepTime > 0 &&
532     time(NULL) >= repLastTime + photonRepTime) {
533     unsigned s;
534     repComplete = pm -> distribTarget * numProc;
535     repProgress = photonCnt [PHOTONCNT_NUMPHOT];
536    
537     for (repEmitted = 0, s = 0; s < nsources; s++)
538     repEmitted += photonCnt [PHOTONCNT_NUMEMIT(s)];
539    
540     pmapDistribReport();
541     }
542     #endif
543 greg 2.1 }
544 rschregle 2.12
545     portCnt++;
546     } while (portCnt < numPhotonPorts);
547    
548 rschregle 2.14 if (pm -> numPhotons == srcNumDistrib) {
549 rschregle 2.12 /* Double predistrib factor in case no photons were stored
550     * for this source and redo pass 1 */
551     srcPreDistrib *= 2;
552 rschregle 2.14 }
553 rschregle 2.12 else {
554     /* Now do pass 2 */
555     passCnt++;
556 greg 2.1 }
557     }
558 rschregle 2.12 }
559    
560     /* Flush heap buffa one final time to prevent data corruption */
561 rschregle 2.14 flushPhotonHeap(pm);
562 rschregle 2.12 /* Flush final photon primary to primary heap file */
563     newPhotonPrimary(pm, NULL, primaryHeap [proc]);
564 rschregle 2.14 /* Heap files closed automatically on exit
565     fclose(pm -> heap);
566     fclose(primaryHeap [proc]); */
567 rschregle 2.12
568     #ifdef DEBUG_PMAP
569 rschregle 2.14 sprintf(errmsg, "Proc %d total %ld photons\n", proc,
570 rschregle 2.12 pm -> numPhotons);
571     eputs(errmsg);
572 rschregle 2.14 fflush(stderr);
573 rschregle 2.12 #endif
574    
575 rschregle 2.14 #if NIX
576     /* Terminate subprocess */
577 rschregle 2.12 exit(0);
578 rschregle 2.14 #endif
579 greg 2.1 }
580 rschregle 2.12 else if (pid < 0)
581     error(SYSTEM, "failed to fork subprocess in distribPhotonContrib");
582     }
583    
584 rschregle 2.14 #if NIX
585 rschregle 2.12 /* PARENT PROCESS CONTINUES HERE */
586     #ifdef SIGCONT
587 rschregle 2.14 /* Enable progress report signal handler */
588 rschregle 2.12 signal(SIGCONT, pmapDistribReport);
589     #endif
590     /* Wait for subprocesses to complete while reporting progress */
591     proc = numProc;
592     while (proc) {
593     while (waitpid(-1, &stat, WNOHANG) > 0) {
594     /* Subprocess exited; check status */
595     if (!WIFEXITED(stat) || WEXITSTATUS(stat))
596     error(USER, "failed photon distribution");
597    
598     --proc;
599     }
600    
601     /* Nod off for a bit and update progress */
602     sleep(1);
603 rschregle 2.14
604     /* Asynchronous progress report from shared subprocess counters */
605     repComplete = pm -> distribTarget * numProc;
606     repProgress = photonCnt [PHOTONCNT_NUMPHOT];
607 rschregle 2.12
608     for (repEmitted = 0, srcIdx = 0; srcIdx < nsources; srcIdx++)
609     repEmitted += photonCnt [PHOTONCNT_NUMEMIT(srcIdx)];
610    
611     /* Get global photon count from shmem updated by subprocs */
612     pm -> numPhotons = photonCnt [PHOTONCNT_NUMPHOT];
613    
614     if (photonRepTime > 0 && time(NULL) >= repLastTime + photonRepTime)
615     pmapDistribReport();
616     #ifdef SIGCONT
617     else signal(SIGCONT, pmapDistribReport);
618     #endif
619 greg 2.1 }
620 rschregle 2.14 #endif /* NIX */
621 greg 2.1
622     /* ================================================================
623     * POST-DISTRIBUTION - Set photon flux and build kd-tree, etc.
624     * ================================================================ */
625 rschregle 2.12 #ifdef SIGCONT
626 rschregle 2.14 /* Reset signal handler */
627 rschregle 2.12 signal(SIGCONT, SIG_DFL);
628     #endif
629 greg 2.1 free(emap.samples);
630    
631 rschregle 2.12 if (!pm -> numPhotons)
632 rschregle 2.14 error(USER, "empty contribution photon map");
633 greg 2.1
634 rschregle 2.12 /* Load per-subprocess primary rays into pm -> primary array */
635 rschregle 2.14 /* Dumb compilers apparently need the char** cast */
636     pm -> numPrimary = buildPrimaries(pm, primaryHeap,
637     (char**)primaryHeapFname,
638     primaryOfs, numProc);
639 rschregle 2.12 if (!pm -> numPrimary)
640 greg 2.1 error(INTERNAL, "no primary rays in contribution photon map");
641 rschregle 2.12
642     /* Set photon flux per source */
643     for (srcIdx = 0; srcIdx < nsources; srcIdx++)
644     srcFlux [srcIdx] /= photonCnt [PHOTONCNT_NUMEMIT(srcIdx)];
645 rschregle 2.14 #if NIX
646 rschregle 2.12 /* Photon counters no longer needed, unmap shared memory */
647     munmap(photonCnt, sizeof(*photonCnt));
648     close(shmFile);
649 rschregle 2.14 unlink(shmFname);
650 rschregle 2.12 #else
651 rschregle 2.14 free(photonCnt);
652 rschregle 2.12 #endif
653 greg 2.1
654 rschregle 2.14 if (verbose) {
655     eputs("\nBuilding contribution photon map...\n");
656     #if NIX
657 greg 2.1 fflush(stderr);
658 rschregle 2.14 #endif
659 greg 2.1 }
660 rschregle 2.12
661     /* Build underlying data structure; heap is destroyed */
662 rschregle 2.14 buildPhotonMap(pm, srcFlux, primaryOfs, numProc);
663    
664     /* Free per-subprocess primary heap files */
665     for (proc = 0; proc < numProc; proc++)
666     free(primaryHeapFname [proc]);
667    
668     free(primaryHeapFname);
669     free(primaryHeap);
670     free(primaryOfs);
671    
672     if (verbose)
673     eputs("\n");
674 rschregle 2.12 }