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

Comparing ray/src/util/pvsum.c (file contents):
Revision 2.1 by greg, Thu Mar 27 01:26:55 2025 UTC vs.
Revision 2.3 by greg, Fri Mar 28 21:36:31 2025 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
11   #include "resolu.h"
12   #include "platform.h"
13   #include "paths.h"
14 + #include "random.h"
15   #include "rmatrix.h"
16   #if !defined(_WIN32) && !defined(_WIN64)
17   #include <sys/mman.h>
# Line 67 | Line 68 | iheadline(char *s, void *p)
68                  ncomp = ncompval(s);
69                  return(1);
70          }
70        if (isexpos(s)) {
71                if (fabs(1. - exposval(s)) > 0.04)
72                        fputs("Warning - ignoring EXPOSURE setting\n", stderr);
73                return(1);
74        }
71          if (formatval(fmt, s)) {
72                  for (in_type = DTend; --in_type > DTfromHeader; )
73                          if (!strcmp(fmt, cm_fmt_id[in_type]))
# Line 154 | Line 150 | get_iotypes(void)
150   int
151   checkline(char *s, void *p)
152   {
153 <        int     *xyres = (int *)p;
154 <        char    fmt[MAXFMTLEN];
153 >        static int      exposWarned = 0;
154 >        int             *xyres = (int *)p;
155 >        char            fmt[MAXFMTLEN];
156  
157          if (!strncmp(s, "NCOLS=", 6)) {
158                  xyres[0] = atoi(s+6);
# Line 175 | Line 172 | checkline(char *s, void *p)
172                  return(1);
173          }
174          if (isexpos(s)) {
175 <                if (fabs(1. - exposval(s)) > 0.04)
176 <                        fputs("Warning - ignoring EXPOSURE setting\n", stderr);
175 >                if (!exposWarned && fabs(1. - exposval(s)) > 0.04) {
176 >                        fputs("Warning - ignoring EXPOSURE setting(s)\n",
177 >                                        stderr);
178 >                        exposWarned++;
179 >                }
180                  return(1);
181          }
182          if (formatval(fmt, s)) {
# Line 227 | Line 227 | open_output(char *ospec, int fno)
227          if (!ospec) {
228                  ospec = "<stdout>";
229                  fp = stdout;
230                SET_FILE_BINARY(fp);
230          } else if (ospec[0] == '!') {
231                  if (!(fp = popen(ospec+1, "w"))) {
232                          fprintf(stderr, "Cannot start: %s\n", ospec);
233                          return(NULL);
234                  }
235 <                SET_FILE_BINARY(fp);
237 <        } else if (!(fp = fopen(ospec, "wb"))) {
235 >        } else if (!(fp = fopen(ospec, "w"))) {
236                  fprintf(stderr, "%s: cannot open for writing\n", ospec);
237                  return(NULL);
238          }
239 +        SET_FILE_BINARY(fp);
240          newheader("RADIANCE", fp);
241 <        fputnow(fp);
241 >        if (cmtx->info)                 /* prepend matrix metadata */
242 >                fputs(cmtx->info, fp);
243 >        else
244 >                fputnow(fp);
245          if (fno >= 0)
246                  fprintf(fp, "FRAME=%d\n", fno);
247          switch (out_type) {
# Line 377 | Line 379 | writerr:
379          return(0);
380   }
381  
382 + /* allocate a scrambled index array of the specified length */
383 + int *
384 + scramble(int n)
385 + {
386 +        int     *scarr = (int *)malloc(sizeof(int)*n);
387 +        int     i;
388 +
389 +        if (!scarr) {
390 +                fprintf(stderr, "Out of memory in scramble(%d)\n", n);
391 +                exit(1);
392 +        }
393 +        for (i = n; i--; )
394 +                scarr[i] = i;
395 +                                        /* perform Fisher-Yates shuffle */
396 +        for (i = 0; i < n-1; i++) {
397 +                int     ix = irandom(n-i) + i;
398 +                int     ndx = scarr[i];
399 +                scarr[i] = scarr[ix];
400 +                scarr[ix] = ndx;
401 +        }
402 +        return(scarr);
403 + }
404 +
405   /* run calculation on multiple processes, using memory maps and fork() */
406   int
407   multi_process(void)
# Line 389 | Line 414 | multi_process(void)
414          int     odd = 0;
415          char    fbuf[512];
416          float   *osum;
417 +        int     *syarr;
418          int     c;
419                                          /* sanity check */
420          if (sizeof(float) != sizeof(COLORV)) {
# Line 409 | Line 435 | multi_process(void)
435                                  xres, yres, ncomp);
436                  return(0);
437          }
438 +        srandom(113*coff + 5669);       /* randomize row access for this process */
439 +        syarr = scramble(yres);
440                                          /* run through our unique set of columns */
441          for (c = coff; c < cmtx->ncols; c += nprocs) {
442                  FILE    *fout;
# Line 419 | Line 447 | multi_process(void)
447                  while (rc-- > 0) {      /* map & sum each input file */
448                          const int       r = odd ? rc : cmtx->nrows-1 - rc;
449                          const rmx_dtype *cval = rmx_val(cmtx, r, c);
450 <                        long            dstart, n;
450 >                        long            dstart;
451                          size_t          maplen;
452                          void            *imap;
453                          FILE            *finp;
454                          float           *dst;
455 <                        int             i;
455 >                        int             i, x;
456                          for (i = ncomp; i--; )
457                                  if (cval[i] != 0) break;
458                          if (i < 0)      /* this coefficient is zero, skip */
# Line 446 | Line 474 | multi_process(void)
474                          maplen = dstart + yres*xres*i;
475                          imap = mmap(NULL, maplen, PROT_READ,
476                                          MAP_FILE|MAP_SHARED, fileno(finp), 0);
477 <                        fclose(finp);           /* will load from map */
477 >                        fclose(finp);           /* will read from map (randomly) */
478                          if (imap == MAP_FAILED) {
479                                  fprintf(stderr, "%s: unable to map input file\n", fbuf);
480                                  return(0);
481                          }
482 <                        dst = osum;             /* -> weighted image sum */
483 <                        if (in_type == DTfloat) {
484 <                            const float *fvp = (float *)((char *)imap + dstart);
485 <                            for (n = (long)yres*xres; n-- > 0;
486 <                                                        dst += ncomp, fvp += ncomp)
487 <                                for (i = ncomp; i--; )
488 <                                    dst[i] += cval[i]*fvp[i];
489 <                        } else {
462 <                            const COLRV *cvp = (COLRV *)((char *)imap + dstart);
463 <                            for (n = (long)yres*xres; n-- > 0;
464 <                                                        dst += ncomp, cvp += ncomp+1) {
465 <                                const rmx_dtype fe = cxponent[cvp[ncomp]];
466 <                                for (i = ncomp; i--; )
467 <                                    dst[i] += cval[i]*(cvp[i]+(rmx_dtype).5)*fe;
482 >                        if (in_type == DTfloat)
483 >                            for (y = yres; y-- > 0; ) {
484 >                                const float     *fvp = (float *)((char *)imap + dstart) +
485 >                                                        (size_t)ncomp*xres*syarr[y];
486 >                                dst = osum + (size_t)ncomp*xres*syarr[y];
487 >                                for (x = xres; x-- > 0; dst += ncomp, fvp += ncomp)
488 >                                    for (i = ncomp; i--; )
489 >                                        dst[i] += cval[i]*fvp[i];
490                              }
491 <                        }
491 >                        else
492 >                            for (y = yres; y-- > 0; ) {
493 >                                const COLRV     *cvp = (COLRV *)((char *)imap + dstart) +
494 >                                                        (ncomp+1L)*xres*syarr[y];
495 >                                dst = osum + (size_t)ncomp*xres*syarr[y];
496 >                                for (x = xres; x-- > 0; dst += ncomp, cvp += ncomp+1) {
497 >                                    const rmx_dtype     fe = cxponent[cvp[ncomp]];
498 >                                    for (i = ncomp; i--; )
499 >                                        dst[i] += cval[i]*(cvp[i]+(rmx_dtype).5)*fe;
500 >                                }
501 >                            }
502                          munmap(imap, maplen);
503 <                }                       /* write out accumulated column result */
503 >                }                       /* write accumulated column picture/matrix */
504                  sprintf(fbuf, out_spec, c);
505                  fout = open_output(fbuf, c);
506                  if (!fout)
# Line 492 | Line 524 | multi_process(void)
524                  odd = !odd;             /* go back & forth to milk page cache */
525          }
526          free(osum);
527 <        if (coff)                       /* children return here... */
527 >        free(syarr);
528 >        if (coff)                       /* child processes return here... */
529                  return(1);
530          c = 0;                          /* ...but parent waits for children */
531          while (++coff < nprocs) {
# Line 544 | Line 577 | badopt:                        fprintf(stderr, "%s: bad option: %s\n", argv
577          if ((argc-a < 1) | (argc-a > 2) || argv[a][0] == '-')
578                  goto userr;
579          in_spec = argv[a];
580 <        cmtx = rmx_load(argv[a+1], RMPnone);    /* may load from stdin */
580 >        cmtx = rmx_load(argv[a+1], RMPnone);    /* loads from stdin if a+1==argc */
581          if (cmtx == NULL)
582                  return(1);              /* error reported */
583 +        if (nprocs > cmtx->ncols)
584 +                nprocs = cmtx->ncols;
585   #if defined(_WIN32) || defined(_WIN64)
586          if (nprocs > 1) {
587                  fprintf(stderr, "%s: warning - Windows only allows -N 1\n", argv[0]);
588                  nprocs = 1;
589          }
590   #else
556        if (nprocs > cmtx->ncols)
557                nprocs = cmtx->ncols;
591          if ((nprocs > 1) & !out_spec) {
592                  fprintf(stderr, "%s: multi-processing result cannot go to stdout\n",
593                                  argv[0]);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines