[Omp] static variables question
Scott Paine
spaine at cfa.harvard.edu
Mon Jan 8 09:04:20 PST 2007
Certainly Tim is right-- even in a single-threaded envrionment, where in
principle the order of recursive function calls is knowable in advance,
things would be conceptually very difficult to keep track of if the
recursively-called function were not reentrant.
Regarding recursion in OpenMP, I've done the following kind of thing
using nested parallelism. In the example below, which is from a
recursive fft, the number (nbranch) of recursive calls to fft_dif_rec()
doubles at each recursion level. New threads are generated unless
nbranch exceeds the maximum number of OMP threads. The idea is to pass
a parameter to each recursive call which lets that call know how deep in
the recursion it is. Then, an if clause on a parallel sections
directive can be used to control whether subsequent recursion is done
serially or in parallel.
Scott Paine
Smithsonian Astrophysical Observatory
spaine at cfa.harvard.edu
========================================
/*
* External interface calls recursive routine with an initial branch
count of 1
*/
void fft_dif(double *z, int n)
{
fft_dif_rec(z, n, 1);
return;
} /* fft_dif() */
static void fft_dif_rec(double *z, int n, int nbranch)
{
. . . code omitted here . . .
nh = n >> 1;
nbranch <<= 1;
#ifdef _OPENMP
#pragma omp parallel sections if (nbranch <= omp_get_max_threads())
#endif
{
#ifdef _OPENMP
#pragma omp section
#endif
fft_dif_rec(z, nh, nbranch);
#ifdef _OPENMP
#pragma omp section
#endif
fft_dif_rec(z + n, nh, nbranch);
}
return;
} /* fft_dif_rec() */
On Sun, 2007-01-07 at 16:29 -0800, Tim Prince wrote:
> Klaas.Vantournhout at UGent.be wrote:
> >
> > I have a question regarding static variables which are declared in
> > routines.
> >
> >
> > What I am thinking about are recursive functions which use static
> > variables to obtain a meaningful result.
> Any variable which has a value belonging to the current scope of a
> recursive function must be automatic, regardless of openmp. I'm having
> difficulty imagining how a recursive function would be employed in
> openmp, other than within task sharing, with each thread having its own
> data (presumably thread private).
> _______________________________________________
> Omp mailing list
> Omp at openmp.org
> http://openmp.org/mailman/listinfo/omp
>
More information about the Omp
mailing list