elpa2_trans_ev_tridi_to_band_template.X90 94.4 KB
Newer Older
1
2
3
4
    subroutine trans_ev_tridi_to_band_&
    &MATH_DATATYPE&
    &_&
    &PRECISION &
Andreas Marek's avatar
Andreas Marek committed
5
    (na, nev, nblk, nbw, q, q_dev, ldq, matrixCols,         &
6
     hh_trans, mpi_comm_rows, mpi_comm_cols, wantDebug, useGPU, success, &
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#if REALCASE == 1
    THIS_REAL_ELPA_KERNEL)
#endif
#if COMPLEXCASE == 1
    THIS_COMPLEX_ELPA_KERNEL)
#endif

    !-------------------------------------------------------------------------------
    !  trans_ev_tridi_to_band_real/complex:
    !  Transforms the eigenvectors of a tridiagonal matrix back to the eigenvectors of the band matrix
    !
    !  Parameters
    !
    !  na          Order of matrix a, number of rows of matrix q
    !
    !  nev         Number eigenvectors to compute (= columns of matrix q)
    !
    !  nblk        blocksize of cyclic distribution, must be the same in both directions!
    !
    !  nb          semi bandwith
    !
    !  q           On input: Eigenvectors of tridiagonal matrix
    !              On output: Transformed eigenvectors
    !              Distribution is like in Scalapack.
    !
    !  q_dev       GPU device pointer to q
    !
    !  ldq         Leading dimension of q
    !  matrixCols  local columns of matrix q
    !
    !  mpi_comm_rows
    !  mpi_comm_cols
    !              MPI-Communicators for rows/columns/both
    !
    !-------------------------------------------------------------------------------
#ifdef HAVE_DETAILED_TIMINGS
      use timings
#else
      use timings_dummy
#endif
      use elpa2_workload
#if REALCASE == 1
      use pack_unpack_real
      use pack_unpack_real_gpu
      use compute_hh_trafo_real

#endif
#if COMPLEXCASE == 1
      use pack_unpack_complex
      use compute_hh_trafo_complex
#endif
      use cuda_functions
      use precision
      use iso_c_binding
      implicit none
      logical, intent(in)                      :: useGPU

#if REALCASE == 1
      integer(kind=ik), intent(in)             :: THIS_REAL_ELPA_KERNEL
#endif
#if COMPLEXCASE == 1
      integer(kind=ik), intent(in)             :: THIS_COMPLEX_ELPA_KERNEL
#endif
      integer(kind=ik), intent(in)             :: na, nev, nblk, nbw, ldq, matrixCols, mpi_comm_rows, mpi_comm_cols

#if REALCASE == 1
#ifdef USE_ASSUMED_SIZE
      real(kind=REAL_DATATYPE)                 :: q(ldq,*)
#else
      real(kind=REAL_DATATYPE)                 :: q(ldq,matrixCols)
#endif

79
      real(kind=REAL_DATATYPE), intent(in)     :: hh_trans(:,:)
80
#endif
Andreas Marek's avatar
Andreas Marek committed
81
82
      integer(kind=c_intptr_t)                 :: q_dev

83
84
85
86
87
88
#if COMPLEXCASE == 1
#ifdef USE_ASSUMED_SIZE
      complex(kind=COMPLEX_DATATYPE)           :: q(ldq,*)
#else
      complex(kind=COMPLEX_DATATYPE)           :: q(ldq,matrixCols)
#endif
89
      complex(kind=COMPLEX_DATATYPE)           :: hh_trans(:,:)
90
91
92
93
94
95
96
97
98
99
100
101
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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
#endif

      integer(kind=ik)                         :: np_rows, my_prow, np_cols, my_pcol
      integer(kind=ik)                         :: tmp

      integer(kind=ik)                         :: i, j, ip, sweep, nbuf, l_nev, a_dim2
      integer(kind=ik)                         :: current_n, current_local_n, current_n_start, current_n_end
      integer(kind=ik)                         :: next_n, next_local_n, next_n_start, next_n_end
      integer(kind=ik)                         :: bottom_msg_length, top_msg_length, next_top_msg_length
      integer(kind=ik)                         :: stripe_width, last_stripe_width, stripe_count
#ifdef WITH_OPENMP
      integer(kind=ik)                         :: thread_width, csw, b_off, b_len
#endif
      integer(kind=ik)                         :: num_result_blocks, num_result_buffers, num_bufs_recvd
      integer(kind=ik)                         :: a_off, current_tv_off, max_blk_size
      integer(kind=ik)                         :: mpierr, src, src_offset, dst, offset, nfact, num_blk

      logical                                  :: flag
#if REALCASE == 1
#ifdef WITH_OPENMP
      real(kind=REAL_DATATYPE), pointer        :: aIntern(:,:,:,:)
#else
      real(kind=REAL_DATATYPE), pointer        :: aIntern(:,:,:)
#endif
      real(kind=REAL_DATATYPE)                 :: a_real
#endif

#if COMPLEXCASE == 1
#ifdef WITH_OPENMP
      complex(kind=COMPLEX_DATATYPE), pointer  :: aIntern(:,:,:,:)
#else
      complex(kind=COMPLEX_DATATYPE), pointer  :: aIntern(:,:,:)
#endif
      complex(kind=COMPLEX_DATATYPE)           :: a_complex
#endif
      type(c_ptr)                              :: aIntern_ptr

#if REALCASE == 1
      real(kind=REAL_DATATYPE)   , allocatable :: row(:)
      real(kind=REAL_DATATYPE)   , allocatable :: row_group(:,:)
#endif
#if COMPLEXCASE == 1
      complex(kind=COMPLEX_DATATYPE), allocatable :: row(:)
      complex(kind=COMPLEX_DATATYPE), allocatable :: row_group(:,:)
#endif

#if REALCASE == 1
#ifdef WITH_OPENMP
      real(kind=REAL_DATATYPE), allocatable    :: top_border_send_buffer(:,:), top_border_recv_buffer(:,:)
      real(kind=REAL_DATATYPE), allocatable    :: bottom_border_send_buffer(:,:), bottom_border_recv_buffer(:,:)
#else
      real(kind=REAL_DATATYPE), allocatable    :: top_border_send_buffer(:,:,:), top_border_recv_buffer(:,:,:)
      real(kind=REAL_DATATYPE), allocatable    :: bottom_border_send_buffer(:,:,:), bottom_border_recv_buffer(:,:,:)
#endif
#endif

#if COMPLEXCASE == 1
#ifdef WITH_OPENMP
      complex(kind=COMPLEX_DATATYPE), allocatable :: top_border_send_buffer(:,:), top_border_recv_buffer(:,:)
      complex(kind=COMPLEX_DATATYPE), allocatable :: bottom_border_send_buffer(:,:), bottom_border_recv_buffer(:,:)
#else
      complex(kind=COMPLEX_DATATYPE), allocatable :: top_border_send_buffer(:,:,:), top_border_recv_buffer(:,:,:)
      complex(kind=COMPLEX_DATATYPE), allocatable :: bottom_border_send_buffer(:,:,:), bottom_border_recv_buffer(:,:,:)
#endif
#endif

      integer(kind=c_intptr_t)                 :: aIntern_dev
      integer(kind=c_intptr_t)                 :: bcast_buffer_dev
      integer(kind=c_size_t)                   :: num
      integer(kind=c_size_t)                   :: dev_offset, dev_offset_1, dev_offset_2
      integer(kind=c_intptr_t)                 :: row_dev
      integer(kind=c_intptr_t)                 :: row_group_dev
      integer(kind=c_intptr_t)                 :: hh_tau_dev
      integer(kind=c_intptr_t)                 :: hh_dot_dev
      integer(kind=ik)                         :: row_group_size, unpack_idx

#if COMPLEXCASE == 1
      integer(kind=ik)                         :: n_times
#endif
      integer(kind=ik)                         :: top, chunk, this_chunk

#if REALCASE == 1
      real(kind=REAL_DATATYPE), allocatable    :: result_buffer(:,:,:)
      real(kind=REAL_DATATYPE), allocatable    :: bcast_buffer(:,:)
#endif
#if COMPLEXCASE == 1
      complex(kind=COMPLEX_DATATYPE), allocatable :: result_buffer(:,:,:)
      complex(kind=COMPLEX_DATATYPE), allocatable :: bcast_buffer(:,:)
#endif


      integer(kind=ik)                         :: n_off

      integer(kind=ik), allocatable            :: result_send_request(:), result_recv_request(:), limits(:)
      integer(kind=ik), allocatable            :: top_send_request(:), bottom_send_request(:)
      integer(kind=ik), allocatable            :: top_recv_request(:), bottom_recv_request(:)
#ifdef WITH_OPENMP
!      integer(kind=ik), allocatable           :: mpi_statuses(:,:)
#endif

#ifdef WITH_OPENMP
#ifdef WITH_MPI
!      integer(kind=ik)                        :: my_MPI_STATUS_(MPI_STATUS_SIZE)
#endif
#endif

#if COMPLEXCASE == 1
#ifdef WITH_MPI
      integer(kind=ik), external               :: numroc
#endif
      integer(kind=ik)                         :: na_rows, na_cols
#endif

      ! MPI send/recv tags, arbitrary

      integer(kind=ik), parameter              :: bottom_recv_tag = 111
      integer(kind=ik), parameter              :: top_recv_tag    = 222
      integer(kind=ik), parameter              :: result_recv_tag = 333
#ifdef WITH_OPENMP
      integer(kind=ik)                         :: max_threads, my_thread
      integer(kind=ik)                         :: omp_get_max_threads
#endif


      ! Just for measuring the kernel performance
      real(kind=c_double)                      :: kernel_time, kernel_time_recv ! MPI_WTIME always needs double
      ! long integer
      integer(kind=lik)                        :: kernel_flops, kernel_flops_recv



      logical, intent(in)                      :: wantDebug
      logical                                  :: success
      integer(kind=ik)                         :: istat
      character(200)                           :: errorMessage
      logical                                  :: successCUDA
#ifndef WITH_MPI
      integer(kind=ik)                         :: j1
#endif

      call timer%start("trans_ev_tridi_to_band_&
      &MATH_DATATYPE&
Andreas Marek's avatar
Cleanup    
Andreas Marek committed
232
      &" // &
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
      &PRECISION_SUFFIX &
      )

      if (useGPU) then
#if COMPLEXCASE == 1
        n_times = 0
#endif
        unpack_idx = 0
        row_group_size = 0
      endif

      success = .true.
      kernel_time = 0.0
      kernel_flops = 0

#ifdef WITH_OPENMP
      max_threads = 1
      max_threads = omp_get_max_threads()
#endif
      call timer%start("mpi_communication")
      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)
      call timer%stop("mpi_communication")

#if COMPLEXCASE == 1
      if (useGPU) then
#ifdef WITH_MPI
        na_rows = numroc(na, nblk, my_prow, 0, np_rows)
        na_cols = numroc(na, nblk, my_pcol, 0, np_cols)
#else
        na_rows = na
        na_cols = na
#endif
      endif
Andreas Marek's avatar
Cleanup    
Andreas Marek committed
269
#endif /* COMPLEXCASE */
270
271
272
273
274
275
276
277
278
279
280
281
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
342
343
344
345
346
347
348
349
350
351
352
353
354

      if (mod(nbw,nblk)/=0) then
        if (my_prow==0 .and. my_pcol==0) then
          if (wantDebug) then
            write(error_unit,*) 'ELPA2_trans_ev_tridi_to_band_&
	    &MATH_DATATYPE&
	    &: ERROR: nbw=',nbw,', nblk=',nblk
            write(error_unit,*) 'ELPA2_trans_ev_tridi_to_band_&
	    &MATH_DATATYPE&
	    &: band backtransform works only for nbw==n*nblk'
          endif
          success = .false.
          return
        endif
      endif

      nfact = nbw / nblk


      ! local number of eigenvectors
      l_nev = local_index(nev, my_pcol, np_cols, nblk, -1)

      if (l_nev==0) then
#ifdef WITH_OPENMP
        thread_width = 0
#endif
        stripe_width = 0
        stripe_count = 0
        last_stripe_width = 0

      else ! l_nev

#if WITH_OPENMP
        ! Suggested stripe width is 48 since 48*64 real*8 numbers should fit into
        ! every primary cache
	! Suggested stripe width is 48 - should this be reduced for the complex case ???

        if (useGPU) then
          stripe_width = 256 ! Must be a multiple of 4
          stripe_count = (l_nev - 1) / stripe_width + 1
        else ! useGPU
          ! openmp only in non-GPU case
          thread_width = (l_nev-1)/max_threads + 1 ! number of eigenvectors per OMP thread
#if REALCASE == 1
#ifdef DOUBLE_PRECISION_REAL
          stripe_width = 48 ! Must be a multiple of 4
#else
          stripe_width = 96 ! Must be a multiple of 8
#endif
#endif /* REALCASE */

#if COMPLEXCASE == 1
#ifdef DOUBLE_PRECISION_COMPLEX
          stripe_width = 48 ! Must be a multiple of 2
#else
          stripe_width = 48 ! Must be a multiple of 4
#endif
#endif /* COMPLEXCASE */

          stripe_count = (thread_width-1)/stripe_width + 1

          ! Adapt stripe width so that last one doesn't get too small

          stripe_width = (thread_width-1)/stripe_count + 1

#if REALCASE == 1
#ifdef DOUBLE_PRECISION_REAL
          stripe_width = ((stripe_width+3)/4)*4 ! Must be a multiple of 4 because of AVX/SSE memory alignment of 32 bytes
	  					! (4 * sizeof(double) == 32)
#else
          stripe_width = ((stripe_width+7)/8)*8 ! Must be a multiple of 8 because of AVX/SSE memory alignment of 32 bytes
	  					! (8 * sizeof(float) == 32)
#endif
#endif /* REALCASE */

#if COMPLEXCASE == 1
#ifdef DOUBLE_PRECISION_COMPLEX
          stripe_width = ((stripe_width+3)/4)*4 ! Must be a multiple of 2 because of AVX/SSE memory alignment of 32 bytes
	  					! (2 * sizeof(double complex) == 32)
#else
          stripe_width = ((stripe_width+3)/4)*4 ! Must be a multiple of 4 because of AVX/SSE memory alignment of 32 bytes
	  					! (4 * sizeof(float complex) == 32)
#endif
#endif /* COMPLEXCASE */

355
#if REALCASE == 1
356
        last_stripe_width = l_nev - (stripe_count-1)*stripe_width
357
358
359
360
361
#endif
#if COMPLEXCASE == 1
! only needed in no OMP case check thsis
! last_stripe_width = l_nev - (stripe_count-1)*stripe_width
#endif
362
363

        endif ! useGPU
Andreas Marek's avatar
Cleanup    
Andreas Marek committed
364

365
#else /* WITH_OPENMP */
Andreas Marek's avatar
Cleanup    
Andreas Marek committed
366

367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
        ! Suggested stripe width is 48 since 48*64 real*8 numbers should fit into
        ! every primary cache
	! Suggested stripe width is 48 - should this be reduced for the complex case ???


        if (useGPU) then
          stripe_width = 256 ! Must be a multiple of 4
          stripe_count = (l_nev - 1) / stripe_width + 1

        else ! useGPU
#if REALCASE == 1
#ifdef DOUBLE_PRECISION_REAL
          stripe_width = 48 ! Must be a multiple of 4
#else
          stripe_width = 96 ! Must be a multiple of 8
#endif
#endif /* REALCASE */

#if COMPLEXCASE == 1
#ifdef DOUBLE_PRECISION_COMPLEX
          stripe_width = 48 ! Must be a multiple of 2
#else
          stripe_width = 48 ! Must be a multiple of 4
#endif
#endif /* COMPLEXCASE */

          stripe_count = (l_nev-1)/stripe_width + 1

          ! Adapt stripe width so that last one doesn't get too small

          stripe_width = (l_nev-1)/stripe_count + 1

#if REALCASE == 1
#ifdef DOUBLE_PRECISION_REAL
          stripe_width = ((stripe_width+3)/4)*4 ! Must be a multiple of 4 because of AVX/SSE memory alignment of 32 bytes
	  					! (4 * sizeof(double) == 32)
#else
          stripe_width = ((stripe_width+7)/8)*8 ! Must be a multiple of 8 because of AVX/SSE memory alignment of 32 bytes
	  					! (8 * sizeof(float) == 32)
#endif
#endif /* REALCASE */

#if COMPLEXCASE == 1
#ifdef DOUBLE_PRECISION_COMPLEX
          stripe_width = ((stripe_width+3)/4)*4 ! Must be a multiple of 2 because of AVX/SSE memory alignment of 32 bytes
	  					! (2 * sizeof(double complex) == 32)
#else
          stripe_width = ((stripe_width+3)/4)*4 ! Must be a multiple of 4 because of AVX/SSE memory alignment of 32 bytes
	  					! (4 * sizeof(float complex) == 32)
#endif
#endif /* COMPLEXCASE */
        endif ! useGPU

        last_stripe_width = l_nev - (stripe_count-1)*stripe_width

#endif /* WITH_OPENMP */
      endif ! l_nev

      ! Determine the matrix distribution at the beginning

      allocate(limits(0:np_rows), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
        print *,"trans_ev_tridi_to_band_&
	&MATH_DATATYPE&
	&: error when allocating limits"//errorMessage
        stop
      endif
      call determine_workload(na, nbw, np_rows, limits)

      max_blk_size = maxval(limits(1:np_rows) - limits(0:np_rows-1))

      a_dim2 = max_blk_size + nbw

      if (useGPU) then
        num =  (stripe_width*a_dim2*stripe_count)*   &
#if REALCASE == 1
                size_of_PRECISION_real
#endif
#if COMPLEXCASE == 1
                size_of_PRECISION_complex
#endif
        successCUDA = cuda_malloc(aIntern_dev, stripe_width*a_dim2*stripe_count*   &
#if REALCASE == 1
                                  size_of_PRECISION_real)
#endif
#if COMPLEXCASE == 1
                                  size_of_PRECISION_complex)
#endif
        if (.not.(successCUDA)) then
          print *,"trans_ev_tridi_to_band_&
          &MATH_DATATYPE&
          &: error in cudaMalloc"//errorMessage
          stop
        endif

        successCUDA = cuda_memset(aIntern_dev , 0, num)
        if (.not.(successCUDA)) then
          print *,"trans_ev_tridi_to_band_&
          &MATH_DATATYPE&
          &: error in cudaMemset"//errorMessage
          stop
        endif

        num =  (l_nev)*    &
#if REALCASE == 1
                size_of_PRECISION_real
#endif
#if COMPLEXCASE == 1
                size_of_PRECISION_complex
#endif
        successCUDA = cuda_malloc( row_dev,num)
        if (.not.(successCUDA)) then
          print *,"trans_ev_tridi_to_band_&
          &MATH_DATATYPE&
          &: error in cudaMalloc "
          stop
        endif

        successCUDA = cuda_memset(row_dev , 0, num)
        if (.not.(successCUDA)) then
          print *,"trans_ev_tridi_to_band_&
          &MATH_DATATYPE&
          &: error in cudaMemset "
          stop
        endif

        ! "row_group" and "row_group_dev" are needed for GPU optimizations
        allocate(row_group(l_nev, nblk), stat=istat, errmsg=errorMessage)
        if (istat .ne. 0) then
          print *,"trans_ev_tridi_to_band_&
          &MATH_DATATYPE&
          &: error when allocating row_group"//errorMessage
          stop
        endif

#if REALCASE == 1
        row_group(:, :) = CONST_0_0
#endif
#if COMPLEXCASE == 1
        row_group(:, :) = CONST_COMPLEX_0_0
#endif

        num =  (l_nev*nblk)*    &
#if REALCASE == 1
               size_of_PRECISION_real
#endif
#if COMPLEXCASE == 1
               size_of_PRECISION_complex
#endif
        successCUDA = cuda_malloc(row_group_dev, num)
        if (.not.(successCUDA)) then
          print *,"trans_ev_tridi_to_band_&
          &MATH_DATATYPE&
          &: error in cudaMalloc"//errorMessage
          stop
        endif

        successCUDA = cuda_memset(row_group_dev , 0, num)
        if (.not.(successCUDA)) then
          print *,"trans_ev_tridi_to_band_&
          &MATH_DATATYPE&
          &: error in cudaMemset"//errorMessage
          stop
        endif

      else ! GPUs are not used

#if 0
! realcase or complexcase
!DEC$ ATTRIBUTES ALIGN: 64:: aIntern
#endif

#ifdef WITH_OPENMP
        if (posix_memalign(aIntern_ptr, 64_C_SIZE_T, stripe_width*a_dim2*stripe_count*max_threads*     &
#if REALCASE == 1
               C_SIZEOF(a_real)) /= 0) then
#endif
#if COMPLEXCASE == 1
               C_SIZEOF(a_complex)) /= 0) then
#endif
          print *,"trans_ev_tridi_to_band_&
          &MATH_DATATYPE&
Andreas Marek's avatar
Cleanup    
Andreas Marek committed
549
          &: error when allocating aIntern"//errorMessage
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
          stop
        endif

        call c_f_pointer(aIntern_ptr, aIntern, [stripe_width,a_dim2,stripe_count,max_threads])
        ! allocate(aIntern(stripe_width,a_dim2,stripe_count,max_threads), stat=istat, errmsg=errorMessage)

        ! aIntern(:,:,:,:) should be set to 0 in a parallel region, not here!
#else
        if (posix_memalign(aIntern_ptr, 64_C_SIZE_T, stripe_width*a_dim2*stripe_count*  &
#if REALCASE == 1
            C_SIZEOF(a_real)) /= 0) then
#endif
#if COMPLEXCASE == 1
            C_SIZEOF(a_complex)) /= 0) then
#endif
          print *,"trans_ev_tridi_to_band_real: error when allocating aIntern"//errorMessage
          stop
        endif

        call c_f_pointer(aIntern_ptr, aIntern,[stripe_width,a_dim2,stripe_count] )
        !allocate(aIntern(stripe_width,a_dim2,stripe_count), stat=istat, errmsg=errorMessage)

#if REALCASE == 1
        aIntern(:,:,:) = CONST_0_0
#endif
#if COMPLEXCASE == 1
        aIntern(:,:,:) = 0
#endif
#endif /* WITH_OPENMP */
      endif !useGPU

      allocate(row(l_nev), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
        print *,"trans_ev_tridi_to_band_&
        &MATH_DATATYPE&
        &: error when allocating row"//errorMessage
        stop
      endif

#if REALCASE == 1
      row(:) = CONST_0_0
#endif
#if COMPLEXCASE == 1
      row(:) = 0
#endif


      ! Copy q from a block cyclic distribution into a distribution with contiguous rows,
      ! and transpose the matrix using stripes of given stripe_width for cache blocking.

      ! The peculiar way it is done below is due to the fact that the last row should be
      ! ready first since it is the first one to start below

#ifdef WITH_OPENMP
      ! Please note about the OMP usage below:
      ! This is not for speed, but because we want the matrix a in the memory and
      ! in the cache of the correct thread (if possible)

      call timer%start("OpenMP parallel" // PRECISION_SUFFIX)
      !$omp parallel do private(my_thread), schedule(static, 1)
      do my_thread = 1, max_threads
#if REALCASE == 1
        aIntern(:,:,:,my_thread) = CONST_0_0 ! if possible, do first touch allocation!
#endif
#if COMPLEXCASE == 1
        aIntern(:,:,:,my_thread) = CONST_COMPLEX_0_0 ! if possible, do first touch allocation!
#endif
      enddo
      !$omp end parallel do

      call timer%stop("OpenMP parallel" // PRECISION_SUFFIX)
#endif /* WITH_OPENMP */

      do ip = np_rows-1, 0, -1
        if (my_prow == ip) then
          ! Receive my rows which have not yet been received
          src_offset = local_index(limits(ip), my_prow, np_rows, nblk, -1)
          do i=limits(ip)+1,limits(ip+1)
            src = mod((i-1)/nblk, np_rows)

            if (src < my_prow) then
#ifdef WITH_OPENMP

#ifdef WITH_MPI
              call timer%start("mpi_communication")
              call MPI_Recv(row, l_nev,      &
#if REALCASE == 1
                            MPI_REAL_PRECISION, &
#endif
#if COMPLEXCASE == 1
                            MPI_COMPLEX_EXPLICIT_PRECISION, &
#endif
                            src, 0, mpi_comm_rows, MPI_STATUS_IGNORE, mpierr)
              call timer%stop("mpi_communication")

#else /* WITH_MPI */

!              row(1:l_nev) = row(1:l_nev)

#endif /* WITH_MPI */

              call timer%start("OpenMP parallel" // PRECISION_SUFFIX)

!$omp parallel do private(my_thread), schedule(static, 1)
              do my_thread = 1, max_threads
655
656
657
658
659
660
661
	        call unpack_row_&
&MATH_DATATYPE&
&_cpu_openmp_&
&PRECISION &
                                  (aIntern, row, i-limits(ip), my_thread, stripe_count, &
                                   thread_width, stripe_width, l_nev)

662
663
664
665
666
667
668
669
              enddo
!$omp end parallel do

              call timer%stop("OpenMP parallel" // PRECISION_SUFFIX)

#else /* WITH_OPENMP */
              if (useGPU) then
                ! An unpacking of the current row group may occur before queuing the next row
Andreas Marek's avatar
Andreas Marek committed
670
671
672
673
674
		call unpack_and_prepare_row_group_&
		&MATH_DATATYPE&
		&_gpu_&
		&PRECISION &
		                          ( &
675
#if REALCASE == 1
Andreas Marek's avatar
Andreas Marek committed
676
677
678
		                          row_group, row_group_dev, aIntern_dev, stripe_count, &
                                          stripe_width, last_stripe_width, a_dim2, l_nev,&
                                          row_group_size, nblk, unpack_idx, &
679
#endif
Andreas Marek's avatar
Andreas Marek committed
680
                                           i - limits(ip), .false.)
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
#ifdef WITH_MPI
                call timer%start("mpi_communication")
                call MPI_Recv(row_group(:, row_group_size), l_nev,     &
#if REALCASE == 1
                              MPI_REAL_PRECISION,    &
#endif
#if COMPLEXCASE == 1
                              MPI_COMPLEX_EXPLICIT_PRECISION, &
#endif
                              src, 0, mpi_comm_rows, MPI_STATUS_IGNORE, mpierr)
                call timer%stop("mpi_communication")

#else /* WITH_MPI */
                row_group(1:l_nev, row_group_size) = row(1:l_nev) ! is this correct?
#endif /* WITH_MPI */

              else ! useGPU
#ifdef WITH_MPI
                call timer%start("mpi_communication")
                call MPI_Recv(row, l_nev,         &
#if REALCASE == 1
                              MPI_REAL_PRECISION, &
#endif
#if COMPLEXCASE == 1
                              MPI_COMPLEX_EXPLICIT_PRECISION, &
#endif
                              src, 0, mpi_comm_rows, MPI_STATUS_IGNORE, mpierr)
                call timer%stop("mpi_communication")

#else /* WITH_MPI */

!                row(1:l_nev) = row(1:l_nev)

#endif /* WITH_MPI */

716
                call unpack_row_&
Andreas Marek's avatar
Cleanup    
Andreas Marek committed
717
718
719
                &MATH_DATATYPE&
                &_cpu_&
                &PRECISION &
720
                                (aIntern, row,i-limits(ip), stripe_count, stripe_width, last_stripe_width)
721
722
723
724
725
726
727
728
729
730
              endif ! useGPU
#endif /* WITH_OPENMP */

            elseif (src == my_prow) then

              src_offset = src_offset+1

              if (useGPU) then
#ifndef WITH_OPENMP

Andreas Marek's avatar
Andreas Marek committed
731
732
733
734
735
736
737
738
739
740
741
742
743
                 ! An unpacking of the current row group may occur before queuing the next row
                 call unpack_and_prepare_row_group_&
		 &MATH_DATATYPE&
		 &_gpu_&
		 &PRECISION &
		              ( &
#if REALCASE == 1
                               row_group, row_group_dev, aIntern_dev, stripe_count, &
                               stripe_width, last_stripe_width, a_dim2, l_nev,&
                               row_group_size, nblk, unpack_idx, &
#endif
                               i - limits(ip), .false.)

744
745
746
747
748
749
750
751
#if REALCASE == 1
                row_group(:, row_group_size) = q(src_offset, 1:l_nev)
#endif

#if COMPLEXCASE == 1
                row_group(:, row_group_size) = q(src_offset, 1:l_nev)
#endif

752
#else /* WITH_OPENMP */
Andreas Marek's avatar
Andreas Marek committed
753

754
#if COMPLEXCASE == 1
Andreas Marek's avatar
Andreas Marek committed
755
756
757
                call unpack_and_prepare_row_group_complex_gpu_&
		&PRECISION&
		&(i - limits(ip),.false.)
758
759
                row_group(:, row_group_size) = q(src_offset, 1:l_nev)
#endif
Andreas Marek's avatar
Andreas Marek committed
760

761
762
763
764
765
766
767
768
769
770
#endif /* not OpenMP */
              else
                row(:) = q(src_offset, 1:l_nev)
              endif

#ifdef WITH_OPENMP
              call timer%start("OpenMP parallel" // PRECISION_SUFFIX)

!$omp parallel do private(my_thread), schedule(static, 1)
              do my_thread = 1, max_threads
771
	        call unpack_row_&
Andreas Marek's avatar
Cleanup    
Andreas Marek committed
772
773
774
                &MATH_DATATYPE&
                &_cpu_openmp_&
                &PRECISION &
775
776
                                   (aIntern, row, i-limits(ip), my_thread, stripe_count, thread_width, stripe_width, l_nev)

777
778
779
780
781
782
783
784
785
786
              enddo
!$omp end parallel do

              call timer%stop("OpenMP parallel" // PRECISION_SUFFIX)

#else /* WITH_OPENMP */

              if (useGPU) then

              else
787
	        call unpack_row_&
Andreas Marek's avatar
Cleanup    
Andreas Marek committed
788
789
790
                &MATH_DATATYPE&
                &_cpu_&
                &PRECISION &
791
                                (aIntern, row,i-limits(ip),  stripe_count, stripe_width, last_stripe_width)
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
              endif

#endif /* WITH_OPENMP */

            endif
          enddo

          ! Send all rows which have not yet been send
          src_offset = 0
          do dst = 0, ip-1
            do i=limits(dst)+1,limits(dst+1)
              if (mod((i-1)/nblk, np_rows) == my_prow) then
                src_offset = src_offset+1
                row(:) = q(src_offset, 1:l_nev)

#ifdef WITH_MPI
                call timer%start("mpi_communication")
                call MPI_Send(row, l_nev,        &
#if REALCASE == 1
                              MPI_REAL_PRECISION, &
#endif
#if COMPLEXCASE == 1
                              MPI_COMPLEX_EXPLICIT_PRECISION, &
#endif
                              dst, 0, mpi_comm_rows, mpierr)
                call timer%stop("mpi_communication")
#endif /* WITH_MPI */
              endif
            enddo
          enddo

        else if (my_prow < ip) then
Andreas Marek's avatar
Cleanup    
Andreas Marek committed
824

825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
          ! Send all rows going to PE ip
          src_offset = local_index(limits(ip), my_prow, np_rows, nblk, -1)
          do i=limits(ip)+1,limits(ip+1)
            src = mod((i-1)/nblk, np_rows)
            if (src == my_prow) then
              src_offset = src_offset+1
              row(:) = q(src_offset, 1:l_nev)
#ifdef WITH_MPI
              call timer%start("mpi_communication")
              call MPI_Send(row, l_nev,        &
#if REALCASE == 1
                            MPI_REAL_PRECISION, &
#endif
#if COMPLEXCASE == 1
                            MPI_COMPLEX_EXPLICIT_PRECISION, &
#endif
                            ip, 0, mpi_comm_rows, mpierr)
              call timer%stop("mpi_communication")
#endif /* WITH_MPI */
            endif
          enddo

          ! Receive all rows from PE ip
          do i=limits(my_prow)+1,limits(my_prow+1)
            src = mod((i-1)/nblk, np_rows)
            if (src == ip) then
#ifdef WITH_OPENMP

#ifdef WITH_MPI
              call timer%start("mpi_communication")
              call MPI_Recv(row, l_nev,     &
#if REALCASE == 1
                            MPI_REAL_PRECISION, &
#endif
#if COMPLEXCASE == 1
                            MPI_COMPLEX_EXPLICIT_PRECISION, &
#endif
                            src, 0, mpi_comm_rows, MPI_STATUS_IGNORE, mpierr)
              call timer%stop("mpi_communication")
#else /* WITH_MPI */

!              row(1:l_nev) = row(1:l_nev)

#endif /* WITH_MPI */

              call timer%start("OpenMP parallel" // PRECISION_SUFFIX)
!$omp parallel do private(my_thread), schedule(static, 1)
              do my_thread = 1, max_threads
873
	        call unpack_row_&
Andreas Marek's avatar
Cleanup    
Andreas Marek committed
874
875
876
                &MATH_DATATYPE&
                &_cpu_openmp_&
                &PRECISION &
877
                                 (aIntern, row, i-limits(my_prow), my_thread, stripe_count, thread_width, stripe_width, l_nev)
878
879
880
881
882
883
884
              enddo
!$omp end parallel do
              call timer%stop("OpenMP parallel" // PRECISION_SUFFIX)

#else /* WITH_OPENMP */
              if (useGPU) then
                ! An unpacking of the current row group may occur before queuing the next row
Andreas Marek's avatar
Andreas Marek committed
885
886
887
888
889
                call unpack_and_prepare_row_group_&
		&MATH_DATATYPE&
		&_gpu_&
		&PRECISION&
		&( &
890
#if REALCASE == 1
Andreas Marek's avatar
Andreas Marek committed
891
892
893
                  row_group, row_group_dev, aIntern_dev, stripe_count,  &
                  stripe_width, last_stripe_width, a_dim2, l_nev,       &
                  row_group_size, nblk, unpack_idx,                     &
894
#endif
Andreas Marek's avatar
Andreas Marek committed
895
                  i - limits(my_prow), .false.)
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939

#ifdef WITH_MPI
               call timer%start("mpi_communication")
               call MPI_Recv(row_group(:, row_group_size), l_nev,   &
#if REALCASE == 1
                             MPI_REAL_PRECISION,    &
#endif
#if COMPLEXCASE == 1
                             MPI_COMPLEX_EXPLICIT_PRECISION, &
#endif
                             src, 0, mpi_comm_rows, MPI_STATUS_IGNORE, mpierr)
               call timer%stop("mpi_communication")
#else /* WITH_MPI */

#if REALCASE == 1
               row_group(1:l_nev,row_group_size) = row(1:l_nev) ! is this correct ?
#endif
#if COMPLEXCASE == 1
! todo: what of this is correct? Was different for double/single precision
#ifdef DOUBLE_PRECISION_COMPLEX
                row_group(1:l_nev,row_group_size) = row(1:l_nev) ! is this correct ?
#else
                row_group(1:l_nev,row_group_size) = row_group(1:l_nev,row_group_size) ! is this correct
#endif
#endif
#endif /* WITH_MPI */

              else ! useGPU
#ifdef WITH_MPI
                call timer%start("mpi_communication")
                call MPI_Recv(row, l_nev,    &
#if REALCASE == 1
                              MPI_REAL_PRECISION,    &
#endif
#if COMPLEXCASE == 1
                              MPI_COMPLEX_EXPLICIT_PRECISION, &
#endif
                              src, 0, mpi_comm_rows, MPI_STATUS_IGNORE, mpierr)
                call timer%stop("mpi_communication")
#else /* WITH_MPI */

!                row(1:l_nev) = row(1:l_nev)

#endif
940
                call unpack_row_&
Andreas Marek's avatar
Cleanup    
Andreas Marek committed
941
942
943
                &MATH_DATATYPE&
                &_cpu_&
                &PRECISION &
944
                                (aIntern, row,i-limits(my_prow), stripe_count, stripe_width, last_stripe_width)
945
946
947
948
949
950
951
952
953
954
955
              endif ! useGPU

#endif /* WITH_OPENMP */

            endif
          enddo
        endif
      enddo

      if (useGPU) then
        ! Force an unpacking of all remaining rows that haven't been unpacked yet
Andreas Marek's avatar
Andreas Marek committed
956
957
958
959
960
        call unpack_and_prepare_row_group_&
	&MATH_DATATYPE&
	&_gpu_&
	&PRECISION&
	&( &
961
#if REALCASE == 1
Andreas Marek's avatar
Andreas Marek committed
962
963
964
          row_group, row_group_dev, aIntern_dev, stripe_count, &
          stripe_width, last_stripe_width, &
          a_dim2, l_nev, row_group_size, nblk, unpack_idx,     &
965
#endif
Andreas Marek's avatar
Andreas Marek committed
966
967
          -1, .true.)

968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
        successCUDA = cuda_devicesynchronize()

         if (.not.(successCUDA)) then
           print *,"trans_ev_tridi_to_band_&
           &MATH_DATATYPE&
           &: error in cudaDeviceSynchronize"//errorMessage
           stop
         endif
      endif

      ! Set up result buffer queue

      num_result_blocks = ((na-1)/nblk + np_rows - my_prow) / np_rows

      num_result_buffers = 4*nfact
      allocate(result_buffer(l_nev,nblk,num_result_buffers), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
        print *,"trans_ev_tridi_to_band_&
        &MATH_DATATYPE&
        &: error when allocating result_buffer"//errorMessage
        stop
      endif

      allocate(result_send_request(num_result_buffers), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
        print *,"trans_ev_tridi_to_band_&
        &MATH_DATATYPE&
        &: error when allocating result_send_request"//errorMessage
        stop
      endif

      allocate(result_recv_request(num_result_buffers), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then