# InPlace FFT Plan and view

**URL:** <https://discourse.julialang.org/t/inplace-fft-plan-and-view/43016>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 14, 2020, 5:21am UTC](https://discourse.julialang.org/t/inplace-fft-plan-and-view/43016 "2020-07-14T05:21:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AmarjeetK](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amarjeetk/32/16366_2.png) [@AmarjeetK](https://discourse.julialang.org/u/AmarjeetK)\
**Post date:** [July 14, 2020, 5:21am UTC](https://discourse.julialang.org/t/inplace-fft-plan-and-view/43016/1 "2020-07-14T05:21:38Z")

</div>

Hi All,

I have this view problem with plan\_fft!,

Part of the code is below:

using FFTW  
L = (128,16,16);  
pad = 2;

data = rand(ComplexF32,128,16,16);  
FFT\_buffer = zeros(ComplexF32,128,32,32);

FFT\_buffer[:,1:L[2],1:L[3]] .= data;

# I want to apply 1D FFT along second dimension in FFT\_buffer, because I know half of the data in the second and third dimension are zero.

Therefore, I have created a FFTPlan to do 1D FFT along second dimension as below:

FFTPlan = plan\_fft!(rand(ComplexF32,L[1],L[2],L[3]._pad),2,flags=FFTW.MEASURE); # for size 128_16\*32

FFTPlan \* view(FFT\_buffer,:,1:L[2],:); # this doesn’t work

=\> ERROR: ArgumentError: FFTW plan applied to wrong-strides array

FFTPlan \* FFT\_buffer[:,1:L[2],:] # this works but then it doesn’t update FFT\_buffer file.

I have to do this to get it working

FFT\_buffer[:,1:L[2],:] .= FFTPlan \* FFT\_buffer[:,1:L[2],:] ;

But then I am allocating the memory by doing this as shown below:

@time FFT\_buffer[:,1:L[2],:] .= FFTPlan \* FFT\_buffer[:,1:L[2],:] ;  
0.000603 seconds (22 allocations: 512.750 KiB)

I don’t want to reallocate memory again. I already have done the allocation in FFT\_buffer file, I want to avoid any memory allocation now.  
I could have avoided if view(FFT\_buffer,:,1:L[2],🙂 had worked.

Does anyone have any idea how to avoid memory allocation here and use view in FFT\_buffer to do such operation?

This is the part of my code, and it runs in the loop for many many iterations, that’s why I want to avoid this memory allocation.

Thank you.

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [July 14, 2020, 8:28am UTC](https://discourse.julialang.org/t/inplace-fft-plan-and-view/43016/2 "2020-07-14T08:28:20Z")

</div>

Welcome AmarjeetK,  
let me try to help. I think what you want is this:

```julia
using FFTW

L = (128,16,16);
pad = 2;

data = rand(ComplexF32,128,16,16);
FFT_buffer = zeros(ComplexF32,128,32,32);

FFT_buffer[:,1:L[2],1:L[3]] .= data;
FFT_buffer_copy = deepcopy(FFT_buffer);

FFT_bufferview=view(FFT_buffer,1:L[1],1:L[2],1:L[3]*pad)
FFTPlan = plan_fft!(FFT_bufferview,2,flags=FFTW.MEASURE);

@time FFTPlan*FFT_bufferview #gives 0 allocations (on the second evaluation)

FFT_buffer==FFT_buffer_copy #gives false, as FFT_buffer is mutated

```

If you don’t like the view syntax, you can also use the macro

```julia
@views X[1:n,1:m,1:l] .= XXX
#is equivalent to
view(X,1:n,1:m,1:l) .= XXX

```

I hope this is what you wanted! Also, check out [Please read: make it easier to help you - #103](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/103) to see how you can highlight code snippets 😉

---

<div class="post-metadata">

**Author:** ![AmarjeetK](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amarjeetk/32/16366_2.png) [@AmarjeetK](https://discourse.julialang.org/u/AmarjeetK)\
**Post date:** [July 15, 2020, 1:43am UTC](https://discourse.julialang.org/t/inplace-fft-plan-and-view/43016/3 "2020-07-15T01:43:41Z")

</div>

Thank you @fgerick

This might serve my problem, will implement in my code and will then let you know.

Does this mean I have to carry FFT\_bufferview file as well?  
because this viewing also takes some allocation as well right?  
For example:

```julia
@time FFT_bufferview=view(FFT_buffer,1:L[1],1:L[2],1:L[3]*pad);
 0.000006 seconds (11 allocations: 464 bytes)

```

---

<div class="post-metadata">

**Author:** ![AmarjeetK](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amarjeetk/32/16366_2.png) [@AmarjeetK](https://discourse.julialang.org/u/AmarjeetK)\
**Post date:** [July 15, 2020, 1:44am UTC](https://discourse.julialang.org/t/inplace-fft-plan-and-view/43016/4 "2020-07-15T01:44:16Z")

</div>

Oh wow this PSA think worked nicely, thanks for letting me know.  
Cheers

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [July 15, 2020, 4:25pm UTC](https://discourse.julialang.org/t/inplace-fft-plan-and-view/43016/5 "2020-07-15T16:25:02Z")

</div>

> [@AmarjeetK](#):
>
> Does this mean I have to carry FFT\_bufferview file as well?

I’m not sure what you mean by this, but a small allocation for each newly generated view is needed (I think). But judging from the fact that you plan your fft I assume that the view remains the same?
