elpa2_compute.F90 323 KB
Newer Older
Andreas Marek's avatar
Andreas Marek committed
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
!    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), fomerly 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 Naturwissenschaftrn,
!      Leipzig, Abt. Komplexe Strukutren in Biologie und Kognition,
!      and
!    - IBM Deutschland GmbH
!
!    This particular source code file contains additions, changes and
!    enhancements authored by Intel Corporation which is not part of
!    the ELPA consortium.
!
!    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.
!
!
! ELPA1 -- Faster replacements for ScaLAPACK symmetric eigenvalue routines
!
! 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".



! ELPA2 -- 2-stage solver for ELPA
!
! 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".


#include "config-f90.h"

module ELPA2_compute

! Version 1.1.2, 2011-02-21

69
  use ELPA_utilities
Andreas Marek's avatar
Andreas Marek committed
70
71
72
73
  USE ELPA1_compute
  use elpa1, only : elpa_print_times, time_evp_back, time_evp_fwd, time_evp_solve
  use elpa2_utilities
  use elpa_pdgeqrf
74
  use precision
Andreas Marek's avatar
Andreas Marek committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

  implicit none

  PRIVATE ! By default, all routines contained are private

  public :: bandred_real
  public :: tridiag_band_real
  public :: trans_ev_tridi_to_band_real
  public :: trans_ev_band_to_full_real

  public :: bandred_complex
  public :: tridiag_band_complex
  public :: trans_ev_tridi_to_band_complex
  public :: trans_ev_band_to_full_complex

  public :: band_band_real
  public :: divide_band

93
  integer(kind=ik), public :: which_qr_decomposition = 1     ! defines, which QR-decomposition algorithm will be used
Andreas Marek's avatar
Andreas Marek committed
94
95
96
97
98
99
                                                    ! 0 for unblocked
                                                    ! 1 for blocked (maxrank: nblk)
  include 'mpif.h'

  contains

100
    subroutine bandred_real(na, a, lda, nblk, nbw, matrixCols, numBlocks, mpi_comm_rows, mpi_comm_cols, &
101
                            tmat, wantDebug, useGPU, success, useQR)
Andreas Marek's avatar
Andreas Marek committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

  !-------------------------------------------------------------------------------
  !  bandred_real: Reduces a distributed symmetric matrix to band form
  !
  !  Parameters
  !
  !  na          Order of matrix
  !
  !  a(lda,matrixCols)    Distributed matrix which should be reduced.
  !              Distribution is like in Scalapack.
  !              Opposed to Scalapack, a(:,:) must be set completely (upper and lower half)
  !              a(:,:) is overwritten on exit with the band and the Householder vectors
  !              in the upper half.
  !
  !  lda         Leading dimension of a
  !  matrixCols  local columns of matrix a
  !
  !  nblk        blocksize of cyclic distribution, must be the same in both directions!
  !
  !  nbw         semi bandwith of output matrix
  !
  !  mpi_comm_rows
  !  mpi_comm_cols
  !              MPI-Communicators for rows/columns
  !
  !  tmat(nbw,nbw,numBlocks)    where numBlocks = (na-1)/nbw + 1
  !              Factors for the Householder vectors (returned), needed for back transformation
  !
  !-------------------------------------------------------------------------------

132
133
      use cuda_functions
      use iso_c_binding
Andreas Marek's avatar
Andreas Marek committed
134
135

#ifdef HAVE_DETAILED_TIMINGS
136
      use timings
Andreas Marek's avatar
Andreas Marek committed
137
#endif
138
139
140
#ifdef WITH_OPENMP
      use omp_lib
#endif
Andreas Marek's avatar
Andreas Marek committed
141
      use precision
142
      implicit none
Andreas Marek's avatar
Andreas Marek committed
143

Andreas Marek's avatar
Andreas Marek committed
144
145
      integer(kind=ik)           :: na, lda, nblk, nbw, matrixCols, numBlocks, mpi_comm_rows, mpi_comm_cols
      real(kind=rk)              :: a(lda,matrixCols), tmat(nbw,nbw,numBlocks)
146
147
      ! was
      ! real a(lda,*), tmat(nbw,nbw,*)
148
149
      real(kind=rk)              :: eps
      logical, intent(in)        :: useGPU
Andreas Marek's avatar
Andreas Marek committed
150
151
152
153
154
      integer(kind=ik)           :: my_prow, my_pcol, np_rows, np_cols, mpierr
      integer(kind=ik)           :: l_cols, l_rows
      integer(kind=ik)           :: i, j, lcs, lce, lrs, lre, lc, lr, cur_pcol, n_cols, nrow
      integer(kind=ik)           :: istep, ncol, lch, lcx, nlc, mynlc
      integer(kind=ik)           :: tile_size, l_rows_tile, l_cols_tile
155

Andreas Marek's avatar
Andreas Marek committed
156
      real(kind=rk)              :: vnorm2, xf, aux1(nbw), aux2(nbw), vrl, tau, vav(nbw,nbw)
157

158
159
160
      real(kind=rk), allocatable :: tmpCUDA(:),  vmrCUDA(:),  umcCUDA(:)
      real(kind=rk), allocatable :: tmpCPU(:,:), vmrCPU(:,:), umcCPU(:,:)
      real(kind=rk), allocatable :: vr(:)
161
      ! needed for blocked QR decomposition
Andreas Marek's avatar
Andreas Marek committed
162
163
164
      integer(kind=ik)           :: PQRPARAM(11), work_size
      real(kind=rk)              :: dwork_size(1)
      real(kind=rk), allocatable :: work_blocked(:), tauvector(:), blockheuristic(:)
Andreas Marek's avatar
Andreas Marek committed
165

166
167
168
169
170
171
172
      integer(kind=C_intptr_T)   :: a_dev, vmr_dev, umc_dev, tmat_dev, vav_dev
      integer(kind=ik), external :: numroc
      integer(kind=ik)           :: ierr
      integer(kind=ik)           :: cur_l_rows, cur_l_cols, vmr_size, umc_size
      integer(kind=c_size_t)     :: lc_start, lc_end
      integer(kind=ik)           :: lr_end
      integer(kind=ik)           :: na_rows, na_cols
Andreas Marek's avatar
Andreas Marek committed
173

Andreas Marek's avatar
Andreas Marek committed
174
175
      logical, intent(in)        :: wantDebug
      logical, intent(out)       :: success
176
177
178
      logical                    :: successCUDA
      integer(kind=ik)           :: istat
      character(200)             :: errorMessage
Andreas Marek's avatar
Andreas Marek committed
179

Andreas Marek's avatar
Andreas Marek committed
180
      logical, intent(in)        :: useQR
Andreas Marek's avatar
Andreas Marek committed
181

Andreas Marek's avatar
Andreas Marek committed
182
      integer(kind=ik)           :: mystart, myend, m_way, n_way, work_per_thread, m_id, n_id, n_threads, ii, pp, transformChunkSize
Andreas Marek's avatar
Andreas Marek committed
183
184

#ifdef HAVE_DETAILED_TIMINGS
185
      call timer%start("bandred_real")
Andreas Marek's avatar
Andreas Marek committed
186
#endif
187
188
189
190
191
      call mpi_comm_rank(mpi_comm_rows,my_prow,mpierr)
      call mpi_comm_size(mpi_comm_rows,np_rows,mpierr)
      call mpi_comm_rank(mpi_comm_cols,my_pcol,mpierr)
      call mpi_comm_size(mpi_comm_cols,np_cols,mpierr)
      success = .true.
Andreas Marek's avatar
Andreas Marek committed
192
193


194
195
196
197
198
199
200
201
202
203
204
      ! Semibandwith nbw must be a multiple of blocksize nblk
      if (mod(nbw,nblk)/=0) then
        if (my_prow==0 .and. my_pcol==0) then
          if (wantDebug) then
            write(error_unit,*) 'ELPA2_bandred_real: ERROR: nbw=',nbw,', nblk=',nblk
            write(error_unit,*) 'ELPA2_bandred_real: ELPA2 works only for nbw==n*nblk'
          endif
          success = .false.
          return
        endif
      endif
Andreas Marek's avatar
Andreas Marek committed
205

206
207
208
209
      if (useGPU) then
        na_rows = numroc(na, nblk, my_prow, 0, np_rows)
        na_cols = numroc(na, nblk, my_pcol, 0, np_cols)
      endif
Andreas Marek's avatar
Andreas Marek committed
210

211
      ! Matrix is split into tiles; work is done only for tiles on the diagonal or above
Andreas Marek's avatar
Andreas Marek committed
212

213
214
      tile_size = nblk*least_common_multiple(np_rows,np_cols) ! minimum global tile size
      tile_size = ((128*max(np_rows,np_cols)-1)/tile_size+1)*tile_size ! make local tiles at least 128 wide
Andreas Marek's avatar
Andreas Marek committed
215

216
217
      l_rows_tile = tile_size/np_rows ! local rows of a tile
      l_cols_tile = tile_size/np_cols ! local cols of a tile
Andreas Marek's avatar
Andreas Marek committed
218

219
      if (useQR) then
Andreas Marek's avatar
Andreas Marek committed
220

221
222
223
224
        if (useGPU) then
          print *,"qr decomposition at the moment not supported with GPU"
          stop
        endif
Andreas Marek's avatar
Andreas Marek committed
225

226
227
        if (which_qr_decomposition == 1) then
          call qr_pqrparam_init(pqrparam,    nblk,'M',0,   nblk,'M',0,   nblk,'M',1,'s')
228
229
230
231
232
          allocate(tauvector(na), stat=istat, errmsg=errorMessage)
          if (istat .ne. 0) then
            print *,"bandred_real: error when allocating tauvector "//errorMessage
            stop
          endif
Andreas Marek's avatar
Andreas Marek committed
233

234
235
236
237
238
          allocate(blockheuristic(nblk), stat=istat, errmsg=errorMessage)
          if (istat .ne. 0) then
            print *,"bandred_real: error when allocating blockheuristic "//errorMessage
            stop
          endif
Andreas Marek's avatar
Andreas Marek committed
239

240
          l_rows = local_index(na, my_prow, np_rows, nblk, -1)
241
242
243
244
245
          allocate(vmrCPU(max(l_rows,1),na), stat=istat, errmsg=errorMessage)
          if (istat .ne. 0) then
            print *,"bandred_real: error when allocating vmrCPU "//errorMessage
            stop
          endif
Andreas Marek's avatar
Andreas Marek committed
246

247
          call qr_pdgeqrf_2dcomm(a, lda, vmrCPU, max(l_rows,1), tauvector, tmat(1,1,1), nbw, dwork_size(1), -1, na, &
248
249
                                nbw, nblk, nblk, na, na, 1, 0, PQRPARAM, mpi_comm_rows, mpi_comm_cols, blockheuristic)
          work_size = dwork_size(1)
250
251
252
253
254
          allocate(work_blocked(work_size), stat=istat, errmsg=errorMessage)
          if (istat .ne. 0) then
            print *,"bandred_real: error when allocating work_blocked "//errorMessage
            stop
          endif
Andreas Marek's avatar
Andreas Marek committed
255

256
          work_blocked = 0.0d0
257
258
259
260
261
          deallocate(vmrCPU, stat=istat, errmsg=errorMessage)
          if (istat .ne. 0) then
            print *,"bandred_real: error when deallocating vmrCPU "//errorMessage
            stop
          endif
Andreas Marek's avatar
Andreas Marek committed
262

263
        endif ! which_qr_decomposition
Andreas Marek's avatar
Andreas Marek committed
264

265
      endif ! useQr
Andreas Marek's avatar
Andreas Marek committed
266

267
268
269
270
271
272
      if (useGPU) then
        ! Here we convert the regular host array into a pinned host array
        successCUDA = cuda_malloc(a_dev, lda*na_cols*size_of_real_datatype)
        if (.not.(successCUDA)) then
          print *,"bandred_real: error in cudaMalloc"
          stop
273
        endif
Andreas Marek's avatar
Andreas Marek committed
274

275
276
277
278
279
        successCUDA = cuda_malloc(tmat_dev, nbw*nbw*size_of_real_datatype)
        if (.not.(successCUDA)) then
          print *,"bandred_real: error in cudaMalloc"
          stop
        endif
Andreas Marek's avatar
Andreas Marek committed
280

281
282
283
284
285
        successCUDA = cuda_malloc(vav_dev, nbw*nbw*size_of_real_datatype)
        if (.not.(successCUDA)) then
          print *,"bandred_real: error in cudaMalloc"
          stop
        endif
Andreas Marek's avatar
Andreas Marek committed
286

287
288
        cur_l_rows = 0
        cur_l_cols = 0
Andreas Marek's avatar
Andreas Marek committed
289

290
291
292
293
294
295
        successCUDA = cuda_memcpy(a_dev, loc(a(1,1)), (lda)*(na_cols)*size_of_real_datatype,cudaMemcpyHostToDevice)
        if (.not.(successCUDA)) then
          print *,"bandred_real: error in cudaMemcpy"
          stop
        endif
      endif ! useGPU
Andreas Marek's avatar
Andreas Marek committed
296
297


298
      do istep = (na-1)/nbw, 1, -1
Andreas Marek's avatar
Andreas Marek committed
299

300
        n_cols = MIN(na,(istep+1)*nbw) - istep*nbw ! Number of columns in current step
Andreas Marek's avatar
Andreas Marek committed
301

302
303
304
        ! Number of local columns/rows of remaining matrix
        l_cols = local_index(istep*nbw, my_pcol, np_cols, nblk, -1)
        l_rows = local_index(istep*nbw, my_prow, np_rows, nblk, -1)
Andreas Marek's avatar
Andreas Marek committed
305

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
        if (useGPU) then
          cur_l_rows = max(l_rows, 1)
          cur_l_cols = max(l_cols, 1)

          vmr_size = cur_l_rows * 2 * n_cols
          umc_size = cur_l_cols * 2 * n_cols

          ! Allocate vmr and umc only if the inew size exceeds their current capacity
          ! Added for FORTRAN CALLS
          if ((.not. allocated(vr)) .or. (l_rows + 1 .gt. ubound(vr, dim=1))) then
            if (allocated(vr)) then
              deallocate(vr, stat=istat, errmsg=errorMessage)
              if (istat .ne. 0) then
                print *,"bandred_real: error when deallocating vr "//errorMessage
                stop
              endif
            endif
            allocate(vr(l_rows + 1), stat=istat, errmsg=errorMessage)
            if (istat .ne. 0) then
              print *,"bandred_real: error when allocating vr "//errorMessage
              stop
            endif
Andreas Marek's avatar
Andreas Marek committed
328

329
          endif
Andreas Marek's avatar
Andreas Marek committed
330

331
332
333
334
335
336
337
          if ((.not. allocated(vmrCUDA)) .or. (vmr_size .gt. ubound(vmrCUDA, dim=1))) then
            if (allocated(vmrCUDA)) then
              deallocate(vmrCUDA, stat=istat, errmsg=errorMessage)
              if (istat .ne. 0) then
                print *,"bandred_real: error when allocating vmrCUDA "//errorMessage
                stop
              endif
Andreas Marek's avatar
Andreas Marek committed
338

339
340
341
342
343
344
              successCUDA = cuda_free(vmr_dev)
              if (.not.(successCUDA)) then
                print *,"bandred_real: error in cuda_free"
                stop
              endif
            endif
Andreas Marek's avatar
Andreas Marek committed
345

346
347
348
349
350
            allocate(vmrCUDA(vmr_size), stat=istat, errmsg=errorMessage)
            if (istat .ne. 0) then
              print *,"bandred_real: error when allocating vmrCUDA "//errorMessage
              stop
            endif
Andreas Marek's avatar
Andreas Marek committed
351

352
353
354
355
356
            successCUDA = cuda_malloc(vmr_dev, vmr_size*size_of_real_datatype)
            if (.not.(successCUDA)) then
              print *,"bandred_real: error in cudaMalloc"
              stop
            endif
Andreas Marek's avatar
Andreas Marek committed
357

358
          endif
Andreas Marek's avatar
Andreas Marek committed
359

360
361
362
363
364
365
366
          if ((.not. allocated(umcCUDA)) .or. (umc_size .gt. ubound(umcCUDA, dim=1))) then
            if (allocated(umcCUDA)) then
              deallocate(umcCUDA, stat=istat, errmsg=errorMessage)
              if (istat .ne. 0) then
                print *,"bandred_real: error when deallocating umcCUDA "//errorMessage
                stop
              endif
Andreas Marek's avatar
Andreas Marek committed
367

368
369
370
371
372
              successCUDA = cuda_free(umc_dev)
              if (.not.(successCUDA)) then
                 print *,"bandred_real: error in cudaFree"
                 stop
              endif
Andreas Marek's avatar
Andreas Marek committed
373

374
            endif
Andreas Marek's avatar
Andreas Marek committed
375

376
377
378
379
380
            allocate(umcCUDA(umc_size), stat=istat, errmsg=errorMessage)
            if (istat .ne. 0) then
              print *,"bandred_real: error when deallocating umcCUDA "//errorMessage
              stop
            endif
Andreas Marek's avatar
Andreas Marek committed
381

382
383
384
385
386
            successCUDA = cuda_malloc(umc_dev, umc_size*size_of_real_datatype)
            if (.not.(successCUDA)) then
              print *,"bandred_real: error in cudaMalloc"
              stop
            endif
Andreas Marek's avatar
Andreas Marek committed
387

388
389
390
          endif
        else ! GPU not used
          ! Allocate vmr and umc to their exact sizes so that they can be used in bcasts and reduces
Andreas Marek's avatar
Andreas Marek committed
391

392
393
394
395
396
          allocate(vmrCPU(max(l_rows,1),2*n_cols), stat=istat, errmsg=errorMessage)
          if (istat .ne. 0) then
            print *,"bandred_real: error when allocating vmrCPU "//errorMessage
            stop
          endif
Andreas Marek's avatar
Andreas Marek committed
397

398
399
400
401
402
          allocate(umcCPU(max(l_cols,1),2*n_cols), stat=istat, errmsg=errorMessage)
          if (istat .ne. 0) then
            print *,"bandred_real: error when allocating umcCPU "//errorMessage
            stop
          endif
Andreas Marek's avatar
Andreas Marek committed
403

404
405
406
407
408
409
          allocate(vr(l_rows+1), stat=istat, errmsg=errorMessage)
          if (istat .ne. 0) then
            print *,"bandred_real: error when allocating vr "//errorMessage
            stop
          endif
        endif ! use GPU
Andreas Marek's avatar
Andreas Marek committed
410

411
412
413
414
415
        if (useGPU) then
          vmrCUDA(1 : cur_l_rows * n_cols) = 0.
        else
          vmrCPU(1:l_rows,1:n_cols) = 0.
        endif
Andreas Marek's avatar
Andreas Marek committed
416

417
418
        vr(:) = 0
        tmat(:,:,istep) = 0
Andreas Marek's avatar
Andreas Marek committed
419

420
421
        if (useGPU) then
          umcCUDA(1 : umc_size) = 0.
Andreas Marek's avatar
Andreas Marek committed
422

423
424
425
          lc_start = local_index(istep*nbw+1, my_pcol, np_cols, nblk, -1)
          lc_end   = local_index(istep*nbw+n_cols, my_pcol, np_cols, nblk, -1)
          lr_end   = local_index((istep-1)*nbw + n_cols, my_prow, np_rows, nblk, -1)
Andreas Marek's avatar
Andreas Marek committed
426

427
          if(lc_start .le. 0) lc_start = 1
Andreas Marek's avatar
Andreas Marek committed
428

429
430
          ! Here we assume that the processor grid and the block grid are aligned
          cur_pcol = pcol(istep*nbw+1, nblk, np_cols)
Andreas Marek's avatar
Andreas Marek committed
431

432
          if(my_pcol == cur_pcol) then
Andreas Marek's avatar
Andreas Marek committed
433

434
435
436
437
438
439
440
441
            successCUDA = cuda_memcpy2d(loc(a(1, lc_start)), lda*size_of_real_datatype,         &
                                       (a_dev + ((lc_start-1) * lda*size_of_real_datatype)),    &
                                       lda*size_of_real_datatype, lr_end*size_of_real_datatype, &
                                       (lc_end - lc_start+1), cudaMemcpyDeviceToHost)
            if (.not.(successCUDA)) then
              print *,"bandred_real: error in cudaMemcpy2d"
              stop
            endif
Andreas Marek's avatar
Andreas Marek committed
442

443
444
          endif
        endif ! useGPU
Andreas Marek's avatar
Andreas Marek committed
445

446
        ! Reduce current block to lower triangular form
Andreas Marek's avatar
Andreas Marek committed
447

448
449
        if (useQR) then
          if (which_qr_decomposition == 1) then
450
            call qr_pdgeqrf_2dcomm(a, lda, vmrCPU, max(l_rows,1), tauvector(1), &
Andreas Marek's avatar
Andreas Marek committed
451
452
453
454
455
                                  tmat(1,1,istep), nbw, work_blocked,       &
                                  work_size, na, n_cols, nblk, nblk,        &
                                  istep*nbw+n_cols-nbw, istep*nbw+n_cols, 1,&
                                  0, PQRPARAM, mpi_comm_rows, mpi_comm_cols,&
                                  blockheuristic)
456
          endif
457
       else !useQR
Andreas Marek's avatar
Andreas Marek committed
458

459
         do lc = n_cols, 1, -1
Andreas Marek's avatar
Andreas Marek committed
460

461
462
           ncol = istep*nbw + lc ! absolute column number of householder vector
           nrow = ncol - nbw ! Absolute number of pivot row
Andreas Marek's avatar
Andreas Marek committed
463

464
465
           lr  = local_index(nrow, my_prow, np_rows, nblk, -1) ! current row length
           lch = local_index(ncol, my_pcol, np_cols, nblk, -1) ! HV local column number
Andreas Marek's avatar
Andreas Marek committed
466

467
           tau = 0
Andreas Marek's avatar
Andreas Marek committed
468

469
           if (nrow == 1) exit ! Nothing to do
Andreas Marek's avatar
Andreas Marek committed
470

471
           cur_pcol = pcol(ncol, nblk, np_cols) ! Processor column owning current block
Andreas Marek's avatar
Andreas Marek committed
472

473
           if (my_pcol==cur_pcol) then
Andreas Marek's avatar
Andreas Marek committed
474

475
476
             ! Get vector to be transformed; distribute last element and norm of
             ! remaining elements to all procs in current column
Andreas Marek's avatar
Andreas Marek committed
477

478
             vr(1:lr) = a(1:lr,lch) ! vector to be transformed
Andreas Marek's avatar
Andreas Marek committed
479

480
481
482
483
484
485
486
             if (my_prow==prow(nrow, nblk, np_rows)) then
               aux1(1) = dot_product(vr(1:lr-1),vr(1:lr-1))
               aux1(2) = vr(lr)
             else
               aux1(1) = dot_product(vr(1:lr),vr(1:lr))
               aux1(2) = 0.
             endif
Andreas Marek's avatar
Andreas Marek committed
487

488
             call mpi_allreduce(aux1,aux2,2,MPI_REAL8,MPI_SUM,mpi_comm_rows,mpierr)
Andreas Marek's avatar
Andreas Marek committed
489

490
491
             vnorm2 = aux2(1)
             vrl    = aux2(2)
Andreas Marek's avatar
Andreas Marek committed
492

493
             ! Householder transformation
Andreas Marek's avatar
Andreas Marek committed
494

495
             call hh_transform_real(vrl, vnorm2, xf, tau)
Andreas Marek's avatar
Andreas Marek committed
496

497
             ! Scale vr and store Householder vector for back transformation
Andreas Marek's avatar
Andreas Marek committed
498

499
500
501
502
503
504
505
506
             vr(1:lr) = vr(1:lr) * xf
             if (my_prow==prow(nrow, nblk, np_rows)) then
               a(1:lr-1,lch) = vr(1:lr-1)
               a(lr,lch) = vrl
               vr(lr) = 1.
             else
               a(1:lr,lch) = vr(1:lr)
             endif
507

508
           endif
509

510
           ! Broadcast Householder vector and tau along columns
511

512
513
           vr(lr+1) = tau
           call MPI_Bcast(vr,lr+1,MPI_REAL8,cur_pcol,mpi_comm_cols,mpierr)
514

515
516
           if (useGPU) then
             vmrCUDA(cur_l_rows * (lc - 1) + 1 : cur_l_rows * (lc - 1) + lr) = vr(1:lr)
Andreas Marek's avatar
Andreas Marek committed
517
           else
518
             vmrCPU(1:lr,lc) = vr(1:lr)
Andreas Marek's avatar
Andreas Marek committed
519
520
           endif

521
522
           tau = vr(lr+1)
           tmat(lc,lc,istep) = tau ! Store tau in diagonal of tmat
523

524
525
           ! Transform remaining columns in current block with Householder vector
           ! Local dot product
526

527
           aux1 = 0
528

529
530
531
532
#ifdef WITH_OPENMP
           !Open up one omp region to avoid paying openmp overhead.
           !This does not help performance due to the addition of two openmp barriers around the MPI call,
           !But in the future this may be beneficial if these barriers are replaced with a faster implementation
533

534
535
           !$omp parallel private(mynlc, j, lcx, ii, pp ) shared(aux1)
           mynlc = 0 ! number of local columns
536

537
538
539
540
541
542
543
544
545
546
547
548
549
550
           !This loop does not have independent iterations,
           !'mynlc' is incremented each iteration, and it is difficult to remove this dependency
           !Thus each thread executes every iteration of the loop, except it only does the work if it 'owns' that iteration
           !That is, a thread only executes the work associated with an iteration if its thread id is congruent to
           !the iteration number modulo the number of threads
           do j=1,lc-1
             lcx = local_index(istep*nbw+j, my_pcol, np_cols, nblk, 0)
             if (lcx>0 ) then
               mynlc = mynlc+1
               if ( mod((j-1), omp_get_num_threads()) .eq. omp_get_thread_num() ) then
                   if (lr>0) aux1(mynlc) = dot_product(vr(1:lr),a(1:lr,lcx))
               endif
             endif
           enddo
551

552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
           ! Get global dot products
           !$omp barrier
           !$omp single
           if (mynlc>0) call mpi_allreduce(aux1,aux2,mynlc,MPI_REAL8,MPI_SUM,mpi_comm_rows,mpierr)
           !$omp end single
           !$omp barrier

           ! Transform
           transformChunkSize=32
           mynlc = 0
           do j=1,lc-1
             lcx = local_index(istep*nbw+j, my_pcol, np_cols, nblk, 0)
             if (lcx>0) then
               mynlc = mynlc+1
               !This loop could be parallelized with an openmp pragma with static scheduling and chunk size 32
               !However, for some reason this is slower than doing it manually, so it is parallelized as below.
               do ii=omp_get_thread_num()*transformChunkSize,lr,omp_get_num_threads()*transformChunkSize
                  do pp = 1,transformChunkSize
                      if (pp + ii > lr) exit
                          a(ii+pp,lcx) = a(ii+pp,lcx) - tau*aux2(mynlc)*vr(ii+pp)
                  enddo
               enddo
             endif
           enddo
           !$omp end parallel
#else /* WITH_OPENMP */
           nlc = 0 ! number of local columns
           do j=1,lc-1
             lcx = local_index(istep*nbw+j, my_pcol, np_cols, nblk, 0)
             if (lcx>0) then
               nlc = nlc+1
               if (lr>0) aux1(nlc) = dot_product(vr(1:lr),a(1:lr,lcx))
             endif
           enddo
586

587
588
           ! Get global dot products
           if (nlc>0) call mpi_allreduce(aux1,aux2,nlc,MPI_REAL8,MPI_SUM,mpi_comm_rows,mpierr)
Andreas Marek's avatar
Andreas Marek committed
589

590
           ! Transform
Andreas Marek's avatar
Andreas Marek committed
591

592
593
594
595
596
597
598
599
600
601
           nlc = 0
           do j=1,lc-1
             lcx = local_index(istep*nbw+j, my_pcol, np_cols, nblk, 0)
             if (lcx>0) then
               nlc = nlc+1
               a(1:lr,lcx) = a(1:lr,lcx) - tau*aux2(nlc)*vr(1:lr)
             endif
           enddo
#endif /* WITH_OPENMP */
         enddo ! lc
Andreas Marek's avatar
Andreas Marek committed
602
603

         if (useGPU) then
604
605
606
607
608
609
610
611
612
613
614
           ! store column tiles back to GPU
           cur_pcol = pcol(istep*nbw+1, nblk, np_cols)
           if (my_pcol == cur_pcol) then
             successCUDA = cuda_memcpy2d((a_dev+((lc_start-1)*lda*size_of_real_datatype)),          &
                                          lda*size_of_real_datatype, loc(a(1, lc_start)),           &
                                          lda*size_of_real_datatype,  lr_end*size_of_real_datatype, &
                                          (lc_end - lc_start+1),cudaMemcpyHostToDevice)
             if (.not.(successCUDA)) then
               print *,"bandred_real: error in cudaMemcpy2d"
               stop
             endif
615

616
           endif
Andreas Marek's avatar
Andreas Marek committed
617
618
         endif

619
620
         ! Calculate scalar products of stored Householder vectors.
         ! This can be done in different ways, we use dsyrk
Andreas Marek's avatar
Andreas Marek committed
621

622
         vav = 0
Andreas Marek's avatar
Andreas Marek committed
623

624
625
626
627
628
629
630
631
         if (useGPU) then
           if (l_rows>0) &
             call dsyrk('U','T',n_cols,l_rows,1.d0,vmrCUDA,cur_l_rows,0.d0,vav,ubound(vav,dim=1))
         else
           if (l_rows>0) &
             call dsyrk('U','T',n_cols,l_rows,1.d0,vmrCPU,ubound(vmrCPU,dim=1),0.d0,vav,ubound(vav,dim=1))
         endif
         call symm_matrix_allreduce(n_cols,vav, nbw, nbw,mpi_comm_rows)
Andreas Marek's avatar
Andreas Marek committed
632

633
         ! Calculate triangular matrix T for block Householder Transformation
Andreas Marek's avatar
Andreas Marek committed
634

635
636
637
638
639
         do lc=n_cols,1,-1
           tau = tmat(lc,lc,istep)
           if (lc<n_cols) then
             call dtrmv('U','T','N',n_cols-lc,tmat(lc+1,lc+1,istep),ubound(tmat,dim=1),vav(lc+1,lc),1)
             tmat(lc,lc+1:n_cols,istep) = -tau * vav(lc+1:n_cols,lc)
Andreas Marek's avatar
Andreas Marek committed
640
641
           endif
         enddo
642
       endif
Andreas Marek's avatar
Andreas Marek committed
643

644
       ! Transpose vmr -> vmc (stored in umc, second half)
Andreas Marek's avatar
Andreas Marek committed
645
646

       if (useGPU) then
647
648
649
650
651
652
653
         call elpa_transpose_vectors_real  (vmrCUDA, cur_l_rows, mpi_comm_rows, &
                                            umcCUDA(cur_l_cols * n_cols + 1), cur_l_cols, mpi_comm_cols, &
                                            1, istep*nbw, n_cols, nblk)
       else
         call elpa_transpose_vectors_real  (vmrCPU, ubound(vmrCPU,dim=1), mpi_comm_rows, &
                                            umcCPU(1,n_cols+1), ubound(umcCPU,dim=1), mpi_comm_cols, &
                                            1, istep*nbw, n_cols, nblk)
Andreas Marek's avatar
Andreas Marek committed
654
655
       endif

656
657
658
659
660
661
662
       ! Calculate umc = A**T * vmr
       ! Note that the distributed A has to be transposed
       ! Opposed to direct tridiagonalization there is no need to use the cache locality
       ! of the tiles, so we can use strips of the matrix

       ! here the GPU version and CPU version diverged substantially, due to the newest
       ! optimizations due to Intel. The GPU version has to be re-written
Andreas Marek's avatar
Andreas Marek committed
663
       if (useGPU) then
664
665
         umcCUDA(1 : l_cols * n_cols) = 0.d0
         vmrCUDA(cur_l_rows * n_cols + 1 : cur_l_rows * n_cols * 2) = 0
Andreas Marek's avatar
Andreas Marek committed
666

667
668
669
670
671
672
         if (l_cols>0 .and. l_rows>0) then
           successCUDA = cuda_memcpy(vmr_dev, loc(vmrCUDA(1)), vmr_size*size_of_real_datatype,cudaMemcpyHostToDevice)
           if (.not.(successCUDA)) then
             print *,"bandred_real: error in cudaMemcpy"
             stop
           endif
Andreas Marek's avatar
Andreas Marek committed
673

674
675
676
677
678
           successCUDA = cuda_memcpy(umc_dev, loc(umcCUDA(1)), umc_size*size_of_real_datatype,cudaMemcpyHostToDevice)
           if (.not.(successCUDA)) then
             print *,"bandred_real: error in cudaMemcpy"
             stop
           endif
Andreas Marek's avatar
Andreas Marek committed
679

680
           do i=0,(istep*nbw-1)/tile_size
Andreas Marek's avatar
Andreas Marek committed
681

682
683
684
             lcs = i*l_cols_tile+1
             lce = min(l_cols,(i+1)*l_cols_tile)
             if (lce<lcs) cycle
Andreas Marek's avatar
Andreas Marek committed
685

686
             lre = min(l_rows,(i+1)*l_rows_tile)
Andreas Marek's avatar
Andreas Marek committed
687

688
689
690
             call cublas_dgemm('T','N',lce-lcs+1,n_cols,lre, &
                               1.d0, (a_dev + ((lcs-1)*lda*size_of_real_datatype)), lda, vmr_dev,cur_l_rows, &
                               1.d0, (umc_dev+ (lcs-1)*size_of_real_datatype), cur_l_cols)
Andreas Marek's avatar
Andreas Marek committed
691

692
693
             if(i==0) cycle
             lre = min(l_rows,i*l_rows_tile)
Andreas Marek's avatar
Andreas Marek committed
694

695
696
697
698
699
             call cublas_dgemm('N','N',lre,n_cols,lce-lcs+1,&
                               1.d0, (a_dev+ ((lcs-1)*lda*size_of_real_datatype)),lda,                  &
                               (umc_dev+(cur_l_cols * n_cols+lcs-1)*size_of_real_datatype), cur_l_cols, &
                               1.d0, (vmr_dev+(cur_l_rows * n_cols)*size_of_real_datatype), cur_l_rows)
           enddo
Andreas Marek's avatar
Andreas Marek committed
700

701
702
703
704
705
           successCUDA = cuda_memcpy(loc(vmrCUDA(1)), vmr_dev,vmr_size*size_of_real_datatype,cudaMemcpyDeviceToHost)
           if (.not.(successCUDA)) then
             print *,"bandred_real: error in cudaMemcpy"
             stop
           endif
Andreas Marek's avatar
Andreas Marek committed
706

707
708
709
710
711
           successCUDA = cuda_memcpy(loc(umcCUDA(1)), umc_dev, umc_size*size_of_real_datatype,cudaMemcpyDeviceToHost)
           if (.not.(successCUDA)) then
             print *,"bandred_real: error in cudaMemcpy"
             stop
           endif
