| 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> |
| 240 |
|
return(NULL); |
| 241 |
|
} |
| 242 |
|
newheader("RADIANCE", fp); |
| 243 |
< |
fputnow(fp); |
| 243 |
> |
if (cmtx->info) /* prepend matrix metadata */ |
| 244 |
> |
fputs(cmtx->info, fp); |
| 245 |
> |
else |
| 246 |
> |
fputnow(fp); |
| 247 |
|
if (fno >= 0) |
| 248 |
|
fprintf(fp, "FRAME=%d\n", fno); |
| 249 |
|
switch (out_type) { |
| 381 |
|
return(0); |
| 382 |
|
} |
| 383 |
|
|
| 384 |
+ |
/* allocate a scrambled index array of the specified length */ |
| 385 |
+ |
int * |
| 386 |
+ |
scramble(int n) |
| 387 |
+ |
{ |
| 388 |
+ |
int *scarr = (int *)malloc(sizeof(int)*n); |
| 389 |
+ |
int i; |
| 390 |
+ |
|
| 391 |
+ |
if (!scarr) { |
| 392 |
+ |
fprintf(stderr, "Out of memory in scramble(%d)\n", n); |
| 393 |
+ |
exit(1); |
| 394 |
+ |
} |
| 395 |
+ |
for (i = n; i--; ) |
| 396 |
+ |
scarr[i] = i; |
| 397 |
+ |
/* perform Fisher-Yates shuffle */ |
| 398 |
+ |
for (i = 0; i < n-1; i++) { |
| 399 |
+ |
int ix = irandom(n-i) + i; |
| 400 |
+ |
int ndx = scarr[i]; |
| 401 |
+ |
scarr[i] = scarr[ix]; |
| 402 |
+ |
scarr[ix] = ndx; |
| 403 |
+ |
} |
| 404 |
+ |
return(scarr); |
| 405 |
+ |
} |
| 406 |
+ |
|
| 407 |
|
/* run calculation on multiple processes, using memory maps and fork() */ |
| 408 |
|
int |
| 409 |
|
multi_process(void) |
| 416 |
|
int odd = 0; |
| 417 |
|
char fbuf[512]; |
| 418 |
|
float *osum; |
| 419 |
+ |
int *syarr; |
| 420 |
|
int c; |
| 421 |
|
/* sanity check */ |
| 422 |
|
if (sizeof(float) != sizeof(COLORV)) { |
| 437 |
|
xres, yres, ncomp); |
| 438 |
|
return(0); |
| 439 |
|
} |
| 440 |
+ |
srandom(113*coff + 5669); /* randomize row access for this process */ |
| 441 |
+ |
syarr = scramble(yres); |
| 442 |
|
/* run through our unique set of columns */ |
| 443 |
|
for (c = coff; c < cmtx->ncols; c += nprocs) { |
| 444 |
|
FILE *fout; |
| 449 |
|
while (rc-- > 0) { /* map & sum each input file */ |
| 450 |
|
const int r = odd ? rc : cmtx->nrows-1 - rc; |
| 451 |
|
const rmx_dtype *cval = rmx_val(cmtx, r, c); |
| 452 |
< |
long dstart, n; |
| 452 |
> |
long dstart; |
| 453 |
|
size_t maplen; |
| 454 |
|
void *imap; |
| 455 |
|
FILE *finp; |
| 456 |
|
float *dst; |
| 457 |
< |
int i; |
| 457 |
> |
int i, x; |
| 458 |
|
for (i = ncomp; i--; ) |
| 459 |
|
if (cval[i] != 0) break; |
| 460 |
|
if (i < 0) /* this coefficient is zero, skip */ |
| 476 |
|
maplen = dstart + yres*xres*i; |
| 477 |
|
imap = mmap(NULL, maplen, PROT_READ, |
| 478 |
|
MAP_FILE|MAP_SHARED, fileno(finp), 0); |
| 479 |
< |
fclose(finp); /* will load from map */ |
| 479 |
> |
fclose(finp); /* will load from map (randomly) */ |
| 480 |
|
if (imap == MAP_FAILED) { |
| 481 |
|
fprintf(stderr, "%s: unable to map input file\n", fbuf); |
| 482 |
|
return(0); |
| 483 |
|
} |
| 484 |
< |
dst = osum; /* -> weighted image sum */ |
| 485 |
< |
if (in_type == DTfloat) { |
| 486 |
< |
const float *fvp = (float *)((char *)imap + dstart); |
| 487 |
< |
for (n = (long)yres*xres; n-- > 0; |
| 488 |
< |
dst += ncomp, fvp += ncomp) |
| 489 |
< |
for (i = ncomp; i--; ) |
| 490 |
< |
dst[i] += cval[i]*fvp[i]; |
| 491 |
< |
} 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; |
| 484 |
> |
if (in_type == DTfloat) |
| 485 |
> |
for (y = yres; y-- > 0; ) { |
| 486 |
> |
const float *fvp = (float *)((char *)imap + dstart) + |
| 487 |
> |
(size_t)ncomp*xres*syarr[y]; |
| 488 |
> |
dst = osum + (size_t)ncomp*xres*syarr[y]; |
| 489 |
> |
for (x = xres; x-- > 0; dst += ncomp, fvp += ncomp) |
| 490 |
> |
for (i = ncomp; i--; ) |
| 491 |
> |
dst[i] += cval[i]*fvp[i]; |
| 492 |
|
} |
| 493 |
< |
} |
| 493 |
> |
else |
| 494 |
> |
for (y = yres; y-- > 0; ) { |
| 495 |
> |
const COLRV *cvp = (COLRV *)((char *)imap + dstart) + |
| 496 |
> |
(ncomp+1L)*xres*syarr[y]; |
| 497 |
> |
dst = osum + (size_t)ncomp*xres*syarr[y]; |
| 498 |
> |
for (x = xres; x-- > 0; dst += ncomp, cvp += ncomp+1) { |
| 499 |
> |
const rmx_dtype fe = cxponent[cvp[ncomp]]; |
| 500 |
> |
for (i = ncomp; i--; ) |
| 501 |
> |
dst[i] += cval[i]*(cvp[i]+(rmx_dtype).5)*fe; |
| 502 |
> |
} |
| 503 |
> |
} |
| 504 |
|
munmap(imap, maplen); |
| 505 |
|
} /* write out accumulated column result */ |
| 506 |
|
sprintf(fbuf, out_spec, c); |
| 526 |
|
odd = !odd; /* go back & forth to milk page cache */ |
| 527 |
|
} |
| 528 |
|
free(osum); |
| 529 |
+ |
free(syarr); |
| 530 |
|
if (coff) /* children return here... */ |
| 531 |
|
return(1); |
| 532 |
|
c = 0; /* ...but parent waits for children */ |