elpa_impl.F90 63.3 KB
Newer Older
1 2 3
!
!    Copyright 2017, L. Hüdepohl and A. Marek, MPCDF
!
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
!    This file is part of ELPA.
!
!    The ELPA library was originally created by the ELPA consortium,
!    consisting of the following organizations:
!
!    - Max Planck Computing and Data Facility (MPCDF), formerly known as
!      Rechenzentrum Garching der Max-Planck-Gesellschaft (RZG),
!    - Bergische Universität Wuppertal, Lehrstuhl für angewandte
!      Informatik,
!    - Technische Universität München, Lehrstuhl für Informatik mit
!      Schwerpunkt Wissenschaftliches Rechnen ,
!    - Fritz-Haber-Institut, Berlin, Abt. Theorie,
!    - Max-Plack-Institut für Mathematik in den Naturwissenschaften,
!      Leipzig, Abt. Komplexe Strukutren in Biologie und Kognition,
!      and
!    - IBM Deutschland GmbH
!
!    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.
!
48
#include "config-f90.h"
49

50
!> \brief Fortran module which provides the actual implementation of the API. Do not use directly! Use the module "elpa"
51
module elpa_impl
52 53 54 55 56 57 58 59
  use precision
  use elpa2_impl
  use elpa1_impl
  use elpa1_auxiliary_impl
  use elpa_mpi
  use elpa_generated_fortran_interfaces
  use elpa_utilities, only : error_unit

60
  use elpa_abstract_impl
61
#ifdef ENABLE_AUTOTUNING
62
  use elpa_autotune_impl
63
#endif
64
  use, intrinsic :: iso_c_binding
65
  use iso_fortran_env
66
  implicit none
67

68 69
  private
  public :: elpa_impl_allocate
70

71
!> \brief Definition of the extended elpa_impl_t type
72
  type, extends(elpa_abstract_impl_t) :: elpa_impl_t
Andreas Marek's avatar
Andreas Marek committed
73
   private
74
   integer :: communicators_owned
75

76 77 78
   !This object has been created through the legacy api.
   integer :: from_legacy_api

79
   !> \brief methods available with the elpa_impl_t type
80
   contains
81
     !> \brief the puplic methods
82
     ! con-/destructor
83 84
     procedure, public :: setup => elpa_setup                   !< a setup method: implemented in elpa_setup
     procedure, public :: destroy => elpa_destroy               !< a destroy method: implemented in elpa_destroy
85

86
     ! KV store
87 88 89 90
     procedure, public :: is_set => elpa_is_set                 !< a method to check whether a key/value pair has been set : implemented
                                                                !< in elpa_is_set
     procedure, public :: can_set => elpa_can_set               !< a method to check whether a key/value pair can be set : implemented
                                                                !< in elpa_can_set
91

92 93 94
     ! call before setup if created from the legacy api
     ! remove this function completely after the legacy api is dropped
     procedure, public :: creating_from_legacy_api => elpa_creating_from_legacy_api
95 96 97 98

     ! timer
     procedure, public :: get_time => elpa_get_time
     procedure, public :: print_times => elpa_print_times
99 100
     procedure, public :: timer_start => elpa_timer_start
     procedure, public :: timer_stop => elpa_timer_stop
101 102


103 104 105 106 107 108 109 110 111 112 113 114 115 116
     !> \brief the implemenation methods

     procedure, public :: elpa_eigenvectors_d                  !< public methods to implement the solve step for real/complex
                                                               !< double/single matrices
     procedure, public :: elpa_eigenvectors_f
     procedure, public :: elpa_eigenvectors_dc
     procedure, public :: elpa_eigenvectors_fc

     procedure, public :: elpa_eigenvalues_d                   !< public methods to implement the solve step for real/complex
                                                               !< double/single matrices; only the eigenvalues are computed
     procedure, public :: elpa_eigenvalues_f
     procedure, public :: elpa_eigenvalues_dc
     procedure, public :: elpa_eigenvalues_fc

Pavel Kus's avatar
Pavel Kus committed
117 118 119 120 121 122
     procedure, public :: elpa_generalized_eigenvectors_d      !< public methods to implement the solve step for generalized 
                                                               !< eigenproblem and real/complex double/single matrices
     procedure, public :: elpa_generalized_eigenvectors_f
     procedure, public :: elpa_generalized_eigenvectors_dc
     procedure, public :: elpa_generalized_eigenvectors_fc

123 124 125 126 127 128
     procedure, public :: elpa_generalized_eigenvalues_d      !< public methods to implement the solve step for generalized 
                                                              !< eigenproblem and real/complex double/single matrices
     procedure, public :: elpa_generalized_eigenvalues_f
     procedure, public :: elpa_generalized_eigenvalues_dc
     procedure, public :: elpa_generalized_eigenvalues_fc

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
     procedure, public :: elpa_hermitian_multiply_d            !< public methods to implement a "hermitian" multiplication of matrices a and b
     procedure, public :: elpa_hermitian_multiply_f            !< for real valued matrices:   a**T * b
     procedure, public :: elpa_hermitian_multiply_dc           !< for complex valued matrices:   a**H * b
     procedure, public :: elpa_hermitian_multiply_fc

     procedure, public :: elpa_cholesky_d                      !< public methods to implement the cholesky factorisation of
                                                               !< real/complex double/single matrices
     procedure, public :: elpa_cholesky_f
     procedure, public :: elpa_cholesky_dc
     procedure, public :: elpa_cholesky_fc

     procedure, public :: elpa_invert_trm_d                    !< public methods to implement the inversion of a triangular
                                                               !< real/complex double/single matrix
     procedure, public :: elpa_invert_trm_f
     procedure, public :: elpa_invert_trm_dc
     procedure, public :: elpa_invert_trm_fc

     procedure, public :: elpa_solve_tridiagonal_d             !< public methods to implement the solve step for a real valued
     procedure, public :: elpa_solve_tridiagonal_f             !< double/single tridiagonal matrix

     procedure, public :: associate_int => elpa_associate_int  !< public method to set some pointers
150

Pavel Kus's avatar
Pavel Kus committed
151
     procedure, private :: elpa_transform_generalized_d
152
     procedure, private :: elpa_transform_back_generalized_d
Pavel Kus's avatar
Pavel Kus committed
153
     procedure, private :: elpa_transform_generalized_dc
154
     procedure, private :: elpa_transform_back_generalized_dc
Pavel Kus's avatar
Pavel Kus committed
155 156
#ifdef WANT_SINGLE_PRECISION_REAL
     procedure, private :: elpa_transform_generalized_f
157
     procedure, private :: elpa_transform_back_generalized_f
Pavel Kus's avatar
Pavel Kus committed
158 159 160
#endif
#ifdef WANT_SINGLE_PRECISION_COMPLEX
     procedure, private :: elpa_transform_generalized_fc
161
     procedure, private :: elpa_transform_back_generalized_fc
Pavel Kus's avatar
Pavel Kus committed
162
#endif
163

164 165
     procedure, public :: print_settings => elpa_print_settings
     procedure, public :: store_settings => elpa_store_settings
166
     procedure, public :: load_settings => elpa_load_settings
167
#ifdef ENABLE_AUTOTUNING
168
     procedure, public :: autotune_setup => elpa_autotune_setup
169 170
     procedure, public :: autotune_step => elpa_autotune_step
     procedure, public :: autotune_set_best => elpa_autotune_set_best
171
     procedure, public :: autotune_print_best => elpa_autotune_print_best
172
     procedure, public :: autotune_print_state => elpa_autotune_print_state
173
     procedure, public :: autotune_save_state => elpa_autotune_save_state
174
     procedure, public :: autotune_load_state => elpa_autotune_load_state
175
#endif
176
     procedure, private :: construct_scalapack_descriptor => elpa_construct_scalapack_descriptor
177
  end type elpa_impl_t
178 179

  !> \brief the implementation of the generic methods
180
  contains
181 182


183 184 185 186
    !> \brief function to allocate an ELPA object
    !> Parameters
    !> \param   error      integer, optional to get an error code
    !> \result  obj        class(elpa_impl_t) allocated ELPA object
187
    function elpa_impl_allocate(error) result(obj)
188 189 190 191 192 193 194
      type(elpa_impl_t), pointer     :: obj
#ifdef USE_FORTRAN2008
      integer, optional, intent(out) :: error
#else
      integer, intent(out)           :: error
#endif
      integer                        :: error2
195

196

197 198 199
      allocate(obj, stat=error2)
      if (error2 .ne. 0) then
        write(error_unit, *) "elpa_allocate(): could not allocate object"
200 201 202
      endif

      obj%from_legacy_api = 0
203

Andreas Marek's avatar
Andreas Marek committed
204
      ! check whether init has ever been called
205
      if ( elpa_initialized() .ne. ELPA_OK) then
206
        write(error_unit, *) "elpa_allocate(): you must call elpa_init() once before creating instances of ELPA"
207
        if (present(error)) then
208
          error = ELPA_ERROR_API_VERSION
209
        endif
Andreas Marek's avatar
Andreas Marek committed
210 211
        return
      endif
212

213
      obj%index = elpa_index_instance_c()
214 215

      ! Associate some important integer pointers for convenience
216 217 218 219 220 221
      obj%na => obj%associate_int("na")
      obj%nev => obj%associate_int("nev")
      obj%local_nrows => obj%associate_int("local_nrows")
      obj%local_ncols => obj%associate_int("local_ncols")
      obj%nblk => obj%associate_int("nblk")

222
      if (present(error)) then
223
        error = ELPA_OK
224 225
      endif
    end function
226

Andreas Marek's avatar
Andreas Marek committed
227

228 229 230 231 232
    !c> /*! \brief C interface for the implementation of the elpa_allocate method
    !c> *
    !c> *  \param  none
    !c> *  \result elpa_t handle
    !c> */
233
    !c> elpa_t elpa_allocate(int *error);
234
    function elpa_impl_allocate_c(error) result(ptr) bind(C, name="elpa_allocate")
235 236 237 238 239 240 241 242
      integer(kind=c_int) :: error
      type(c_ptr) :: ptr
      type(elpa_impl_t), pointer :: obj

      obj => elpa_impl_allocate(error)
      ptr = c_loc(obj)
    end function

Andreas Marek's avatar
Andreas Marek committed
243

244 245 246
    !c> /*! \brief C interface for the implementation of the elpa_deallocate method
    !c> *
    !c> *  \param  elpa_t  handle of ELPA object to be deallocated
247
    !c> *  \param  int*    error code
248 249
    !c> *  \result void
    !c> */
250 251 252
    !c> void elpa_deallocate(elpa_t handle, int *error);
    subroutine elpa_impl_deallocate_c(handle, error) bind(C, name="elpa_deallocate")
      type(c_ptr), value         :: handle
253
      type(elpa_impl_t), pointer :: self
254
      integer(kind=c_int)        :: error
255 256

      call c_f_pointer(handle, self)
257
      call self%destroy(error)
258 259 260
      deallocate(self)
    end subroutine

261 262 263 264 265 266 267 268 269 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 355 356 357 358 359 360 361 362 363 364 365 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
    !> \brief function to load all the parameters, which have been saved to a file
    !> Parameters
    !> \param   self        class(elpa_impl_t) the allocated ELPA object
    !> \param   file_name   string, the name of the file from which to load the parameters
    !> \param   error       integer, optional
    subroutine elpa_load_settings(self, file_name, error)
      implicit none
      class(elpa_impl_t), intent(inout) :: self
      character(*), intent(in)          :: file_name
#ifdef USE_FORTRAN2008
      integer(kind=c_int), optional, intent(out)    :: error
#else
      integer(kind=c_int), intent(out)              :: error
