Using aligned memory for Fortran FFTs (FFTW) without memory leaksC++: memory leaksMulti threaded FFTW 3.1.2 on a shared memory computerCreating a memory leak with JavaIs this code generating memory leaks or is it clean?Optimization of FFTW to top Matlab FFT2D FFT using 1D FFT with mpiHow to avoid Memory Leak with returning const char *DCT of complex arrays with FFTW in Fortran: How to point to the imaginary part array?FFTW in Fortran result contains only zerosMemory leak by returning allocated strings

Pretty heat maps

Why should public servants be apolitical?

Secure my password from unsafe servers

What would happen to an adventurer's personal identity when turning into a God?

Should I self-publish my novella on Amazon or try my luck getting publishers?

Acceptable to cut steak before searing?

How to realistically deal with a shield user?

Could one become a successful researcher by writing some really good papers while being outside academia?

Is this cheap "air conditioner" able to cool a room?

Replace data between quotes in a file

In the movie Harry Potter and the Order or the Phoenix, why didn't Mr. Filch succeed to open the Room of Requirement if it's what he needed?

What word can be used to describe a bug in a movie?

Infeasibility in mathematical optimization models

Should I take out a personal loan to pay off credit card debt?

Ex-contractor published company source code and secrets online

Can we use other things than single-word verbs in our dialog tags?

Is the beaming of this score following a vocal practice or it is just outdated and obscuring the beat?

Is it really ~648.69 km/s delta-v to "land" on the surface of the Sun?

How does The Fools Guild make its money?

How to say "fit" in Latin?

Look mom! I made my own (Base 10) numeral system!

What are good ways to improve as a writer other than writing courses?

Shabbat clothing on shabbat chazon

How can I tell if a flight itinerary is fake?



Using aligned memory for Fortran FFTs (FFTW) without memory leaks


C++: memory leaksMulti threaded FFTW 3.1.2 on a shared memory computerCreating a memory leak with JavaIs this code generating memory leaks or is it clean?Optimization of FFTW to top Matlab FFT2D FFT using 1D FFT with mpiHow to avoid Memory Leak with returning const char *DCT of complex arrays with FFTW in Fortran: How to point to the imaginary part array?FFTW in Fortran result contains only zerosMemory leak by returning allocated strings






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I want to use the modern Fortran interface of FFTW, but in a way that allows simple function calls like ifftshift(fft_c2c(vec)*exp(vec)) et cetera. This is my understanding of how to do this (I also understand that doing a new plan every call is not the most efficient thing). Currently this code is functional (returns correct results); however, there is a memory leak so that repeated calls result in losses. I'm not quite sure where though! I had hoped that the association of the return variable `fft' with the only unfreed memory would result in no leaks but this is evidently not true. What am I missing, and how can I better structure what I want to do with proper modern fortran? Thanks!



function fft_c2c(x) result(fft) 
integer :: N
type(C_PTR) :: plan
complex(C_DOUBLE_COMPLEX), pointer :: fft(:)
complex(C_DOUBLE_COMPLEX), dimension(:), intent(in) :: x

! Use an auxiliary array that is allocated with fftw_alloc_complex
! to ensure memory alignment for performance, see FFTW docs
complex(C_DOUBLE_COMPLEX), pointer :: x_align(:)
type(C_PTR) :: p

N = size(x)
p = fftw_alloc_complex(int(N, C_SIZE_T))
call c_f_pointer(p, fft, [N]);
p = fftw_alloc_complex(int(N, C_SIZE_T))
call c_f_pointer(p, x_align, [N]);

plan = fftw_plan_dft_1d(N, x_align, fft, FFTW_FORWARD, FFTW_MEASURE);
! FFTW overwrites x_align and fft during planning process, so assign
! data here
x_align = x
call fftw_execute_dft(plan, x_align, fft);
call fftw_free(p);
end function fft_c2c









share|improve this question






























    1















    I want to use the modern Fortran interface of FFTW, but in a way that allows simple function calls like ifftshift(fft_c2c(vec)*exp(vec)) et cetera. This is my understanding of how to do this (I also understand that doing a new plan every call is not the most efficient thing). Currently this code is functional (returns correct results); however, there is a memory leak so that repeated calls result in losses. I'm not quite sure where though! I had hoped that the association of the return variable `fft' with the only unfreed memory would result in no leaks but this is evidently not true. What am I missing, and how can I better structure what I want to do with proper modern fortran? Thanks!



    function fft_c2c(x) result(fft) 
    integer :: N
    type(C_PTR) :: plan
    complex(C_DOUBLE_COMPLEX), pointer :: fft(:)
    complex(C_DOUBLE_COMPLEX), dimension(:), intent(in) :: x

    ! Use an auxiliary array that is allocated with fftw_alloc_complex
    ! to ensure memory alignment for performance, see FFTW docs
    complex(C_DOUBLE_COMPLEX), pointer :: x_align(:)
    type(C_PTR) :: p

    N = size(x)
    p = fftw_alloc_complex(int(N, C_SIZE_T))
    call c_f_pointer(p, fft, [N]);
    p = fftw_alloc_complex(int(N, C_SIZE_T))
    call c_f_pointer(p, x_align, [N]);

    plan = fftw_plan_dft_1d(N, x_align, fft, FFTW_FORWARD, FFTW_MEASURE);
    ! FFTW overwrites x_align and fft during planning process, so assign
    ! data here
    x_align = x
    call fftw_execute_dft(plan, x_align, fft);
    call fftw_free(p);
    end function fft_c2c









    share|improve this question


























      1












      1








      1








      I want to use the modern Fortran interface of FFTW, but in a way that allows simple function calls like ifftshift(fft_c2c(vec)*exp(vec)) et cetera. This is my understanding of how to do this (I also understand that doing a new plan every call is not the most efficient thing). Currently this code is functional (returns correct results); however, there is a memory leak so that repeated calls result in losses. I'm not quite sure where though! I had hoped that the association of the return variable `fft' with the only unfreed memory would result in no leaks but this is evidently not true. What am I missing, and how can I better structure what I want to do with proper modern fortran? Thanks!



      function fft_c2c(x) result(fft) 
      integer :: N
      type(C_PTR) :: plan
      complex(C_DOUBLE_COMPLEX), pointer :: fft(:)
      complex(C_DOUBLE_COMPLEX), dimension(:), intent(in) :: x

      ! Use an auxiliary array that is allocated with fftw_alloc_complex
      ! to ensure memory alignment for performance, see FFTW docs
      complex(C_DOUBLE_COMPLEX), pointer :: x_align(:)
      type(C_PTR) :: p

      N = size(x)
      p = fftw_alloc_complex(int(N, C_SIZE_T))
      call c_f_pointer(p, fft, [N]);
      p = fftw_alloc_complex(int(N, C_SIZE_T))
      call c_f_pointer(p, x_align, [N]);

      plan = fftw_plan_dft_1d(N, x_align, fft, FFTW_FORWARD, FFTW_MEASURE);
      ! FFTW overwrites x_align and fft during planning process, so assign
      ! data here
      x_align = x
      call fftw_execute_dft(plan, x_align, fft);
      call fftw_free(p);
      end function fft_c2c









      share|improve this question














      I want to use the modern Fortran interface of FFTW, but in a way that allows simple function calls like ifftshift(fft_c2c(vec)*exp(vec)) et cetera. This is my understanding of how to do this (I also understand that doing a new plan every call is not the most efficient thing). Currently this code is functional (returns correct results); however, there is a memory leak so that repeated calls result in losses. I'm not quite sure where though! I had hoped that the association of the return variable `fft' with the only unfreed memory would result in no leaks but this is evidently not true. What am I missing, and how can I better structure what I want to do with proper modern fortran? Thanks!



      function fft_c2c(x) result(fft) 
      integer :: N
      type(C_PTR) :: plan
      complex(C_DOUBLE_COMPLEX), pointer :: fft(:)
      complex(C_DOUBLE_COMPLEX), dimension(:), intent(in) :: x

      ! Use an auxiliary array that is allocated with fftw_alloc_complex
      ! to ensure memory alignment for performance, see FFTW docs
      complex(C_DOUBLE_COMPLEX), pointer :: x_align(:)
      type(C_PTR) :: p

      N = size(x)
      p = fftw_alloc_complex(int(N, C_SIZE_T))
      call c_f_pointer(p, fft, [N]);
      p = fftw_alloc_complex(int(N, C_SIZE_T))
      call c_f_pointer(p, x_align, [N]);

      plan = fftw_plan_dft_1d(N, x_align, fft, FFTW_FORWARD, FFTW_MEASURE);
      ! FFTW overwrites x_align and fft during planning process, so assign
      ! data here
      x_align = x
      call fftw_execute_dft(plan, x_align, fft);
      call fftw_free(p);
      end function fft_c2c






      memory-leaks fortran return-type fftw






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 6:56









      Thomas AndersonThomas Anderson

      152 bronze badges




      152 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can't do that easily. You are forcing your notin of "modern"="everything is a function" on Fortran, here it does not fit that well (or not at all).



          For the meory leaks the rule is simple - deallocate all the pointers. Using them for the result variable is a guarantee of a memory leak. If you need local allocted aligned memory, you need to locally allocate it, copy the data there, copy the data out and deallocate it.



          Every pointer in Fortran need explicit deallocation, there is no reference counting or garbage collection to deallocate them for you.



          You think about just using the nonaligned memory with the appropriate flags and measure the difference, you seem not to care about the top performance anyway.



          Finally, doingFFTW_MEASURE before every transform is not just "not the most efficient thing", it is an absolute performance disaster. You should, at the very least, use FFTW_ESTIMATE to mitigate it.






          share|improve this answer



























          • Thanks, I'll have to do the copying around (or re-align my code to be slightly uglier and in line with fortran realities about "modernity"). Yes, I'm aware this will be a performance disaster, but I wanted to post a minimal working example showing the memory-leak problem alone.

            – Thomas Anderson
            Mar 27 at 7:18












          • @ThomasAnderson Copying in and out is what I do in my code github.com/LadaF/PoisFFT/tree/master/src

            – Vladimir F
            Mar 27 at 7:25










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55371403%2fusing-aligned-memory-for-fortran-ffts-fftw-without-memory-leaks%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can't do that easily. You are forcing your notin of "modern"="everything is a function" on Fortran, here it does not fit that well (or not at all).



          For the meory leaks the rule is simple - deallocate all the pointers. Using them for the result variable is a guarantee of a memory leak. If you need local allocted aligned memory, you need to locally allocate it, copy the data there, copy the data out and deallocate it.



          Every pointer in Fortran need explicit deallocation, there is no reference counting or garbage collection to deallocate them for you.



          You think about just using the nonaligned memory with the appropriate flags and measure the difference, you seem not to care about the top performance anyway.



          Finally, doingFFTW_MEASURE before every transform is not just "not the most efficient thing", it is an absolute performance disaster. You should, at the very least, use FFTW_ESTIMATE to mitigate it.






          share|improve this answer



























          • Thanks, I'll have to do the copying around (or re-align my code to be slightly uglier and in line with fortran realities about "modernity"). Yes, I'm aware this will be a performance disaster, but I wanted to post a minimal working example showing the memory-leak problem alone.

            – Thomas Anderson
            Mar 27 at 7:18












          • @ThomasAnderson Copying in and out is what I do in my code github.com/LadaF/PoisFFT/tree/master/src

            – Vladimir F
            Mar 27 at 7:25















          1














          You can't do that easily. You are forcing your notin of "modern"="everything is a function" on Fortran, here it does not fit that well (or not at all).



          For the meory leaks the rule is simple - deallocate all the pointers. Using them for the result variable is a guarantee of a memory leak. If you need local allocted aligned memory, you need to locally allocate it, copy the data there, copy the data out and deallocate it.



          Every pointer in Fortran need explicit deallocation, there is no reference counting or garbage collection to deallocate them for you.



          You think about just using the nonaligned memory with the appropriate flags and measure the difference, you seem not to care about the top performance anyway.



          Finally, doingFFTW_MEASURE before every transform is not just "not the most efficient thing", it is an absolute performance disaster. You should, at the very least, use FFTW_ESTIMATE to mitigate it.






          share|improve this answer



























          • Thanks, I'll have to do the copying around (or re-align my code to be slightly uglier and in line with fortran realities about "modernity"). Yes, I'm aware this will be a performance disaster, but I wanted to post a minimal working example showing the memory-leak problem alone.

            – Thomas Anderson
            Mar 27 at 7:18












          • @ThomasAnderson Copying in and out is what I do in my code github.com/LadaF/PoisFFT/tree/master/src

            – Vladimir F
            Mar 27 at 7:25













          1












          1








          1







          You can't do that easily. You are forcing your notin of "modern"="everything is a function" on Fortran, here it does not fit that well (or not at all).



          For the meory leaks the rule is simple - deallocate all the pointers. Using them for the result variable is a guarantee of a memory leak. If you need local allocted aligned memory, you need to locally allocate it, copy the data there, copy the data out and deallocate it.



          Every pointer in Fortran need explicit deallocation, there is no reference counting or garbage collection to deallocate them for you.



          You think about just using the nonaligned memory with the appropriate flags and measure the difference, you seem not to care about the top performance anyway.



          Finally, doingFFTW_MEASURE before every transform is not just "not the most efficient thing", it is an absolute performance disaster. You should, at the very least, use FFTW_ESTIMATE to mitigate it.






          share|improve this answer















          You can't do that easily. You are forcing your notin of "modern"="everything is a function" on Fortran, here it does not fit that well (or not at all).



          For the meory leaks the rule is simple - deallocate all the pointers. Using them for the result variable is a guarantee of a memory leak. If you need local allocted aligned memory, you need to locally allocate it, copy the data there, copy the data out and deallocate it.



          Every pointer in Fortran need explicit deallocation, there is no reference counting or garbage collection to deallocate them for you.



          You think about just using the nonaligned memory with the appropriate flags and measure the difference, you seem not to care about the top performance anyway.



          Finally, doingFFTW_MEASURE before every transform is not just "not the most efficient thing", it is an absolute performance disaster. You should, at the very least, use FFTW_ESTIMATE to mitigate it.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 27 at 7:16

























          answered Mar 27 at 7:10









          Vladimir FVladimir F

          42.5k4 gold badges42 silver badges73 bronze badges




          42.5k4 gold badges42 silver badges73 bronze badges















          • Thanks, I'll have to do the copying around (or re-align my code to be slightly uglier and in line with fortran realities about "modernity"). Yes, I'm aware this will be a performance disaster, but I wanted to post a minimal working example showing the memory-leak problem alone.

            – Thomas Anderson
            Mar 27 at 7:18












          • @ThomasAnderson Copying in and out is what I do in my code github.com/LadaF/PoisFFT/tree/master/src

            – Vladimir F
            Mar 27 at 7:25

















          • Thanks, I'll have to do the copying around (or re-align my code to be slightly uglier and in line with fortran realities about "modernity"). Yes, I'm aware this will be a performance disaster, but I wanted to post a minimal working example showing the memory-leak problem alone.

            – Thomas Anderson
            Mar 27 at 7:18












          • @ThomasAnderson Copying in and out is what I do in my code github.com/LadaF/PoisFFT/tree/master/src

            – Vladimir F
            Mar 27 at 7:25
















          Thanks, I'll have to do the copying around (or re-align my code to be slightly uglier and in line with fortran realities about "modernity"). Yes, I'm aware this will be a performance disaster, but I wanted to post a minimal working example showing the memory-leak problem alone.

          – Thomas Anderson
          Mar 27 at 7:18






          Thanks, I'll have to do the copying around (or re-align my code to be slightly uglier and in line with fortran realities about "modernity"). Yes, I'm aware this will be a performance disaster, but I wanted to post a minimal working example showing the memory-leak problem alone.

          – Thomas Anderson
          Mar 27 at 7:18














          @ThomasAnderson Copying in and out is what I do in my code github.com/LadaF/PoisFFT/tree/master/src

          – Vladimir F
          Mar 27 at 7:25





          @ThomasAnderson Copying in and out is what I do in my code github.com/LadaF/PoisFFT/tree/master/src

          – Vladimir F
          Mar 27 at 7:25








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55371403%2fusing-aligned-memory-for-fortran-ffts-fftw-without-memory-leaks%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해