elpa2_compute_real_template.X90 213 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
#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), 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,
14
!    - Max-Plack-Institut für Mathematik in den Naturwissenschaften,
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
!      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".
#endif

64
    subroutine M_bandred_real_PRECISSION(na, a, a_dev, lda, nblk, nbw, matrixCols, numBlocks, mpi_comm_rows, mpi_comm_cols, &
65
                            tmat, tmat_dev, wantDebug, useGPU, success, useQR)
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

  !-------------------------------------------------------------------------------
  !  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
  !
  !-------------------------------------------------------------------------------

      use cuda_functions
      use iso_c_binding
      use elpa1_compute
#ifdef HAVE_DETAILED_TIMINGS
      use timings
#endif
#ifdef WITH_OPENMP
      use omp_lib
#endif
      use precision
      implicit none

Andreas Marek's avatar
Andreas Marek committed
108
      integer(kind=ik)                      :: na, lda, nblk, nbw, matrixCols, numBlocks, mpi_comm_rows, mpi_comm_cols
109
#ifdef USE_ASSUMED_SIZE
110
111
112
113
114
      real(kind=REAL_DATATYPE)              :: a(lda,*), tmat(nbw,nbw,*)
#else
      real(kind=REAL_DATATYPE)              :: a(lda,matrixCols), tmat(nbw,nbw,numBlocks)
#endif
      real(kind=REAL_DATATYPE)              :: eps
Andreas Marek's avatar
Andreas Marek committed
115
      logical, intent(in)                   :: useGPU
116

Andreas Marek's avatar
Andreas Marek committed
117
118
119
120
121
      integer(kind=ik)                      :: my_prow, my_pcol, np_rows, np_cols, mpierr
      integer(kind=ik)                      :: l_cols, l_rows, vmrCols
      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
122
123
124
125
126
127
128

      real(kind=REAL_DATATYPE)              :: vnorm2, xf, aux1(nbw), aux2(nbw), vrl, tau, vav(nbw,nbw)

      real(kind=REAL_DATATYPE), allocatable :: tmpCUDA(:),  vmrCUDA(:),  umcCUDA(:)
      real(kind=REAL_DATATYPE), allocatable :: tmpCPU(:,:), vmrCPU(:,:), umcCPU(:,:)
      real(kind=REAL_DATATYPE), allocatable :: vr(:)
      ! needed for blocked QR decomposition
Andreas Marek's avatar
Andreas Marek committed
129
      integer(kind=ik)                      :: PQRPARAM(11), work_size
130
131
132
      real(kind=REAL_DATATYPE)              :: dwork_size(1)
      real(kind=REAL_DATATYPE), allocatable :: work_blocked(:), tauvector(:), blockheuristic(:)

133
      ! a_dev is passed from bandred_real to trans_ev_band
Andreas Marek's avatar
Andreas Marek committed
134
      integer(kind=C_intptr_T)              :: a_dev, vmr_dev, umc_dev, tmat_dev, vav_dev
135
#ifdef WITH_MPI
Andreas Marek's avatar
Andreas Marek committed
136
      integer(kind=ik), external            :: numroc
137
#endif
Andreas Marek's avatar
Andreas Marek committed
138
139
140
141
142
      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_cols !, na_rows
143

Andreas Marek's avatar
Andreas Marek committed
144
145
146
147
148
      logical, intent(in)                   :: wantDebug
      logical, intent(out)                  :: success
      logical                               :: successCUDA
      integer(kind=ik)                      :: istat
      character(200)                        :: errorMessage
149

Andreas Marek's avatar
Andreas Marek committed
150
      logical, intent(in)                   :: useQR
151

Andreas Marek's avatar
Andreas Marek committed
152
153
      integer(kind=ik)                      :: mystart, myend, m_way, n_way, work_per_thread, m_id, n_id, n_threads, &
                                               ii, pp, transformChunkSize
154
#ifdef HAVE_DETAILED_TIMINGS
155
      call timer%start("bandred_real" + M_PRECISSION_SUFFIX)
156
#endif
157
158
159
160
#ifdef HAVE_DETAILED_TIMINGS
      call timer%start("mpi_communication")
#endif

161
162
163
164
      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)
165
166
167
#ifdef HAVE_DETAILED_TIMINGS
      call timer%stop("mpi_communication")
#endif
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
      success = .true.


      ! 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
183
! na_rows in used nowhere; only na_cols
184
185
      if (useGPU) then
#ifdef WITH_MPI
Andreas Marek's avatar
Andreas Marek committed
186
!        na_rows = numroc(na, nblk, my_prow, 0, np_rows)
187
188
        na_cols = numroc(na, nblk, my_pcol, 0, np_cols)
#else
Andreas Marek's avatar
Andreas Marek committed
189
!        na_rows = na
190
        na_cols = na
191
#endif
Andreas Marek's avatar
Andreas Marek committed
192
      endif ! useGPU
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

      ! Matrix is split into tiles; work is done only for tiles on the diagonal or above

      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

      l_rows_tile = tile_size/np_rows ! local rows of a tile
      l_cols_tile = tile_size/np_cols ! local cols of a tile

      if (useQR) then

        if (useGPU) then
          print *,"qr decomposition at the moment not supported with GPU"
          stop
        endif

        if (which_qr_decomposition == 1) then
          call qr_pqrparam_init(pqrparam(1:11),    nblk,'M',0,   nblk,'M',0,   nblk,'M',1,'s')
          allocate(tauvector(na), stat=istat, errmsg=errorMessage)
          if (istat .ne. 0) then
            print *,"bandred_real: error when allocating tauvector "//errorMessage
            stop
          endif

          allocate(blockheuristic(nblk), stat=istat, errmsg=errorMessage)
          if (istat .ne. 0) then
            print *,"bandred_real: error when allocating blockheuristic "//errorMessage
            stop
          endif

          l_rows = local_index(na, my_prow, np_rows, nblk, -1)
          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

          vmrCols = na

232
#ifdef USE_ASSUMED_SIZE_QR
233
          call M_qr_pdgeqrf_2dcomm_PRECISSION(a, lda, matrixCols, vmrCPU, max(l_rows,1), vmrCols, tauvector(1), na, tmat(1,1,1), &
234
235
236
237
                                 nbw, nbw, dwork_size, 1, -1, na, nbw, nblk, nblk, na, na, 1, 0, PQRPARAM(1:11), &
                                 mpi_comm_rows, mpi_comm_cols, blockheuristic)

#else
238
          call M_qr_pdgeqrf_2dcomm_PRECISSION(a(1:lda,1:matrixCols), matrixCols, lda, vmrCPU(1:max(l_rows,1),1:vmrCols), max(l_rows,1), &
239
240
241
242
243
244
245
246
247
248
249
                                 vmrCols, tauvector(1:na), na, tmat(1:nbw,1:nbw,1), nbw, &
                                 nbw, dwork_size(1:1), 1, -1, na, nbw, nblk, nblk, na, na, 1, 0, PQRPARAM(1:11), &
                                 mpi_comm_rows, mpi_comm_cols, blockheuristic)
#endif

          work_size = dwork_size(1)
          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
250
          work_blocked = M_CONST_0_0
251
252
253
254
255
256
257
258
259
260
261
262
          deallocate(vmrCPU, stat=istat, errmsg=errorMessage)
          if (istat .ne. 0) then
            print *,"bandred_real: error when deallocating vmrCPU "//errorMessage
            stop
          endif

        endif ! which_qr_decomposition

      endif ! useQr

      if (useGPU) then
        ! Here we convert the regular host array into a pinned host array
263
        successCUDA = cuda_malloc(a_dev, lda*na_cols*M_size_of_PRECISSION_real)
264
265
266
267
        if (.not.(successCUDA)) then
          print *,"bandred_real: error in cudaMalloc"
          stop
        endif
268
        successCUDA = cuda_malloc(tmat_dev, nbw*nbw*M_size_of_PRECISSION_real)
269
270
271
272
        if (.not.(successCUDA)) then
          print *,"bandred_real: error in cudaMalloc"
          stop
        endif
273
        successCUDA = cuda_malloc(vav_dev, nbw*nbw*M_size_of_PRECISSION_real)
274
275
276
277
278
279
280
        if (.not.(successCUDA)) then
          print *,"bandred_real: error in cudaMalloc"
          stop
        endif

        cur_l_rows = 0
        cur_l_cols = 0
281
        successCUDA = cuda_memcpy(a_dev, loc(a(1,1)), (lda)*(na_cols)*M_size_of_PRECISSION_real, cudaMemcpyHostToDevice)
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
        if (.not.(successCUDA)) then
          print *,"bandred_real: error in cudaMemcpy"
          stop
        endif
      endif ! useGPU


      do istep = (na-1)/nbw, 1, -1

        n_cols = MIN(na,(istep+1)*nbw) - istep*nbw ! Number of columns in current step

        ! 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)

        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

          endif

          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

              successCUDA = cuda_free(vmr_dev)
              if (.not.(successCUDA)) then
                print *,"bandred_real: error in cuda_free"
                stop
              endif
            endif

            allocate(vmrCUDA(vmr_size), stat=istat, errmsg=errorMessage)
            if (istat .ne. 0) then
              print *,"bandred_real: error when allocating vmrCUDA "//errorMessage
              stop
            endif
342
            successCUDA = cuda_malloc(vmr_dev, vmr_size*M_size_of_PRECISSION_real)
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
            if (.not.(successCUDA)) then
              print *,"bandred_real: error in cudaMalloc"
              stop
            endif

          endif

          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

              successCUDA = cuda_free(umc_dev)
              if (.not.(successCUDA)) then
                 print *,"bandred_real: error in cudaFree"
                 stop
              endif

            endif

            allocate(umcCUDA(umc_size), stat=istat, errmsg=errorMessage)
            if (istat .ne. 0) then
              print *,"bandred_real: error when deallocating umcCUDA "//errorMessage
              stop
            endif
371
            successCUDA = cuda_malloc(umc_dev, umc_size*M_size_of_PRECISSION_real)
372
373
374
375
376
377
            if (.not.(successCUDA)) then
              print *,"bandred_real: error in cudaMalloc"
              stop
            endif

          endif
Andreas Marek's avatar
Andreas Marek committed
378

379
        else ! GPU not used
Andreas Marek's avatar
Andreas Marek committed
380

381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
          ! Allocate vmr and umc to their exact sizes so that they can be used in bcasts and reduces

          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

          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

          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
400

401
        endif ! use GPU
Andreas Marek's avatar
Andreas Marek committed
402

403
        if (useGPU) then
404
          vmrCUDA(1 : cur_l_rows * n_cols) = M_CONST_0_0
405
        else
406
          vmrCPU(1:l_rows,1:n_cols) = M_CONST_0_0
407
        endif
408
409
        vr(:) = M_CONST_0_0
        tmat(:,:,istep) = M_CONST_0_0
410
411

        if (useGPU) then
412
          umcCUDA(1 : umc_size) = M_CONST_0_0
413
414
415
416
417
418
419
420
421
422
          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)

          if(lc_start .le. 0) lc_start = 1

          ! Here we assume that the processor grid and the block grid are aligned
          cur_pcol = pcol(istep*nbw+1, nblk, np_cols)

          if(my_pcol == cur_pcol) then
423
424
425
            successCUDA = cuda_memcpy2d(loc(a(1, lc_start)), lda*M_size_of_PRECISSION_real,         &
                                       (a_dev + ((lc_start-1) * lda*M_size_of_PRECISSION_real)),    &
                                       lda*M_size_of_PRECISSION_real, lr_end*M_size_of_PRECISSION_real, &
426
427
428
429
430
431
432
433
434
435
436
437
438
439
                                       (lc_end - lc_start+1), cudaMemcpyDeviceToHost)
            if (.not.(successCUDA)) then
              print *,"bandred_real: error in cudaMemcpy2d"
              stop
            endif

          endif
        endif ! useGPU

        ! Reduce current block to lower triangular form

        if (useQR) then
          if (which_qr_decomposition == 1) then
            vmrCols = 2*n_cols
440
#ifdef USE_ASSUMED_SIZE_QR
441
            call M_qr_pdgeqrf_2dcomm_PRECISSION(a, lda, matrixCols, vmrCPU, max(l_rows,1), vmrCols, tauvector(1), &
442
443
444
445
446
447
448
                                   na, tmat(1,1,istep), nbw, nbw, work_blocked, work_size,        &
                                     work_size, na, n_cols, nblk, nblk,        &
                                     istep*nbw+n_cols-nbw, istep*nbw+n_cols, 1,&
                                     0, PQRPARAM(1:11), mpi_comm_rows, mpi_comm_cols,&
                                     blockheuristic)

#else
449
            call M_qr_pdgeqrf_2dcomm_PRECISSION(a(1:lda,1:matrixCols), lda, matrixCols, vmrCPU(1:max(l_rows,1),1:vmrCols) ,   &
450
451
452
453
454
455
456
457
                                    max(l_rows,1), vmrCols, tauvector(1:na), na, &
                                     tmat(1:nbw,1:nbw,istep), nbw, nbw, work_blocked(1:work_size), work_size, &
                                     work_size, na, n_cols, nblk, nblk,        &
                                     istep*nbw+n_cols-nbw, istep*nbw+n_cols, 1,&
                                     0, PQRPARAM(1:11), mpi_comm_rows, mpi_comm_cols,&
                                     blockheuristic)
#endif
          endif
Andreas Marek's avatar
Andreas Marek committed
458

459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
       else !useQR

         do lc = n_cols, 1, -1

           ncol = istep*nbw + lc ! absolute column number of householder vector
           nrow = ncol - nbw ! Absolute number of pivot row

           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

           tau = 0

           if (nrow == 1) exit ! Nothing to do

           cur_pcol = pcol(ncol, nblk, np_cols) ! Processor column owning current block

           if (my_pcol==cur_pcol) then

             ! Get vector to be transformed; distribute last element and norm of
             ! remaining elements to all procs in current column

             vr(1:lr) = a(1:lr,lch) ! vector to be transformed

             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))
487
               aux1(2) = M_CONST_0_0
488
489
490
             endif

#ifdef WITH_MPI
491
492
493
#ifdef HAVE_DETAILED_TIMINGS
             call timer%start("mpi_communication")
#endif
494
             call mpi_allreduce(aux1, aux2, 2, M_MPI_REAL_PRECISSION, MPI_SUM, mpi_comm_rows, mpierr)
495
496
497
#ifdef HAVE_DETAILED_TIMINGS
             call timer%stop("mpi_communication")
#endif
498
499
500

#else /* WITH_MPI */
              aux2 = aux1 ! this should be optimized
501
#endif 
502
503
504
505
506

             vnorm2 = aux2(1)
             vrl    = aux2(2)

             ! Householder transformation
507
508
             call M_hh_transform_real_PRECISSION(vrl, vnorm2, xf, tau)

509
510
511
512
513
514
             ! Scale vr and store Householder vector for back transformation

             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
515
               vr(lr) = M_CONST_1_0
516
517
518
519
520
521
522
523
524
525
             else
               a(1:lr,lch) = vr(1:lr)
             endif

           endif

           ! Broadcast Householder vector and tau along columns

           vr(lr+1) = tau
#ifdef WITH_MPI
526
527
528
#ifdef HAVE_DETAILED_TIMINGS
           call timer%start("mpi_communication")
#endif
529
           call MPI_Bcast(vr, lr+1, M_MPI_REAL_PRECISSION, cur_pcol, mpi_comm_cols, mpierr)
530
531
532
#ifdef HAVE_DETAILED_TIMINGS
           call timer%stop("mpi_communication")
#endif
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
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

#endif /* WITH_MPI */
           if (useGPU) then
             vmrCUDA(cur_l_rows * (lc - 1) + 1 : cur_l_rows * (lc - 1) + lr) = vr(1:lr)
           else
             vmrCPU(1:lr,lc) = vr(1:lr)
           endif

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

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

           aux1 = 0

#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

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

           !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

           ! Get global dot products

           !$omp barrier
           !$omp single
#ifdef WITH_MPI
577
578
579
#ifdef HAVE_DETAILED_TIMINGS
           call timer%start("mpi_communication")
#endif
580
           if (mynlc>0) call mpi_allreduce(aux1, aux2, mynlc, M_MPI_REAL_PRECISSION, MPI_SUM, mpi_comm_rows, mpierr)
581
582
583
#ifdef HAVE_DETAILED_TIMINGS
           call timer%stop("mpi_communication")
#endif
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#else /* WITH_MPI */
           if (mynlc>0) aux2 = aux1
#endif /* WITH_MPI */
           !$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
Andreas Marek's avatar
Andreas Marek committed
608

609
#else /* WITH_OPENMP */
Andreas Marek's avatar
Andreas Marek committed
610

611
612
613
614
615
616
617
618
619
620
621
           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

           ! Get global dot products
#ifdef WITH_MPI
622
623
624
#ifdef HAVE_DETAILED_TIMINGS
           call timer%start("mpi_communication")
#endif
625
           if (nlc>0) call mpi_allreduce(aux1, aux2, nlc, M_MPI_REAL_PRECISSION, MPI_SUM, mpi_comm_rows, mpierr)
626
627
628
#ifdef HAVE_DETAILED_TIMINGS
           call timer%stop("mpi_communication")
#endif
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#else /* WITH_MPI */
           if (nlc>0) aux2=aux1
#endif /* WITH_MPI */
           ! Transform

           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

         if (useGPU) then
           ! store column tiles back to GPU
           cur_pcol = pcol(istep*nbw+1, nblk, np_cols)
           if (my_pcol == cur_pcol) then
649
650
651
             successCUDA = cuda_memcpy2d((a_dev+((lc_start-1)*lda*M_size_of_PRECISSION_real)),          &
                                          lda*M_size_of_PRECISSION_real, loc(a(1, lc_start)),           &
                                          lda*M_size_of_PRECISSION_real,  lr_end*M_size_of_PRECISSION_real, &
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
                                          (lc_end - lc_start+1),cudaMemcpyHostToDevice)
             if (.not.(successCUDA)) then
               print *,"bandred_real: error in cudaMemcpy2d"
               stop
             endif

           endif
         endif

         ! Calculate scalar products of stored Householder vectors.
         ! This can be done in different ways, we use dsyrk

         vav = 0

         if (useGPU) then
           if (l_rows>0) &
668
             call M_PRECISSION_SYRK('U', 'T', n_cols, l_rows, M_CONST_1_0, vmrCUDA, cur_l_rows, M_CONST_0_0, vav, ubound(vav,dim=1))
669
670
         else
           if (l_rows>0) &
671
             call M_PRECISSION_SYRK('U', 'T', n_cols, l_rows, M_CONST_1_0, vmrCPU, ubound(vmrCPU,dim=1), M_CONST_0_0, vav, ubound(vav,dim=1))
672
673
         endif

674
         call M_symm_matrix_allreduce_PRECISSION(n_cols,vav, nbw, nbw,mpi_comm_rows)
675

676
         ! Calculate triangular matrix T for block Householder Transformation
677
678
679
         do lc=n_cols,1,-1
           tau = tmat(lc,lc,istep)
           if (lc<n_cols) then
680
             call M_PRECISSION_TRMV('U', 'T', 'N', n_cols-lc, tmat(lc+1,lc+1,istep), ubound(tmat,dim=1), vav(lc+1,lc), 1)
681
682
683
684
685
686
687
688
             tmat(lc,lc+1:n_cols,istep) = -tau * vav(lc+1:n_cols,lc)
           endif
         enddo
       endif

       ! Transpose vmr -> vmc (stored in umc, second half)

       if (useGPU) then
689
         call M_elpa_transpose_vectors_real_PRECISSION  (vmrCUDA, cur_l_rows, mpi_comm_rows, &
690
691
692
                                            umcCUDA(cur_l_cols * n_cols + 1), cur_l_cols, mpi_comm_cols, &
                                            1, istep*nbw, n_cols, nblk)
       else
693
         call M_elpa_transpose_vectors_real_PRECISSION  (vmrCPU, ubound(vmrCPU,dim=1), mpi_comm_rows, &
694
695
696
697
698
699
700
701
702
703
704
705
                                            umcCPU(1,n_cols+1), ubound(umcCPU,dim=1), mpi_comm_cols, &
                                            1, istep*nbw, n_cols, nblk)
       endif

       ! 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
       if (useGPU) then
706
707
         umcCUDA(1 : l_cols * n_cols) = M_CONST_0_0
         vmrCUDA(cur_l_rows * n_cols + 1 : cur_l_rows * n_cols * 2) = M_CONST_0_0
708
709

         if (l_cols>0 .and. l_rows>0) then
710
           successCUDA = cuda_memcpy(vmr_dev, loc(vmrCUDA(1)), vmr_size*M_size_of_PRECISSION_real,cudaMemcpyHostToDevice)
711
712
713
714
           if (.not.(successCUDA)) then
             print *,"bandred_real: error in cudaMemcpy"
             stop
           endif
715
           successCUDA = cuda_memcpy(umc_dev, loc(umcCUDA(1)), umc_size*M_size_of_PRECISSION_real,cudaMemcpyHostToDevice)
716
717
718
719
720
721
722
723
724
725
726
727
           if (.not.(successCUDA)) then
             print *,"bandred_real: error in cudaMemcpy"
             stop
           endif

           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)
728
             call M_cublas_PRECISSION_gemm('T', 'N', lce-lcs+1, n_cols, lre, &
729
730
                               M_CONST_1_0, (a_dev + ((lcs-1)*lda*M_size_of_PRECISSION_real)), lda, vmr_dev,cur_l_rows, &
                               M_CONST_1_0, (umc_dev+ (lcs-1)*M_size_of_PRECISSION_real), cur_l_cols)
731
732
             if(i==0) cycle
             lre = min(l_rows,i*l_rows_tile)
733
             call M_cublas_PRECISSION_gemm('N', 'N', lre,n_cols, lce-lcs+1,&
734
735
736
                               M_CONST_1_0, (a_dev+ ((lcs-1)*lda*M_size_of_PRECISSION_real)), lda,                  &
                               (umc_dev+(cur_l_cols * n_cols+lcs-1)*M_size_of_PRECISSION_real), cur_l_cols, &
                               M_CONST_1_0, (vmr_dev+(cur_l_rows * n_cols)*M_size_of_PRECISSION_real), cur_l_rows)
737
           enddo
738
           successCUDA = cuda_memcpy(loc(vmrCUDA(1)), vmr_dev,vmr_size*M_size_of_PRECISSION_real,cudaMemcpyDeviceToHost)
739
740
741
742
           if (.not.(successCUDA)) then
             print *,"bandred_real: error in cudaMemcpy"
             stop
           endif
743
           successCUDA = cuda_memcpy(loc(umcCUDA(1)), umc_dev, umc_size*M_size_of_PRECISSION_real,cudaMemcpyDeviceToHost)
744
745
746
747
748
749
750
751
           if (.not.(successCUDA)) then
             print *,"bandred_real: error in cudaMemcpy"
             stop
           endif

         endif ! l_cols>0 .and. l_rows>0

       else ! do not useGPU version
Andreas Marek's avatar
Andreas Marek committed
752

753
754
755
756
757
758
759
760
761
762
763
764
765
766
         !Code for Algorithm 4

         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)
767
             umcCPU(i,1:n_cols) = M_CONST_0_0
768
769
770
771
           enddo

           !$omp do
           do i=1,l_rows
772
             vmrCPU(i,n_cols+1:2*n_cols) = M_CONST_0_0
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
           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
802
803
                 call M_PRECISSION_GEMM('N', 'N', lre-lrs+1, n_cols, l_cols-lcs+1,          &
                            M_CONST_1_0, a(lrs,lcs), ubound(a,dim=1),                 &
804
                                  umcCPU(lcs,n_cols+1), ubound(umcCPU,dim=1),  &
805
                            M_CONST_0_0, vmrCPU(lrs,n_cols+1), ubound(vmrCPU,dim=1))
806
807
808
809
               endif

               ! C1 += A10' B0
               if ( lce > lcs .and. i > 0 ) then
810
811
                 call M_PRECISSION_GEMM('T', 'N', lce-lcs+1, n_cols, lrs-1,           &
                            M_CONST_1_0, a(1,lcs),   ubound(a,dim=1),           &
812
                                  vmrCPU(1,1),   ubound(vmrCPU,dim=1),   &
813
                            M_CONST_0_0, umcCPU(lcs,1), ubound(umcCPU,dim=1))
814
815
816
817
               endif
             enddo
           endif ! l_cols>0 .and. l_rows>0
         else ! n_way > 1
818
819
           umcCPU(1:l_cols,1:n_cols) = M_CONST_0_0
           vmrCPU(1:l_rows,n_cols+1:2*n_cols) = M_CONST_0_0
820
821
822
823
824
825
826
827
           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)
828
829
               call M_PRECISSION_GEMM('T', 'N', lce-lcs+1, n_cols, lre, M_CONST_1_0, a(1,lcs), ubound(a,dim=1), &
                            vmrCPU, ubound(vmrCPU,dim=1), M_CONST_1_0, umcCPU(lcs,1), ubound(umcCPU,dim=1))
830
831
832

               if (i==0) cycle
                 lre = min(l_rows,i*l_rows_tile)
833
834
                 call M_PRECISSION_GEMM('N', 'N', lre, n_cols, lce-lcs+1, M_CONST_1_0, a(1,lcs), lda, &
                            umcCPU(lcs,n_cols+1), ubound(umcCPU,dim=1), M_CONST_1_0, vmrCPU(1,n_cols+1), ubound(vmrCPU,dim=1))
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
             enddo
           endif
         endif ! n_way > 1
#ifdef WITH_OPENMP
        !$omp end parallel
#endif
       endif ! do not useGPU version

       ! 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

       if (useGPU) then
         ! here the GPU version and CPU version divereged due to the same reasons as above

         if (tile_size < istep*nbw) then
852
           call M_elpa_reduce_add_vectors_real_PRECISSION  (vmrCUDA(cur_l_rows * n_cols + 1),cur_l_rows,mpi_comm_rows, &
853
854
855
856
857
858
859
860
861
862
863
864
                                               umcCUDA, cur_l_cols, mpi_comm_cols, &
                                               istep*nbw, n_cols, nblk)
         endif

         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

#ifdef WITH_MPI
865
866
867
#ifdef HAVE_DETAILED_TIMINGS
           call timer%start("mpi_communication")
#endif
868

869
           call mpi_allreduce(umcCUDA, tmpCUDA, l_cols*n_cols, M_MPI_REAL_PRECISSION, MPI_SUM, mpi_comm_rows, ierr)
870
           umcCUDA(1 : l_cols * n_cols) = tmpCUDA(1 : l_cols * n_cols)
871
872
873
#ifdef HAVE_DETAILED_TIMINGS
           call timer%stop("mpi_communication")
#endif
874
#else /* WITH_MPI */
875
876
877

!           tmpCUDA(1 : l_cols * n_cols) = umcCUDA(1 : l_cols * n_cols)

878
879
880
881
882
883
884
885
886
887
888
889
#endif /* WITH_MPI */

           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

         ! U = U * Tmat**T
890
         successCUDA = cuda_memcpy(umc_dev, loc(umcCUDA(1)), umc_size*M_size_of_PRECISSION_real, cudaMemcpyHostToDevice)
891
892
893
894
         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif
895
         successCUDA = cuda_memcpy(tmat_dev,loc(tmat(1,1,istep)),nbw*nbw*M_size_of_PRECISSION_real,cudaMemcpyHostToDevice)
896
897
898
899
         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif
900
901
         call M_cublas_PRECISSION_trmm('Right', 'Upper', 'Trans', 'Nonunit', l_cols, n_cols, &
                           M_CONST_1_0, tmat_dev, nbw, umc_dev, cur_l_cols)
902
         ! VAV = Tmat * V**T * A * V * Tmat**T = (U*Tmat**T)**T * V * Tmat**T
903
         successCUDA = cuda_memcpy(vav_dev,loc(vav(1,1)), nbw*nbw*M_size_of_PRECISSION_real,cudaMemcpyHostToDevice)
904
905
906
907
         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif
908
         call M_cublas_PRECISSION_gemm('T', 'N', n_cols, n_cols, l_cols, &
909
                           M_CONST_1_0, umc_dev, cur_l_cols, (umc_dev+(cur_l_cols * n_cols )*M_size_of_PRECISSION_real),cur_l_cols, &
910
                           M_CONST_0_0, vav_dev, nbw)
911

912
913
         call M_cublas_PRECISSION_trmm('Right', 'Upper', 'Trans', 'Nonunit', n_cols, n_cols, &
                           M_CONST_1_0, tmat_dev, nbw, vav_dev, nbw)
914

915
         successCUDA = cuda_memcpy(loc(vav(1,1)), vav_dev, nbw*nbw*M_size_of_PRECISSION_real, cudaMemcpyDeviceToHost)
916
917
918
919
920
         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif

921
         call M_symm_matrix_allreduce_PRECISSION(n_cols,vav, nbw,nbw,mpi_comm_cols)
922

923
         successCUDA = cuda_memcpy(vav_dev, loc(vav(1,1)), nbw*nbw*M_size_of_PRECISSION_real,cudaMemcpyHostToDevice)
924
925
926
927
928
929
         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif

         ! U = U - 0.5 * V * VAV
930
         call M_cublas_PRECISSION_gemm('N', 'N', l_cols, n_cols, n_cols,&
931
                           -M_CONST_0_5, (umc_dev+(cur_l_cols * n_cols )*M_size_of_PRECISSION_real),cur_l_cols, vav_dev,nbw,&
932
                           M_CONST_1_0, umc_dev, cur_l_cols)
933

934
         successCUDA = cuda_memcpy(loc(umcCUDA(1)), umc_dev, umc_size*M_size_of_PRECISSION_real, cudaMemcpyDeviceToHost)
935
936
937
938
939
940
941

         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif

         ! Transpose umc -> umr (stored in vmr, second half)
942
         call M_elpa_transpose_vectors_real_PRECISSION  (umcCUDA, cur_l_cols, mpi_comm_cols, &
943
944
945
                                            vmrCUDA(cur_l_rows * n_cols + 1), cur_l_rows, mpi_comm_rows, &
                                            1, istep*nbw, n_cols, nblk)

946
         successCUDA = cuda_memcpy(vmr_dev, loc(vmrCUDA(1)), vmr_size*M_size_of_PRECISSION_real, cudaMemcpyHostToDevice)
947
948
949
950
951
         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif

952
         successCUDA = cuda_memcpy(umc_dev, loc(umcCUDA(1)), umc_size*M_size_of_PRECISSION_real, cudaMemcpyHostToDevice)
953
954
955
956
957
958
959
960
961
962
963
         if (.not.(successCUDA)) then
           print *,"bandred_real: error in cudaMemcpy"
           stop
         endif

         ! 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
964
           call M_cublas_PRECISSION_gemm('N', 'T', lre, lce-lcs+1, 2*n_cols, -M_CONST_1_0, &
965
966
                             vmr_dev, cur_l_rows, (umc_dev +(lcs-1)*M_size_of_PRECISSION_real), cur_l_cols, &
                             M_CONST_1_0, (a_dev+(lcs-1)*lda*M_size_of_PRECISSION_real), lda)
967
         enddo
968

969
       else ! do not useGPU
970

971
972
         ! Or if we used the Algorithm 4
         if (tile_size < istep*nbw .or. n_way > 1) then
973
           call M_elpa_reduce_add_vectors_real_PRECISSION  (vmrCPU(1,n_cols+1),ubound(vmrCPU,dim=1),mpi_comm_rows, &
974
975
976
977
978
979
980
981
982
983
984
985
                                             umcCPU, ubound(umcCPU,dim=1), mpi_comm_cols, &
                                             istep*nbw, n_cols, nblk)
         endif

         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

#ifdef WITH_MPI
986
987
988
#ifdef HAVE_DETAILED_TIMINGS
           call timer%start("mpi_communication")
#endif
989
           call mpi_allreduce(umcCPU, tmpCPU, l_cols*n_cols, M_MPI_REAL_PRECISSION, MPI_SUM, mpi_comm_rows, mpierr)
990
           umcCPU(1:l_cols,1:n_cols) = tmpCPU(1:l_cols,1:n_cols)
991
992
993
#ifdef HAVE_DETAILED_TIMINGS
           call timer%stop("mpi_communication")
#endif
994
#else /* WITH_MPI */
995
!           tmpCPU(1:l_cols,1:n_cols) = umcCPU(1:l_cols,1:n_cols)
996
997
998
999
1000
1001
1002
1003
1004
1005
#endif /* WITH_MPI */

           deallocate(tmpCPU, stat=istat, errmsg=errorMessage)
           if (istat .ne. 0) then
             print *,"bandred_real: error when deallocating tmpCPU "//errorMessage
             stop
           endif
         endif

         ! U = U * Tmat**T
1006
         call M_PRECISSION_TRMM('Right', 'Upper', 'Trans', 'Nonunit', l_cols,n_cols, M_CONST_1_0, tmat(1,1,istep), ubound(tmat,dim=1), &
1007
1008
1009
1010
                    umcCPU, ubound(umcCPU,dim=1))

         ! VAV = Tmat * V**T * A * V * Tmat**T = (U*Tmat**T)**T * V * Tmat**T

1011
1012
         call M_PRECISSION_GEMM('T', 'N', n_cols, n_cols, l_cols, M_CONST_1_0, umcCPU, ubound(umcCPU,dim=1), umcCPU(1,n_cols+1), &
                    ubound(umcCPU,dim=1), M_CONST_0_0, vav, ubound(vav,dim=1))
1013

1014
         call M_PRECISSION_TRMM('Right', 'Upper', 'Trans', 'Nonunit', n_cols, n_cols, M_CONST_1_0, tmat(1,1,istep),    &
1015
1016
                    ubound(tmat,dim=1), vav, ubound(vav,dim=1))

1017
         call M_symm_matrix_allreduce_PRECISSION(n_cols,vav, nbw, nbw ,mpi_comm_cols)
1018
1019

         ! U = U - 0.5 * V * VAV
1020
1021
         call M_PRECISSION_GEMM('N', 'N', l_cols, n_cols, n_cols, -M_CONST_0_5, umcCPU(1,n_cols+1), ubound(umcCPU,dim=1), vav, &
                     ubound(vav,dim=1), M_CONST_1_0, umcCPU, ubound(umcCPU,dim=1))
1022
1023

         ! Transpose umc -> umr (stored in vmr, second half)
1024
         call M_elpa_transpose_vectors_real_PRECISSION(umcCPU, ubound(umcCPU,dim=1), mpi_comm_cols, &
1025
1026
                                         vmrCPU(1,n_cols+1), ubound(vmrCPU,dim=1), mpi_comm_rows, &
                                         1, istep*nbw, n_cols, nblk)
Andreas Marek's avatar
Andreas Marek committed
1027

1028
         ! A = A - V*U**T - U*V**T
Andreas Marek's avatar
Andreas Marek committed
1029

1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
#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

         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
1058
           call M_PRECISSION_GEMM('N', 'T', myend-mystart+1, lce-lcs+1, 2*n_cols, -M_CONST_1_0, &
1059
                      vmrCPU(mystart, 1), ubound(vmrCPU,1), umcCPU(lcs,1), ubound(umcCPU,1), &
1060
                       M_CONST_1_0, a(mystart,lcs), ubound(a,1))
1061
1062
         enddo
         !$omp end parallel
Andreas Marek's avatar
Andreas Marek committed
1063

1064
#else /* WITH_OPENMP */
Andreas Marek's avatar
Andreas Marek committed
1065

1066
1067
1068
1069
1070
         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
1071
           call M_PRECISSION_GEMM('N', 'T', lre,lce-lcs+1, 2*n_cols, -M_CONST_1_0, &
1072
                       vmrCPU, ubound(vmrCPU,dim=1), umcCPU(lcs,1), ubound(umcCPU,dim=1), &
1073
                       M_CONST_1_0, a(1,lcs), lda)
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
         enddo
#endif /* WITH_OPENMP */

       endif ! useGPU

       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

         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

         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

       endif !useGPU

     enddo ! istep

     if (useGPU) then
1109
       ! this is not needed since a_dev is passed along from one subroutine to the other
1110
       successCUDA = cuda_memcpy ( loc (a), a_dev, lda*na_cols*M_size_of_PRECISSION_real,cudaMemcpyDeviceToHost)
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
       if (.not.(successCUDA)) then
         print *,"bandred_real: error in cudaMemcpy"
         stop
       endif

       successCUDA = cuda_free(a_dev)
       if (.not.(successCUDA)) then
         print *,"bandred_real: error in cudaFree"
         stop
       endif

1122
1123
1124
1125
1126
1127
1128
1129
1130
!#ifdef WITH_MPI
! it should be possible to keep tmat dev on the device and not copy it arround
! this is not necessary tmat_dev is passed (unchanged) from one routine to the other
       successCUDA = cuda_free(tmat_dev)
       if (.not.(successCUDA)) then
         print *,"bandred_real: error in cudaFree"
         stop
       endif
!#endif
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208

       successCUDA = cuda_free(vav_dev)
       if (.not.(successCUDA)) then
         print *,"bandred_real: error in cudaFree"
         stop
       endif
     endif ! useGPU

     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

     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

     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

     if (useGPU) then
       successCUDA = cuda_free(vmr_dev)
       if (.not.(successCUDA)) then
         print *,"bandred_real: error in cudaFree"
         stop
       endif

       successCUDA = cuda_free(umc_dev)
       if (.not.(successCUDA)) then
         print *,"bandred_real: error in cudaFree"
         stop
       endif
       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
       endif
       if (allocated(vmrCUDA)) then
         deallocate(vmrCUDA, stat=istat, errmsg=errorMessage)
         if (istat .ne. 0) then
           print *,"bandred_real: error when deallocating vmrCUDA "//errorMessage
           stop
         endif
       endif

     endif ! useGPU

     if (useQR) then
       if (which_qr_decomposition == 1) then
         deallocate(work_blocked, stat=istat, errmsg=errorMessage)
         if (istat .ne. 0) then
           print *,"bandred_real: error when deallocating work_blocked "//errorMessage
           stop
         endif

         deallocate(tauvector, stat=istat, errmsg=errorMessage)
         if (istat .ne. 0) then
           print *,"bandred_real: error when deallocating tauvector "//errorMessage
           stop
         endif
       endif
     endif

#ifdef HAVE_DETAILED_TIMINGS
1209
     call timer%stop("bandred_real" + M_PRECISSION_SUFFIX)
1210
1211
#endif

1212
   end subroutine M_bandred_real_PRECISSION ! slower for gpu on 10000 10000 ???
1213

1214
    subroutine M_symm_matrix_allreduce_PRECISSION(n,a,lda,ldb,comm)
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
    !-------------------------------------------------------------------------------
    !  symm_matrix_allreduce: Does an mpi_allreduce for a symmetric matrix A.
    !  On entry, only the upper half of A needs to be set
    !  On exit, the complete matrix is set
    !-------------------------------------------------------------------------------
#ifdef HAVE_DETAILED_TIMINGS
      use timings
#endif
      use precision
      implicit none
Andreas Marek's avatar
Andreas Marek committed
1225
      integer(kind=ik)             :: n, lda, ldb, comm
1226
#ifdef USE_ASSUMED_SIZE
1227
1228
1229
1230
      real(kind=REAL_DATATYPE)     :: a(lda,*)
#else
      real(kind=REAL_DATATYPE)     :: a(lda,ldb)
#endif
Andreas Marek's avatar
Andreas Marek committed
1231
      integer(kind=ik)             :: i, nc, mpierr
1232
1233
1234
      real(kind=REAL_DATATYPE)     :: h1(n*n), h2(n*n)

#ifdef HAVE_DETAILED_TIMINGS
1235
      call timer%start("symm_matrix_allreduce" + M_PRECISSION_SUFFIX)
1236
1237
1238
1239
1240
1241
1242
1243
1244
#endif

      nc = 0
      do i=1,n
        h1(nc+1:nc+i) = a(1:i,i)
        nc = nc+i
      enddo

#ifdef WITH_MPI
1245
1246
1247
#ifdef HAVE_DETAILED_TIMINGS
      call timer%start("mpi_communication")
#endif
1248
      call mpi_allreduce(h1, h2, nc, M_MPI_REAL_PRECISSION, MPI_SUM, comm, mpierr)
1249
1250
1251
#ifdef HAVE_DETAILED_TIMINGS
      call timer%stop("mpi_communication")
#endif
1252
1253
1254
1255
1256
1257
1258
      nc = 0
      do i=1,n
        a(1:i,i) = h2(nc+1:nc+i)
        a(i,1:i-1) = a(1:i-1,i)
        nc = nc+i
      enddo

1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
#else /* WITH_MPI */
!      h2=h1

      nc = 0
      do i=1,n
        a(1:i,i) = h1(nc+1:nc+i)
        a(i,1:i-1) = a(1:i-1,i)
        nc = nc+i
      enddo

#endif /* WITH_MPI */
!      nc = 0
!      do i=1,n
!        a(1:i,i) = h2(nc+1:nc+i)
!        a(i,1:i-1) = a(1:i-1,i)
!        nc = nc+i
!      enddo

1277
#ifdef HAVE_DETAILED_TIMINGS
1278
      call timer%stop("symm_matrix_allreduce" + M_PRECISSION_SUFFIX)
1279
1280
#endif

1281
    end subroutine M_symm_matrix_allreduce_PRECISSION
1282

1283
1284
    subroutine M_trans_ev_band_to_full_real_PRECISSION(na, nqc, nblk, nbw, a, a_dev, lda, tmat, tmat_dev, q, q_dev, ldq, matrixCols, &
                                                       numBlocks, mpi_comm_rows, mpi_comm_cols, useGPU, useQR)
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
    !-------------------------------------------------------------------------------
    !  trans_ev_band_to_full_real:
    !  Transforms the eigenvectors of a band matrix back to the eigenvectors of the original matrix
    !
    !  Parameters
    !
    !  na          Order of matrix a, number of rows of matrix q
    !
    !  nqc         Number of columns of matrix q
    !
    !  nblk        blocksize of cyclic distribution, must be the same in both directions!
    !
    !  nbw         semi bandwith
    !
    !  a(lda,matrixCols)    Matrix containing the Householder vectors (i.e. matrix a after bandred_real)
    !              Distribution is like in Scalapack.
    !
    !  lda         Leading dimension of a
    !  matrixCols  local columns of matrix a and q
    !
    !  tmat(nbw,nbw,numBlocks) Factors returned by bandred_real
    !
    !  q           On input: Eigenvectors of band matrix
    !              On output: Transformed eigenvectors
    !              Distribution is like in Scalapack.
    !
    !  ldq         Leading dimension of q
    !
    !  mpi_comm_rows
    !  mpi_comm_cols
    !              MPI-Communicators for rows/columns
    !
    !-------------------------------------------------------------------------------
#ifdef HAVE_DETAILED_TIMINGS
      use timings
#endif
      use precision
      use cuda_functions
      use iso_c_binding

      implicit none

Andreas Marek's avatar
Andreas Marek committed
1327
      integer(kind=ik)                       :: na, nqc, lda, ldq, nblk, nbw, matrixCols, numBlocks, mpi_comm_rows, mpi_comm_cols
1328
#ifdef USE_ASSUMED_SIZE
Andreas Marek's avatar
Andreas Marek committed
1329
      real(kind=REAL_DATATYPE)               :: a(lda,*), q(ldq,*), tmat(nbw,nbw,*)
1330
#else
Andreas Marek's avatar
Andreas Marek committed
1331
      real(kind=REAL_DATATYPE)               :: a(lda,matrixCols), q(ldq,matrixCols), tmat(nbw, nbw, numBlocks)
1332
#endif
1333

Andreas Marek's avatar
Andreas Marek committed
1334
      integer(kind=C_intptr_T)               :: a_dev ! passed from bandred_real at the moment not used since copied in bandred_real
1335

Andreas Marek's avatar
Andreas Marek committed
1336
1337
1338
1339
1340
      integer(kind=ik)                       :: my_prow, my_pcol, np_rows, np_cols, mpierr
      integer(kind=ik)                       :: max_blocks_row, max_blocks_col, max_local_rows, &
                                                max_local_cols
      integer(kind=ik)                       :: l_cols, l_rows, l_colh, n_cols
      integer(kind=ik)                       :: istep, lc, ncol, nrow, nb, ns
1341
1342
1343

      real(kind=REAL_DATATYPE), allocatable  :: tmp1(:), tmp2(:), hvb(:), hvm(:,:)

1344
1345
1346
1347
      ! hvm_dev is fist used and set in this routine
      ! q is changed in trans_ev_tridi on the host, copied to device and passed here. this can be adapted
      ! tmp_dev is first used in this routine
      ! tmat_dev is passed along from bandred_real
Andreas Marek's avatar
Andreas Marek committed
1348
      integer(kind=C_intptr_T)               :: hvm_dev, q_dev, tmp_dev, tmat_dev
1349

Andreas Marek's avatar
Andreas Marek committed
1350
      integer(kind=ik)                       :: i
1351
1352

      real(kind=REAL_DATATYPE), allocatable  :: tmat_complete(:,:), t_tmp(:,:), t_tmp2(:,:)
Andreas Marek's avatar
Andreas Marek committed
1353
1354
1355
1356
1357
      integer(kind=ik)                       :: cwy_blocking, t_blocking, t_cols, t_rows
      logical, intent(in)                    :: useQR, useGPU
      integer(kind=ik)                       :: istat
      character(200)                         :: errorMessage
      logical                                :: successCUDA
1358
1359

#ifdef HAVE_DETAILED_TIMINGS
1360
      call timer%start("trans_ev_band_to_full_real" + M_PRECISSION_SUFFIX)
1361
1362
1363
#endif
#ifdef HAVE_DETAILED_TIMINGS
      call timer%start("mpi_communication")
1364
1365
1366
1367
1368
#endif
      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)
1369
1370
1371
#ifdef HAVE_DETAILED_TIMINGS
      call timer%stop("mpi_communication")
#endif
1372
1373
1374
1375
1376
1377
1378
      max_blocks_row = ((na -1)/nblk)/np_rows + 1  ! Rows of A
      max_blocks_col = ((nqc-1)/nblk)/np_cols + 1  ! Columns of q!

      max_local_rows = max_blocks_row*nblk
      max_local_cols = max_blocks_col*nblk

      if (useGPU) then
Andreas Marek's avatar
Andreas Marek committed
1379

1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
        ! here the GPU and CPU version diverged: the CPU version now always uses the useQR path which
        ! is not implemented in the GPU version
        allocate(tmp1(max_local_cols*nbw), stat=istat, errmsg=errorMessage)
        if (istat .ne. 0) then
          print *,"trans_ev_band_to_full_real: error when allocating tmp1 "//errorMessage
          stop
        endif

        allocate(tmp2(max_local_cols*nbw), stat=istat, errmsg=errorMessage)
        if (istat .ne. 0) then
          print *,"trans_ev_band_to_full_real: error when allocating tmp2 "//errorMessage
          stop
        endif

        allocate(hvb(max_local_rows*nbw), stat=istat, errmsg=errorMessage)
        if (istat .ne. 0) then
          print *,"trans_ev_band_to_full_real: error when allocating hvb "//errorMessage
          stop
        endif

        allocate(hvm(max_local_rows,nbw), stat=istat, errmsg=errorMessage)
        if (istat .ne. 0) then
          print *,"trans_ev_band_to_full_real: error when allocating hvm "//errorMessage
          stop
        endif
1405
        successCUDA = cuda_malloc(hvm_dev, (max_local_rows)*nbw*M_size_of_PRECISSION_real)
1406
1407
1408
1409
        if (.not.(successCUDA)) then
          print *,"trans_ev_band_to_full_real: error in cudaMalloc"
          stop
        endif
1410
        successCUDA = cuda_malloc(tmp_dev, (max_local_cols)*nbw*M_size_of_PRECISSION_real)
1411
1412
1413
1414
        if (.not.(successCUDA)) then
          print *,"trans_ev_band_to_full_real: error in cudaMalloc"
          stop
        endif
1415

1416
1417
!#ifdef WITH_MPI
! it should be possible to keep tmat dev on the device and not copy it around
1418
! already existent on GPU
1419
        successCUDA = cuda_malloc(tmat_dev, nbw*nbw*M_size_of_PRECISSION_real)
1420
1421
1422
1423
        if (.not.(successCUDA)) then
          print *,"trans_ev_band_to_full_real: error in cudaMalloc"
          stop
        endif
1424
1425
1426
!#endif

! q_dev already living on device
1427
!        successCUDA = cuda_malloc(q_dev, ldq*matrixCols*M_size_of_PRECISSION_real)
1428
1429
1430
1431
!        if (.not.(successCUDA)) then
!          print *,"trans_ev_band_to_full_real: error in cudaMalloc"
!          stop
!        endif
1432
1433
1434

  !      q_temp(:,:) = 0.0
  !      q_temp(1:ldq,1:na_cols) = q(1:ldq,1:na_cols)
1435
1436

!        ! copy q_dev to device, maybe this can be avoided if q_dev can be kept on device in trans_ev_tridi_to_band
1437
!        successCUDA = cuda_memcpy(q_dev, loc(q), (ldq)*(matrixCols)*M_size_of_PRECISSION_real, cudaMemcpyHostToDevice)
1438
1439
1440
1441
1442
1443
!        if (.not.(successCUDA)) then
!          print *,"trans_ev_band_to_full_real: error in cudaMalloc"
!          stop
!        endif

        ! if MPI is NOT used the following steps could be done on the GPU and memory transfers could be avoided
1444
        successCUDA = cuda_memset(hvm_dev, 0, (max_local_rows)*(nbw)*M_size_of_PRECISSION_real)
1445
1446
1447
1448
        if (.not.(successCUDA)) then
          print *,"trans_ev_band_to_full_real: error in cudaMalloc"
          stop
        endif
1449
1450
1451
        hvm = M_CONST_0_0   ! Must be set to 0 !!!
        hvb = M_CONST_0_0   ! Safety only
        
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
        l_cols = local_index(nqc, my_pcol, np_cols, nblk, -1) ! Local columns of q

        do istep=1,(na-1)/nbw

          n_cols = MIN(na,(istep+1)*nbw) - istep*nbw ! Number of columns in current step

          ! Broadcast all Householder vectors for current step compressed in hvb

          nb = 0
          ns = 0

          do lc = 1, n_cols
            ncol = istep*nbw + lc ! absolute column number of householder vector
            nrow = ncol - nbw ! absolute number of pivot row

            l_rows = local_index(nrow-1, my_prow, np_rows, nblk, -1) ! row length for bcast
            l_colh = local_index(ncol  , my_pcol, np_cols, nblk, -1) ! HV local column number

            if (my_pcol==pcol(ncol, nblk, np_cols)) hvb(nb+1:nb+l_rows) = a(1:l_rows,l_colh)

            nb = nb+l_rows

            if (lc==n_cols .or. mod(ncol,nblk)==0) then
#ifdef WITH_MPI
1476
1477
1478
#ifdef HAVE_DETAILED_TIMINGS
              call timer%start("mpi_communication")
#endif
1479
              call MPI_Bcast(hvb(ns+1), nb-ns, M_MPI_REAL_PRECISSION, pcol(ncol, nblk, np_cols), mpi_comm_cols, mpierr)
1480
1481
1482
#ifdef HAVE_DETAILED_TIMINGS
              call timer%stop("mpi_communication")
#endif
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496

#endif /* WITH_MPI */
              ns = nb
            endif
          enddo

          ! Expand compressed Householder vectors into matrix hvm

          nb = 0
          do lc = 1, n_cols
            nrow = (istep-1)*nbw+lc ! absolute number of pivot row
            l_rows = local_index(nrow-1, my_prow, np_rows, nblk, -1) ! row length for bcast

            hvm(1:l_rows,lc) = hvb(nb+1:nb+l_rows)
1497
            if (my_prow==prow(nrow, nblk, np_rows)) hvm(l_rows+1,lc) = M_CONST_1_0
1498
1499
1500

            nb = nb+l_rows
          enddo
1501
          successCUDA = cuda_memcpy(hvm_dev, loc(hvm), ((max_local_rows)*nbw*M_size_of_PRECISSION_real),cudaMemcpyHostToDevice)
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513

          if (.not.(successCUDA)) then
            print *,"trans_ev_band_to_full_real: error in cudaMemcpy"
            stop

          endif

          l_rows = local_index(MIN(na,(istep+1)*nbw), my_prow, np_rows, nblk, -1)

          ! Q = Q - V * T**T * V**T * Q

          if (l_rows>0) then
1514
1515
            call M_cublas_PRECISSION_gemm('T', 'N', n_cols, l_cols, l_rows, M_CONST_1_0, hvm_dev, max_local_rows, &
                              q_dev, ldq , M_CONST_0_0, tmp_dev, n_cols)
1516

1517
1518
1519
1520
#ifdef WITH_MPI
            ! copy data from device to host for a later MPI_ALLREDUCE

            ! copy to host maybe this can be avoided this is needed if MPI is used (allreduce)
1521
            successCUDA = cuda_memcpy(loc(tmp1), tmp_dev, l_cols*n_cols*M_size_of_PRECISSION_real, cudaMemcpyDeviceToHost)
1522
1523
1524
1525
            if (.not.(successCUDA)) then
              print *,"trans_ev_band_to_full_real: error in cudaMemcpy"
              stop
            endif
1526
#endif /* WITH_MPI */
1527

1528
1529
1530
          else ! l_rows>0

#ifdef WITH_MPI
1531
            tmp1(1:l_cols*n_cols) = 0
1532
1533
#else

1534
            ! if MPI is not used (we do not need to transfer because of MPI_ALLREDUCE) we can set to zero on the device
1535
1536
#ifdef WITH_GPU_VERSION

1537
            successCUDA = cuda_memset(tmp_dev, 0, l_cols*n_cols*M_size_of_PRECISSION_real)
1538
1539
1540
1541
1542
1543
1544
1545
1546
            if (.not.(successCUDA)) then
              print *,"trans_ev_band_to_full_real: error in cudaMemset"
              stop
             endif
#endif

#endif /* WITH_MPI */

          endif ! l_rows>0
1547
1548
1549
1550
1551
1552
1553
1554
1555

          !#ifdef WITH_GPU_VERSION
          !       istat = cuda_memcpy(loc(tmp1), tmp_dev, max_local_cols*nbw*size_of_real_datatype,cudaMemcpyDeviceToHost)
          !       if (istat .ne. 0) then
          !         print *,"error in cudaMemcpy"
          !         stop
          !       endif
          !#endif
#ifdef WITH_MPI
1556
1557
1558
#ifdef HAVE_DETAILED_TIMINGS
          call timer%start("mpi_communication")
#endif
1559
          call mpi_allreduce(tmp1, tmp2, n_cols*l_cols, M_MPI_REAL_PRECISSION, MPI_SUM, mpi_comm_rows, mpierr)
1560
1561
1562
#ifdef HAVE_DETAILED_TIMINGS
          call timer%stop("mpi_communication")
#endif
1563
#else /* WITH_MPI */
1564
!          tmp2(1:n_cols*l_cols) = tmp1(1:n_cols*l_cols)
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
#endif /* WITH_MPI */
          !#ifdef WITH_GPU_VERSION
          !       istat = cuda_memcpy(tmp_dev, loc(tmp2), max_local_cols*nbw*size_of_real_datatype,cudaMemcpyHostToDevice)
          !       if (istat .ne. 0) then
          !         print *,"error in cudaMemcpy"
          !         stop
          !       endif
          !#endif

          if (l_rows>0) then
1575
#ifdef WITH_MPI
1576
            ! after the mpi_allreduce we have to copy back to the device
1577
            ! copy back to device
1578
            successCUDA = cuda_memcpy(tmp_dev, loc(tmp2), n_cols*l_cols*M_size_of_PRECISSION_real,cudaMemcpyHostToDevice)
1579
1580
1581
1582
            if (.not.(successCUDA)) then
              print *,"trans_ev_band_to_full_real: error in cudaMemcpy"
              stop
            endif
1583
1584
#endif /* WITH_MPI */

1585
1586
!#ifdef WITH_MPI
! it should be possible to keep tmat on the device and not copy it aroud
1587
!            ! copy to device, maybe this can be avoided tmat is input from bandred_real
1588

1589
            successCUDA = cuda_memcpy(tmat_dev, loc(tmat(1,1,istep)), nbw*nbw*M_size_of_PRECISSION_real,cudaMemcpyHostToDevice)
1590
1591
1592
1593
1594
1595
            if (.not.(successCUDA)) then
              print *,"trans_ev_band_to_full_real: error in cudaMemcpy"
              stop
            endif
!#endif /* WITH_MPI */

1596
1597
1598
            call M_cublas_PRECISSION_trmm('L', 'U', 'T', 'N', n_cols, l_cols, M_CONST_1_0, tmat_dev, nbw, tmp_dev, n_cols)
            call M_cublas_PRECISSION_gemm('N', 'N', l_rows, l_cols, n_cols, -M_CONST_1_0, hvm_dev, max_local_rows, &
                              tmp_dev, n_cols, M_CONST_1_0, q_dev, ldq)
1599

1600
1601
            ! copy to host maybe this can be avoided
            ! this is not necessary hvm is not used anymore
1602
            successCUDA = cuda_memcpy(loc(hvm), hvm_dev, ((max_local_rows)*nbw*M_size_of_PRECISSION_real),cudaMemcpyDeviceToHost)
1603
1604
1605
1606
            if (.not.(successCUDA)) then
              print *,"trans_ev_band_to_full_real: error in cudaMemcpy"
              stop
            endif
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640

          endif ! l_rows > 0
          !#ifdef WITH_GPU_VERSION
          !       istat = cuda_memcpy(loc(hvm), hvm_dev, ((max_local_rows)*nbw*size_of_real_datatype),cudaMemcpyDeviceToHost)
          !       if (istat .ne. 0) then
          !         print *,"error in cudaMemcpy"
          !         stop
          !       endif
          !
          !#endif
        enddo ! istep

      else ! do not useGPU

        ! t_blocking was formerly 2; 3 is a better choice
        t_blocking = 3 ! number of matrices T (tmat) which are aggregated into a new (larger) T matrix (tmat_complete) and applied at once

        ! we only use the t_blocking if we could call it fully, this is might be better but needs to benchmarked.
!       if ( na >= ((t_blocking+1)*nbw) ) then
        cwy_blocking = t_blocking * nbw

        allocate(tmp1(max_local_cols*cwy_blocking))
        allocate(tmp2(max_local_cols*cwy_blocking))
        allocate(hvb(max_local_rows*cwy_blocking))
        allocate(hvm(max_local_rows,cwy_blocking))
        allocate(tmat_complete(cwy_blocking,cwy_blocking))
        allocate(t_tmp(cwy_blocking,nbw))
        allocate(t_tmp2(cwy_blocking,nbw))
!        else
!          allocate(tmp1(max_local_cols*nbw))
!          allocate(tmp2(max_local_cols*nbw))
!          allocate(hvb(max_local_rows*nbw))
!          allocate(hvm(max_local_rows,nbw))
!        endif
1641
1642
        hvm = M_CONST_0_0   ! Must be set to 0 !!!
        hvb = M_CONST_0_0   ! Safety only
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
        l_cols = local_index(nqc, my_pcol, np_cols, nblk, -1) ! Local columns of q

!       if ( na >= ((t_blocking+1)*nbw) ) then

        do istep=1,((na-1)/nbw-1)/t_blocking + 1
          ! This the call when using  na >= ((t_blocking+1)*nbw)
          !      n_cols = MIN(na,istep*cwy_blocking+nbw) - (istep-1)*cwy_blocking - nbw ! Number of columns in current step
          ! As an alternative we add some special case handling if na < cwy_blocking
          IF (na < cwy_blocking) THEN
            n_cols = MAX(0, na-nbw)
            IF ( n_cols .eq. 0 ) THEN
              EXIT
            END IF
          ELSE
            n_cols = MIN(na,istep*cwy_blocking+nbw) - (istep-1)*cwy_blocking - nbw ! Number of columns in current step
          END IF
          ! Broadcast all Householder vectors for current step compressed in hvb

          nb = 0
          ns = 0

          do lc = 1, n_cols
            ncol = (istep-1)*cwy_blocking + nbw + lc ! absolute column number of householder vector
            nrow = ncol - nbw ! absolute number of pivot row

            l_rows = local_index(nrow-1, my_prow, np_rows, nblk, -1) ! row length for bcast
            l_colh = local_index(ncol  , my_pcol, np_cols, nblk, -1) ! HV local column number

            if (my_pcol==pcol(ncol, nblk, np_cols)) hvb(nb+1:nb+l_rows) = a(1:l_rows,l_colh)

            nb = nb+l_rows

            if (lc==n_cols .or. mod(ncol,nblk)==0) then
#ifdef WITH_MPI
1677
1678
1679
#ifdef HAVE_DETAILED_TIMINGS
              call timer%start("mpi_communication")
#endif
1680
              call MPI_Bcast(hvb(ns+1), nb-ns, M_MPI_REAL_PRECISSION, pcol(ncol, nblk, np_cols), mpi_comm_cols, mpierr)
1681
1682
1683
#ifdef HAVE_DETAILED_TIMINGS
              call timer%stop("mpi_communication")
#endif
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697

#endif /* WITH_MPI */
              ns = nb
            endif
          enddo

          ! Expand compressed Householder vectors into matrix hvm

          nb = 0
          do lc = 1, n_cols
            nrow = (istep-1)*cwy_blocking + lc ! absolute number of pivot row
            l_rows = local_index(nrow-1, my_prow, np_rows, nblk, -1) ! row length for bcast

            hvm(1:l_rows,lc) = hvb(nb+1:nb+l_rows)
1698
            if (my_prow==prow(nrow, nblk, np_rows)) hvm(l_rows+1,lc) = M_CONST_1_0
1699
1700
1701
1702
1703
1704

            nb = nb+l_rows
          enddo

          l_rows = local_index(MIN(na,(istep+1)*cwy_blocking), my_prow, np_rows, nblk, -1)

1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
          ! compute tmat2 out of tmat(:,:,)
          tmat_complete = 0
          do i = 1, t_blocking
            t_cols = MIN(nbw, n_cols - (i-1)*nbw)
            if (t_cols <= 0) exit
            t_rows = (i - 1) * nbw
            tmat_complete(t_rows+1:t_rows+t_cols,t_rows+1:t_rows+t_cols) = tmat(1:t_cols,1:t_cols,(istep-1)*t_blocking + i)
            if (i > 1) then
              call M_PRECISSION_GEMM('T', 'N', t_rows, t_cols, l_rows, M_CONST_1_0, hvm(1,1), max_local_rows, hvm(1,(i-1)*nbw+1), &
                        max_local_rows, M_CONST_0_0, t_tmp, cwy_blocking)
1715
#ifdef WITH_MPI
1716
1717
1718
1719
#ifdef HAVE_DETAILED_TIMINGS
              call timer%start("mpi_communication")
#endif

1720
              call mpi_allreduce(t_tmp, t_tmp2, cwy_blocking*nbw, M_MPI_REAL_PRECISSION, MPI_SUM, mpi_comm_rows, mpierr)