blas_block4_template.F90 6.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#if 0
!    This file is part of ELPA.
!
!    The ELPA library was originally created by the ELPA consortium,
!    consisting of the following organizations:
!
!    - Max Planck Computing and Data Facility (MPCDF), formerly known as
!      Rechenzentrum Garching der Max-Planck-Gesellschaft (RZG),
!    - Bergische Universität Wuppertal, Lehrstuhl für angewandte
!      Informatik,
!    - Technische Universität München, Lehrstuhl für Informatik mit
!      Schwerpunkt Wissenschaftliches Rechnen ,
!    - Fritz-Haber-Institut, Berlin, Abt. Theorie,
!    - Max-Plack-Institut für Mathematik in den Naturwissenschaften,
!      Leipzig, Abt. Komplexe Strukutren in Biologie und Kognition,
!      and
!    - IBM Deutschland GmbH
!
!
!    More information can be found here:
!    http://elpa.mpcdf.mpg.de/
!
!    ELPA is free software: you can redistribute it and/or modify
!    it under the terms of the version 3 of the license of the
!    GNU Lesser General Public License as published by the Free
!    Software Foundation.
!
!    ELPA is distributed in the hope that it will be useful,
!    but WITHOUT ANY WARRANTY; without even the implied warranty of
!    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
!    GNU Lesser General Public License for more details.
!
!    You should have received a copy of the GNU Lesser General Public License
!    along with ELPA.  If not, see <http://www.gnu.org/licenses/>
!
!    ELPA reflects a substantial effort on the part of the original
!    ELPA consortium, and we ask you to respect the spirit of the
!    license that we chose: i.e., please contribute any changes you
!    may have back to the original ELPA library distribution, and keep
!    any derivatives of ELPA under the same license that we chose for
!    the original distribution, the GNU Lesser General Public License.
!
!
! --------------------------------------------------------------------------------------------------
!
! This file contains the compute intensive kernels for the Householder transformations.
!
! This is the small and simple version (no hand unrolling of loops etc.) but for some
! compilers this performs better than a sophisticated version with transformed and unrolled loops.
!
! It should be compiled with the highest possible optimization level.
!
! Copyright of the original code rests with the authors inside the ELPA
! consortium. The copyright of any additional modifications shall rest
! with their original authors, but shall adhere to the licensing terms
! distributed along with the original code in the file "COPYING".
!
! --------------------------------------------------------------------------------------------------
#endif


#if REALCASE==1
  subroutine quad_hh_trafo_&
  &MATH_DATATYPE&
  &_generic_blas_4hv_&
  &PRECISION&
  & (q, hh, nb, nq, ldq, ldh)

    use precision
    use elpa_abstract_impl
    implicit none

    !class(elpa_abstract_impl_t), intent(inout) :: obj
    integer(kind=ik), intent(in)    :: nb, nq, ldq, ldh

#ifdef USE_ASSUMED_SIZE
    real(kind=C_DATATYPE_KIND), intent(inout) :: q(ldq,*)
    real(kind=C_DATATYPE_KIND), intent(in)    :: hh(ldh,*)
#else
    real(kind=C_DATATYPE_KIND), intent(inout) :: q(1:ldq,1:nb+3)
    real(kind=C_DATATYPE_KIND), intent(in)    :: hh(1:ldh,1:6)
#endif

    real(kind=C_DATATYPE_KIND)                :: h_2_1, h_3_2, h_3_1, h_4_3, h_4_2, h_4_1
    real(kind=C_DATATYPE_KIND)                :: a_1_1(nq), a_2_1(nq), a_3_1(nq), a_4_1(nq)
    real(kind=C_DATATYPE_KIND)                :: h1, h2, h3, h4
    real(kind=C_DATATYPE_KIND)                :: w(nq), z(nq), x(nq), y(nq)
88
    real(kind=C_DATATYPE_KIND)                :: w_orig(nq), z_orig(nq), x_orig(nq), y_orig(nq)
89 90 91

    real(kind=C_DATATYPE_KIND)                :: w_comb(nq, 4)
    real(kind=C_DATATYPE_KIND)                :: h_comb(4)
92
    real(kind=C_DATATYPE_KIND)                :: h_mat(4, nb+3)
93
    real(kind=C_DATATYPE_KIND)                :: h4m(4, 4)
94
    real(kind=C_DATATYPE_KIND)                :: s_mat(4, 4)
95 96 97 98 99 100 101 102

    real(kind=C_DATATYPE_KIND)                :: tau1, tau2, tau3, tau4

    integer(kind=ik)                             :: i


    ! Calculate dot product of the two Householder vectors

