tags:

views:

32

answers:

2

Some libraries offer the ability to separate setup and execution, esp if the setup portion has undesirable characteristics such as unbounded latency.

If the program needs to have this reflected in its structure, then it is natural to have:

void setupXXX(...); // which calls the setup stuff
void doXXX(...); // which calls the execute stuff

The problem with this is that the structure of setupXXX and doXXX is going to be quite similar (at least textually -- control flow will prob be more complex in doXXX).

Wondering if there are any ways to avoid this.


Example:

Let's say we're doing signal processing: filtering with a known kernel in the frequency domain.

so, setupXXX and doXXX would probably be something like...

void doFilter(FilterStuff *c) {
    for (int i = 0; i < c->N; ++i) {
        doFFT(c->x[i], c->fft_forward_setup, c->tmp);
        doMultiplyVector(c->tmp, c->filter);
        doFFT(c->tmp, c->fft_inverse_setup, c->x[i]);
    }
}

void setupFilter(FilterStuff *c) {
    setupFFT(..., &(c->fft_forward_setup));
    // assign the kernel to c->filter
    ...
    setupFFT(..., &(c->fft_inverse_setup));
}
A: 

I am not sure how your code looks like, but if it is similar in traversing the data structures you use for your computation, maybe you could write a traverse method and do computation in it, in a "Template method design pattern" fashion, which you can implement in C/C++ even without OOP - using function pointers.

Gabriel Ščerbák
A: 

What are you trying to avoid? You have a set of statements using some things, and a set of statements that initialise those things. Having the two sets be similar could be seen as allowing visual verification that they are consistent.

I assume that you are wanting to avoid repetition. I was considering whether some approach using arrays of function pointers, so that initialisation is done in a for() loop, might be better. But presumably there will variability in the initialisation arguments you still end up needing a matching list of values in some array, ie. there will be repetition.

 setupFFT(fftarg1[i],fftarg2[i], &(c.ffts[i]));

requires setting up fftarg1, etc, so I don't think we've actually improved things much.

djna