# CUDA adapter for FFTW plan

**URL:** <https://discourse.julialang.org/t/cuda-adapter-for-fftw-plan/91353>\
**Category:** GPU\
**Tags:** fftw, cuda, adapt\
**Created:** [December 7, 2022, 9:55am UTC](https://discourse.julialang.org/t/cuda-adapter-for-fftw-plan/91353 "2022-12-07T09:55:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fedoroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedoroff/32/53209_2.png) [@fedoroff](https://discourse.julialang.org/u/fedoroff)\
**Post date:** [December 7, 2022, 9:55am UTC](https://discourse.julialang.org/t/cuda-adapter-for-fftw-plan/91353/1 "2022-12-07T09:55:26Z")

</div>

I am writing a code where I want to use a custom structure inside CUDA kernel. Following the CUDA.jl manual ([https://cuda.juliagpu.org/stable/tutorials/custom\_structs/](https://cuda.juliagpu.org/stable/tutorials/custom_structs/)), I need to write an [Adapt.jl](https://github.com/JuliaGPU/Adapt.jl) adapter for my structure. However, one of the fields of this structure is the Fourier transform FFTW.jl plan. Therefore, first, I have to write the adapter for this FFTW plan. The MWE can be the following:

```julia
using Adapt
using CUDA
using FFTW

abstract type ARCH{T} end
struct CPU{T} <: ARCH{T} end
struct GPU{T} <: ARCH{T} end
CPU() = CPU{Float64}()
GPU() = GPU{Float32}()

function Adapt.adapt_storage(::CPU{T}, p::FFTW.cFFTWPlan) where T
    tmp = zeros(Complex{T}, p.sz)
    return plan_fft!(tmp)
end

function Adapt.adapt_storage(::GPU{T}, p::FFTW.cFFTWPlan) where T
    tmp = CUDA.zeros(Complex{T}, p.sz)
    return plan_fft!(tmp)
end

E = zeros(ComplexF64, 128)

p = plan_fft!(E) # FFTW in-place forward plan for 128-element array of ComplexF64

pa = adapt(GPU(), p) # CUFFT in-place complex forward plan for 128-element CuArray of ComplexF32

```

This code works perfectly, but for each call of `adapt_storage` it allocates `tmp` array, which in my case can be very large. Therefore, I am searching a way to convert the FFTW plan using a low level definition of the plan structure.

The plan structure in FFTW.jl is defined [here](https://github.com/JuliaMath/FFTW.jl/blob/6f123e8184a78433c7d09daa6e486b33efd3dd3b/src/fft.jl#L242). For my adapter I would like to have a code similar to this one:

```julia
p = plan_fft!(E)

(; plan, sz, osz, istride, ostride, ialign, oalign, flags, region) = p

pa = FFTW.cFFTWPlan{ComplexF64, -1, true, 1, UnitRange{Int64}}(plan, sz, osz, istride, ostride, ialign, oalign, flags, region)
# It does not work!

```

Any ideas how I can do it?
