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

Comparing ray/src/rt/pmap.c (file contents):
Revision 2.12 by greg, Mon Sep 26 20:19:30 2016 UTC vs.
Revision 2.13 by rschregle, Mon Aug 14 21:12:10 2017 UTC

# Line 2 | Line 2
2   static const char RCSid[] = "$Id$";
3   #endif
4  
5 +
6   /*
7     ======================================================================
8     Photon map main module
# Line 16 | Line 17 | static const char RCSid[] = "$Id$";
17   */
18  
19  
19
20   #include "pmap.h"
21   #include "pmapmat.h"
22   #include "pmapsrc.h"
# Line 26 | Line 26 | static const char RCSid[] = "$Id$";
26   #include "pmapdiag.h"
27   #include "otypes.h"
28   #include <time.h>
29 < #include <sys/stat.h>
30 < #include <sys/mman.h>
31 < #include <sys/wait.h>
29 > #if NIX
30 >   #include <sys/stat.h>
31 >   #include <sys/mman.h>
32 >   #include <sys/wait.h>
33 > #endif
34  
35  
34
36   void savePmaps (const PhotonMap **pmaps, int argc, char **argv)
37   {
38     unsigned t;
# Line 127 | Line 128 | void tracePhoton (RAY *ray)
128   /* Follow photon as it bounces around... */
129   {
130     long mod;
131 <   OBJREC* mat;
131 >   OBJREC *mat, *port = NULL;
132 >  
133 >   if (!ray -> parent) {
134 >      /* !!!  PHOTON PORT REJECTION SAMPLING HACK: get photon port for
135 >       * !!!  primary ray from ray -> ro, then reset the latter to NULL so
136 >       * !!!  as not to interfere with localhit() */
137 >      port = ray -> ro;
138 >      ray -> ro = NULL;
139 >   }
140  
141     if (ray -> rlvl > photonMaxBounce) {
142   #ifdef PMAP_RUNAWAY_WARN  
# Line 138 | Line 147 | void tracePhoton (RAY *ray)
147    
148     if (colorAvg(ray -> cext) > FTINY && !photonParticipate(ray))
149        return;
150 <      
150 >
151     if (localhit(ray, &thescene)) {
152        mod = ray -> ro -> omod;
153 <      
153 >
154 >      if (port && ray -> ro != port) {
155 >         /* !!! PHOTON PORT REJECTION SAMPLING HACK !!!
156 >          * Terminate photon if emitted from port without intersecting it;
157 >          * this can happen when the port's partitions extend beyond its
158 >          * actual geometry, e.g.  with polygons.  Since the total flux
159 >          * relayed by the port is based on the (in this case) larger
160 >          * partition area, it is overestimated; terminating these photons
161 >          * constitutes rejection sampling and thereby compensates any bias
162 >          * incurred by the overestimated flux.  */
163 > #ifdef PMAP_PORTREJECT_WARN
164 >         sprintf(errmsg, "photon outside port %s", ray -> ro -> oname);
165 >         error(WARNING, errmsg);
166 > #endif        
167 >         return;
168 >      }
169 >
170        if ((ray -> clipset && inset(ray -> clipset, mod)) || mod == OVOID) {
171           /* Transfer ray if modifier is VOID or clipped within antimatta */
172           RAY tray;
# Line 173 | Line 198 | static void preComputeGlobal (PhotonMap *pmap)
198  
199     repComplete = numPreComp = finalGather * pmap -> numPhotons;
200    
201 <   if (photonRepTime) {
202 <      sprintf(errmsg, "Precomputing irradiance for %ld global photons...\n",
201 >   if (verbose) {
202 >      sprintf(errmsg,
203 >              "\nPrecomputing irradiance for %ld global photons\n",
204                numPreComp);
205        eputs(errmsg);
206 + #if NIX      
207        fflush(stderr);
208 + #endif      
209     }
210    
211     /* Copy photon map for precomputed photons */
# Line 204 | Line 232 | static void preComputeGlobal (PhotonMap *pmap)
232    
233     for (i = 0; i < numPreComp; i++) {
234        /* Get random photon from stratified distribution in source heap to
235 <       * avoid duplicates and clutering */
235 >       * avoid duplicates and clustering */
236        pIdx = firstPhoton(pmap) +
237               (unsigned long)((i + pmapRandom(pmap -> randState)) /
238                               finalGather);
# Line 242 | Line 270 | static void preComputeGlobal (PhotonMap *pmap)
270     deletePhotons(pmap);
271     memcpy(pmap, &nuPmap, sizeof(PhotonMap));
272    
273 <   if (photonRepTime) {
274 <      eputs("Rebuilding precomputed photon map...\n");
273 >   if (verbose) {
274 >      eputs("\nRebuilding precomputed photon map\n");
275 > #if NIX      
276        fflush(stderr);
277 + #endif      
278     }
279  
280     /* Rebuild underlying data structure, destroying heap */  
# Line 263 | Line 293 | typedef struct {
293   void distribPhotons (PhotonMap **pmaps, unsigned numProc)
294   {
295     EmissionMap    emap;
296 <   char           errmsg2 [128], shmFname [255];
296 >   char           errmsg2 [128], shmFname [PMAP_TMPFNLEN];
297     unsigned       t, srcIdx, proc;
298     double         totalFlux = 0;
299     int            shmFile, stat, pid;
# Line 295 | Line 325 | void distribPhotons (PhotonMap **pmaps, unsigned numPr
325           initPhotonHeap(pmaps [t]);
326           /* Per-subprocess target count */
327           pmaps [t] -> distribTarget /= numProc;
328 +        
329 +         if (!pmaps [t] -> distribTarget)
330 +            error(INTERNAL, "no photons to distribute in distribPhotons");
331        }
332  
333     initPhotonEmissionFuncs();
# Line 307 | Line 340 | void distribPhotons (PhotonMap **pmaps, unsigned numPr
340     /* Get photon sensor modifiers */
341     getPhotonSensors(photonSensorList);
342    
343 + #if NIX
344     /* Set up shared mem for photon counters (zeroed by ftruncate) */
345 < #if 0  
312 <   snprintf(shmFname, 255, PMAP_SHMFNAME, getpid());
313 <   shmFile = shm_open(shmFname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
314 < #else
315 <   strcpy(shmFname, PMAP_SHMFNAME);
345 >   strcpy(shmFname, PMAP_TMPFNAME);
346     shmFile = mkstemp(shmFname);
317 #endif      
347  
348 <   if (shmFile < 0)
349 <      error(SYSTEM, "failed opening shared memory file in distribPhotons");
348 >   if (shmFile < 0 || ftruncate(shmFile, sizeof(*photonCnt)) < 0)
349 >      error(SYSTEM, "failed shared mem init in distribPhotons");
350  
322   if (ftruncate(shmFile, sizeof(*photonCnt)) < 0)
323      error(SYSTEM, "failed setting shared memory size in distribPhotons");
324
351     photonCnt = mmap(NULL, sizeof(*photonCnt), PROT_READ | PROT_WRITE,
352                      MAP_SHARED, shmFile, 0);
353                      
354     if (photonCnt == MAP_FAILED)
355 <      error(SYSTEM, "failed mapping shared memory in distribPhotons");
355 >      error(SYSTEM, "failed mapping shared memory in distribPhotons");
356 > #else
357 >   /* Allocate photon counters statically on Windoze */
358 >   if (!(photonCnt = malloc(sizeof(PhotonCnt))))
359 >      error(SYSTEM, "failed trivial malloc in distribPhotons");
360 >   photonCnt -> numEmitted = photonCnt -> numComplete = 0;      
361 > #endif /* NIX */
362  
363 <   if (photonRepTime)
364 <      eputs("\n");
363 >   if (verbose) {
364 >      sprintf(errmsg, "\nIntegrating flux from %d sources", nsources);
365 >      
366 >      if (photonPorts) {
367 >         sprintf(errmsg2, " via %d ports", numPhotonPorts);
368 >         strcat(errmsg, errmsg2);
369 >      }
370 >      
371 >      strcat(errmsg, "\n");
372 >      eputs(errmsg);
373 >   }  
374    
375     /* ===================================================================
376      * FLUX INTEGRATION - Get total photon flux from light sources
# Line 343 | Line 384 | void distribPhotons (PhotonMap **pmaps, unsigned numPr
384                                                     : NULL;
385           photonPartition [emap.src -> so -> otype] (&emap);
386          
387 <         if (photonRepTime) {
388 <            sprintf(errmsg, "Integrating flux from source %s ",
387 >         if (verbose) {
388 >            sprintf(errmsg, "\tIntegrating flux from source %s ",
389                      source [srcIdx].so -> oname);
390 <                    
390 >
391              if (emap.port) {
392                 sprintf(errmsg2, "via port %s ",
393                         photonPorts [portCnt].so -> oname);
394                 strcat(errmsg, errmsg2);
395              }
396 <            
397 <            sprintf(errmsg2, "(%lu partitions)...\n", emap.numPartitions);
396 >
397 >            sprintf(errmsg2, "(%lu partitions)\n", emap.numPartitions);
398              strcat(errmsg, errmsg2);
399              eputs(errmsg);
400 + #if NIX            
401              fflush(stderr);
402 + #endif            
403           }
404          
405           for (emap.partitionCnt = 0; emap.partitionCnt < emap.numPartitions;
# Line 371 | Line 414 | void distribPhotons (PhotonMap **pmaps, unsigned numPr
414  
415     if (totalFlux < FTINY)
416        error(USER, "zero flux from light sources");
417 +      
418 +   /* Record start time for progress reports */
419 +   repStartTime = time(NULL);
420  
421 +   if (verbose) {
422 +      sprintf(errmsg, "\nPhoton distribution @ %d procs\n", numProc);
423 +      eputs(errmsg);
424 +   }
425 +
426     /* MAIN LOOP */  
427     for (proc = 0; proc < numProc; proc++) {
428 + #if NIX          
429        if (!(pid = fork())) {
430 <         /* SUBPROCESS ENTERS HERE.
431 <            All opened and memory mapped files are inherited */
430 >         /* SUBPROCESS ENTERS HERE; open and mmapped files inherited */
431 > #else
432 >      if (1) {
433 >         /* No subprocess under Windoze */
434 > #endif
435 >         /* Local photon counters for this subprocess */
436           unsigned       passCnt = 0, prePassCnt = 0;
437           unsigned long  lastNumPhotons [NUM_PMAP_TYPES];
438           unsigned long  localNumEmitted = 0; /* Num photons emitted by this
# Line 384 | Line 440 | void distribPhotons (PhotonMap **pmaps, unsigned numPr
440          
441           /* Seed RNGs from PID for decorellated photon distribution */
442           pmapSeed(randSeed + proc, partState);
443 <         pmapSeed(randSeed + proc, emitState);
444 <         pmapSeed(randSeed + proc, cntState);
445 <         pmapSeed(randSeed + proc, mediumState);
446 <         pmapSeed(randSeed + proc, scatterState);
447 <         pmapSeed(randSeed + proc, rouletteState);
443 >         pmapSeed(randSeed + (proc + 1) % numProc, emitState);
444 >         pmapSeed(randSeed + (proc + 2) % numProc, cntState);
445 >         pmapSeed(randSeed + (proc + 3) % numProc, mediumState);
446 >         pmapSeed(randSeed + (proc + 4) % numProc, scatterState);
447 >         pmapSeed(randSeed + (proc + 5) % numProc, rouletteState);
448                    
449           for (t = 0; t < NUM_PMAP_TYPES; t++)
450              lastNumPhotons [t] = 0;
# Line 409 | Line 465 | void distribPhotons (PhotonMap **pmaps, unsigned numPr
465                  * iterations; make it clear to user which photon maps are
466                  * missing so (s)he can check geometry and materials */
467                 if (++prePassCnt > maxPreDistrib) {
468 <                  sprintf(errmsg,
413 <                          "proc %d, source %s: too many prepasses",
414 <                          proc, source [srcIdx].so -> oname);              
468 >                  sprintf(errmsg, "proc %d: too many prepasses", proc);
469  
470                    for (t = 0; t < NUM_PMAP_TYPES; t++)
471                       if (pmaps [t] && !pmaps [t] -> numPhotons) {
# Line 483 | Line 537 | void distribPhotons (PhotonMap **pmaps, unsigned numPr
537                    break;
538              }
539  
540 <            /* Update shared completion counter for prog.report by parent */
540 >            /* Update shared completion counter for progreport by parent */
541              photonCnt -> numComplete += numEmit;                            
542  
543              /* PHOTON DISTRIBUTION LOOP */
# Line 496 | Line 550 | void distribPhotons (PhotonMap **pmaps, unsigned numPr
550                                ? photonPorts + portCnt : NULL;
551                    photonPartition [emap.src -> so -> otype] (&emap);
552  
553 <                  if (photonRepTime && !proc) {
553 >                  if (verbose && !proc) {
554 >                     /* Output from subproc 0 only to avoid race condition
555 >                      * on console I/O */
556                       if (!passCnt)
557 <                        sprintf(errmsg, "PREPASS %d on source %s ",
557 >                        sprintf(errmsg, "\tPREPASS %d on source %s ",
558                                  prePassCnt, source [srcIdx].so -> oname);
559                       else
560 <                        sprintf(errmsg, "MAIN PASS on source %s ",
560 >                        sprintf(errmsg, "\tMAIN PASS on source %s ",
561                                  source [srcIdx].so -> oname);
562 <                            
562 >
563                       if (emap.port) {
564                          sprintf(errmsg2, "via port %s ",
565                                  photonPorts [portCnt].so -> oname);
566                          strcat(errmsg, errmsg2);
567                       }
568 <                    
568 >
569                       sprintf(errmsg2, "(%lu partitions)\n",
570                               emap.numPartitions);
571                       strcat(errmsg, errmsg2);
572                       eputs(errmsg);
573 + #if NIX                    
574                       fflush(stderr);
575 + #endif                    
576                    }
577                    
578                    for (emap.partitionCnt = 0; emap.partitionCnt < emap.numPartitions;
# Line 529 | Line 587 | void distribPhotons (PhotonMap **pmaps, unsigned numPr
587                      
588                       /* Number of photons to emit from ziss partishunn --
589                        * proportional to flux; photon ray weight and scalar
590 <                      * flux are uniform (the latter only varying in RGB).
533 <                      * */
590 >                      * flux are uniform (latter only varying in RGB). */
591                       partNumEmit = numEmit * colorAvg(emap.partFlux) /
592                                     totalFlux;
593                       partEmitCnt = (unsigned long)partNumEmit;
# Line 551 | Line 608 | void distribPhotons (PhotonMap **pmaps, unsigned numPr
608                          /* Emit photon based on PDF and trace through scene
609                           * until absorbed/leaked */
610                          emitPhoton(&emap, &photonRay);
611 + #if 1
612 +                        if (emap.port)
613 +                           /* !!!  PHOTON PORT REJECTION SAMPLING HACK: set
614 +                            * !!!  photon port as fake hit object for
615 +                            * !!!  primary ray to check for intersection in
616 +                            * !!!  tracePhoton() */
617 +                           photonRay.ro = emap.port -> so;
618 + #endif
619                          tracePhoton(&photonRay);
620                       }                                          
621 <                    
621 >
622                       /* Update shared global photon count for each pmap */
623                       for (t = 0; t < NUM_PMAP_TYPES; t++)
624                          if (pmaps [t]) {
# Line 561 | Line 626 | void distribPhotons (PhotonMap **pmaps, unsigned numPr
626                                pmaps [t] -> numPhotons - lastNumPhotons [t];
627                             lastNumPhotons [t] = pmaps [t] -> numPhotons;
628                          }
629 + #if !NIX
630 +                     /* Synchronous progress report on Windoze */
631 +                     if (!proc && photonRepTime > 0 &&
632 +                           time(NULL) >= repLastTime + photonRepTime) {
633 +                        repEmitted = repProgress = photonCnt -> numEmitted;
634 +                        repComplete = photonCnt -> numComplete;                          
635 +                        pmapDistribReport();
636 +                     }
637 + #endif
638                    }
639                    
640                    portCnt++;
# Line 576 | Line 650 | void distribPhotons (PhotonMap **pmaps, unsigned numPr
650                    break;
651                 }
652              
653 <            if (t >= NUM_PMAP_TYPES) {
653 >            if (t >= NUM_PMAP_TYPES)
654                 /* No empty photon maps found; now do pass 2 */
655                 passCnt++;
582 #if 0
583               if (photonRepTime)
584                  eputs("\n");
585 #endif
586            }
656           } while (passCnt < 2);
657          
658 <         /* Unmap shared photon counters */
659 < #if 0        
591 <         munmap(photonCnt, sizeof(*photonCnt));
592 <         close(shmFile);
593 < #endif
594 <        
595 <         /* Flush heap buffa for every pmap one final time; this is required
596 <          * to prevent data corruption! */
658 >         /* Flush heap buffa for every pmap one final time;
659 >          * avoids potential data corruption! */
660           for (t = 0; t < NUM_PMAP_TYPES; t++)
661              if (pmaps [t]) {
599 #if 0            
600               eputs("Final flush\n");
601 #endif              
662                 flushPhotonHeap(pmaps [t]);
663 <               fclose(pmaps [t] -> heap);
663 >               /* Heap file closed automatically on exit
664 >                  fclose(pmaps [t] -> heap); */
665   #ifdef DEBUG_PMAP              
666 <               sprintf(errmsg, "Proc %d: total %ld photons\n", getpid(),
666 >               sprintf(errmsg, "Proc %d: total %ld photons\n", proc,
667                         pmaps [t] -> numPhotons);
668                 eputs(errmsg);
669   #endif              
670              }
671 <
671 > #if NIX
672 >         /* Terminate subprocess */
673           exit(0);
674 + #endif
675        }
676        else if (pid < 0)
677           error(SYSTEM, "failed to fork subprocess in distribPhotons");        
678     }
679  
680 + #if NIX
681     /* PARENT PROCESS CONTINUES HERE */
618   /* Record start time and enable progress report signal handler */
619   repStartTime = time(NULL);
682   #ifdef SIGCONT
683 +   /* Enable progress report signal handler */
684     signal(SIGCONT, pmapDistribReport);
685 < #endif
686 <  
624 <   if (photonRepTime)
625 <      eputs("\n");
626 <  
627 <   /* Wait for subprocesses to complete while reporting progress */
685 > #endif  
686 >   /* Wait for subprocesses complete while reporting progress */
687     proc = numProc;
688     while (proc) {
689        while (waitpid(-1, &stat, WNOHANG) > 0) {
# Line 637 | Line 696 | void distribPhotons (PhotonMap **pmaps, unsigned numPr
696        
697        /* Nod off for a bit and update progress  */
698        sleep(1);
699 <      /* Update progress report from shared subprocess counters */
699 >
700 >      /* Asynchronous progress report from shared subprocess counters */  
701        repEmitted = repProgress = photonCnt -> numEmitted;
702 <      repComplete = photonCnt -> numComplete;
702 >      repComplete = photonCnt -> numComplete;      
703  
704 +      repProgress = repComplete = 0;
705        for (t = 0; t < NUM_PMAP_TYPES; t++)
706           if ((pm = pmaps [t])) {
646 #if 0        
647            /* Get photon count from heapfile size for progress update */
648            fseek(pm -> heap, 0, SEEK_END);
649            pm -> numPhotons = ftell(pm -> heap) / sizeof(Photon); */
650 #else            
707              /* Get global photon count from shmem updated by subprocs */
708 <            pm -> numPhotons = photonCnt -> numPhotons [t];
709 < #endif            
708 >            repProgress += pm -> numPhotons = photonCnt -> numPhotons [t];
709 >            repComplete += pm -> distribTarget;
710           }
711 +      repComplete *= numProc;
712  
713        if (photonRepTime > 0 && time(NULL) >= repLastTime + photonRepTime)
714           pmapDistribReport();
# Line 659 | Line 716 | void distribPhotons (PhotonMap **pmaps, unsigned numPr
716        else signal(SIGCONT, pmapDistribReport);
717   #endif
718     }
719 + #endif /* NIX */
720  
721     /* ===================================================================
722      * POST-DISTRIBUTION - Set photon flux and build data struct for photon
723      * storage, etc.
724      * =================================================================== */
725   #ifdef SIGCONT    
726 +   /* Reset signal handler */
727     signal(SIGCONT, SIG_DFL);
728   #endif
729     free(emap.samples);
730    
731 <   /* Set photon flux (repProgress is total num emitted) */
731 >   /* Set photon flux */
732     totalFlux /= photonCnt -> numEmitted;
733 <  
733 > #if NIX  
734     /* Photon counters no longer needed, unmap shared memory */
735     munmap(photonCnt, sizeof(*photonCnt));
736     close(shmFile);
678 #if 0  
679   shm_unlink(shmFname);
680 #else
737     unlink(shmFname);
738 + #else
739 +   free(photonCnt);  
740   #endif      
741 <  
741 >   if (verbose)
742 >      eputs("\n");
743 >      
744     for (t = 0; t < NUM_PMAP_TYPES; t++)
745        if (pmaps [t]) {
746 <         if (photonRepTime) {
747 <            sprintf(errmsg, "\nBuilding %s photon map...\n", pmapName [t]);
746 >         if (verbose) {
747 >            sprintf(errmsg, "Building %s photon map\n", pmapName [t]);
748              eputs(errmsg);
749 + #if NIX            
750              fflush(stderr);
751 + #endif            
752           }
753          
754           /* Build underlying data structure; heap is destroyed */
755           buildPhotonMap(pmaps [t], &totalFlux, NULL, numProc);
756        }
757 <
757 >      
758     /* Precompute photon irradiance if necessary */
759 <   if (preCompPmap)
759 >   if (preCompPmap) {
760 >      if (verbose)
761 >         eputs("\n");
762        preComputeGlobal(preCompPmap);
763 +   }      
764 +  
765 +   if (verbose)
766 +      eputs("\n");
767   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines