Debian Codegraph @

fftw3.pd

raw
1 =head1 NAME
2
3 PDL::FFTW3 - PDL interface to the Fastest Fourier Transform in the West v3
4
5 =cut
6
7 # -*- cperl -*-
8
9 ##### General layout of the module #####
10 #
11 # Each type of transform that is supported by this module has a plain,
12 # unthreaded perl entry point the user calls. This entry point makes sure the
13 # FFTW plan exists (or makes it). Then it calls the PP function to
14 # actually compute the transform
15
16 use strict;
17 use warnings;
18
19 # I generate code for up to 10-dimensional FFTs
20 my $maxrank = 10;
21
22 our $VERSION = '0.204';
23 pp_setversion($VERSION);
24
25 pp_addpm( {At => 'Top'}, slurp('README.pod') . <<'EOF' );
26
27 use strict;
28 use warnings;
29
30 EOF
31
32 pp_addhdr( '
33 #include <fftw3.h>
34
35 /* the Linux kernel does something similar to assert at compile time */
36 #define static_assert_fftw(x) (void)( sizeof( int[ 1 - 2* !(x) ]) )
37 ' );
38
39 # I want to be able to say $X = fft1($x); rank is required. 'fft()' is ambiguous
40 # about whether broadcasting is desired or if a large fft is desired. Old PDL::FFTW
41 # did one thing, matlab does another, so I do not include this function at all
42
43 my $TEMPLATE_REAL_R2C = <<'EOF';
44 // make sure the PDL data type I'm using matches the FFTW data type
45 static_assert_fftw(sizeof($GENERIC())*2 == sizeof($TFD(fftwf_,fftw_)complex));
46 $TFD(fftwf_,fftw_)plan plan = INT2PTR($TFD(fftwf_,fftw_)plan, $COMP(plan));
47 $TFD(fftwf_,fftw_)execute_dft_r2c(plan, (void*)$P(real), (void*)$P(complexv));
48 EOF
49 my $TEMPLATE_REAL_C2R = <<'EOF';
50 // make sure the PDL data type I'm using matches the FFTW data type
51 static_assert_fftw(sizeof($GENERIC()) == sizeof($TGC(fftwf_,fftw_)complex));
52 $TGC(fftwf_,fftw_)plan plan = INT2PTR($TGC(fftwf_,fftw_)plan, $COMP(plan));
53 // FFTW inverse real transforms clobber their input. I thus make a new
54 // buffer and transform from there
55 PDL_Indx i, nbytes = sizeof($GENERIC());
56 PDL_Indx rank = $PRIV(vtable)->par_realdims[0], *dims = $PDL(complexv)->dims;
57 for (i=0; i<rank; i++) nbytes *= dims[i];
58 void *input_copy = fftw_malloc(nbytes);
59 broadcastloop %{
60 memcpy(input_copy, $P(complexv), nbytes);
61 $TGC(fftwf_,fftw_)execute_dft_c2r(plan, input_copy, (void*)$P(real));
62 %}
63 fftw_free(input_copy);
64 EOF
65 my $TEMPLATE_COMPLEX = <<'EOF';
66 // This is the template used by PP to generate the FFTW routines.
67 // make sure the PDL data type I'm using matches the FFTW data type
68 static_assert_fftw(sizeof($GENERIC()) == sizeof($TGC(fftwf_,fftw_)complex));
69 $TGC(fftwf_,fftw_)plan plan = INT2PTR($TGC(fftwf_,fftw_)plan, $COMP(plan));
70 $TGC(fftwf_,fftw_)execute_dft(plan, (void*)$P(in), (void*)$P(out));
71 EOF
72
73 # I define up to rank-10 FFTs. This is annoyingly arbitrary, but hopefully
74 # should be sufficient
75 for my $rank (1..$maxrank)
76 {
77 generateDefinitions($rank);
78 }
79 pp_export_nothing();
80
81 pp_addxs('', <<'EOXS');
82 MODULE = PDL::FFTW3 PACKAGE = PDL::FFTW3
83
84 SV *
85 dump_plan(planIV)
86 IV planIV
87 CODE:
88 void* plan = NUM2PTR(void *, planIV);
89 char *s = fftw_sprint_plan((const fftw_plan)plan);
90 if (!s) croak("Got NULL string from fftw_sprint_plan");
91 RETVAL = newSVpv(s, 0);
92 free(s);
93 OUTPUT:
94 RETVAL
95
96 IV
97 compute_plan( dims_ref, do_double_precision, is_real_fft, do_inverse_fft, in_pdl, out_pdl, in_alignment, out_alignment )
98 SV* dims_ref
99 bool do_double_precision
100 bool is_real_fft
101 bool do_inverse_fft
102 pdl* in_pdl
103 pdl* out_pdl
104 int in_alignment
105 int out_alignment
106 CODE:
107 {
108 // Given input and output matrices, this function computes the FFTW plan
109
110 // PDL stores its data in the opposite dimension order from what FFTW wants. I
111 // handle this by passing in the dimension counts backwards.
112 AV* dims_av = (AV*)SvRV(dims_ref);
113 int rank = av_len(dims_av) + 1;
114
115 int dims_row_first[rank];
116 for( int i=0; i<rank; i++)
117 dims_row_first[i] = SvIV( *av_fetch( dims_av, rank-i-1, 0) );
118
119 // I apply the requested mis-alignment. This comes from later broadcast slices
120 UVTYPE in_data = PTR2UV(in_pdl->data);
121 if( in_alignment < 16 )
122 in_data |= in_alignment;
123
124 UVTYPE out_data = PTR2UV(out_pdl->data);
125 if( out_alignment < 16 )
126 out_data |= out_alignment;
127
128 void* plan;
129 if( !is_real_fft )
130 {
131 int direction = do_inverse_fft ? FFTW_BACKWARD : FFTW_FORWARD;
132
133 // complex-complex FFT. Input/output have identical dimensions
134 if( !do_double_precision )
135 plan =
136 fftwf_plan_dft( rank, dims_row_first,
137 NUM2PTR(void*, in_data), NUM2PTR(void*, out_data),
138 direction, FFTW_ESTIMATE);
139 else
140 plan =
141 fftw_plan_dft( rank, dims_row_first,
142 NUM2PTR(void*, in_data), NUM2PTR(void*, out_data),
143 direction, FFTW_ESTIMATE);
144 }
145 else
146 {
147 // real-complex FFT. Input/output have different dimensions
148 if( !do_double_precision)
149 {
150 if( !do_inverse_fft )
151 plan =
152 fftwf_plan_dft_r2c( rank, dims_row_first,
153 NUM2PTR(void*, in_data), NUM2PTR(void*, out_data),
154 FFTW_ESTIMATE );
155 else
156 plan =
157 fftwf_plan_dft_c2r( rank, dims_row_first,
158 NUM2PTR(void*, in_data), NUM2PTR(void*, out_data),
159 FFTW_ESTIMATE );
160 }
161 else
162 {
163 if( !do_inverse_fft )
164 plan =
165 fftw_plan_dft_r2c( rank, dims_row_first,
166 NUM2PTR(void*, in_data), NUM2PTR(void*, out_data),
167 FFTW_ESTIMATE );
168 else
169 plan =
170 fftw_plan_dft_c2r( rank, dims_row_first,
171 NUM2PTR(void*, in_data), NUM2PTR(void*, out_data),
172 FFTW_ESTIMATE );
173 }
174 }
175
176 if( plan == NULL )
177 XSRETURN_UNDEF;
178 else
179 RETVAL = PTR2IV(plan);
180 }
181 OUTPUT:
182 RETVAL
183
184
185
186 int
187 is_same_data( in, out )
188 pdl* in
189 pdl* out
190 CODE:
191 {
192 RETVAL = (in->data == out->data) ? 1 : 0;
193 }
194 OUTPUT:
195 RETVAL
196
197
198 #define _get_data_alignment_int( x ) \
199 ( (x & 0xF) == 0 ) ? 16 : \
200 ( (x & 0x7) == 0 ) ? 8 : \
201 ( (x & 0x3) == 0 ) ? 4 : \
202 ( (x & 0x1) == 0 ) ? 2 : 1;
203
204 int
205 get_data_alignment_int( x )
206 UV x
207 CODE:
208 {
209 RETVAL = _get_data_alignment_int( x );
210 }
211 OUTPUT:
212 RETVAL
213
214
215 int
216 get_data_alignment_pdl( in )
217 pdl* in
218 CODE:
219 {
220 RETVAL = _get_data_alignment_int( PTR2UV(in->data) );
221 }
222 OUTPUT:
223 RETVAL
224 EOXS
225
226 pp_addpm( {At => 'Middle'}, <<'EOINCLUDE' );
227 use PDL::Types;
228 use List::Util 'reduce';
229 use threads::shared;
230
231 # When I compute an FFTW plan, it goes here.
232 # This is :shared so that it can be used with Perl threads.
233 my %existingPlans :shared;
234
235 # these are for the unit tests
236 our $_Nplans = 0;
237 our $_last_do_double_precision;
238
239 # This is a function that sits between the user's call into this module and the
240 # PP-generated internals. Specifically, this function is called BEFORE any PDL
241 # broadcasting happens. Here I make sure the FFTW plan exists, or if it doesn't, I
242 # make it. Thus the PP-based internals can safely assume that the plan exists
243 sub __fft_internal {
244 my $thisfunction = shift;
245
246 my ($do_inverse_fft, $is_real_fft, $rank) = $thisfunction =~ /^(i?)(r?)N?.*fft([0-9]+)/;
247
248 # first I parse the variables. This is a very direct translation of what PP
249 # does normally. Plan-creation has to be outside of PP, so I must re-do this
250 # here
251 my $Nargs = scalar @_;
252
253 my ($in, $out);
254 if ( $Nargs == 2 ) {
255 # all variables on stack, read in output and temp vars
256 ($in, $out) = map {defined $_ ? PDL::Core::topdl($_) : $_} @_;
257 } elsif ( $Nargs == 1 ) {
258 $in = PDL::Core::topdl $_[0];
259 if ( $in->is_inplace ) {
260 barf <<EOF if $is_real_fft;
261 $thisfunction: in-place real FFTs are not supported since the input/output types and data sizes differ.
262 Giving up.
263 EOF
264 $out = $in;
265 $in->set_inplace(0);
266 } else {
267 $out = PDL::null();
268 }
269 } else {
270 barf( <<EOF );
271 $thisfunction must be given the input or the input and output as args.
272 Exactly 1 or 2 arguments are required. Instead I got $Nargs args. Giving up.
273 EOF
274 }
275
276 # make sure the in/out types match. Convert $in if needed. This needs to
277 # happen before we instantiate $out (if it's null) to make sure we know the
278 # type
279 processTypes( $thisfunction, \$in, \$out );
280
281 # I now create an ndarray for the null output. Normally PP does this, but I need
282 # to have the ndarray made to create plans. If I don't, the alignment may
283 # differ between plan-time and run-time
284 if ( $out->isnull ) {
285 my ($type, @dims) = getOutArgs($in, $is_real_fft, $do_inverse_fft);
286 $out->set_datatype($type->enum); $out->setdims(\@dims); $out->make_physical;
287 }
288
289 validateArguments( $rank, $is_real_fft, $do_inverse_fft, $thisfunction, $in, $out );
290
291 # I need to physical-ize the ndarrays before I make a plan. Again, normally PP
292 # does this, but to make sure alignments match, I need to do this myself, now
293 $in->make_physical;
294 $out->make_physical;
295
296 my $plan = getPlan( $thisfunction, $rank, $is_real_fft, $do_inverse_fft, $in, $out );
297 barf "$thisfunction couldn't make a plan. Giving up\n" unless defined $plan;
298
299 my $is_native = !$in->type->real; # native complex
300 # I now have the arguments and the plan. Go!
301 my $internal_function = 'PDL::__';
302 $internal_function .=
303 !$is_real_fft ? 'N' :
304 ($is_native && $do_inverse_fft) ? 'irN' :
305 $do_inverse_fft ? barf("irfft no longer supports PDL::Complex") :
306 'rN';
307 $internal_function .= "fft$rank";
308 eval { no strict 'refs'; $internal_function->( $in, $out, $plan ) };
309 barf $@ if $@;
310 $out;
311 }
312
313 sub getOutArgs {
314 my ($in, $is_real_fft, $do_inverse_fft) = @_;
315
316 my @dims = $in->dims;
317 my $is_native = !$in->type->real;
318 my $out_type = $in->type;
319
320 if ( !$is_real_fft ) {
321 # complex fft. Output is the same size as the input.
322 } elsif ( !$do_inverse_fft ) {
323 # forward real fft
324 $dims[0] = int($dims[0]/2)+1;
325 $out_type = typeWithComplexity(getPrecision($out_type), 1);
326 } else {
327 # backward real fft
328 #
329 # there's an ambiguity here. I want int($out->dim(0)/2) + 1 == $in->dim(1),
330 # however this could mean that
331 # $out->dim(0) = 2*$in->dim(1) - 2
332 # or
333 # $out->dim(0) = 2*$in->dim(1) - 1
334 #
335 # WITHOUT ANY OTHER INFORMATION, I ASSUME EVEN INPUT SIZES, SO I ASSUME
336 # $out->dim(0) = 2*$in->dim(1) - 2
337 if ($is_native) {
338 $out_type = ($out_type == cfloat) ? float : double;
339 } else {
340 shift @dims;
341 }
342 $dims[0] = 2*($dims[0]-1);
343 }
344 ($out_type, @dims);
345 }
346
347 sub validateArguments
348 {
349 my ($rank, $is_real_fft, $do_inverse_fft, $thisfunction, $in, $out) = @_;
350
351 for my $arg ( $in, $out )
352 {
353 barf <<EOF unless defined $arg;
354 $thisfunction arguments must all be defined. If you want an auto-growing ndarray, use 'null' such as
355 $thisfunction( \$in, \$out = null )
356 Giving up.
357 EOF
358
359 my $type = ref $arg;
360 $type = 'scalar' unless defined $arg;
361 barf <<EOF unless ref $arg && $arg->isa('PDL');
362 $thisfunction arguments must be of type 'PDL'.
363 Instead I got an arg of type '$type'. Giving up.
364 EOF
365 }
366
367 # validate dimensionality of the ndarrays
368 my @inout = ($in, $out);
369
370 for my $iarg ( 0..1 )
371 {
372 my $arg = $inout[$iarg];
373
374 if( $arg->isnull )
375 {
376 barf "$thisfunction: don't know what to do with a null input. Giving up";
377 }
378
379 if( !$is_real_fft )
380 { validateArgumentDimensions_complex( $rank, $thisfunction, $arg); }
381 else
382 { validateArgumentDimensions_real( $rank, $do_inverse_fft, $thisfunction, $iarg, $arg); }
383 }
384
385 # we have an explicit output ndarray we're filling in. Make sure the
386 # input/output dimensions match up
387 if ( !$is_real_fft )
388 { matchDimensions_complex($thisfunction, $rank, $in, $out); }
389 else
390 { matchDimensions_real($thisfunction, $rank, $do_inverse_fft, $in, $out); }
391 }
392
393 sub validateArgumentDimensions_complex
394 {
395 my ( $rank, $thisfunction, $arg ) = @_;
396 barf "Tried to compute a complex FFT, but non-native-complex argument given"
397 if $arg->type->real;
398 my $dims_cmp = $arg->ndims;
399 barf <<EOF if $dims_cmp < $rank;
400 Tried to compute a $rank-dimensional FFT, but an array has fewer than $rank dimensions.
401 Giving up.
402 EOF
403 }
404
405 sub validateArgumentDimensions_real {
406 my ( $rank, $do_inverse_fft, $thisfunction, $iarg, $arg ) = @_;
407 my $is_native = !$arg->type->real; # native complex
408
409 # real FFT. Forward transform takes in real and spits out complex;
410 # backward transform does the reverse
411 if (!!$do_inverse_fft == !!($iarg == 0)) { # need complex for this
412 my ($verb, $var, $reason) = ($iarg == 0) ? qw(takes input) : qw(produces output);
413 if ( ($iarg == 1 && !$is_native) ||
414 ($iarg == 0 && !$is_native)
415 ) {
416 $reason = "\$$var should be native-complex";
417 } elsif (!$is_native && $arg->dim(0) != 2) {
418 $reason = "\$$var->dim(0) == 2 should be true";
419 }
420 barf <<EOF if $reason;
421 $thisfunction $verb complex $var, so $reason,
422 but it's not (in @{[$arg->info]}: $arg). Giving up.
423 EOF
424 }
425
426 my ($min_dimensionality, $var) = ($rank, $iarg == 0 ? 'input' : 'output');
427 if ( $arg->ndims < $min_dimensionality ) {
428 barf <<EOF;
429 $thisfunction: The $var needs at least $min_dimensionality dimensions, but
430 it has fewer. Giving up.
431 EOF
432 }
433 }
434
435 sub matchDimensions_complex {
436 my ($thisfunction, $rank, $in, $out) = @_;
437 for my $idim (0..$rank) {
438 if ( $in->dim($idim) != $out->dim($idim) ) {
439 barf <<EOF;
440 $thisfunction was given input/output matrices of non-matching sizes.
441 Giving up.
442 EOF
443 }
444 }
445 }
446
447 sub matchDimensions_real {
448 my ($thisfunction, $rank, $do_inverse_fft, $in, $out) = @_;
449 my ($varname1, $varname2, $var1, $var2);
450 if ( !$do_inverse_fft ) {
451 # Forward FFT. The input is real, the output is complex.
452 # $output->dim(1) should be int($input->dim(0)/2) + 1 (Section 2.4 of
453 # the FFTW3 documentation)
454 ($varname1, $varname2, $var1, $var2) = (qw(input output), $in, $out);
455 } else {
456 # Backward FFT. The input is complex, the output is real.
457 ($varname1, $varname2, $var1, $var2) = (qw(output input), $out, $in);
458 }
459 barf <<EOF if int($var1->dim(0)/2) + 1 != $var2->dim(0);
460 $thisfunction: mismatched first dimension:
461 \$$varname2->dim(0) == int(\$$varname1->dim(0)/2) + 1 wasn't true.
462 $varname1: @{[$var1->info]}
463 $varname2: @{[$var2->info]}
464 Giving up.
465 EOF
466 for my $idim (1..$rank-1) {
467 if ( $var1->dim($idim) != $var2->dim($idim) ) {
468 barf <<EOF;
469 $thisfunction was given input/output matrices of non-matching sizes.
470 Giving up.
471 EOF
472 }
473 }
474 }
475
476 sub processTypes
477 {
478 my ($thisfunction, $in, $out) = @_;
479
480 # types:
481 #
482 # Input and output types must match, and I can only really deal with float and
483 # double. If given an output, I refuse to tweak the type of the output,
484 # otherwise, I upgrade to float and then to double
485 if( $$out->isnull ) {
486 if( $$in->type < float ) {
487 forceType( $in, (float) );
488 }
489 } else {
490 # I'm given an output. Make sure this is of a type I can work with,
491 # otherwise give up
492 my $out_type = $$out->type;
493 barf <<EOF if $out_type < float;
494 $thisfunction can only generate 'float' or 'double' output. You gave an output
495 of type '$out_type'. I can't change this so I give up
496 EOF
497 my $in_type = $$in->type;
498 my $in_precision = getPrecision($in_type);
499 my $out_precision = getPrecision($out_type);
500 return if $in_precision == $out_precision;
501 forceType( $in, typeWithComplexity($out_precision, !$in_type->real) );
502 forceType( $out, typeWithComplexity($out_precision, !$out_type->real) );
503 }
504 }
505
506 sub typeWithComplexity {
507 my ($precision, $complex) = @_;
508 $complex ? ($precision == 1 ? cfloat : cdouble) :
509 $precision == 1 ? float : double;
510 }
511
512 sub getPrecision {
513 my ($type) = @_;
514 ($type <= float || $type == cfloat) ? 1 : # float
515 2; # double
516 }
517
518 sub forceType
519 {
520 my ($x, $type) = @_;
521 $$x = convert( $$x, $type ) unless $$x->type == $type;
522 }
523
524 sub getPlan
525 {
526 my ($thisfunction, $rank, $is_real_fft, $do_inverse_fft, $in, $out) = @_;
527
528 # I get the plan ID, check if I already have a plan, and make a new plan if I
529 # don't already have one
530
531 my @dims = ((!$is_real_fft || !$do_inverse_fft) ? $in : $out)->dims; # FFT dimensionality
532
533 my $Nslices = reduce {$a*$b} 1, splice(@dims, $rank);
534
535 my $do_double_precision = ($in->get_datatype == $PDL_F || $in->get_datatype == $PDL_CF)
536 ? 0 : 1;
537 $_last_do_double_precision = $do_double_precision;
538
539 my $do_inplace = is_same_data( $in, $out );
540
541 # I compute a single plan for the whole set of broadcast slices. I make a
542 # worst-case plan, so I find the worst-aligned broadcast slice and plan off of
543 # it. So if $Nslices>1 then the worst-case alignment is the worse of (1st,
544 # 2nd) slices
545 my $in_alignment = get_data_alignment_pdl( $in );
546 my $out_alignment = get_data_alignment_pdl( $out );
547 my $stride_bytes = ($do_double_precision ? 8 : 4) * reduce {$a*$b} @dims;
548 if( $Nslices > 1 )
549 {
550 my $in_alignment_2nd = get_data_alignment_int($in_alignment + $stride_bytes);
551 my $out_alignment_2nd = get_data_alignment_int($out_alignment + $stride_bytes);
552 $in_alignment = $in_alignment_2nd if $in_alignment_2nd < $in_alignment;
553 $out_alignment = $out_alignment_2nd if $out_alignment_2nd < $out_alignment;
554 }
555
556 my $planID = join('_',
557 $thisfunction,
558 $do_double_precision,
559 $do_inplace,
560 $in_alignment,
561 $out_alignment,
562 @dims);
563 if ( !exists $existingPlans{$planID} )
564 {
565 lock(%existingPlans);
566 $existingPlans{$planID} = compute_plan( \@dims, $do_double_precision, $is_real_fft, $do_inverse_fft,
567 $in, $out, $in_alignment, $out_alignment );
568 $_Nplans++;
569 }
570
571 return $existingPlans{$planID};
572 }
573 EOINCLUDE
574
575 for my $rank (1..$maxrank)
576 {
577 my $shapestr = sprintf(q{$a->shape->slice('0:%d')->prodover},$rank-1);
578
579 pp_addpm({At => 'Bot'}, pp_line_numbers(__LINE__, <<EOF));
580 sub fft$rank { __fft_internal( "fft$rank",\@_ ); }
581 *PDL::fft$rank = \\&fft$rank;
582
583 sub ifft$rank {
584 my \$a = __fft_internal( "ifft$rank", \@_ );
585 \$a /= $shapestr;
586 \$a;
587 }
588 *PDL::ifft$rank = \\&ifft$rank;
589
590 sub rfft$rank { __fft_internal( "rfft$rank", \@_ ); }
591 *PDL::rfft$rank = \\&rfft$rank;
592
593 sub rNfft$rank { __fft_internal( "rNfft$rank", \@_ ); }
594 *PDL::rNfft$rank = \\&rNfft$rank;
595
596 sub irfft$rank { my \$a = __fft_internal( "irfft$rank", \@_ ); \$a /= $shapestr; \$a; }
597 *PDL::irfft$rank = \\&irfft$rank;
598 EOF
599
600 pp_add_exported( map "${_}fft$rank", '', 'i', 'r', 'rN', 'ir' );
601 }
602
603
604 ##########
605 # Generate the fftn case. This should probably be done more prettily; for now it's just
606 # a springboard that jumps into __fft_internal.
607 pp_addpm({At=> 'Bot'}, pp_line_numbers(__LINE__, sprintf <<'EOF', $maxrank));
608 sub _rank_springboard {
609 my ($name, $source, $rank, @rest) = @_;
610 my $inverse = ($name =~ m/^i/);
611
612 unless(defined $rank) {
613 die "${name}n: second argument must be the rank of the transform you want";
614 }
615 $rank = 0+$rank; # force numeric context
616 unless($rank>=1 ) {
617 die "${name}n: second argument (rank) must be between 1 and %d";
618 }
619
620 my $active_lo = 0;
621 my $active_hi = $rank-1;
622
623 unless($source->ndims > $active_hi) {
624 die "${name}n: rank is $rank but input has only ".($active_hi-$active_lo)." active dims!";
625 }
626
627 my $out = __fft_internal( $name.$rank, $source, @rest );
628
629 if($inverse) {
630 $out /= $out->shape->slice("$active_lo:$active_hi")->prodover;
631 }
632 return $out;
633 }
634
635 sub fftn { _rank_springboard( "fft", @_ ) }
636 sub ifftn { _rank_springboard( "ifft", @_ ) }
637 sub rfftn { _rank_springboard( "rfft", @_ ) }
638 sub irfftn { _rank_springboard( "irfft", @_ ) }
639
640 *PDL::fftn = \&fftn;
641 *PDL::ifftn = \&ifftn;
642 *PDL::rfftn = \&rfftn;
643 *PDL::irfftn = \&irfftn;
644
645 EOF
646 pp_add_exported( map "${_}fftn", '','i','r','ir' );
647
648 pp_done();
649
650 sub generateDefinitions
651 {
652 my $rank = shift;
653
654 my %pp_def = (
655 HandleBad => 0,
656 OtherPars => 'IV plan', # comes from pre-fft code not user
657 # this is a private function so I don't want to create
658 # user-visible documentation or exports
659 Doc => undef,
660 PMFunc => ''
661 );
662
663 ################################################################################
664 ####### first I generate the definitions for the simple complex-complex FFT case
665 # make dimension string 'n1,n2,n3,n4...'.
666 my @dims_real = my @dims_complex = my @dims = map "n$_", 1..$rank;
667 my $dims_string = join(',', @dims);
668 $pp_def{Pars} = "in($dims_string); [o]out($dims_string);";
669 $pp_def{GenericTypes} = [qw(G C)];
670 pp_def( "__Nfft$rank", %pp_def, Code => $TEMPLATE_COMPLEX );
671
672 ##################################################################################
673 ####### real-native complex and native complex-real
674 $dims_complex[0] = 'nhalf'; # first complex dim is real->dim(0)/2+1
675 my $dims_real_string = join(',', @dims_real);
676 my $dims_complex_string = join(',', @dims_complex);
677
678 # backward
679 $pp_def{RedoDimsCode} = <<'EOF';
680 if( $PDL(real)->dims[0] <= 0 )
681 $SIZE(n1) = 2*$PDL(complexv)->dims[0] - 2;
682 EOF
683 $pp_def{Pars} = "complexv($dims_complex_string); real [o]real($dims_real_string);";
684 pp_def( "__irNfft$rank", %pp_def, Code => $TEMPLATE_REAL_C2R );
685
686 # forward
687 $pp_def{RedoDimsCode} = <<'EOF';
688 if( $PDL(complexv)->ndims <= 1 || $PDL(complexv)->dims[1] <= 0 )
689 $SIZE(nhalf) = (int)( $PDL(real)->dims[0]/2 ) + 1;
690 EOF
691 $pp_def{Pars} = "real($dims_real_string); complex [o]complexv($dims_complex_string);";
692 $pp_def{GenericTypes} = [qw(F D)];
693 pp_def("__rNfft$rank",
694 %pp_def,
695 Code => $TEMPLATE_REAL_R2C,
696 );
697 }
698
699 sub slurp {
700 my $filename = shift;
701 open my $fh, '<', $filename or die "open '$filename' for reading: $!";
702 local $/ = undef;
703 return qq{\n#line 0 "$filename"\n\n} . <$fh>;
704 }