laplace2d.kernels.f90 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ! Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  2. !
  3. ! Redistribution and use in source and binary forms, with or without
  4. ! modification, are permitted provided that the following conditions
  5. ! are met:
  6. ! * Redistributions of source code must retain the above copyright
  7. ! notice, this list of conditions and the following disclaimer.
  8. ! * Redistributions in binary form must reproduce the above copyright
  9. ! notice, this list of conditions and the following disclaimer in the
  10. ! documentation and/or other materials provided with the distribution.
  11. ! * Neither the name of NVIDIA CORPORATION nor the names of its
  12. ! contributors may be used to endorse or promote products derived
  13. ! from this software without specific prior written permission.
  14. !
  15. ! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  16. ! EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. ! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. ! PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. ! CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. ! EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. ! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. ! PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. ! OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. ! (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. ! OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. module laplace2d
  27. public :: initialize
  28. public :: calcNext
  29. public :: swap
  30. public :: dealloc
  31. contains
  32. subroutine initialize(A, Anew, m, n)
  33. integer, parameter :: fp_kind=kind(1.0d0)
  34. real(fp_kind),allocatable,intent(out) :: A(:,:)
  35. real(fp_kind),allocatable,intent(out) :: Anew(:,:)
  36. integer,intent(in) :: m, n
  37. allocate ( A(0:n-1,0:m-1), Anew(0:n-1,0:m-1) )
  38. A = 0.0_fp_kind
  39. Anew = 0.0_fp_kind
  40. A(0,:) = 1.0_fp_kind
  41. Anew(0,:) = 1.0_fp_kind
  42. end subroutine initialize
  43. function calcNext(A, Anew, m, n)
  44. integer, parameter :: fp_kind=kind(1.0d0)
  45. real(fp_kind),intent(inout) :: A(0:n-1,0:m-1)
  46. real(fp_kind),intent(inout) :: Anew(0:n-1,0:m-1)
  47. integer,intent(in) :: m, n
  48. integer :: i, j
  49. real(fp_kind) :: error
  50. error=0.0_fp_kind
  51. !$acc kernels
  52. do j=1,m-2
  53. do i=1,n-2
  54. Anew(i,j) = 0.25_fp_kind * ( A(i+1,j ) + A(i-1,j ) + &
  55. A(i ,j-1) + A(i ,j+1) )
  56. error = max( error, abs(Anew(i,j)-A(i,j)) )
  57. end do
  58. end do
  59. !$acc end kernels
  60. calcNext = error
  61. end function calcNext
  62. subroutine swap(A, Anew, m, n)
  63. integer, parameter :: fp_kind=kind(1.0d0)
  64. real(fp_kind),intent(out) :: A(0:n-1,0:m-1)
  65. real(fp_kind),intent(in) :: Anew(0:n-1,0:m-1)
  66. integer,intent(in) :: m, n
  67. integer :: i, j
  68. !$acc kernels
  69. do j=1,m-2
  70. do i=1,n-2
  71. A(i,j) = Anew(i,j)
  72. end do
  73. end do
  74. !$acc end kernels
  75. end subroutine swap
  76. subroutine dealloc(A, Anew)
  77. integer, parameter :: fp_kind=kind(1.0d0)
  78. real(fp_kind),allocatable,intent(in) :: A
  79. real(fp_kind),allocatable,intent(in) :: Anew
  80. deallocate (A,Anew)
  81. end subroutine
  82. end module laplace2d