103 104 105 106 107 108 109 110 111 112 113 114
   h_mat(:,:) = 0.0

   h_mat(1,4) = -1.0
   h_mat(2,3) = -1.0
   h_mat(3,2) = -1.0
   h_mat(4,1) = -1.0

   h_mat(1,5:nb+3) = -hh(2:nb, 1)
   h_mat(2,4:nb+2) = -hh(2:nb, 2)
   h_mat(3,3:nb+1) = -hh(2:nb, 3)
   h_mat(4,2:nb)   = -hh(2:nb, 4)

115 116 117 118 119
   ! TODO we actually need just the strictly upper triangle of s_mat
   ! TODO take care when changing to BLAS
   ! TODO we do not even need diagonal, which might not be achievable by blas.
   ! TODO lets see how much does it matter
   s_mat = matmul(h_mat, transpose(h_mat))
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

    ! Do the Householder transformations
    a_1_1(1:nq) = q(1:nq,4)
    a_2_1(1:nq) = q(1:nq,3)
    a_3_1(1:nq) = q(1:nq,2)
    a_4_1(1:nq) = q(1:nq,1)

    h_2_1 = hh(2,2)
    h_3_2 = hh(2,3)
    h_3_1 = hh(3,3)
    h_4_3 = hh(2,4)
    h_4_2 = hh(3,4)
    h_4_1 = hh(4,4)

    w(1:nq) = a_3_1(1:nq) * h_4_3 + a_4_1(1:nq)
    w(1:nq) = a_2_1(1:nq) * h_4_2 +     w(1:nq)
    w(1:nq) = a_1_1(1:nq) * h_4_1 +     w(1:nq)

    z(1:nq) = a_2_1(1:nq) * h_3_2 + a_3_1(1:nq)
    z(1:nq) = a_1_1(1:nq) * h_3_1 +     z(1:nq)

    y(1:nq) = a_1_1(1:nq) * h_2_1 + a_2_1(1:nq)

    x(1:nq) = a_1_1(1:nq)

    do i=5,nb
      h1 = hh(i-3,1)
      h2 = hh(i-2,2)
      h3 = hh(i-1,3)
      h4 = hh(i  ,4)

      x(1:nq) = x(1:nq) + q(1:nq,i) * h1
      y(1:nq) = y(1:nq) + q(1:nq,i) * h2
      z(1:nq) = z(1:nq) + q(1:nq,i) * h3
      w(1:nq) = w(1:nq) + q(1:nq,i) * h4
    enddo

    h1 = hh(nb-2,1)
    h2 = hh(nb-1,2)
    h3 = hh(nb  ,3)

    x(1:nq) = x(1:nq) + q(1:nq,nb+1) * h1 
    y(1:nq) = y(1:nq) + q(1:nq,nb+1) * h2
    z(1:nq) = z(1:nq) + q(1:nq,nb+1) * h3

    h1 = hh(nb-1,1)
    h2 = hh(nb  ,2)

    x(1:nq) = x(1:nq) + q(1:nq,nb+2) * h1
    y(1:nq) = y(1:nq) + q(1:nq,nb+2) * h2

    h1 = hh(nb,1)

    x(1:nq) = x(1:nq) + q(1:nq,nb+3) * h1

    ! Rank-1 update
    tau1 = hh(1,1)
    tau2 = hh(1,2)
    tau3 = hh(1,3)
    tau4 = hh(1,4)

181 182 183 184
    x_orig = x
    y_orig = y
    z_orig = z
    w_orig = w
185

186
    h4m = 0.0
187

188 189
    h4m(1,1) = tau1
    x(1:nq) = x(1:nq) * h4m(1,1)
190

191 192 193
    h4m(2,1) = - tau2 * s_mat(1,2)
    h4m(2,2) = tau2
    y(1:nq) = x(1:nq) * h4m(2,1) + y(1:nq) * h4m(2,2)
194

195 196 197 198 199 200 201 202 203 204 205
    h4m(3,1) = - tau3 * s_mat(1,3)
    h4m(3,2) = - tau3 * s_mat(2,3)
    h4m(3,3) = tau3
    z(1:nq) = x(1:nq) * h4m(3,1) + y(1:nq) * h4m(3,2) + z(1:nq) * h4m(3,3)

    h4m(4,1) = - tau4 * s_mat(1,4)
    h4m(4,2) = - tau4 * s_mat(2,4)
    h4m(4,3) = - tau4 * s_mat(3,4)
    h4m(4,4) = tau4

    w(1:nq) = x(1:nq) * h4m(4,1) + y(1:nq) * h4m(4,2) + z(1:nq) * h4m(4,3) + w(1:nq) * h4m(4,4)
206 207 208 209 210 211

    w_comb(:,1) = x
    w_comb(:,2) = y
    w_comb(:,3) = z
    w_comb(:,4) = w

212
   q(1:nq, 1:nb+3) = matmul(w_comb, h_mat) + q(1:nq, 1:nb+3)
213 214 215 216

  end subroutine

#endif