#endif
      if (present(error)) then
        error = ELPA_OK
      endif
      if (elpa_index_load_settings_c(self%index, file_name // c_null_char) /= 1) then
        write(error_unit, *) "This should not happen (in elpa_load_settings())"
        if (present(error)) then
          error = ELPA_ERROR_CANNOT_OPEN_FILE
        endif
      endif
    end subroutine

    !c> /*! \brief C interface for the implementation of the elpa_load_settings method
    !c> *
    !c> *  \param elpa_t handle
    !c> *  \param  char* filename
    !c> */
    !c> void elpa_load_settings(elpa_t handle, const char *filename, int *error);
    subroutine elpa_load_settings_c(handle, filename_p, error) bind(C, name="elpa_load_settings")
      type(c_ptr), value         :: handle
      type(elpa_impl_t), pointer :: self

      integer(kind=c_int)        :: error
      type(c_ptr), intent(in), value :: filename_p
      character(len=elpa_strlen_c(filename_p)), pointer :: filename

      call c_f_pointer(handle, self)
      call c_f_pointer(filename_p, filename)
      call elpa_load_settings(self, filename, error)

    end subroutine

    !> \brief function to print all the parameters, that have been set
    !> Parameters
    !> \param   self            class(elpa_impl_t) the allocated ELPA object
    !> \param   error           optional, integer
    subroutine elpa_print_settings(self, error)
      implicit none
      class(elpa_impl_t), intent(inout) :: self
#ifdef USE_FORTRAN2008
      integer(kind=c_int), optional, intent(out)    :: error
#else
      integer(kind=c_int), intent(out)              :: error
#endif
      if (present(error)) then
        error = ELPA_OK
      endif
      if (elpa_index_print_settings_c(self%index, c_null_char) /= 1) then
        write(error_unit, *) "This should not happen (in elpa_print_settings())"
        if (present(error)) then
          error = ELPA_ERROR
        endif
      endif
    end subroutine

    !c> /*! \brief C interface for the implementation of the elpa_print_settings method
    !c> *
    !c> *  \param elpa_t handle
    !c> *  \param  char* filename
    !c> */
    !c> void elpa_print_settings(elpa_t handle, int *error);
    subroutine elpa_print_settings_c(handle, error) bind(C, name="elpa_print_settings")
      type(c_ptr), value         :: handle
      type(elpa_impl_t), pointer :: self
 
      integer(kind=c_int)        :: error

      call c_f_pointer(handle, self)
      call elpa_print_settings(self, error)

    end subroutine


    !> \brief function to save all the parameters, that have been set
    !> Parameters
    !> \param   self        class(elpa_impl_t) the allocated ELPA object
    !> \param   file_name   string, the name of the file where to save the parameters
    !> \param   error       integer, optional
    subroutine elpa_store_settings(self, file_name, error)
      implicit none
      class(elpa_impl_t), intent(inout) :: self
      character(*), intent(in)          :: file_name
#ifdef USE_FORTRAN2008
      integer(kind=c_int), optional, intent(out)    :: error
#else
      integer(kind=c_int), intent(out)              :: error
#endif

      if (present(error)) then
        error = ELPA_OK
      endif
      if (elpa_index_print_settings_c(self%index, file_name // c_null_char) /= 1) then
        write(error_unit, *) "This should not happen (in elpa_store_settings())"
        if (present(error)) then
          error = ELPA_ERROR_CANNOT_OPEN_FILE
        endif

      endif
    end subroutine


    !c> /*! \brief C interface for the implementation of the elpa_store_settings method
    !c> *
    !c> *  \param elpa_t handle
    !c> *  \param  char* filename
    !c> */
    !c> void elpa_store_settings(elpa_t handle, const char *filename, int *error);
    subroutine elpa_store_settings_c(handle, filename_p, error) bind(C, name="elpa_store_settings")
      type(c_ptr), value         :: handle
      type(elpa_impl_t), pointer :: self
      type(c_ptr), intent(in), value :: filename_p
      character(len=elpa_strlen_c(filename_p)), pointer :: filename
      integer(kind=c_int)        :: error

      call c_f_pointer(handle, self)
      call c_f_pointer(filename_p, filename)
      call elpa_store_settings(self, filename, error)

    end subroutine




Andreas Marek's avatar
Andreas Marek committed
397

398
#ifdef ENABLE_AUTOTUNING
399 400 401 402 403
    !c> /*! \brief C interface for the implementation of the elpa_autotune_deallocate method
    !c> *
    !c> *  \param  elpa_autotune_impl_t  handle of ELPA autotune object to be deallocated
    !c> *  \result void
    !c> */
404
    !c> void elpa_autotune_deallocate(elpa_autotune_t handle, int *error);
405 406
    subroutine elpa_autotune_impl_deallocate_c( autotune_handle) bind(C, name="elpa_autotune_deallocate")
      type(c_ptr), value                  :: autotune_handle
407

408 409 410
      type(elpa_autotune_impl_t), pointer :: self

      call c_f_pointer(autotune_handle, self)
411 412 413
      call self%destroy()
      deallocate(self)
    end subroutine
414
#endif
415

416 417 418 419
    !> \brief function to setup an ELPA object and to store the MPI communicators internally
    !> Parameters
    !> \param   self       class(elpa_impl_t), the allocated ELPA object
    !> \result  error      integer, the error code
420
    function elpa_setup(self) result(error)
421
      class(elpa_impl_t), intent(inout)   :: self
422
      integer                             :: error, timings, performance
423

424
#ifdef WITH_MPI
425 426
      integer                             :: mpi_comm_parent, mpi_comm_rows, mpi_comm_cols, np_rows, np_cols, my_id, &
                                             mpierr, mpierr2, process_row, process_col, mpi_string_length, &
427
                                             present_np_rows, present_np_cols, np_total
428
      character(len=MPI_MAX_ERROR_STRING) :: mpierr_string
429 430 431
      character(*), parameter             :: MPI_CONSISTENCY_MSG = &
        "Provide mpi_comm_parent and EITHER process_row and process_col OR mpi_comm_rows and mpi_comm_cols. Aborting..."

432
#endif
433

434
#ifdef HAVE_DETAILED_TIMINGS
435
      call self%get("timings",timings, error)
436 437
      call self%get("measure_performance",performance, error)
      print *,error
438
      if (check_elpa_get(error, ELPA_ERROR_SETUP)) return
439 440
      if (timings == 1) then
        call self%timer%enable()
441 442
        if (performance == 1) then
          call self%timer%measure_flops(.true.)
Andreas Marek's avatar
Andreas Marek committed
443
          call self%timer%set_print_options(print_flop_count=.true.,print_flop_rate=.true.)
444
        endif
445 446 447 448
      endif
#endif

      error = ELPA_OK
449

450 451 452 453 454 455 456 457
      ! In most cases, we actually need the parent communicator to be supplied,
      ! ELPA internally requires it when either GPU is enabled or when ELPA2 is
      ! used. It thus seems reasonable that we should ALLWAYS require it. It
      ! should then be accompanied by EITHER process_row and process_col
      ! indices, OR mpi_comm_rows and mpi_comm_cols communicators, but NOT both.
      ! This assumption will significanlty simplify the logic, avoid possible
      ! inconsistencies and is rather natural from the user point of view

458
#ifdef WITH_MPI
459
      if (self%is_set("mpi_comm_parent") == 1) then
460
        call self%get("mpi_comm_parent", mpi_comm_parent, error)
461
        if (check_elpa_get(error, ELPA_ERROR_SETUP)) return
462 463 464

        call mpi_comm_rank(mpi_comm_parent, my_id, mpierr)
        call self%set("process_id", my_id, error)
465
        if (check_elpa_set(error, ELPA_ERROR_SETUP)) return
466 467 468

        call mpi_comm_size(mpi_comm_parent, np_total, mpierr)
        call self%set("num_processes", np_total, error)
469
        if (check_elpa_set(error, ELPA_ERROR_SETUP)) return
470
      else
471
        if (self%from_legacy_api .ne. 1) then
472 473 474 475 476 477 478 479 480
          write(error_unit,*) MPI_CONSISTENCY_MSG
          error = ELPA_ERROR
          return
        endif
      endif

      ! Create communicators ourselves
      if (self%is_set("process_row") == 1 .and. self%is_set("process_col") == 1) then

481
        if (self%is_set("mpi_comm_rows") == 1 .or. self%is_set("mpi_comm_cols") == 1) then
482 483 484 485 486
          write(error_unit,*) MPI_CONSISTENCY_MSG
          error = ELPA_ERROR
          return
        endif

487
        call self%get("process_row", process_row, error)
488
        if (check_elpa_get(error, ELPA_ERROR_SETUP)) return
489

490
        call self%get("process_col", process_col, error)
491
        if (check_elpa_get(error, ELPA_ERROR_SETUP)) return
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510

        ! mpi_comm_rows is used for communicating WITHIN rows, i.e. all processes
        ! having the same column coordinate share one mpi_comm_rows.
        ! So the "color" for splitting is process_col and the "key" is my row coordinate.
        ! Analogous for mpi_comm_cols

        call mpi_comm_split(mpi_comm_parent,process_col,process_row,mpi_comm_rows,mpierr)
        if (mpierr .ne. MPI_SUCCESS) then
          call MPI_ERROR_STRING(mpierr,mpierr_string, mpi_string_length, mpierr2)
          write(error_unit,*) "MPI ERROR occured during mpi_comm_split for row communicator: ", trim(mpierr_string)
          return
        endif

        call mpi_comm_split(mpi_comm_parent,process_row,process_col,mpi_comm_cols, mpierr)
        if (mpierr .ne. MPI_SUCCESS) then
          call MPI_ERROR_STRING(mpierr,mpierr_string, mpi_string_length, mpierr2)
          write(error_unit,*) "MPI ERROR occured during mpi_comm_split for col communicator: ", trim(mpierr_string)
          return
        endif
511

512
        call self%set("mpi_comm_rows", mpi_comm_rows,error)
513
        if (check_elpa_set(error, ELPA_ERROR_SETUP)) return
514

515
        call self%set("mpi_comm_cols", mpi_comm_cols,error)
516
        if (check_elpa_set(error, ELPA_ERROR_SETUP)) return
517

518 519 520
        ! remember that we created those communicators and we need to free them later
        self%communicators_owned = 1

521
      ! Externally supplied communicators
522 523
      else if ( self%is_set("mpi_comm_rows") == 1 .and.  self%is_set("mpi_comm_cols") == 1) then

524
        if (self%is_set("process_row") == 1 .or. self%is_set("process_col") == 1) then
525 526 527
          write(error_unit,*) MPI_CONSISTENCY_MSG
          error = ELPA_ERROR
          return
528
        endif
529 530

        call self%get("mpi_comm_rows", mpi_comm_rows,error)
531
        if (check_elpa_get(error, ELPA_ERROR_SETUP)) return
532

533
        call self%get("mpi_comm_cols", mpi_comm_cols,error)
534
        if (check_elpa_get(error, ELPA_ERROR_SETUP)) return
535 536 537

        call mpi_comm_rank(mpi_comm_rows, process_row, mpierr)
        call self%set("process_row", process_row, error)
538
        if (check_elpa_set(error, ELPA_ERROR_SETUP)) return
539

540 541
        call mpi_comm_rank(mpi_comm_cols, process_col, mpierr)
        call self%set("process_col", process_col, error)
542
        if (check_elpa_set(error, ELPA_ERROR_SETUP)) return
543 544

        ! remember that we DID NOT created those communicators and we WILL NOT free them later
545
        self%communicators_owned = 0
546 547 548 549 550 551
      else
        ! Otherwise parameters are missing
        write(error_unit,*) MPI_CONSISTENCY_MSG
        error = ELPA_ERROR
        return
      endif
552

553 554 555 556
      ! set num_process_rows (and cols), if they are not supplied. Check them
      ! for consistency if they are. Maybe we could instead require, that they
      ! are never supplied?
      call mpi_comm_size(mpi_comm_rows, np_rows, mpierr)
557
      if (self%is_set("num_process_rows") == 1) then
558
        call self%get("num_process_rows", present_np_rows, error)
559
        if (check_elpa_get(error, ELPA_ERROR_SETUP)) return
560

561
        if (np_rows .ne. present_np_rows) then
562
          print *,"MPI row communicator not set correctly. Aborting..."
563 564
          stop
        endif
565 566
      else
        call self%set("num_process_rows", np_rows, error)
567
        if (check_elpa_set(error, ELPA_ERROR_SETUP)) return
568 569 570
      endif

      call mpi_comm_size(mpi_comm_cols, np_cols, mpierr)
571
      if (self%is_set("num_process_cols") == 1) then
572
        call self%get("num_process_cols", present_np_cols, error)
573
        if (check_elpa_get(error, ELPA_ERROR_SETUP)) return
574

575
        if (np_cols .ne. present_np_cols) then
576
          print *,"MPI column communicator not set correctly. Aborting..."
577 578
          stop
        endif
579 580
      else
        call self%set("num_process_cols", np_cols, error)
581
        if (check_elpa_set(error, ELPA_ERROR_SETUP)) return
582
      endif
583

584
      if (self%from_legacy_api .ne. 1) then
585
        if (np_total .ne. np_rows * np_cols) then
586 587 588
          print *,"MPI parent communicator and row/col communicators do not match. Aborting..."
          stop
        endif
589
      endif
590

591
#else
592
      call self%set("process_row", 0, error)
593
      if (check_elpa_set(error, ELPA_ERROR_SETUP)) return
594
      call self%set("process_col", 0, error)
595
      if (check_elpa_set(error, ELPA_ERROR_SETUP)) return
596
      call self%set("process_id", 0, error)
597
      if (check_elpa_set(error, ELPA_ERROR_SETUP)) return
598
      call self%set("num_process_rows", 1, error)
599
      if (check_elpa_set(error, ELPA_ERROR_SETUP)) return
600
      call self%set("num_process_cols", 1, error)
601
      if (check_elpa_set(error, ELPA_ERROR_SETUP)) return
602
      call self%set("num_processes", 1, error)
603
      if (check_elpa_set(error, ELPA_ERROR_SETUP)) return
604
#endif
605

606
    end function
607

Andreas Marek's avatar
Andreas Marek committed
608

609 610 611 612 613 614
    !c> /*! \brief C interface for the implementation of the elpa_setup method
    !c> *
    !c> *  \param  elpa_t  handle of the ELPA object which describes the problem to
    !c> *                  be set up
    !c> *  \result int     error code, which can be queried with elpa_strerr
    !c> */
615
    !c> int elpa_setup(elpa_t handle);
616
    function elpa_setup_c(handle) result(error) bind(C, name="elpa_setup")
617 618 619 620 621 622 623 624
      type(c_ptr), intent(in), value :: handle
      type(elpa_impl_t), pointer :: self
      integer(kind=c_int) :: error

      call c_f_pointer(handle, self)
      error = self%setup()
    end function

625
    function elpa_construct_scalapack_descriptor(self, sc_desc, rectangular_for_ev) result(error)
626
      class(elpa_impl_t), intent(inout)   :: self
627
      logical, intent(in)                 :: rectangular_for_ev
628 629 630 631 632 633 634 635 636
      integer                             :: error, blacs_ctx
      integer, intent(out)                :: sc_desc(SC_DESC_LEN)

#ifdef WITH_MPI
      if (self%is_set("blacs_context") == 0) then
        print *,"BLACS context has not been set beforehand. Aborting..."
        stop
      endif
      call self%get("blacs_context", blacs_ctx, error)
637
      if (check_elpa_get(error, ELPA_ERROR_CRITICAL)) return
638 639 640 641

      sc_desc(1) = 1
      sc_desc(2) = blacs_ctx
      sc_desc(3) = self%na
642
      if (rectangular_for_ev) then
643 644 645 646
        sc_desc(4) = self%nev
      else
        sc_desc(4) = self%na
      endif
647 648 649 650 651 652 653 654 655 656
      sc_desc(5) = self%nblk
      sc_desc(6) = self%nblk
      sc_desc(7) = 0
      sc_desc(8) = 0
      sc_desc(9) = self%local_nrows
#else
      sc_desc = 0
#endif
      error = ELPA_OK
    end function
657

Andreas Marek's avatar
Andreas Marek committed
658

659 660 661 662 663 664 665 666 667
    !c> /*! \brief C interface for the implementation of the elpa_set_integer method
    !c> *  This method is available to the user as C generic elpa_set method
    !c> *
    !c> *  \param  handle  handle of the ELPA object for which a key/value pair should be set
    !c> *  \param  name    the name of the key
    !c> *  \param  value   the value to be set for the key
    !c> *  \param  error   on return the error code, which can be queried with elpa_strerr()
    !c> *  \result void
    !c> */
668
    !c> void elpa_set_integer(elpa_t handle, const char *name, int value, int *error);
669
    subroutine elpa_set_integer_c(handle, name_p, value, error) bind(C, name="elpa_set_integer")
670 671 672
      type(c_ptr), intent(in), value                :: handle
      type(elpa_impl_t), pointer                    :: self
      type(c_ptr), intent(in), value                :: name_p
673
      character(len=elpa_strlen_c(name_p)), pointer :: name
674 675
      integer(kind=c_int), intent(in), value        :: value
      integer(kind=c_int) , intent(in)              :: error
676 677 678 679 680 681 682

      call c_f_pointer(handle, self)
      call c_f_pointer(name_p, name)
      call elpa_set_integer(self, name, value, error)
    end subroutine


683 684 685 686 687 688 689 690 691
    !c> /*! \brief C interface for the implementation of the elpa_get_integer method
    !c> *  This method is available to the user as C generic elpa_get method
    !c> *
    !c> *  \param  handle  handle of the ELPA object for which a key/value pair should be queried
    !c> *  \param  name    the name of the key
    !c> *  \param  value   the value to be obtain for the key
    !c> *  \param  error   on return the error code, which can be queried with elpa_strerr()
    !c> *  \result void
    !c> */
692 693
    !c> void elpa_get_integer(elpa_t handle, const char *name, int *value, int *error);
    subroutine elpa_get_integer_c(handle, name_p, value, error) bind(C, name="elpa_get_integer")
694 695 696
      type(c_ptr), intent(in), value                :: handle
      type(elpa_impl_t), pointer                    :: self
      type(c_ptr), intent(in), value                :: name_p
697
      character(len=elpa_strlen_c(name_p)), pointer :: name
698 699
      integer(kind=c_int)                           :: value
      integer(kind=c_int), intent(inout)            :: error
700
 
701 702
      call c_f_pointer(handle, self)
      call c_f_pointer(name_p, name)
703 704
      call elpa_get_integer(self, name, value, error)
    end subroutine
705 706


707 708 709 710 711
    !> \brief function to check whether a key/value pair is set
    !> Parameters
    !> \param   self       class(elpa_impl_t) the allocated ELPA object
    !> \param   name       string, the key
    !> \result  state      integer, the state of the key/value pair
712 713
    function elpa_is_set(self, name) result(state)
      class(elpa_impl_t)       :: self
714
      character(*), intent(in) :: name
715
      integer                  :: state
716

717
      state = elpa_index_value_is_set_c(self%index, name // c_null_char)
718 719
    end function

720 721 722 723 724 725
    !> \brief function to check whether a key/value pair can be set
    !> Parameters
    !> \param   self       class(elpa_impl_t) the allocated ELPA object
    !> \param   name       string, the key
    !> \param   value      integer, value
    !> \result  error      integer, error code
726 727 728 729 730 731 732 733 734 735
    function elpa_can_set(self, name, value) result(error)
      class(elpa_impl_t)       :: self
      character(*), intent(in) :: name
      integer(kind=c_int), intent(in) :: value
      integer                  :: error

      error = elpa_index_int_is_valid_c(self%index, name // c_null_char, value)
    end function


736 737 738 739 740 741
    !> \brief function to convert a value to an human readable string
    !> Parameters
    !> \param   self        class(elpa_impl_t) the allocated ELPA object
    !> \param   option_name string: the name of the options, whose value should be converted
    !> \param   error       integer: errpr code
    !> \result  string      string: the humanreadable string   
742
    function elpa_value_to_string(self, option_name, error) result(string)
743 744
      class(elpa_impl_t), intent(in) :: self
      character(kind=c_char, len=*), intent(in) :: option_name
745 746 747 748
      type(c_ptr) :: ptr
      integer, intent(out), optional :: error
      integer :: val, actual_error
      character(kind=c_char, len=elpa_index_int_value_to_strlen_c(self%index, option_name // C_NULL_CHAR)), pointer :: string
749

750 751
      nullify(string)

752
      call self%get(option_name, val, actual_error)
753 754 755 756 757
      if (actual_error /= ELPA_OK) then
        if (present(error)) then
          error = actual_error
        endif
        return
758 759
      endif

760 761 762 763
      actual_error = elpa_int_value_to_string_c(option_name // C_NULL_CHAR, val, ptr)
      if (c_associated(ptr)) then
        call c_f_pointer(ptr, string)
      endif
764

765 766 767 768
      if (present(error)) then
        error = actual_error
      endif
    end function
769

770

771 772 773 774 775 776 777 778 779
    !c> /*! \brief C interface for the implementation of the elpa_set_double method
    !c> *  This method is available to the user as C generic elpa_set method
    !c> *
    !c> *  \param  handle  handle of the ELPA object for which a key/value pair should be set
    !c> *  \param  name    the name of the key
    !c> *  \param  value   the value to be set for the key
    !c> *  \param  error   on return the error code, which can be queried with elpa_strerr()
    !c> *  \result void
    !c> */
780
    !c> void elpa_set_double(elpa_t handle, const char *name, double value, int *error);
781
    subroutine elpa_set_double_c(handle, name_p, value, error) bind(C, name="elpa_set_double")
782 783 784
      type(c_ptr), intent(in), value                :: handle
      type(elpa_impl_t), pointer                    :: self
      type(c_ptr), intent(in), value                :: name_p
785
      character(len=elpa_strlen_c(name_p)), pointer :: name
786 787
      real(kind=c_double), intent(in), value        :: value
      integer(kind=c_int), intent(in)               :: error
788

789 790 791 792 793
      call c_f_pointer(handle, self)
      call c_f_pointer(name_p, name)
      call elpa_set_double(self, name, value, error)
    end subroutine

794

795
    !c> /*! \brief C interface for the implementation of the elpa_get_double method
796 797 798 799 800 801 802 803
    !c> *  This method is available to the user as C generic elpa_get method
    !c> *
    !c> *  \param  handle  handle of the ELPA object for which a key/value pair should be queried
    !c> *  \param  name    the name of the key
    !c> *  \param  value   the value to be obtain for the key
    !c> *  \param  error   on return the error code, which can be queried with elpa_strerr()
    !c> *  \result void
    !c> */
804 805
    !c> void elpa_get_double(elpa_t handle, const char *name, double *value, int *error);
    subroutine elpa_get_double_c(handle, name_p, value, error) bind(C, name="elpa_get_double")
806 807 808
      type(c_ptr), intent(in), value                :: handle
      type(elpa_impl_t), pointer                    :: self
      type(c_ptr), intent(in), value                :: name_p
809
      character(len=elpa_strlen_c(name_p)), pointer :: name
810 811
      real(kind=c_double)                           :: value
      integer(kind=c_int), intent(inout)            :: error
812

813 814
      call c_f_pointer(handle, self)
      call c_f_pointer(name_p, name)
815 816
      call elpa_get_double(self, name, value, error)
    end subroutine
817
 
818

819 820 821 822 823
    !> \brief function to associate a pointer with an integer value
    !> Parameters
    !> \param   self        class(elpa_impl_t) the allocated ELPA object
    !> \param   name        string: the name of the entry
    !> \result  value       integer, pointer: the value for the entry
824
    function elpa_associate_int(self, name) result(value)
825
      class(elpa_impl_t)             :: self
826 827
      character(*), intent(in)       :: name
      integer(kind=c_int), pointer   :: value
828

829 830
      type(c_ptr)                    :: value_p

831
      value_p = elpa_index_get_int_loc_c(self%index, name // c_null_char)
832 833 834
      if (.not. c_associated(value_p)) then
        write(error_unit, '(a,a,a)') "ELPA: Warning, received NULL pointer for entry '", name, "'"
      endif
835 836
      call c_f_pointer(value_p, value)
    end function
837

838

839 840 841 842 843 844 845
    !> \brief function to querry the timing information at a certain level
    !> Parameters
    !> \param   self            class(elpa_impl_t) the allocated ELPA object
    !> \param   name1 .. name6  string: the string identifier for the timer region.
    !>                                  at the moment 6 nested levels can be queried
    !> \result  s               double: the timer metric for the region. Might be seconds,
    !>                                  or any other supported metric
846 847 848 849 850 851
    function elpa_get_time(self, name1, name2, name3, name4, name5, name6) result(s)
      class(elpa_impl_t), intent(in) :: self
      ! this is clunky, but what can you do..
      character(len=*), intent(in), optional :: name1, name2, name3, name4, name5, name6
      real(kind=c_double) :: s

852
#ifdef HAVE_DETAILED_TIMINGS
853
      s = self%timer%get(name1, name2, name3, name4, name5, name6)
854 855 856
#else
      s = -1.0
#endif
857 858 859
    end function


860 861 862 863 864
    !> \brief function to print the timing tree below at a certain level
    !> Parameters
    !> \param   self            class(elpa_impl_t) the allocated ELPA object
    !> \param   name1 .. name6  string: the string identifier for the timer region.
    !>                                  at the moment 4 nested levels can be specified
865
    subroutine elpa_print_times(self, name1, name2, name3, name4)
866
      class(elpa_impl_t), intent(in) :: self
867
      character(len=*), intent(in), optional :: name1, name2, name3, name4
868
#ifdef HAVE_DETAILED_TIMINGS
869
      call self%timer%print(name1, name2, name3, name4)
870
#endif
871 872
    end subroutine

873

874 875 876 877
    !> \brief function to start the timing of a code region
    !> Parameters
    !> \param   self            class(elpa_impl_t) the allocated ELPA object
    !> \param   name            string: a chosen identifier name for the code region
878 879 880 881 882 883 884 885 886
    subroutine elpa_timer_start(self, name)
      class(elpa_impl_t), intent(inout) :: self
      character(len=*), intent(in) :: name
#ifdef HAVE_DETAILED_TIMINGS
      call self%timer%start(name)
#endif
    end subroutine


887 888 889 890
    !> \brief function to stop the timing of a code region
    !> Parameters
    !> \param   self            class(elpa_impl_t) the allocated ELPA object
    !> \param   name            string: identifier name for the code region to stop
891 892 893 894 895 896 897 898 899
    subroutine elpa_timer_stop(self, name)
      class(elpa_impl_t), intent(inout) :: self
      character(len=*), intent(in) :: name
#ifdef HAVE_DETAILED_TIMINGS
      call self%timer%stop(name)
#endif
    end subroutine


900 901 902
    !> \brief function to destroy an elpa object
    !> Parameters
    !> \param   self            class(elpa_impl_t) the allocated ELPA object
903 904
    !> \param   error           integer, optional error code
    subroutine elpa_destroy(self, error)
905
#ifdef WITH_MPI
906 907 908
      integer                              :: mpi_comm_rows, mpi_comm_cols, &
                                              mpierr, mpierr2, mpi_string_length
      character(len=MPI_MAX_ERROR_STRING)  :: mpierr_string
909
#endif
910 911 912 913 914 915 916 917 918 919 920
      class(elpa_impl_t)                   :: self
#ifdef USE_FORTRAN2008
      integer, optional, intent(out)       :: error
#else
      integer, intent(out)                 :: error
#endif
      integer                              :: error2

      if (present(error)) then
        error = ELPA_OK
      endif
921 922 923

#ifdef WITH_MPI
      if (self%communicators_owned == 1) then
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
        call self%get("mpi_comm_rows", mpi_comm_rows, error2)
        if (error2 .ne. ELPA_OK) then
          if (present(error)) then
            error = error2
            return
          else
            write(error_unit, *) "Error in elpa_destroy but you do not check the error codes!"
            return
          endif
        endif
        call self%get("mpi_comm_cols", mpi_comm_cols,error2)
        if (error2 .ne. ELPA_OK) then
          if (present(error)) then
            error = error2
            return
          else
            write(error_unit, *) "Error in elpa_destroy but you do not check the error codes!"
            return
          endif
        endif
944

945 946
        ! this is just for debugging ! do not leave in a relase
        !write(error_unit, '(A,2I13)') "FREE comms", mpi_comm_rows, mpi_comm_cols
947
        call mpi_comm_free(mpi_comm_rows, mpierr)
948 949 950
        if (mpierr .ne. MPI_SUCCESS) then
          call MPI_ERROR_STRING(mpierr,mpierr_string, mpi_string_length, mpierr2)
          write(error_unit,*) "MPI ERROR occured during mpi_comm_free for row communicator: ", trim(mpierr_string)
951 952 953
          if (present(error)) then
            error = ELPA_ERROR_CRITICAL
          endif
954 955
          return
        endif
956 957 958 959 960 961 962 963 964 965
        call self%set("mpi_comm_cols", -12345, error2)
        if (error2 .ne. ELPA_OK) then
          if (present(error)) then
            error = error2
            return
          else
            write(error_unit, *) "Error in elpa_destroy but you do not check the error codes!"
            return
          endif
        endif
966
        call mpi_comm_free(mpi_comm_cols, mpierr)
967 968 969
        if (mpierr .ne. MPI_SUCCESS) then
          call MPI_ERROR_STRING(mpierr,mpierr_string, mpi_string_length, mpierr2)
          write(error_unit,*) "MPI ERROR occured during mpi_comm_free for col communicator: ", trim(mpierr_string)
970 971 972
          if (present(error)) then
            error = ELPA_ERROR_CRITICAL
          endif
973 974
          return
        endif
975 976 977 978 979 980 981 982 983 984
        call self%set("mpi_comm_rows", -12345,error2)
        if (error2 .ne. ELPA_OK) then
          if (present(error)) then
            error = error2
            return
          else
            write(error_unit, *) "Error in elpa_destroy but you do not check the error codes!"
            return
          endif
        endif
985 986 987
      endif
#endif

988
      call timer_free(self%timer)
989
      call timer_free(self%autotune_timer)
990 991
      call elpa_index_free_c(self%index)

992
    end subroutine
993

Pavel Kus's avatar
Pavel Kus committed
994 995
#define REALCASE 1
#define DOUBLE_PRECISION 1
Andreas Marek's avatar
Andreas Marek committed
996
#define INCLUDE_ROUTINES 1
Pavel Kus's avatar
Pavel Kus committed
997
#include "general/precision_macros.h"
Andreas Marek's avatar
Andreas Marek committed
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
#include "elpa_impl_math_template.F90"
#undef REALCASE
#undef DOUBLE_PRECISION
#undef INCLUDE_ROUTINES

#ifdef WANT_SINGLE_PRECISION_REAL
#define INCLUDE_ROUTINES 1
#endif
#define REALCASE 1
#define SINGLE_PRECISION 1
#include "general/precision_macros.h"
#include "elpa_impl_math_template.F90"
#undef REALCASE
#undef SINGLE_PRECISION
#undef INCLUDE_ROUTINES

#define COMPLEXCASE 1
#define DOUBLE_PRECISION 1
#define INCLUDE_ROUTINES 1
#include "general/precision_macros.h"
#include "elpa_impl_math_template.F90"
#undef DOUBLE_PRECISION
#undef COMPLEXCASE
#undef INCLUDE_ROUTINES

#ifdef WANT_SINGLE_PRECISION_COMPLEX
#define INCLUDE_ROUTINES 1
#endif
#define COMPLEXCASE 1
#define SINGLE_PRECISION
#include "general/precision_macros.h"
#include "elpa_impl_math_template.F90"
#undef COMPLEXCASE
#undef SINGLE_PRECISION
#undef INCLUDE_ROUTINES

#define REALCASE 1
#define DOUBLE_PRECISION 1
#include "general/precision_macros.h"
#include "elpa_impl_generalized_transform_template.F90"
Pavel Kus's avatar
Pavel Kus committed
1038 1039 1040 1041 1042 1043 1044
#undef REALCASE
#undef DOUBLE_PRECISION

#ifdef WANT_SINGLE_PRECISION_REAL
#define REALCASE 1
#define SINGLE_PRECISION 1
#include "general/precision_macros.h"
Andreas Marek's avatar
Andreas Marek committed
1045
#include "elpa_impl_generalized_transform_template.F90"
Pavel Kus's avatar
Pavel Kus committed
1046 1047
#undef REALCASE
#undef SINGLE_PRECISION
Andreas Marek's avatar
Andreas Marek committed
1048
#endif
Pavel Kus's avatar
Pavel Kus committed
1049 1050

#define COMPLEXCASE 1
Andreas Marek's avatar
Andreas Marek committed
1051

Pavel Kus's avatar
Pavel Kus committed
1052 1053
#define DOUBLE_PRECISION 1
#include "general/precision_macros.h"
Andreas Marek's avatar
Andreas Marek committed
1054
#include "elpa_impl_generalized_transform_template.F90"
Pavel Kus's avatar
Pavel Kus committed
1055 1056 1057 1058 1059 1060 1061
#undef DOUBLE_PRECISION
#undef COMPLEXCASE

#ifdef WANT_SINGLE_PRECISION_COMPLEX
#define COMPLEXCASE 1
#define SINGLE_PRECISION
#include "general/precision_macros.h"
Andreas Marek's avatar
Andreas Marek committed
1062
#include "elpa_impl_generalized_transform_template.F90"
Pavel Kus's avatar
Pavel Kus committed
1063 1064
#undef COMPLEXCASE
#undef SINGLE_PRECISION
Andreas Marek's avatar
Andreas Marek committed
1065 1066
#endif

Andreas Marek's avatar
Andreas Marek committed
1067

1068 1069 1070 1071 1072 1073
!    function use_cannons_algorithm(self) result(use_cannon, do_print)
!      class(elpa_impl_t), intent(inout), target :: self
!      logical                                   :: use_cannon
!      logical, intent(in)                       :: do_print
!    end function
!
1074 1075 1076 1077




1078
#ifdef ENABLE_AUTOTUNING
1079 1080
    !> \brief function to setup the ELPA autotuning and create the autotune object
    !> Parameters
1081
    !> \param   self            the allocated ELPA object
1082 1083
    !> \param   level           integer: the "thoroughness" of the planed autotuning
    !> \param   domain          integer: the domain (real/complex) which should be tuned
1084
    !> \result  tune_state      the created autotuning object
1085
    function elpa_autotune_setup(self, level, domain, error) result(tune_state)
1086
      class(elpa_impl_t), intent(inout), target :: self
1087 1088 1089
      integer, intent(in)                       :: level, domain
      type(elpa_autotune_impl_t), pointer       :: ts_impl
      class(elpa_autotune_t), pointer           :: tune_state
1090 1091 1092 1093 1094
#ifdef USE_FORTRAN2008
      integer(kind=c_int), optional             :: error
#else
      integer(kind=c_int)                       :: error
#endif
1095

1096 1097 1098 1099 1100 1101 1102
#ifdef USE_FORTRAN2008
      if (present(error)) then
        error = ELPA_OK
      endif
#else
      error = ELPA_OK
#endif
1103
      if (elpa_get_api_version() < EARLIEST_AUTOTUNE_VERSION) then
1104 1105 1106
        write(error_unit, "(a,i0,a)") "ELPA: Error API version: Autotuning does not support ", elpa_get_api_version()
#ifdef USE_FORTRAN2008
        if (present(error)) then
1107
          error = ELPA_ERROR_AUTOTUNE_API_VERSION
1108 1109
        endif
#else
1110
        error = ELPA_ERROR_AUTOTUNE_API_VERSION
1111 1112
#endif
        return
1113 1114
      endif

1115 1116 1117 1118 1119
      allocate(ts_impl)
      ts_impl%parent => self
      ts_impl%level = level
      ts_impl%domain = domain

1120
      ts_impl%current = -1
1121
      ts_impl%min_loc = -1
1122
      ts_impl%cardinality = elpa_index_autotune_cardinality_c(self%index, level, domain)
1123 1124 1125 1126 1127 1128 1129

      tune_state => ts_impl

      call self%autotune_timer%enable()
    end function


1130

1131 1132 1133 1134 1135 1136 1137
    !c> /*! \brief C interface for the implementation of the elpa_autotune_setup method
    !c> *
    !c> *  \param  elpa_t           handle: of the ELPA object which should be tuned
    !c> *  \param  int              level:  "thoroughness" of autotuning
    !c> *  \param  int              domain: real/complex autotuning
    !c> *  \result elpa_autotune_t  handle:  on the autotune object
    !c> */
1138 1139
    !c> elpa_autotune_t elpa_autotune_setup(elpa_t handle, int level, int domain, int *error);
    function elpa_autotune_setup_c(handle ,level, domain, error) result(ptr) bind(C, name="elpa_autotune_setup")
1140 1141 1142 1143 1144 1145 1146
      type(c_ptr), intent(in), value         :: handle
      type(elpa_impl_t), pointer             :: self
      class(elpa_autotune_t), pointer        :: tune_state
      type(elpa_autotune_impl_t), pointer    :: obj        
      integer(kind=c_int), intent(in), value :: level
      integer(kind=c_int), intent(in), value :: domain
      type(c_ptr)                            :: ptr
1147
      integer(kind=c_int) , intent(in)       :: error
1148 1149

      call c_f_pointer(handle, self)
1150

1151
      tune_state => self%autotune_setup(level, domain, error)
1152
      select type(tune_state)
1153
        type is (elpa_autotune_impl_t)
1154 1155 1156
          obj => tune_state
        class default
          print *, "This should not happen"
1157
          stop
1158 1159
      end select                
      ptr = c_loc(obj)
1160

1161
    end function
1162 1163


1164 1165 1166 1167 1168
    !> \brief function to do an autotunig step
    !> Parameters
    !> \param   self            class(elpa_impl_t) the allocated ELPA object
    !> \param   tune_state      class(elpa_autotune_t): the autotuning object
    !> \result  unfinished      logical: describes the state of the autotuning (completed/uncompleted)
1169
    function elpa_autotune_step(self, tune_state, error) result(unfinished)
1170
      implicit none
1171
      class(elpa_impl_t), intent(inout)             :: self
1172
      class(elpa_autotune_t), intent(inout), target :: tune_state
1173
      type(elpa_autotune_impl_t), pointer           :: ts_impl
Andreas Marek's avatar
Andreas Marek committed
1174 1175 1176 1177 1178
#ifdef USE_FORTRAN2008
      integer(kind=c_int), optional, intent(out)    :: error
#else
      integer(kind=c_int),  intent(out)             :: error
#endif
1179 1180
      integer(kind=c_int)                           :: error2, error3
      integer                                       :: mpierr, mpierr2, mpi_comm_parent, mpi_string_length, np_total
Andreas Marek's avatar
Andreas Marek committed
1181 1182
      logical                                       :: unfinished
      integer                                       :: i
1183 1184 1185 1186
      real(kind=C_DOUBLE)                           :: time_spent, sendbuf(1), recvbuf(1)
#ifdef WITH_MPI
      character(len=MPI_MAX_ERROR_STRING)           :: mpierr_string
#endif
1187

1188 1189 1190
      if (present(error)) then
        error = ELPA_OK
      endif
1191
      select type(tune_state)
1192
        type is (elpa_autotune_impl_t)
1193 1194 1195
          ts_impl => tune_state
        class default
          print *, "This should not happen"
1196 1197 1198
          if (present(error)) then
            error = ELPA_OK
          endif
1199 1200 1201 1202
      end select

      unfinished = .false.

1203
      if (ts_impl%current >= 0) then
1204
#ifdef HAVE_DETAILED_TIMINGS
1205
        time_spent = self%autotune_timer%get("accumulator")
1206 1207
#else
        print *, "Cannot do autotuning without detailed timings"
1208

1209
        ! TODO check this. Do we really want to return only if error is present? And should it be ELPA_OK?
1210 1211 1212 1213
        if (present(error)) then
          error = ELPA_OK
          return
        endif
1214
#endif
1215 1216 1217 1218 1219

#ifdef WITH_MPI
        ! find the average time spent .. we need a unique value on all ranks
        call self%get("mpi_comm_parent", mpi_comm_parent, error2)
        call self%get("num_processes", np_total, error3)
1220
        if ((error2 .ne. ELPA_OK) .or. (error3 .ne. ELPA_OK)) then
1221
          print *, "Parrent communicator is not set properly. Aborting..."
1222
          if (present(error)) &
1223 1224 1225 1226 1227 1228 1229 1230
            error = ELPA_ERROR_CRITICAL
          return
        endif

        sendbuf(1) = time_spent
        call MPI_Allreduce(sendbuf, recvbuf, 1, MPI_REAL8, MPI_SUM, mpi_comm_parent, mpierr)
        if (mpierr .ne. MPI_SUCCESS) then
          call MPI_ERROR_STRING(mpierr,mpierr_string, mpi_string_length, mpierr2)
Pavel Kus's avatar
Pavel Kus committed
1231
          write(error_unit,*) "MPI ERROR occured during elpa_autotune_step: ", trim(mpierr_string)
1232 1233 1234 1235 1236
          return
        endif
        time_spent = recvbuf(1) / np_total
#endif

1237 1238
        if (ts_impl%min_loc == -1 .or. (time_spent < ts_impl%min_val)) then
          ts_impl%min_val = time_spent
1239
          ts_impl%min_loc = ts_impl%current
1240 1241 1242 1243
        end if
        call self%autotune_timer%free()
      endif

1244 1245 1246
      do while (ts_impl%current < ts_impl%cardinality - 1)
        ts_impl%current = ts_impl%current + 1
        if (elpa_index_set_autotune_parameters_c(self%index, ts_impl%level, ts_impl%domain, ts_impl%current) == 1) then
1247 1248 1249 1250 1251 1252 1253 1254
          unfinished = .true.
          return
        end if
      end do

    end function


1255

1256 1257 1258 1259 1260 1261 1262
    !c> /*! \brief C interface for the implementation of the elpa_autotune_step method
    !c> *
    !c> *  \param  elpa_t           handle: of the ELPA object which should be tuned
    !c> *  \param  elpa_autotune_t  autotune_handle: the autotuning object
    !c> *  \param  error            int *error code
    !c> *  \result int              unfinished:  describes whether autotuning finished (0) or not (1)
    !c> */
Andreas Marek's avatar
Andreas Marek committed
1263 1264 1265
    !c> int elpa_autotune_step(elpa_t handle, elpa_autotune_t autotune_handle, int *error);
    function elpa_autotune_step_c(handle, autotune_handle, &
                    error) result(unfinished) bind(C, name="elpa_autotune_step")
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
      type(c_ptr), intent(in), value       :: handle
      type(c_ptr), intent(in), value       :: autotune_handle
      type(elpa_impl_t), pointer           :: self
      type(elpa_autotune_impl_t), pointer  :: tune_state
      logical                              :: unfinished_f
      integer(kind=c_int)                  :: unfinished
      integer(kind=c_int)                  :: error

      call c_f_pointer(handle, self)
      call c_f_pointer(autotune_handle, tune_state)

      unfinished_f = self%autotune_step(tune_state, error)
      if (unfinished_f) then
        unfinished = 1
      else
        unfinished = 0
      endif

    end function
1285

Pavel Kus's avatar
Pavel Kus committed
1286
    !> \brief function to set the up-to-now best options of the autotuning
1287 1288 1289
    !> Parameters
    !> \param   self            class(elpa_impl_t) the allocated ELPA object
    !> \param   tune_state      class(elpa_autotune_t): the autotuning object
1290 1291
    !> \param   error code      optional, integer
    subroutine elpa_autotune_set_best(self, tune_state, error)
1292
      implicit none
1293
      class(elpa_impl_t), intent(inout)          :: self
1294
      class(elpa_autotune_t), intent(in), target :: tune_state
1295
      type(elpa_autotune_impl_t), pointer        :: ts_impl
Andreas Marek's avatar
Andreas Marek committed
1296
#ifdef USE_FORTRAN2008
1297
      integer(kind=ik), optional, intent(out)    :: error
Andreas Marek's avatar
Andreas Marek committed
1298 1299 1300
#else
      integer(kind=ik), intent(out)              :: error
#endif
1301 1302 1303
      if