2

I have C-type SIMD allocations using FFTW3 of two arrays f and g using the same plans. I have simply set f to 1, then g to f, then zeroed f. This also ends up zeroing g.

Why does this arise and what are some ways I can ensure that any derived arrays by slicing for example, don't get modified.

I would like f and g to point to two different blocks of two-dimensioanl double precision memory. I have not used the usual explicit shape (double precision, dimension(n,n)) definitions as the FFTW3 documentation states that arrays allocated in this manner are faster to deal with.

program main

use,intrinsic::iso_c_binding
implicit none
include 'fftw3.f03'

integer,parameter::n=16
real(C_DOUBLE),pointer::f(:,:),g(:,:)
type(C_PTR)::p

p=fftw_alloc_real(int(n**2,C_SIZE_T))
!i am thinking of these as FFTW plans that store only the stencil to
!allocate space starting from addresses given by real(C_DOUBLE),pointers above.

call c_f_pointer(p,f,[n,n])
call c_f_pointer(p,g,[n,n])

f=1.0d0
print*,sum(f)
g=f

f=0.0d0
print*,sum(g)

call fftw_free(p)
end program

The output is

256.00000000000000     
0.0000000000000000
8
  • You have set f and g to point to the same thing. Do you understand the effects of that (ie., how pointers in Fortran work)? Commented Jul 15, 2018 at 10:54
  • Does the use of the same plan p do this? I have used the array declaration statements ALLOCATABLE,DIMENSION() within fortran always. I do have a general knowledge of pointers but not specific to Fortran. Commented Jul 15, 2018 at 11:02
  • Typo in call c_f_pointer(p,g,[n,n])? Commented Jul 15, 2018 at 11:02
  • c_f_pointer does nothing special (ie., it knows nothing about FFTW plans). The two calls just point f and g (in this case) to the same target as the C pointer p. Commented Jul 15, 2018 at 11:04
  • 2
    Could you clarify, in the question (as an edit) what you want to happen? That is, how you intend to use the various memory aspects/pointers. As it is, we're probably guessing about what you mean by re-using FFTW plans (consider also the next reader with the same problem who isn't using FFTW but something else). Commented Jul 15, 2018 at 11:11

1 Answer 1

2

The allocation of memory is done by fftw_alloc_real. You call that only once, so only one block of memory is allocated. c_f_pointer does not perform any allocation.

c_f_pointer takes a C pointer and pointer associates a Fortran pointer with the target of the C pointer. When you

call c_f_pointer(p,f,[n,n])
call c_f_pointer(p,g,[n,n])

you are associating both f and g with the same lump of memory, that pointed to by p.

Simply allocate two distinct parts, with two calls to fftw_alloc_real and point f to one and g to the other.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.