Andreas Marek's avatar
Andreas Marek committed
712

713
         endif ! l_cols>0 .and. l_rows>0
Andreas Marek's avatar
Andreas Marek committed
714

715
716
       else ! do not useGPU version
         !Code for Algorithm 4
Andreas Marek's avatar
Andreas Marek committed
717

718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
         n_way = 1
#ifdef WITH_OPENMP
         n_way = omp_get_max_threads()
#endif
         !umc(1:l_cols,1:n_cols) = 0.d0
         !vmr(1:l_rows,n_cols+1:2*n_cols) = 0
#ifdef WITH_OPENMP
         !$omp parallel private( i,lcs,lce,lrs,lre)
#endif
         if (n_way > 1) then
           !$omp do
           do i=1,min(l_cols_tile, l_cols)
             umcCPU(i,1:n_cols) = 0.d0
           enddo
           !$omp do
           do i=1,l_rows
             vmrCPU(i,n_cols+1:2*n_cols) = 0.d0
           enddo
           if (l_cols>0 .and. l_rows>0) then

             !SYMM variant 4
             !Partitioned Matrix Expression:
             ! Ct = Atl Bt + Atr Bb
             ! Cb = Atr' Bt + Abl Bb
             !
             !Loop invariant:
             ! Ct = Atl Bt + Atr Bb
             !
             !Update:
             ! C1 = A10'B0 + A11B1 + A21 B2
             !
             !This algorithm chosen because in this algoirhtm, the loop around the dgemm calls
             !is easily parallelized, and regardless of choise of algorithm,
             !the startup cost for parallelizing the dgemms inside the loop is too great

             !$omp do schedule(static,1)
             do i=0,(istep*nbw-1)/tile_size
               lcs = i*l_cols_tile+1                   ! local column start
               lce = min(l_cols, (i+1)*l_cols_tile)    ! local column end

               lrs = i*l_rows_tile+1                   ! local row start
               lre = min(l_rows, (i+1)*l_rows_tile)    ! local row end

               !C1 += [A11 A12] [B1
               !                 B2]
               if ( lre > lrs .and. l_cols > lcs ) then
                 call DGEMM('N','N', lre-lrs+1, n_cols, l_cols-lcs+1,          &
                            1.d0, a(lrs,lcs), ubound(a,dim=1),                 &
                                  umcCPU(lcs,n_cols+1), ubound(umcCPU,dim=1),  &
                            0.d0, vmrCPU(lrs,n_cols+1), ubound(vmrCPU,dim=1))
               endif
Andreas Marek's avatar
Andreas Marek committed
769

770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
               ! C1 += A10' B0
               if ( lce > lcs .and. i > 0 ) then
                 call DGEMM('T','N', lce-lcs+1, n_cols, lrs-1,           &
                            1.d0, a(1,lcs),   ubound(a,dim=1),           &
                                  vmrCPU(1,1),   ubound(vmrCPU,dim=1),   &
                            0.d0, umcCPU(lcs,1), ubound(umcCPU,dim=1))
               endif
             enddo
           endif ! l_cols>0 .and. l_rows>0
         else ! n_way > 1
           umcCPU(1:l_cols,1:n_cols) = 0.d0
           vmrCPU(1:l_rows,n_cols+1:2*n_cols) = 0
           if (l_cols>0 .and. l_rows>0) then
             do i=0,(istep*nbw-1)/tile_size

               lcs = i*l_cols_tile+1
               lce = min(l_cols,(i+1)*l_cols_tile)
               if (lce<lcs) cycle

               lre = min(l_rows,(i+1)*l_rows_tile)
               call DGEMM('T','N',lce-lcs+1,n_cols,lre,1.d0,a(1,lcs),ubound(a,dim=1), &
                            vmrCPU,ubound(vmrCPU,dim=1),1.d0,umcCPU(lcs,1),ubound(umcCPU,dim=1))

               if (i==0) cycle
                 lre = min(l_rows,i*l_rows_tile)
                 call DGEMM('N','N',lre,n_cols,lce-lcs+1,1.d0,a(1,lcs),lda, &
                            umcCPU(lcs,n_cols+1),ubound(umcCPU,dim=1),1.d0,vmrCPU(1,n_cols+1),ubound(vmrCPU,dim=1))
             enddo
           endif
         endif ! n_way > 1
#ifdef WITH_OPENMP
        !$omp end parallel
802
#endif
803
       endif ! do not useGPU version
Andreas Marek's avatar
Andreas Marek committed
804

805
806
807
808
       ! Sum up all ur(:) parts along rows and add them to the uc(:) parts
       ! on the processors containing the diagonal
       ! This is only necessary if ur has been calculated, i.e. if the
       ! global tile size is smaller than the global remaining matrix
Andreas Marek's avatar
Andreas Marek committed
809

810
811
       if (useGPU) then
         ! here the GPU version and CPU version divereged due to the same reasons as above
Andreas Marek's avatar
Andreas Marek committed
812

813
814
815
816
817
         if (tile_size < istep*nbw) then
           call elpa_reduce_add_vectors_real  (vmrCUDA(cur_l_rows * n_cols + 1),cur_l_rows,mpi_comm_rows, &
                                               umcCUDA, cur_l_cols, mpi_comm_cols, &
                                               istep*nbw, n_cols, nblk)
         endif
Andreas Marek's avatar
Andreas Marek committed
818

819
820
821
822
823
824
         if (l_cols>0) then
           allocate(tmpCUDA(l_cols * n_cols), stat=istat, errmsg=errorMessage)
           if (istat .ne. 0) then
             print *,"bandred_real: error when allocating tmpCUDA "//errorMessage
             stop
           endif
Andreas Marek's avatar
Andreas Marek committed
825

826
827
           call mpi_allreduce(umcCUDA,tmpCUDA,l_cols*n_cols,MPI_REAL8,MPI_SUM,mpi_comm_rows,ierr)
           umcCUDA(1 : l_cols * n_cols) = tmpCUDA(1 : l_cols * n_cols)
Andreas Marek's avatar
Andreas Marek committed
828

829
830
831
832
833
834
835
836
           if (allocated(tmpCUDA)) then
             deallocate(tmpCUDA, stat=istat, errmsg=errorMessage)
             if (istat .ne. 0) then
               print *,"bandred_real: error when deallocating tmpCUDA "//errorMessage
               stop
             endif
           endif
         endif ! l_cols
Andreas Marek's avatar
Andreas Marek committed
837

838
839
840
841
842
843
         ! U = U * Tmat**T
         successCUDA = cuda_memcpy(umc_dev, loc(umcCUDA(1)), umc_size*size_of_real_datatype, cudaMemcpyHostToDevice)
         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif
Andreas Marek's avatar
Andreas Marek committed
844

845
846
847
848
849
         successCUDA = cuda_memcpy(tmat_dev,loc(tmat(1,1,istep)),nbw*nbw*size_of_real_datatype,cudaMemcpyHostToDevice)
         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif
Andreas Marek's avatar
Andreas Marek committed
850

851
852
         call cublas_dtrmm('Right','Upper','Trans','Nonunit',l_cols,n_cols, &
                           1.d0, tmat_dev,nbw,umc_dev,cur_l_cols)
