Peak memory usage of Wavelet Tool reduced by width*height bytes, no Issue

This commit is contained in:
Ingo
2015-02-01 21:48:40 +01:00
parent d8463e4b14
commit 2a18857b84
2 changed files with 21 additions and 35 deletions

View File

@@ -43,7 +43,7 @@ namespace rtengine {
int lvltot, subsamp; int lvltot, subsamp;
int numThreads; int numThreads;
size_t m_w, m_h;//dimensions int m_w, m_h;//dimensions
int wavfilt_len, wavfilt_offset; int wavfilt_len, wavfilt_offset;
float *wavfilt_anal; float *wavfilt_anal;
@@ -173,29 +173,15 @@ namespace rtengine {
return; return;
} }
E *buffer[2];
buffer[0] = coeff0;
buffer[1] = new (std::nothrow) E[(m_w/2+1)*(m_h/2+1)];
if(buffer[1] == NULL) {
memoryAllocationFailed = true;
delete[] tmpHi;
delete[] tmpLo;
return;
}
int bufferindex = 0;
for (int lvl=lvltot; lvl>0; lvl--) { for (int lvl=lvltot; lvl>0; lvl--) {
wavelet_decomp[lvl]->reconstruct_level(tmpLo, tmpHi, buffer[bufferindex], buffer[bufferindex^1], wavfilt_synth, wavfilt_synth, wavfilt_len, wavfilt_offset); wavelet_decomp[lvl]->reconstruct_level(tmpLo, tmpHi, coeff0, coeff0, wavfilt_synth, wavfilt_synth, wavfilt_len, wavfilt_offset);
bufferindex ^= 1;
} }
wavelet_decomp[0]->reconstruct_level(tmpLo, tmpHi, buffer[bufferindex], dst, wavfilt_synth, wavfilt_synth, wavfilt_len, wavfilt_offset); wavelet_decomp[0]->reconstruct_level(tmpLo, tmpHi, coeff0, dst, wavfilt_synth, wavfilt_synth, wavfilt_len, wavfilt_offset);
delete[] buffer[0]; delete[] coeff0;
delete[] buffer[1];
coeff0 = NULL; coeff0 = NULL;
delete[] tmpLo; delete[] tmpLo;
delete[] tmpHi; delete[] tmpHi;
} }
}; };

View File