Andreas Marek's avatar
Andreas Marek committed
853

854
         ! VAV = Tmat * V**T * A * V * Tmat**T = (U*Tmat**T)**T * V * Tmat**T
Andreas Marek's avatar
Andreas Marek committed
855

856
857
858
859
860
         successCUDA = cuda_memcpy(vav_dev,loc(vav(1,1)), nbw*nbw*size_of_real_datatype,cudaMemcpyHostToDevice)
         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif
Andreas Marek's avatar
Andreas Marek committed
861

862
863
864
         call cublas_dgemm('T','N',n_cols,n_cols,l_cols, &
                           1.d0, umc_dev,cur_l_cols,(umc_dev+(cur_l_cols * n_cols )*size_of_real_datatype),cur_l_cols, &
                           0.d0, vav_dev,nbw)
Andreas Marek's avatar
Andreas Marek committed
865

866
867
         call cublas_dtrmm('Right','Upper','Trans','Nonunit',n_cols,n_cols, &
                           1.d0, tmat_dev,nbw, vav_dev, nbw)
Andreas Marek's avatar
Andreas Marek committed
868
869


870
871
872
873
874
         successCUDA = cuda_memcpy(loc(vav(1,1)), vav_dev, nbw*nbw*size_of_real_datatype, cudaMemcpyDeviceToHost)
         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif
Andreas Marek's avatar
Andreas Marek committed
875

876
         call symm_matrix_allreduce(n_cols,vav, nbw,nbw,mpi_comm_cols)
Andreas Marek's avatar
Andreas Marek committed
877

878
879
880
881
882
         successCUDA = cuda_memcpy(vav_dev, loc(vav(1,1)), nbw*nbw*size_of_real_datatype,cudaMemcpyHostToDevice)
         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif
Andreas Marek's avatar
Andreas Marek committed
883

884
885
886
887
         ! U = U - 0.5 * V * VAV
         call cublas_dgemm('N','N',l_cols,n_cols,n_cols,&
                           -0.5d0, (umc_dev+(cur_l_cols * n_cols )*size_of_real_datatype),cur_l_cols, vav_dev,nbw,&
                           1.0d0, umc_dev,cur_l_cols)
Andreas Marek's avatar
Andreas Marek committed
888

889
890
891
892
893
         successCUDA = cuda_memcpy(loc(umcCUDA(1)), umc_dev, umc_size*size_of_real_datatype, cudaMemcpyDeviceToHost)
         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif
Andreas Marek's avatar
Andreas Marek committed
894

895
         ! Transpose umc -> umr (stored in vmr, second half)
Andreas Marek's avatar
Andreas Marek committed
896

897
898
899
900
901
902
903
904
         call elpa_transpose_vectors_real  (umcCUDA, cur_l_cols, mpi_comm_cols, &
                                            vmrCUDA(cur_l_rows * n_cols + 1), cur_l_rows, mpi_comm_rows, &
                                            1, istep*nbw, n_cols, nblk)
         successCUDA = cuda_memcpy(vmr_dev, loc(vmrCUDA(1)), vmr_size*size_of_real_datatype, cudaMemcpyHostToDevice)
         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif
Andreas Marek's avatar
Andreas Marek committed
905

906
907
908
909
910
         successCUDA = cuda_memcpy(umc_dev, loc(umcCUDA(1)), umc_size*size_of_real_datatype, cudaMemcpyHostToDevice)
         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif
Andreas Marek's avatar
Andreas Marek committed
911

912
913
914
915
916
917
         ! A = A - V*U**T - U*V**T
         do i=0,(istep*nbw-1)/tile_size
           lcs = i*l_cols_tile+1
           lce = min(l_cols,(i+1)*l_cols_tile)
           lre = min(l_rows,(i+1)*l_rows_tile)
           if (lce<lcs .or. lre<1) cycle
Andreas Marek's avatar
Andreas Marek committed
918

919
920
921
922
923
924
925
926
927
928
929
           call cublas_dgemm('N', 'T', lre, lce-lcs+1, 2*n_cols, -1.d0, &
                             vmr_dev,cur_l_rows,(umc_dev +(lcs-1)*size_of_real_datatype),cur_l_cols, &
                             1.d0,(a_dev+(lcs-1)*lda*size_of_real_datatype),lda)
         enddo
       else ! do not useGPU
         ! Or if we used the Algorithm 4
         if (tile_size < istep*nbw .or. n_way > 1) then
         call elpa_reduce_add_vectors_real  (vmrCPU(1,n_cols+1),ubound(vmrCPU,dim=1),mpi_comm_rows, &
                                             umcCPU, ubound(umcCPU,dim=1), mpi_comm_cols, &
                                             istep*nbw, n_cols, nblk)
         endif
Andreas Marek's avatar
Andreas Marek committed
930

931
932
933
934
935
936
         if (l_cols>0) then
           allocate(tmpCPU(l_cols,n_cols), stat=istat, errmsg=errorMessage)
           if (istat .ne. 0) then
             print *,"bandred_real: error when allocating tmpCPU "//errorMessage
             stop
           endif
Andreas Marek's avatar
Andreas Marek committed
937

938
939
           call mpi_allreduce(umcCPU,tmpCPU,l_cols*n_cols,MPI_REAL8,MPI_SUM,mpi_comm_rows,mpierr)
           umcCPU(1:l_cols,1:n_cols) = tmpCPU(1:l_cols,1:n_cols)
Andreas Marek's avatar
Andreas Marek committed
940

941
942
943
944
945
946
           deallocate(tmpCPU, stat=istat, errmsg=errorMessage)
           if (istat .ne. 0) then
             print *,"bandred_real: error when deallocating tmpCPU "//errorMessage
             stop
           endif
         endif
Andreas Marek's avatar
Andreas Marek committed
947

948
         ! U = U * Tmat**T
Andreas Marek's avatar
Andreas Marek committed
949

950
951
         call dtrmm('Right','Upper','Trans','Nonunit',l_cols,n_cols,1.d0,tmat(1,1,istep),ubound(tmat,dim=1), &
                    umcCPU,ubound(umcCPU,dim=1))
Andreas Marek's avatar
Andreas Marek committed
952

953
         ! VAV = Tmat * V**T * A * V * Tmat**T = (U*Tmat**T)**T * V * Tmat**T
Andreas Marek's avatar
Andreas Marek committed
954

955
956
         call dgemm('T','N',n_cols,n_cols,l_cols,1.d0,umcCPU,ubound(umcCPU,dim=1),umcCPU(1,n_cols+1), &
                    ubound(umcCPU,dim=1),0.d0,vav,ubound(vav,dim=1))
Andreas Marek's avatar
Andreas Marek committed
957

958
959
960
961
         call dtrmm('Right','Upper','Trans','Nonunit',n_cols,n_cols,1.d0,tmat(1,1,istep),    &
                    ubound(tmat,dim=1),vav,ubound(vav,dim=1))

         call symm_matrix_allreduce(n_cols,vav, nbw, nbw ,mpi_comm_cols)
Andreas Marek's avatar
Andreas Marek committed
962