@@ -45,7 +45,7 @@ namespace rtengine {
bool bigBlockOfMemory; bool bigBlockOfMemory;
// allocation and destruction of data storage // allocation and destruction of data storage
T ** create(size_t n); T ** create(int n);
void destroy(T ** subbands); void destroy(T ** subbands);
// load a row/column of input data, possibly with padding // load a row/column of input data, possibly with padding
@@ -76,13 +76,13 @@ namespace rtengine {
T ** wavcoeffs; T ** wavcoeffs;
// full size // full size
size_t m_w, m_h; int m_w, m_h;
// size of low frequency part // size of low frequency part
size_t m_w2, m_h2; int m_w2, m_h2;
template<typename E> template<typename E>
wavelet_level(E * src, E * dst, int level, int subsamp, size_t w, size_t h, float *filterV, float *filterH, int len, int offset, int skipcrop, int numThreads) wavelet_level(E * src, E * dst, int level, int subsamp, int w, int h, float *filterV, float *filterH, int len, int offset, int skipcrop, int numThreads)
: lvl(level), subsamp_out((subsamp>>level)&1), numThreads(numThreads), skip(1<<level), bigBlockOfMemory(true), memoryAllocationFailed(false), wavcoeffs(NULL), m_w(w), m_h(h), m_w2(w), m_h2(h) : lvl(level), subsamp_out((subsamp>>level)&1), numThreads(numThreads), skip(1<<level), bigBlockOfMemory(true), memoryAllocationFailed(false), wavcoeffs(NULL), m_w(w), m_h(h), m_w2(w), m_h2(h)
{ {
if (subsamp) { if (subsamp) {
@@ -118,17 +118,17 @@ namespace rtengine {
return wavcoeffs[0]; return wavcoeffs[0];
} }
size_t width() const int width() const
{ {
return m_w2; return m_w2;
} }
size_t height() const int height() const
{ {
return m_h2; return m_h2;
} }
size_t stride() const int stride() const
{ {
return skip; return skip;
} }
@@ -141,13 +141,13 @@ namespace rtengine {
}; };
template<typename T> template<typename T>
T ** wavelet_level<T>::create(size_t n) { T ** wavelet_level<T>::create(int n) {
T * data = new (std::nothrow) T[3*n]; T * data = new (std::nothrow) T[3*n];
if(data == NULL) { if(data == NULL) {
bigBlockOfMemory = false; bigBlockOfMemory = false;
} }
T ** subbands = new T*[4]; T ** subbands = new T*[4];
for(size_t j = 1; j < 4; j++) { for(int j = 1; j < 4; j++) {
if(bigBlockOfMemory) if(bigBlockOfMemory)
subbands[j] = data + n * (j-1); subbands[j] = data + n * (j-1);
else { else {
@@ -167,7 +167,7 @@ namespace rtengine {
if(bigBlockOfMemory) if(bigBlockOfMemory)
delete[] subbands[1]; delete[] subbands[1];
else { else {
for(size_t j = 1; j < 4; j++) { for(int j = 1; j < 4; j++) {
if(subbands[j] != NULL) if(subbands[j] != NULL)
delete[] subbands[j]; delete[] subbands[j];
} }
@@ -185,7 +185,7 @@ namespace rtengine {
dstLo[row*width+i] = (srcbuffer[i] + srcbuffer[i+skip]); dstLo[row*width+i] = (srcbuffer[i] + srcbuffer[i+skip]);
dstHi[row*width+i] = (srcbuffer[i] - srcbuffer[i+skip]); dstHi[row*width+i] = (srcbuffer[i] - srcbuffer[i+skip]);
} }
for(size_t i = max(width-skip,skip); i < (width); i++) { for(int i = max(width-skip,skip); i < (width); i++) {
dstLo[row*width+i] = (srcbuffer[i] + srcbuffer[i-skip]); dstLo[row*width+i] = (srcbuffer[i] + srcbuffer[i-skip]);
dstHi[row*width+i] = (srcbuffer[i] - srcbuffer[i-skip]); dstHi[row*width+i] = (srcbuffer[i] - srcbuffer[i-skip]);
} }
@@ -221,10 +221,10 @@ namespace rtengine {
#pragma omp parallel for num_threads(numThreads) if(numThreads>1) #pragma omp parallel for num_threads(numThreads) if(numThreads>1)
#endif #endif
for (int k=0; k<height; k++) { for (int k=0; k<height; k++) {
for(size_t i = 0; i < skip; i++) { for(int i = 0; i < skip; i++) {
dst[k*width+i] = (srcLo[k*width+i] + srcHi[k*width+i]); dst[k*width+i] = (srcLo[k*width+i] + srcHi[k*width+i]);
} }
for(size_t i = skip; i < width; i++) { for(int i = skip; i < width; i++) {
dst[k*width+i] = 0.5f*(srcLo[k*width+i] + srcHi[k*width+i] + srcLo[k*width+i-skip] - srcHi[k*width+i-skip]); dst[k*width+i] = 0.5f*(srcLo[k*width+i] + srcHi[k*width+i] + srcLo[k*width+i-skip] - srcHi[k*width+i-skip]);
} }
} }
@@ -243,14 +243,14 @@ namespace rtengine {
#ifdef _OPENMP #ifdef _OPENMP
#pragma omp for nowait #pragma omp for nowait
#endif #endif
for(size_t i = 0; i < skip; i++) { for(int i = 0; i < skip; i++) {
for(int j=0;j<width;j++) for(int j=0;j<width;j++)
dst[width*i+j] = (srcLo[i*width+j] + srcHi[i*width+j]); dst[width*i+j] = (srcLo[i*width+j] + srcHi[i*width+j]);
} }
#ifdef _OPENMP #ifdef _OPENMP
#pragma omp for #pragma omp for
#endif #endif
for(size_t i = skip; i < height; i++) { for(int i = skip; i < height; i++) {
for(int j=0;j<width;j++) for(int j=0;j<width;j++)
dst[width*i+j] = 0.5f*(srcLo[i*width+j] + srcHi[i*width+j] + srcLo[(i-skip)*width+j] - srcHi[(i-skip)*width+j]); dst[width*i+j] = 0.5f*(srcLo[i*width+j] + srcHi[i*width+j] + srcLo[(i-skip)*width+j] - srcHi[(i-skip)*width+j]);
} }
@@ -453,7 +453,7 @@ namespace rtengine {
#ifdef _OPENMP #ifdef _OPENMP
#pragma omp parallel for num_threads(numThreads) if(numThreads>1) #pragma omp parallel for num_threads(numThreads) if(numThreads>1)
#endif #endif
for(size_t i = 0; i < dstheight; i++) { for(int i = 0; i < dstheight; i++) {
int i_src = (i+shift)/2; int i_src = (i+shift)/2;
int begin = (i+shift)%2; int begin = (i+shift)%2;
//TODO: this is correct only if skip=1; otherwise, want to work with cosets of length 'skip' //TODO: this is correct only if skip=1; otherwise, want to work with cosets of length 'skip'
@@ -511,7 +511,7 @@ namespace rtengine {
#ifdef _OPENMP #ifdef _OPENMP
#pragma omp parallel for num_threads(numThreads) if(numThreads>1) #pragma omp parallel for num_threads(numThreads) if(numThreads>1)
#endif #endif
for(size_t i = 0; i < dstheight; i++) { for(int i = 0; i < dstheight; i++) {
int i_src = (i+shift)/2; int i_src = (i+shift)/2;
int begin = (i+shift)%2; int begin = (i+shift)%2;
//TODO: this is correct only if skip=1; otherwise, want to work with cosets of length 'skip' //TODO: this is correct only if skip=1; otherwise, want to work with cosets of length 'skip'