963
964
965
         ! U = U - 0.5 * V * VAV
         call dgemm('N','N',l_cols,n_cols,n_cols,-0.5d0,umcCPU(1,n_cols+1),ubound(umcCPU,dim=1),vav, &
                     ubound(vav,dim=1),1.d0,umcCPU,ubound(umcCPU,dim=1))
Andreas Marek's avatar
Andreas Marek committed
966

967
         ! Transpose umc -> umr (stored in vmr, second half)
Andreas Marek's avatar
Andreas Marek committed
968

969
         call elpa_transpose_vectors_real(umcCPU, ubound(umcCPU,dim=1), mpi_comm_cols, &
Andreas Marek's avatar
Andreas Marek committed
970
971
972
                                         vmrCPU(1,n_cols+1), ubound(vmrCPU,dim=1), mpi_comm_rows, &
                                         1, istep*nbw, n_cols, nblk)

973
974
975
976
977
978
979
980
981
         ! A = A - V*U**T - U*V**T
#ifdef WITH_OPENMP
         !$omp parallel private( ii, i, lcs, lce, lre, n_way, m_way, m_id, n_id, work_per_thread, mystart, myend  )
         n_threads = omp_get_num_threads()
         if (mod(n_threads, 2) == 0) then
           n_way = 2
         else
           n_way = 1
         endif
Andreas Marek's avatar
Andreas Marek committed
982

983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
         m_way = n_threads / n_way

         m_id = mod(omp_get_thread_num(),  m_way)
         n_id = omp_get_thread_num() / m_way

         do ii=n_id*tile_size,(istep*nbw-1),tile_size*n_way
           i = ii / tile_size
           lcs = i*l_cols_tile+1
           lce = min(l_cols,(i+1)*l_cols_tile)
           lre = min(l_rows,(i+1)*l_rows_tile)
           if (lce<lcs .or. lre<1) cycle

           !Figure out this thread's range
           work_per_thread = lre / m_way
           if (work_per_thread * m_way < lre) work_per_thread = work_per_thread + 1
           mystart = m_id * work_per_thread + 1
           myend   = mystart + work_per_thread - 1
           if ( myend > lre ) myend = lre
           if ( myend-mystart+1 < 1) cycle

           call dgemm('N','T',myend-mystart+1, lce-lcs+1, 2*n_cols, -1.d0, &
                      vmrCPU(mystart, 1), ubound(vmrCPU,1), umcCPU(lcs,1), ubound(umcCPU,1), &
                       1.d0,a(mystart,lcs),ubound(a,1))
         enddo
         !$omp end parallel
Andreas Marek's avatar
Andreas Marek committed
1008

1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
#else /* WITH_OPENMP */
         do i=0,(istep*nbw-1)/tile_size
           lcs = i*l_cols_tile+1
           lce = min(l_cols,(i+1)*l_cols_tile)
           lre = min(l_rows,(i+1)*l_rows_tile)
           if (lce<lcs .or. lre<1) cycle
           call dgemm('N','T',lre,lce-lcs+1,2*n_cols,-1.d0, &
                       vmrCPU,ubound(vmrCPU,dim=1),umcCPU(lcs,1),ubound(umcCPU,dim=1), &
                       1.d0,a(1,lcs),lda)
         enddo
#endif /* WITH_OPENMP */
Andreas Marek's avatar
Andreas Marek committed
1020

1021
       endif ! useGPU
Andreas Marek's avatar
Andreas Marek committed
1022

1023
1024
1025
1026
1027
1028
1029
1030
       if (.not.(useGPU)) then
         if (allocated(vr)) then
           deallocate(vr, stat=istat, errmsg=errorMessage)
           if (istat .ne. 0) then
             print *,"bandred_real: error when deallocating vr "//errorMessage
             stop
           endif
         endif
Andreas Marek's avatar
Andreas Marek committed
1031

1032
1033
1034
1035
1036
1037
1038
         if (allocated(umcCPU)) then
           deallocate(umcCPU, stat=istat, errmsg=errorMessage)
           if (istat .ne. 0) then
             print *,"bandred_real: error when deallocating vmrCPU "//errorMessage
             stop
           endif
         endif
Andreas Marek's avatar
Andreas Marek committed
1039

1040
1041
1042
1043
1044
1045
1046
         if (allocated(vmrCPU)) then
           deallocate(vmrCPU, stat=istat, errmsg=errorMessage)
           if (istat .ne. 0) then
             print *,"bandred_real: error when deallocating vmrCPU "//errorMessage
             stop
           endif
         endif
Andreas Marek's avatar
Andreas Marek committed
1047

1048
       endif !useGPU
Andreas Marek's avatar
Andreas Marek committed
1049

1050
     enddo ! istep
Andreas Marek's avatar
Andreas Marek committed
1051

1052
1053
1054
1055
1056
1057
     if (useGPU) then
       successCUDA = cuda_memcpy ( loc (a), a_dev, lda*na_cols*size_of_real_datatype,cudaMemcpyDeviceToHost)
       if (.not.(successCUDA)) then
         print *,"bandred_real: error in cudaMemcpy"
         stop
       endif
Andreas Marek's avatar
Andreas Marek committed
1058

1059
1060
1061
1062
1063
       successCUDA = cuda_free(a_dev)
       if (.not.(successCUDA)) then
         print *,"bandred_real: error in cudaFree"
         stop
       endif
Andreas Marek's avatar
Andreas Marek committed
1064

1065
1066
1067
1068
1069
       successCUDA = cuda_free(tmat_dev)
       if (.not.(successCUDA)) then
         print *,"bandred_real: error in cudaFree"
         stop
       endif
Andreas Marek's avatar
Andreas Marek committed
1070

1071
1072
1073
1074
1075
1076
       successCUDA = cuda_free(vav_dev)
       if (.not.(successCUDA)) then
         print *,"bandred_real: error in cudaFree"
         stop
       endif
     endif ! useGPU
Andreas Marek's avatar
Andreas Marek committed
1077

1078
1079
1080
1081
1082
1083
1084
     if (allocated(vr)) then
       deallocate(vr, stat=istat, errmsg=errorMessage)
       if (istat .ne. 0) then
         print *,"bandred_real: error when deallocating vr "//errorMessage
         stop
       endif
     endif
Andreas Marek's avatar
Andreas Marek committed
1085

1086
1087
1088
1089
1090
1091
1092
     if (allocated(umcCPU)) then
       deallocate(umcCPU, stat=istat, errmsg=errorMessage)
       if (istat .ne. 0) then
         print *,"bandred_real: error when deallocating umcCPU "//errorMessage
         stop
       endif
     endif
Andreas Marek's avatar
Andreas Marek committed
1093

1094
1095
1096
1097
1098
1099
1100
     if (allocated(vmrCPU)) then
       deallocate(vmrCPU, stat=istat, errmsg=errorMessage)
       if (istat .ne. 0) then
         print *,"bandred_real: error when deallocating vmrCPU "//errorMessage
         stop
       endif
     endif
Andreas Marek's avatar
Andreas Marek committed
1101

1102
1103
1104
1105
1106
1107
     if (useGPU) then
       successCUDA = cuda_free(vmr_dev)
       if (.not.(successCUDA)) then
         print *,"bandred_real: error in cudaFree"
         stop
       endif
Andreas Marek's avatar
Andreas Marek committed
1108

1109
1110
1111
1112