LCOV - code coverage report
Current view: top level - src/tmc - tmc_master.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:71c3ab0) Lines: 71.6 % 338 242
Test Date: 2026-07-25 06:35:44 Functions: 100.0 % 4 4

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief module contains the master routine handling the tree creation,
      10              : !>        communication with workers and task distribution
      11              : !>        For each idle working group the master creates a new global tree
      12              : !>        element, and if neccessay a related sub tree element,
      13              : !>        OR find the next element to calculate the exact energy.
      14              : !>        Goal is to keep at least the exact energy calculation working groups
      15              : !>        as busy as possible.
      16              : !>        Master also checks for incomming results and update the tree and the
      17              : !>        acceptance ratios.
      18              : !> \par History
      19              : !>      11.2012 created [Mandes Schoenherr]
      20              : !> \author Mandes
      21              : ! **************************************************************************************************
      22              : 
      23              : MODULE tmc_master
      24              :    USE cell_methods,                    ONLY: init_cell
      25              :    USE cp_external_control,             ONLY: external_control
      26              :    USE cp_log_handling,                 ONLY: cp_to_string
      27              :    USE global_types,                    ONLY: global_environment_type
      28              :    USE kinds,                           ONLY: dp,&
      29              :                                               int_8
      30              :    USE machine,                         ONLY: m_flush,&
      31              :                                               m_memory,&
      32              :                                               m_walltime
      33              :    USE message_passing,                 ONLY: mp_para_env_type
      34              :    USE tmc_calculations,                ONLY: get_subtree_efficiency
      35              :    USE tmc_cancelation,                 ONLY: free_cancelation_list
      36              :    USE tmc_dot_tree,                    ONLY: create_dot_color,&
      37              :                                               create_global_tree_dot_color,&
      38              :                                               finalize_draw_tree,&
      39              :                                               init_draw_trees
      40              :    USE tmc_file_io,                     ONLY: print_restart_file,&
      41              :                                               write_element_in_file
      42              :    USE tmc_messages,                    ONLY: communicate_atom_types,&
      43              :                                               recv_msg,&
      44              :                                               send_msg,&
      45              :                                               stop_whole_group,&
      46              :                                               tmc_message
      47              :    USE tmc_move_handle,                 ONLY: check_moves,&
      48              :                                               print_move_types
      49              :    USE tmc_stati,                       ONLY: &
      50              :         TMC_CANCELING_MESSAGE, TMC_CANCELING_RECEIPT, TMC_STATUS_FAILED, &
      51              :         TMC_STATUS_WAIT_FOR_NEW_TASK, TMC_STATUS_WORKER_INIT, TMC_STAT_ANALYSIS_REQUEST, &
      52              :         TMC_STAT_ANALYSIS_RESULT, TMC_STAT_APPROX_ENERGY_REQUEST, TMC_STAT_APPROX_ENERGY_RESULT, &
      53              :         TMC_STAT_ENERGY_REQUEST, TMC_STAT_ENERGY_RESULT, TMC_STAT_INIT_ANALYSIS, &
      54              :         TMC_STAT_MD_REQUEST, TMC_STAT_MD_RESULT, TMC_STAT_NMC_REQUEST, TMC_STAT_NMC_RESULT, &
      55              :         TMC_STAT_SCF_STEP_ENER_RECEIVE, TMC_STAT_START_CONF_REQUEST, TMC_STAT_START_CONF_RESULT
      56              :    USE tmc_tree_acceptance,             ONLY: check_acceptance_of_depending_subtree_nodes,&
      57              :                                               check_elements_for_acc_prob_update,&
      58              :                                               tree_update
      59              :    USE tmc_tree_build,                  ONLY: create_new_gt_tree_node,&
      60              :                                               deallocate_sub_tree_node,&
      61              :                                               finalize_init,&
      62              :                                               finalize_trees,&
      63              :                                               init_tree_mod,&
      64              :                                               remove_all_trees
      65              :    USE tmc_tree_search,                 ONLY: count_nodes_in_trees,&
      66              :                                               count_prepared_nodes_in_trees,&
      67              :                                               search_next_energy_calc
      68              :    USE tmc_tree_types,                  ONLY: &
      69              :         elem_array_type, elem_list_type, global_tree_type, status_accepted, &
      70              :         status_calc_approx_ener, status_calculate_MD, status_calculate_NMC_steps, &
      71              :         status_calculate_energy, status_calculated, status_cancel_ener, status_cancel_nmc, &
      72              :         status_canceled_ener, status_canceled_nmc, status_created, status_rejected, tree_type
      73              :    USE tmc_types,                       ONLY: tmc_env_type
      74              : #include "../base/base_uses.f90"
      75              : 
      76              :    IMPLICIT NONE
      77              : 
      78              :    PRIVATE
      79              : 
      80              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'tmc_master'
      81              : 
      82              :    PUBLIC :: do_tmc_master
      83              : 
      84              :    INTEGER, PARAMETER :: DEBUG = 0
      85              : 
      86              : CONTAINS
      87              : 
      88              : ! **************************************************************************************************
      89              : !> \brief send cancel request to all workers processing elements in the list
      90              : !> \param cancel_list list with elements to cancel
      91              : !> \param work_list list with all elements processed by working groups
      92              : !> \param cancel_count counter of canceled elements
      93              : !> \param para_env communication environment
      94              : !> \param tmc_env ...
      95              : !> \author Mandes 12.2012
      96              : ! **************************************************************************************************
      97         8789 :    SUBROUTINE cancel_calculations(cancel_list, work_list, cancel_count, &
      98              :                                   para_env, tmc_env)
      99              :       TYPE(elem_list_type), POINTER                      :: cancel_list
     100              :       TYPE(elem_array_type), DIMENSION(:), POINTER       :: work_list
     101              :       INTEGER                                            :: cancel_count
     102              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     103              :       TYPE(tmc_env_type), POINTER                        :: tmc_env
     104              : 
     105              :       INTEGER                                            :: i, stat, wg
     106              :       TYPE(elem_list_type), POINTER                      :: tmp_element
     107              : 
     108         8789 :       IF (.NOT. ASSOCIATED(cancel_list)) RETURN
     109            0 :       NULLIFY (tmp_element)
     110              : 
     111            0 :       CPASSERT(ASSOCIATED(tmc_env))
     112            0 :       CPASSERT(ASSOCIATED(tmc_env%params))
     113            0 :       CPASSERT(ASSOCIATED(tmc_env%m_env))
     114            0 :       CPASSERT(ASSOCIATED(work_list))
     115            0 :       CPASSERT(ASSOCIATED(para_env))
     116              : 
     117            0 :       stat = TMC_STATUS_FAILED
     118            0 :       wg = -1
     119            0 :       cancel_elem_loop: DO
     120              :          ! find certain working group calculating this element
     121            0 :          working_elem_loop: DO i = 1, SIZE(work_list)
     122              :             ! in special cases element could be distributed to several working groups,
     123              :             ! but all, except of one, should already be in canceling process
     124            0 :             IF ((.NOT. work_list(i)%canceled) .AND. &
     125            0 :                 ASSOCIATED(work_list(i)%elem)) THEN
     126            0 :                IF (ASSOCIATED(cancel_list%elem, work_list(i)%elem)) THEN
     127            0 :                   stat = TMC_CANCELING_MESSAGE
     128            0 :                   wg = i
     129            0 :                   EXIT working_elem_loop
     130              :                END IF
     131              :             END IF
     132              :          END DO working_elem_loop
     133              : 
     134            0 :          CPASSERT(wg >= 0)
     135            0 :          CPASSERT(stat /= TMC_STATUS_FAILED)
     136            0 :          CPASSERT(work_list(wg)%elem%stat /= status_calc_approx_ener)
     137              : 
     138              :          IF (DEBUG >= 1) THEN
     139              :             WRITE (tmc_env%m_env%io_unit, *) &
     140              :                "TMC|master: cancel group "//cp_to_string(wg)
     141              :          END IF
     142              :          CALL tmc_message(msg_type=stat, send_recv=send_msg, dest=wg, &
     143            0 :                           para_env=para_env, tmc_params=tmc_env%params)
     144            0 :          work_list(wg)%canceled = .TRUE.
     145              : 
     146              :          ! counting the amount of canceled elements
     147            0 :          cancel_count = cancel_count + 1
     148              : 
     149              :          ! delete element from canceling list
     150            0 :          IF (.NOT. ASSOCIATED(cancel_list%next)) THEN
     151            0 :             DEALLOCATE (cancel_list)
     152              :             cancel_list => NULL()
     153              :             EXIT cancel_elem_loop
     154              :          ELSE
     155            0 :             tmp_element => cancel_list%next
     156            0 :             DEALLOCATE (cancel_list)
     157            0 :             cancel_list => tmp_element
     158              :          END IF
     159              :       END DO cancel_elem_loop
     160              :    END SUBROUTINE cancel_calculations
     161              : 
     162              : ! **************************************************************************************************
     163              : !> \brief send analysis request to a worker
     164              : !> \param ana_list list with elements to be analysed
     165              : !> \param ana_worker_info ...
     166              : !> \param para_env communication environment
     167              : !> \param tmc_env ...
     168              : !> \author Mandes 12.2012
     169              : ! **************************************************************************************************
     170       255431 :    SUBROUTINE send_analysis_tasks(ana_list, ana_worker_info, para_env, tmc_env)
     171              :       TYPE(elem_list_type), POINTER                      :: ana_list
     172              :       TYPE(elem_array_type), DIMENSION(:), POINTER       :: ana_worker_info
     173              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     174              :       TYPE(tmc_env_type), POINTER                        :: tmc_env
     175              : 
     176              :       INTEGER                                            :: dest, stat, wg
     177              :       TYPE(elem_list_type), POINTER                      :: list_tmp
     178              : 
     179       255431 :       NULLIFY (list_tmp)
     180              : 
     181       255431 :       CPASSERT(ASSOCIATED(ana_worker_info))
     182       255431 :       CPASSERT(ASSOCIATED(para_env))
     183              : 
     184       255431 :       wg_loop: DO wg = 1, SIZE(ana_worker_info)
     185            0 :          IF (.NOT. ASSOCIATED(ana_list)) EXIT wg_loop
     186       255431 :          IF (.NOT. ana_worker_info(wg)%busy) THEN
     187            0 :             stat = TMC_STAT_ANALYSIS_REQUEST
     188            0 :             dest = wg
     189              :             CALL tmc_message(msg_type=stat, send_recv=send_msg, dest=dest, &
     190              :                              para_env=para_env, tmc_params=tmc_env%params, &
     191            0 :                              list_elem=ana_list)
     192            0 :             IF (.NOT. ASSOCIATED(ana_list%next)) THEN
     193            0 :                DEALLOCATE (ana_list)
     194              :                ana_list => NULL()
     195              :             ELSE
     196            0 :                list_tmp => ana_list%next
     197            0 :                DEALLOCATE (ana_list)
     198            0 :                ana_list => list_tmp
     199              :             END IF
     200              :          END IF
     201              :       END DO wg_loop
     202       255431 :    END SUBROUTINE send_analysis_tasks
     203              : 
     204              : ! **************************************************************************************************
     205              : !> \brief global master handling tree creation and communication/work
     206              : !>        distribution with workers
     207              : !> \param tmc_env structure for storing all the tmc parameters
     208              : !> \param globenv global environment for external control
     209              : !> \author Mandes 11.2012
     210              : ! **************************************************************************************************
     211           14 :    SUBROUTINE do_tmc_master(tmc_env, globenv)
     212              :       TYPE(tmc_env_type), POINTER                        :: tmc_env
     213              :       TYPE(global_environment_type), POINTER             :: globenv
     214              : 
     215              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'do_tmc_master'
     216              : 
     217              :       INTEGER :: cancel_count, handle, last_output, reactivation_cc_count, &
     218              :          reactivation_ener_count, restart_count, restarted_elem_nr, stat, walltime_delay, &
     219              :          walltime_offset, wg, worker_counter
     220              :       INTEGER(KIND=int_8)                                :: mem
     221              :       INTEGER, DIMENSION(6)                              :: nr_of_job
     222           14 :       INTEGER, DIMENSION(:), POINTER                     :: tree_elem_counters, tree_elem_heads
     223              :       LOGICAL                                            :: external_stop, flag, l_update_tree
     224              :       REAL(KIND=dp)                                      :: run_time_start
     225              :       REAL(KIND=dp), DIMENSION(4)                        :: worker_timings_aver
     226           14 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: efficiency
     227           14 :       TYPE(elem_array_type), DIMENSION(:), POINTER       :: ana_worker_info, worker_info
     228              :       TYPE(global_tree_type), POINTER                    :: gt_elem_tmp
     229              :       TYPE(tree_type), POINTER                           :: init_conf
     230              : 
     231           14 :       external_stop = .FALSE.
     232           14 :       restarted_elem_nr = 0
     233           14 :       NULLIFY (init_conf, worker_info, ana_worker_info, gt_elem_tmp, tree_elem_counters)
     234              : 
     235            0 :       CPASSERT(ASSOCIATED(tmc_env))
     236              : 
     237           14 :       CPASSERT(tmc_env%tmc_comp_set%group_nr == 0)
     238           14 :       CPASSERT(ASSOCIATED(tmc_env%tmc_comp_set))
     239           14 :       CPASSERT(ASSOCIATED(tmc_env%tmc_comp_set%para_env_m_w))
     240              : 
     241           14 :       CPASSERT(ASSOCIATED(tmc_env%m_env))
     242              : 
     243              :       !-- run time measurment, to end just in time
     244              :       ! start the timing
     245           14 :       CALL timeset(routineN, handle)
     246           14 :       run_time_start = m_walltime()
     247           14 :       walltime_delay = 0
     248           14 :       walltime_offset = 20 ! default value the whole program needs to finalize
     249              : 
     250              :       ! initialize the different modules
     251           14 :       IF (tmc_env%params%DRAW_TREE) THEN
     252            1 :          CALL init_draw_trees(tmc_params=tmc_env%params)
     253              :       END IF
     254              : 
     255              :       !-- initialize variables
     256              :       ! nr_of_job: counting the different task send / received
     257              :       !  (1:NMC submitted, 2:energies submitted, 3:NMC finished 4:energy finished, 5:NMC canceled, 6:energy canceled)
     258           14 :       nr_of_job(:) = 0
     259           14 :       worker_counter = -1
     260           14 :       reactivation_ener_count = 0
     261           14 :       reactivation_cc_count = 0
     262           14 :       cancel_count = 0
     263           54 :       tmc_env%m_env%result_count = 0
     264           14 :       l_update_tree = .FALSE.
     265           14 :       restart_count = 1
     266           14 :       last_output = -1
     267              :       ! average timings
     268              :       !  (1:calculated NMC, 2:calculated ener, 3:canceled NMC, 4: canceled ener)
     269           14 :       worker_timings_aver(:) = 0.0_dp
     270              :       ! remembers state of workers and their actual configurations
     271              :       ! the actual working group, communicating with
     272           56 :       ALLOCATE (worker_info(tmc_env%tmc_comp_set%para_env_m_w%num_pe - 1))
     273           28 :       ALLOCATE (ana_worker_info(tmc_env%tmc_comp_set%para_env_m_ana%num_pe - 1))
     274              : 
     275              :       ! get the start configuration form the first (exact energy) worker,
     276              :       !   master should/could have no Force environment
     277           14 :       stat = TMC_STAT_START_CONF_REQUEST
     278           14 :       wg = 1
     279              :       CALL tmc_message(msg_type=stat, send_recv=send_msg, dest=wg, &
     280              :                        para_env=tmc_env%tmc_comp_set%para_env_m_w, &
     281              :                        tmc_params=tmc_env%params, &
     282           14 :                        wait_for_message=.TRUE.)
     283              :       !-- wait for start configuration results and number of dimensions
     284              :       !-- get start configuration (init_conf element should not be allocated already)
     285              :       CALL tmc_message(msg_type=stat, send_recv=recv_msg, dest=wg, &
     286              :                        para_env=tmc_env%tmc_comp_set%para_env_m_w, &
     287              :                        tmc_params=tmc_env%params, &
     288           14 :                        elem=init_conf, success=flag, wait_for_message=.TRUE.)
     289           14 :       IF (stat /= TMC_STAT_START_CONF_RESULT) THEN
     290              :          CALL cp_abort(__LOCATION__, &
     291              :                        "receiving start configuration failed, received stat "// &
     292            0 :                        cp_to_string(stat))
     293              :       END IF
     294              :       ! get the atom names from first energy worker
     295              :       CALL communicate_atom_types(atoms=tmc_env%params%atoms, &
     296              :                                   source=1, &
     297           14 :                                   para_env=tmc_env%tmc_comp_set%para_env_m_first_w)
     298              : 
     299           14 :       CALL init_cell(cell=tmc_env%params%cell)
     300              : 
     301              :       ! check the configuration consitency with selected moves
     302              :       CALL check_moves(tmc_params=tmc_env%params, &
     303              :                        move_types=tmc_env%params%move_types, &
     304           14 :                        mol_array=init_conf%mol)
     305           14 :       IF (ASSOCIATED(tmc_env%params%nmc_move_types)) THEN
     306              :          CALL check_moves(tmc_params=tmc_env%params, &
     307              :                           move_types=tmc_env%params%nmc_move_types, &
     308            5 :                           mol_array=init_conf%mol)
     309              :       END IF
     310              : 
     311              :       ! set initial configuration
     312              :       ! set initial random number generator seed (rng seed)
     313              :       ! initialize the tree structure espacially for parallel tmepering,
     314              :       !   seting the subtrees
     315              :       CALL init_tree_mod(start_elem=init_conf, tmc_env=tmc_env, &
     316              :                          job_counts=nr_of_job, &
     317           14 :                          worker_timings=worker_timings_aver)
     318              : 
     319              :       ! init restart counter (espacially for restart case)
     320           14 :       IF (tmc_env%m_env%restart_out_step /= 0) THEN
     321              :          restart_count = INT(tmc_env%m_env%result_count(0)/ &
     322            3 :                              REAL(tmc_env%m_env%restart_out_step, KIND=dp)) + 1
     323              :       END IF
     324           14 :       restarted_elem_nr = tmc_env%m_env%result_count(0)
     325              : 
     326              : !TODO check conf and cell of both input files (cell has to be equal,
     327              : !           because it is used as reference cell for scaling the cell)
     328              :       ! communicate the reference cell size
     329           28 :       DO wg = 1, tmc_env%tmc_comp_set%para_env_m_w%num_pe - 1
     330           14 :          stat = TMC_STATUS_WORKER_INIT
     331              :          CALL tmc_message(msg_type=stat, send_recv=send_msg, dest=wg, &
     332              :                           para_env=tmc_env%tmc_comp_set%para_env_m_w, &
     333           28 :                           tmc_params=tmc_env%params)
     334              :       END DO
     335              : 
     336              :       ! send the atom informations to all analysis workers
     337           14 :       IF (tmc_env%tmc_comp_set%para_env_m_ana%num_pe > 1) THEN
     338            0 :          DO wg = 1, tmc_env%tmc_comp_set%para_env_m_ana%num_pe - 1
     339            0 :             stat = TMC_STAT_INIT_ANALYSIS
     340              :             CALL tmc_message(msg_type=stat, send_recv=send_msg, dest=wg, &
     341              :                              para_env=tmc_env%tmc_comp_set%para_env_m_ana, &
     342              :                              result_count=tmc_env%m_env%result_count, &
     343              :                              tmc_params=tmc_env%params, &
     344              :                              elem=init_conf, &
     345            0 :                              wait_for_message=.TRUE.)
     346              :          END DO
     347              :          CALL communicate_atom_types(atoms=tmc_env%params%atoms, &
     348              :                                      source=0, &
     349            0 :                                      para_env=tmc_env%tmc_comp_set%para_env_m_ana)
     350              :       END IF
     351              : 
     352           14 :       CALL deallocate_sub_tree_node(tree_elem=init_conf)
     353              : 
     354              :       ! regtest output
     355           14 :       IF (tmc_env%params%print_test_output .OR. DEBUG > 0) THEN
     356           14 :          WRITE (tmc_env%m_env%io_unit, *) "TMC|first_global_tree_rnd_nr_X= ", &
     357           28 :             tmc_env%m_env%gt_head%rnd_nr
     358              :       END IF
     359              : 
     360              :       ! calculate the approx energy of the first element (later the exact)
     361           14 :       IF (tmc_env%m_env%gt_head%conf(1)%elem%stat == status_calc_approx_ener) THEN
     362            5 :          wg = 1
     363            5 :          IF (tmc_env%tmc_comp_set%group_cc_nr > 0) THEN
     364            0 :             wg = tmc_env%tmc_comp_set%group_ener_nr + 1
     365              :          END IF
     366            5 :          stat = TMC_STAT_APPROX_ENERGY_REQUEST
     367              :          CALL tmc_message(msg_type=stat, send_recv=send_msg, dest=wg, &
     368              :                           para_env=tmc_env%tmc_comp_set%para_env_m_w, &
     369              :                           tmc_params=tmc_env%params, &
     370            5 :                           elem=tmc_env%m_env%gt_head%conf(1)%elem)
     371            5 :          worker_info(wg)%busy = .TRUE.
     372            5 :          worker_info(wg)%elem => tmc_env%m_env%gt_head%conf(1)%elem
     373            5 :          init_conf => tmc_env%m_env%gt_head%conf(1)%elem
     374            9 :       ELSE IF (tmc_env%m_env%gt_head%conf(1)%elem%stat == status_created) THEN
     375            7 :          init_conf => tmc_env%m_env%gt_head%conf(1)%elem
     376              :          ! calculation will be done automatically,
     377              :          !   by searching the next conf for energy calculation
     378              :       END IF
     379              :       !-- START WORK --!
     380              :       !-- distributing work:
     381              :       !   1. receive incoming results
     382              :       !   2. check new results in tree
     383              :       !   3. if idle worker, create new tree element and send them to worker
     384       255431 :       task_loop: DO
     385              :          ! =======================================================================
     386              :          !-- RECEIVING ALL incoming messages and handling them
     387              :          ! results of tree node 1 is distributed to all other subtree nodes
     388              :          ! =======================================================================
     389              :          worker_request_loop: DO
     390       259825 :             wg = 1
     391       259825 :             flag = .FALSE.
     392              :             CALL tmc_message(msg_type=stat, send_recv=recv_msg, dest=wg, &
     393              :                              para_env=tmc_env%tmc_comp_set%para_env_m_w, &
     394              :                              tmc_params=tmc_env%params, &
     395       259825 :                              elem_array=worker_info(:), success=flag)
     396              : 
     397       259825 :             IF (flag .EQV. .FALSE.) EXIT worker_request_loop
     398              :             ! messages from worker group could be faster then the canceling request
     399         4394 :             IF (worker_info(wg)%canceled .AND. (stat /= TMC_CANCELING_RECEIPT)) THEN
     400              :                IF (DEBUG >= 1) THEN
     401              :                   WRITE (tmc_env%m_env%io_unit, *) &
     402              :                      "TMC|master: recv stat "//cp_to_string(stat)// &
     403              :                      " of canceled worker group"
     404              :                END IF
     405              :                CYCLE worker_request_loop
     406              :             END IF
     407              : 
     408              :             ! in case of parallel tempering canceled element could be reactivated,
     409              :             !   calculated faster and deleted
     410         4394 :             IF (.NOT. ASSOCIATED(worker_info(wg)%elem)) THEN
     411              :                CALL cp_abort(__LOCATION__, &
     412              :                              "no tree elem exist when receiving stat "// &
     413            0 :                              cp_to_string(stat)//"of group"//cp_to_string(wg))
     414              :             END IF
     415              : 
     416              :             IF (DEBUG >= 1) THEN
     417              :                WRITE (tmc_env%m_env%io_unit, *) &
     418              :                   "TMC|master: received stat "//cp_to_string(stat)// &
     419              :                   " of sub tree "//cp_to_string(worker_info(wg)%elem%sub_tree_nr)// &
     420              :                   " elem"//cp_to_string(worker_info(wg)%elem%nr)// &
     421              :                   " with stat"//cp_to_string(worker_info(wg)%elem%stat)// &
     422              :                   " of group"//cp_to_string(wg)//" group canceled ", worker_info(wg)%canceled
     423              :             END IF
     424       255431 :             SELECT CASE (stat)
     425              :                ! -- FAILED --------------------------
     426              :             CASE (TMC_STATUS_FAILED)
     427            0 :                EXIT task_loop
     428              :                ! -- CANCEL_RECEIPT ------------------
     429              :             CASE (TMC_CANCELING_RECEIPT)
     430              :                ! worker should got cancel message before
     431            0 :                CPASSERT(worker_info(wg)%canceled)
     432            0 :                worker_info(wg)%canceled = .FALSE.
     433            0 :                worker_info(wg)%busy = .FALSE.
     434              : 
     435            0 :                IF (ASSOCIATED(worker_info(wg)%elem)) THEN
     436            0 :                   SELECT CASE (worker_info(wg)%elem%stat)
     437              :                   CASE (status_cancel_ener)
     438              :                      !-- timings
     439              :                      worker_timings_aver(4) = (worker_timings_aver(4)*nr_of_job(6) + &
     440            0 :                                                (m_walltime() - worker_info(wg)%start_time))/REAL(nr_of_job(6) + 1, KIND=dp)
     441            0 :                      nr_of_job(6) = nr_of_job(6) + 1
     442              : 
     443            0 :                      worker_info(wg)%elem%stat = status_canceled_ener
     444            0 :                      worker_info(wg)%elem%potential = 8000.0_dp
     445            0 :                      IF (tmc_env%params%DRAW_TREE) THEN
     446              :                         CALL create_dot_color(tree_element=worker_info(wg)%elem, &
     447            0 :                                               tmc_params=tmc_env%params)
     448              :                      END IF
     449              :                   CASE (status_cancel_nmc)
     450              :                      !-- timings
     451              :                      worker_timings_aver(3) = (worker_timings_aver(3)*nr_of_job(5) + &
     452            0 :                                                (m_walltime() - worker_info(wg)%start_time))/REAL(nr_of_job(5) + 1, KIND=dp)
     453            0 :                      nr_of_job(5) = nr_of_job(5) + 1
     454              : 
     455            0 :                      worker_info(wg)%elem%stat = status_canceled_nmc
     456            0 :                      worker_info(wg)%elem%potential = 8000.0_dp
     457            0 :                      IF (tmc_env%params%DRAW_TREE) THEN
     458              :                         CALL create_dot_color(tree_element=worker_info(wg)%elem, &
     459            0 :                                               tmc_params=tmc_env%params)
     460              :                      END IF
     461              :                   CASE DEFAULT
     462              :                      ! the subtree element is again in use (reactivated)
     463              :                   END SELECT
     464            0 :                   worker_info(wg)%elem => NULL()
     465              :                END IF
     466              :                ! -- START_CONF_RESULT ---------------
     467              :             CASE (TMC_STAT_START_CONF_RESULT)
     468              :                ! start configuration should already be handeled
     469            0 :                CPABORT("TMC_STAT_START_CONF_RESULT is invalid")
     470              :                ! -- ENERGY RESULT -----------------
     471              :             CASE (TMC_STAT_APPROX_ENERGY_RESULT)
     472           14 :                nr_of_job(3) = nr_of_job(3) + 1
     473           14 :                worker_info(wg)%busy = .FALSE.
     474           14 :                worker_info(wg)%elem%stat = status_created
     475           14 :                IF (tmc_env%params%DRAW_TREE) THEN
     476              :                   CALL create_dot_color(tree_element=worker_info(wg)%elem, &
     477            0 :                                         tmc_params=tmc_env%params)
     478              :                END IF
     479           14 :                worker_info(wg)%elem => NULL()
     480              :                ! nothing to do, the approximate potential
     481              :                !   should be updated in the message interface
     482              :                ! -- NMC / MD RESULT -----------------
     483              :             CASE (TMC_STAT_NMC_RESULT, TMC_STAT_MD_RESULT)
     484           57 :                IF (.NOT. worker_info(wg)%canceled) worker_info(wg)%busy = .FALSE.
     485              :                !-- timings for Nested Monte Carlo calculation
     486              :                worker_timings_aver(1) = (worker_timings_aver(1)*nr_of_job(3) + &
     487           57 :                                          (m_walltime() - worker_info(wg)%start_time))/REAL(nr_of_job(3) + 1, KIND=dp)
     488           57 :                nr_of_job(3) = nr_of_job(3) + 1
     489              : 
     490           57 :                worker_info(wg)%start_time = m_walltime() - worker_info(wg)%start_time
     491           57 :                CALL set_walltime_delay(worker_info(wg)%start_time, walltime_delay)
     492           57 :                worker_info(wg)%elem%stat = status_created
     493           57 :                IF (tmc_env%params%DRAW_TREE) THEN
     494              :                   CALL create_dot_color(tree_element=worker_info(wg)%elem, &
     495            0 :                                         tmc_params=tmc_env%params)
     496              :                END IF
     497              :                !-- send energy request
     498              :                ! in case of one singe input file, energy is already calculated
     499           57 :                IF (tmc_env%params%NMC_inp_file == "") THEN
     500            0 :                   worker_info(wg)%elem%potential = worker_info(wg)%elem%e_pot_approx
     501            0 :                   worker_info(wg)%elem%stat = status_calculated
     502              :                   ! check acceptance of depending nodes
     503            0 :                   IF (.NOT. (ASSOCIATED(worker_info(wg)%elem, init_conf))) THEN
     504              :                      CALL check_acceptance_of_depending_subtree_nodes(tree_elem=worker_info(wg)%elem, &
     505            0 :                                                                       tmc_env=tmc_env)
     506              :                   END IF
     507            0 :                   IF (tmc_env%params%DRAW_TREE) THEN
     508              :                      CALL create_dot_color(tree_element=worker_info(wg)%elem, &
     509            0 :                                            tmc_params=tmc_env%params)
     510              :                   END IF
     511              :                   !-- CANCELING the calculations of the elements, which are definetively not needed anymore
     512              :                   CALL cancel_calculations(cancel_list=tmc_env%m_env%cancelation_list, &
     513              :                                            work_list=worker_info, &
     514              :                                            para_env=tmc_env%tmc_comp_set%para_env_m_w, &
     515              :                                            tmc_env=tmc_env, &
     516            0 :                                            cancel_count=cancel_count)
     517            0 :                   worker_info(wg)%elem => NULL()
     518              :                ELSE
     519              :                   ! if all working groups are equal, the same group calculates the energy
     520              :                   IF (tmc_env%tmc_comp_set%group_cc_nr <= 0 &
     521           57 :                       .AND. (.NOT. worker_info(wg)%canceled)) THEN
     522           57 :                      worker_info(wg)%elem%stat = status_calculate_energy
     523           57 :                      stat = TMC_STAT_ENERGY_REQUEST
     524              :                      ! immediately send energy request
     525              :                      CALL tmc_message(msg_type=stat, send_recv=send_msg, dest=wg, &
     526              :                                       para_env=tmc_env%tmc_comp_set%para_env_m_w, &
     527              :                                       tmc_params=tmc_env%params, &
     528           57 :                                       elem=worker_info(wg)%elem)
     529           57 :                      worker_info(wg)%busy = .TRUE.
     530           57 :                      nr_of_job(2) = nr_of_job(2) + 1
     531           57 :                      IF (tmc_env%params%DRAW_TREE) THEN
     532              :                         CALL create_dot_color(tree_element=worker_info(wg)%elem, &
     533            0 :                                               tmc_params=tmc_env%params)
     534              :                      END IF
     535              :                      !-- set start time for energy calculation
     536           57 :                      worker_info(wg)%start_time = m_walltime()
     537              :                   ELSE
     538            0 :                      worker_info(wg)%elem => NULL()
     539              :                   END IF
     540              :                END IF
     541              :                ! -- ENERGY RESULT --------------------
     542              :             CASE (TMC_STAT_ENERGY_RESULT)
     543              :                !-- timings
     544              :                worker_timings_aver(2) = (worker_timings_aver(2)*nr_of_job(4) + &
     545         4323 :                                          (m_walltime() - worker_info(wg)%start_time))/REAL(nr_of_job(4) + 1, KIND=dp)
     546         4323 :                nr_of_job(4) = nr_of_job(4) + 1
     547              : 
     548         4323 :                worker_info(wg)%start_time = m_walltime() - worker_info(wg)%start_time
     549         4323 :                CALL set_walltime_delay(worker_info(wg)%start_time, walltime_delay)
     550              : 
     551         4323 :                IF (.NOT. worker_info(wg)%canceled) THEN
     552         4323 :                   worker_info(wg)%busy = .FALSE.
     553              :                END IF
     554              :                ! the first node in tree is always accepted.!.
     555         4323 :                IF (ASSOCIATED(worker_info(wg)%elem, init_conf)) THEN
     556              :                   !-- distribute energy of first element to all subtrees
     557              :                   CALL finalize_init(gt_tree_ptr=tmc_env%m_env%gt_head, &
     558           12 :                                      tmc_env=tmc_env)
     559           12 :                   IF (tmc_env%params%DRAW_TREE) THEN
     560              :                      CALL create_global_tree_dot_color(gt_tree_element=tmc_env%m_env%gt_act, &
     561            0 :                                                        tmc_params=tmc_env%params)
     562              :                      CALL create_dot_color(tree_element=worker_info(wg)%elem, &
     563            0 :                                            tmc_params=tmc_env%params)
     564              :                   END IF
     565           12 :                   init_conf => NULL()
     566              :                ELSE
     567         4311 :                   worker_info(wg)%elem%stat = status_calculated
     568         4311 :                   IF (tmc_env%params%DRAW_TREE) THEN
     569              :                      CALL create_dot_color(worker_info(wg)%elem, &
     570           36 :                                            tmc_params=tmc_env%params)
     571              :                   END IF
     572              :                   ! check acceptance of depending nodes
     573              :                   ! first (initial) configuration do not have to be checked
     574              :                   CALL check_acceptance_of_depending_subtree_nodes(tree_elem=worker_info(wg)%elem, &
     575         4311 :                                                                    tmc_env=tmc_env)
     576              :                END IF
     577              :                !-- write out all configurations (not only Markov Chain) e.g. for fitting
     578         4323 :                IF (tmc_env%params%all_conf_file_name /= "") THEN
     579              :                   CALL write_element_in_file(elem=worker_info(wg)%elem, &
     580              :                                              file_name=tmc_env%params%all_conf_file_name, &
     581              :                                              tmc_params=tmc_env%params, &
     582            0 :                                              conf_nr=nr_of_job(4))
     583              :                END IF
     584              : 
     585              :                !-- CANCELING the calculations of the elements,
     586              :                !      which are definetively not needed anymore
     587              :                CALL cancel_calculations(cancel_list=tmc_env%m_env%cancelation_list, &
     588              :                                         work_list=worker_info, &
     589              :                                         para_env=tmc_env%tmc_comp_set%para_env_m_w, &
     590              :                                         tmc_env=tmc_env, &
     591         4323 :                                         cancel_count=cancel_count)
     592              :                IF (DEBUG >= 9) THEN
     593              :                   WRITE (tmc_env%m_env%io_unit, *) &
     594              :                      "TMC|master: handled energy result of sub tree ", &
     595              :                      worker_info(wg)%elem%sub_tree_nr, " elem ", worker_info(wg)%elem%nr, &
     596              :                      " with stat", worker_info(wg)%elem%stat
     597              :                END IF
     598         4323 :                worker_info(wg)%elem => NULL()
     599              : 
     600              :                !-- SCF ENERGY -----------------------
     601              :             CASE (TMC_STAT_SCF_STEP_ENER_RECEIVE)
     602              :                IF (.NOT. (ASSOCIATED(worker_info(wg)%elem, init_conf)) .AND. &
     603            0 :                    worker_info(wg)%elem%stat /= status_cancel_ener .AND. &
     604              :                    worker_info(wg)%elem%stat /= status_cancel_nmc) THEN
     605              :                   ! update the acceptance probability and the canceling list
     606              :                   CALL check_elements_for_acc_prob_update(tree_elem=worker_info(wg)%elem, &
     607            0 :                                                           tmc_env=tmc_env)
     608              :                END IF
     609              :                ! cancel inlikely elements
     610              :                CALL cancel_calculations(cancel_list=tmc_env%m_env%cancelation_list, &
     611              :                                         work_list=worker_info, &
     612              :                                         para_env=tmc_env%tmc_comp_set%para_env_m_w, &
     613              :                                         tmc_env=tmc_env, &
     614            0 :                                         cancel_count=cancel_count)
     615              :             CASE (TMC_STAT_ANALYSIS_RESULT)
     616            0 :                ana_worker_info(wg)%busy = .FALSE.
     617            0 :                ana_worker_info(wg)%elem => NULL()
     618              :             CASE DEFAULT
     619         4394 :                CPABORT("received message with unknown info/stat type")
     620              :             END SELECT
     621              :          END DO worker_request_loop
     622              :          !-- do tree update (check new results)
     623              :          CALL tree_update(tmc_env=tmc_env, result_acc=flag, &
     624       255431 :                           something_updated=l_update_tree)
     625              :          IF (DEBUG >= 2 .AND. l_update_tree) THEN
     626              :             WRITE (tmc_env%m_env%io_unit, *) &
     627              :                "TMC|master: tree updated "//cp_to_string(l_update_tree)// &
     628              :                " of with gt elem "//cp_to_string(tmc_env%m_env%gt_act%nr)// &
     629              :                " with stat"//cp_to_string(tmc_env%m_env%gt_act%stat)
     630              :          END IF
     631              : 
     632              :          CALL send_analysis_tasks(ana_list=tmc_env%m_env%analysis_list, &
     633              :                                   ana_worker_info=ana_worker_info, &
     634              :                                   para_env=tmc_env%tmc_comp_set%para_env_m_ana, &
     635       255431 :                                   tmc_env=tmc_env)
     636              : 
     637              :          ! =======================================================================
     638              :          !-- ALL CALCULATIONS DONE (check) ---
     639              :          ! =======================================================================
     640              :          ! if enough configurations are sampled or walltime is exeeded,
     641              :          !   finish building trees
     642              : !TODO set correct logger para_env to use this
     643       255431 :          CALL external_control(should_stop=external_stop, flag="TMC", globenv=globenv)
     644              :          IF ((ANY(tmc_env%m_env%result_count(1:) >= tmc_env%m_env%num_MC_elem) &
     645              :               .AND. flag) .OR. &
     646              :              (m_walltime() - run_time_start > &
     647       576344 :               tmc_env%m_env%walltime - walltime_delay - walltime_offset) .OR. &
     648              :              external_stop) THEN
     649           14 :             WRITE (tmc_env%m_env%io_unit, FMT="(/,T2,A)") REPEAT("=", 79)
     650              :             ! calculations NOT finished, walltime exceeded
     651           20 :             IF (.NOT. ANY(tmc_env%m_env%result_count(1:) &
     652              :                           >= tmc_env%m_env%num_MC_elem)) THEN
     653            1 :                WRITE (tmc_env%m_env%io_unit, *) "Walltime exceeded.", &
     654            1 :                   m_walltime() - run_time_start, " of ", tmc_env%m_env%walltime - walltime_delay - walltime_offset, &
     655            2 :                   "(incl. delay", walltime_delay, "and offset", walltime_offset, ") left"
     656              :             ELSE
     657              :                ! calculations finished
     658           13 :                IF (tmc_env%params%print_test_output) THEN
     659           13 :                   WRITE (tmc_env%m_env%io_unit, *) "Total energy: ", &
     660           26 :                      tmc_env%m_env%result_list(1)%elem%potential
     661              :                END IF
     662              :             END IF
     663           14 :             IF (tmc_env%m_env%restart_out_step /= 0) THEN
     664              :                CALL print_restart_file(tmc_env=tmc_env, job_counts=nr_of_job, &
     665            3 :                                        timings=worker_timings_aver)
     666              :             END IF
     667              :             EXIT task_loop
     668              :          END IF
     669              : 
     670              :          ! =======================================================================
     671              :          ! update the rest of the tree (canceling and deleting elements)
     672              :          ! =======================================================================
     673       255417 :          IF (l_update_tree) THEN
     674              :             IF (DEBUG >= 2) THEN
     675              :                WRITE (tmc_env%m_env%io_unit, *) &
     676              :                   "TMC|master: start remove elem and cancel calculation"
     677              :             END IF
     678              :             !-- CLEANING tree nodes beside the path through the tree from
     679              :             !      end_of_clean_tree to tree_ptr
     680              :             ! --> getting back the end of clean tree
     681         4466 :             CALL remove_all_trees(working_elem_list=worker_info, tmc_env=tmc_env)
     682              :             !-- CANCELING the calculations of the elements,
     683              :             !     which are definetively not needed anymore
     684              :             !   elements are added to canceling list if no global tree reference
     685              :             !     exist anymore
     686              :             CALL cancel_calculations(cancel_list=tmc_env%m_env%cancelation_list, &
     687              :                                      work_list=worker_info, &
     688              :                                      cancel_count=cancel_count, &
     689              :                                      para_env=tmc_env%tmc_comp_set%para_env_m_w, &
     690         4466 :                                      tmc_env=tmc_env)
     691              :          END IF
     692              : 
     693              :          ! =====================================================================
     694              :          !-- NEW TASK (if worker not busy submit next task)
     695              :          ! =====================================================================
     696       255417 :          worker_counter = worker_counter + 1
     697       255417 :          wg = MODULO(worker_counter, tmc_env%tmc_comp_set%para_env_m_w%num_pe - 1) + 1
     698              : 
     699       506333 :          IF (DEBUG >= 16 .AND. ALL(worker_info(:)%busy)) THEN
     700              :             WRITE (tmc_env%m_env%io_unit, *) "all workers are busy"
     701              :          END IF
     702              : 
     703       255417 :          IF (.NOT. worker_info(wg)%busy) THEN
     704              :             IF (DEBUG >= 13) THEN
     705              :                WRITE (tmc_env%m_env%io_unit, *) &
     706              :                   "TMC|master: search new task for worker ", wg
     707              :             END IF
     708              :             ! no group separation
     709         4501 :             IF (tmc_env%tmc_comp_set%group_cc_nr <= 0) THEN
     710              :                ! search next element to calculate the energy
     711              :                CALL search_next_energy_calc(gt_head=tmc_env%m_env%gt_act, &
     712              :                                             new_gt_elem=gt_elem_tmp, stat=stat, &
     713         4501 :                                             react_count=reactivation_ener_count)
     714         4501 :                IF (stat == TMC_STATUS_WAIT_FOR_NEW_TASK) THEN
     715              :                   CALL create_new_gt_tree_node(tmc_env=tmc_env, stat=stat, &
     716              :                                                new_elem=gt_elem_tmp, &
     717         4480 :                                                reactivation_cc_count=reactivation_cc_count)
     718              :                END IF
     719            0 :             ELSE IF (wg > tmc_env%tmc_comp_set%group_ener_nr) THEN
     720              :                ! specialized groups (groups for exact energy and groups for configurational change)
     721              :                ! creating new element (configurational change group)
     722              :                !-- crate new node, configurational change is handled in tmc_tree module
     723              :                CALL create_new_gt_tree_node(tmc_env=tmc_env, stat=stat, &
     724              :                                             new_elem=gt_elem_tmp, &
     725            0 :                                             reactivation_cc_count=reactivation_cc_count)
     726              :                ! element could be already created, hence CC worker has nothing to do for this element
     727              :                ! in next round he will get a task
     728            0 :                IF (stat == status_created .OR. stat == status_calculate_energy) THEN
     729            0 :                   stat = TMC_STATUS_WAIT_FOR_NEW_TASK
     730              :                END IF
     731              :             ELSE
     732              :                ! search next element to calculate the energy
     733              :                CALL search_next_energy_calc(gt_head=tmc_env%m_env%gt_act, &
     734              :                                             new_gt_elem=gt_elem_tmp, stat=stat, &
     735            0 :                                             react_count=reactivation_ener_count)
     736              :             END IF
     737              : 
     738              :             IF (DEBUG >= 10) THEN
     739              :                WRITE (tmc_env%m_env%io_unit, *) &
     740              :                   "TMC|master: send task with elem stat "//cp_to_string(stat)// &
     741              :                   " to group "//cp_to_string(wg)
     742              :             END IF
     743              :             ! MESSAGE settings: status informations and task for communication
     744              :             SELECT CASE (stat)
     745              :             CASE (TMC_STATUS_WAIT_FOR_NEW_TASK)
     746              :                CYCLE task_loop
     747              :             CASE (TMC_STATUS_FAILED)
     748              :                !STOP "in creating new task, status failed should be handled before"
     749              :                CYCLE task_loop
     750              :             CASE (status_calculated, status_accepted, status_rejected)
     751            9 :                CYCLE task_loop
     752              :             CASE (status_calc_approx_ener)
     753              :                ! e.g. after volume move, we need the approximate potential for 2 potential check of following NMC nodes
     754            9 :                stat = TMC_STAT_APPROX_ENERGY_REQUEST
     755              :                CALL tmc_message(msg_type=stat, send_recv=send_msg, dest=wg, &
     756              :                                 para_env=tmc_env%tmc_comp_set%para_env_m_w, &
     757              :                                 tmc_params=tmc_env%params, &
     758            9 :                                 elem=gt_elem_tmp%conf(gt_elem_tmp%mv_conf)%elem)
     759            9 :                nr_of_job(1) = nr_of_job(1) + 1
     760              :             CASE (status_created, status_calculate_energy)
     761              :                ! in case of parallel tempering the node can be already be calculating (related to another global tree node
     762              :                !-- send task to calculate system property
     763         4267 :                gt_elem_tmp%conf(gt_elem_tmp%mv_conf)%elem%stat = status_calculate_energy
     764         4267 :                IF (tmc_env%params%DRAW_TREE) THEN
     765              :                   CALL create_dot_color(tree_element=gt_elem_tmp%conf(gt_elem_tmp%mv_conf)%elem, &
     766           36 :                                         tmc_params=tmc_env%params)
     767              :                END IF
     768         4267 :                stat = TMC_STAT_ENERGY_REQUEST
     769              :                CALL tmc_message(msg_type=stat, send_recv=send_msg, dest=wg, &
     770              :                                 para_env=tmc_env%tmc_comp_set%para_env_m_w, &
     771              :                                 tmc_params=tmc_env%params, &
     772         4267 :                                 elem=gt_elem_tmp%conf(gt_elem_tmp%mv_conf)%elem)
     773         4267 :                nr_of_job(2) = nr_of_job(2) + 1
     774              :             CASE (status_calculate_MD)
     775            0 :                stat = TMC_STAT_MD_REQUEST
     776              :                CALL tmc_message(msg_type=stat, send_recv=send_msg, dest=wg, &
     777              :                                 para_env=tmc_env%tmc_comp_set%para_env_m_w, &
     778              :                                 tmc_params=tmc_env%params, &
     779            0 :                                 elem=gt_elem_tmp%conf(gt_elem_tmp%mv_conf)%elem)
     780              : !                           temperature=tmc_env%params%Temp(gt_elem_tmp%mv_conf), &
     781            0 :                nr_of_job(1) = nr_of_job(1) + 1
     782              :             CASE (status_calculate_NMC_steps)
     783              :                !-- send information of element, which should be calculated
     784           57 :                stat = TMC_STAT_NMC_REQUEST
     785              :                CALL tmc_message(msg_type=stat, send_recv=send_msg, dest=wg, &
     786              :                                 para_env=tmc_env%tmc_comp_set%para_env_m_w, &
     787              :                                 tmc_params=tmc_env%params, &
     788           57 :                                 elem=gt_elem_tmp%conf(gt_elem_tmp%mv_conf)%elem)
     789           57 :                nr_of_job(1) = nr_of_job(1) + 1
     790              :             CASE (status_cancel_nmc, status_cancel_ener)
     791              :                ! skip that task until receipt is received
     792              :                ! no status update
     793              :             CASE DEFAULT
     794              :                CALL cp_abort(__LOCATION__, &
     795              :                              "new task of tree element"// &
     796              :                              cp_to_string(gt_elem_tmp%nr)// &
     797         4501 :                              "has unknown status"//cp_to_string(stat))
     798              :             END SELECT
     799         4333 :             worker_info(wg)%elem => gt_elem_tmp%conf(gt_elem_tmp%mv_conf)%elem
     800         4333 :             worker_info(wg)%busy = .TRUE.
     801              :             ! set timer for maximum calculation time recognition
     802         4333 :             worker_info(wg)%start_time = m_walltime()
     803              : 
     804              :             !===================== write out info after x requested tasks==========
     805              :             IF (nr_of_job(4) > last_output .AND. &
     806         4333 :                 (MODULO(nr_of_job(4), tmc_env%m_env%info_out_step_size) == 0) .AND. &
     807              :                 (stat /= TMC_STATUS_FAILED)) THEN
     808          181 :                last_output = nr_of_job(4)
     809          181 :                WRITE (tmc_env%m_env%io_unit, FMT="(/,T2,A)") REPEAT("-", 79)
     810              :                WRITE (tmc_env%m_env%io_unit, *) &
     811          181 :                   "Tasks submitted:  E ", nr_of_job(2), ", cc", nr_of_job(1)
     812              :                WRITE (tmc_env%m_env%io_unit, *) &
     813          181 :                   "Results received: E ", nr_of_job(4), ", cc", nr_of_job(3)
     814              :                WRITE (tmc_env%m_env%io_unit, *) &
     815          181 :                   "Configurations used:", tmc_env%m_env%result_count(0), &
     816          779 :                   ", sub trees", tmc_env%m_env%result_count(1:)
     817              : 
     818              :                CALL print_move_types(init=.FALSE., file_io=tmc_env%m_env%io_unit, &
     819          181 :                                      tmc_params=tmc_env%params)
     820          543 :                ALLOCATE (tree_elem_counters(0:SIZE(tmc_env%params%Temp)))
     821          543 :                ALLOCATE (tree_elem_heads(0:SIZE(tmc_env%params%Temp)))
     822              :                CALL count_nodes_in_trees(global_tree_ptr=tmc_env%m_env%gt_act, &
     823              :                                          end_of_clean_trees=tmc_env%m_env%st_clean_ends, &
     824          181 :                                          counters=tree_elem_counters, head_elements_nr=tree_elem_heads)
     825          779 :                WRITE (tmc_env%m_env%io_unit, *) "nodes in trees", tree_elem_counters(:)
     826          779 :                WRITE (tmc_env%m_env%io_unit, *) "tree heads    ", tree_elem_heads(:)
     827          181 :                IF (tmc_env%params%NMC_inp_file /= "") THEN
     828              :                   CALL count_prepared_nodes_in_trees(global_tree_ptr=tmc_env%m_env%gt_act, &
     829           28 :                                                      counters=tree_elem_counters)
     830              :                   WRITE (tmc_env%m_env%io_unit, FMT=*) &
     831           86 :                      "ener prepared ", tree_elem_counters
     832              :                END IF
     833          181 :                IF (tmc_env%params%SPECULATIVE_CANCELING) THEN
     834              :                   WRITE (tmc_env%m_env%io_unit, *) &
     835          181 :                      "canceled cc|E:     ", nr_of_job(5:6), &
     836          181 :                      ", reactivated: cc ", &
     837          181 :                      reactivation_cc_count, &
     838          181 :                      ", reactivated: E ", &
     839          362 :                      reactivation_ener_count
     840              :                END IF
     841              :                WRITE (tmc_env%m_env%io_unit, FMT='(A,2F10.2)') &
     842          181 :                   " Average time for cc/ener calc          ", &
     843          362 :                   worker_timings_aver(1), worker_timings_aver(2)
     844          181 :                IF (tmc_env%params%SPECULATIVE_CANCELING) THEN
     845              :                   WRITE (tmc_env%m_env%io_unit, FMT='(A,2F10.2)') &
     846          181 :                      " Average time until cancel cc/ener calc ", &
     847          362 :                      worker_timings_aver(3), worker_timings_aver(4)
     848              :                END IF
     849          181 :                IF (tmc_env%params%esimate_acc_prob) THEN
     850              :                   WRITE (tmc_env%m_env%io_unit, *) &
     851          181 :                      "Estimate correct (acc/Nacc) | wrong (acc/nacc)", &
     852          181 :                      tmc_env%m_env%estim_corr_wrong(1), &
     853          181 :                      tmc_env%m_env%estim_corr_wrong(3), " | ", &
     854          181 :                      tmc_env%m_env%estim_corr_wrong(2), &
     855          362 :                      tmc_env%m_env%estim_corr_wrong(4)
     856              :                END IF
     857              :                WRITE (tmc_env%m_env%io_unit, *) &
     858          181 :                   "Time: ", INT(m_walltime() - run_time_start), "of", &
     859          181 :                   INT(tmc_env%m_env%walltime - walltime_delay - walltime_offset), &
     860          362 :                   "sec needed."
     861          181 :                CALL m_memory(mem)
     862              :                WRITE (tmc_env%m_env%io_unit, *) &
     863          181 :                   "Memory used: ", INT(mem/(1024*1024), KIND=KIND(0)), "MiBytes"
     864          181 :                CALL m_flush(tmc_env%m_env%io_unit)
     865          181 :                DEALLOCATE (tree_elem_heads)
     866          362 :                DEALLOCATE (tree_elem_counters)
     867              :             END IF
     868              :             !===================== write out restart file after x results============
     869         4333 :             IF (tmc_env%m_env%restart_out_step > 0 .AND. &
     870              :                 tmc_env%m_env%result_count(0) > &
     871              :                 restart_count*tmc_env%m_env%restart_out_step) THEN
     872              :                CALL print_restart_file(tmc_env=tmc_env, job_counts=nr_of_job, &
     873            0 :                                        timings=worker_timings_aver)
     874            0 :                restart_count = restart_count + 1
     875              :             END IF
     876              : 
     877              :          END IF !worker busy?
     878              :       END DO task_loop
     879              : 
     880              :       ! -- END OF WORK (enough configurations are calculated or walltime exceeded
     881           14 :       WRITE (tmc_env%m_env%io_unit, FMT="(/,T2,A)") REPEAT("=", 79)
     882           14 :       WRITE (UNIT=tmc_env%m_env%io_unit, FMT="(T2,A,T35,A,T80,A)") "=", &
     883           28 :          "finalizing TMC", "="
     884           14 :       WRITE (tmc_env%m_env%io_unit, *) "acceptance rates:"
     885              :       CALL print_move_types(init=.FALSE., file_io=tmc_env%m_env%io_unit, &
     886           14 :                             tmc_params=tmc_env%params)
     887           14 :       WRITE (tmc_env%m_env%io_unit, FMT="(/,T2,A)") REPEAT("-", 79)
     888              :       ! program efficiency result outputs
     889           42 :       ALLOCATE (efficiency(0:tmc_env%params%nr_temp))
     890           14 :       CALL get_subtree_efficiency(tmc_env=tmc_env, eff=efficiency)
     891           14 :       WRITE (tmc_env%m_env%io_unit, *) "Efficiencies:"
     892              :       WRITE (tmc_env%m_env%io_unit, FMT="(A,F5.2,A,1000F5.2)") &
     893           14 :          " (MC elements/calculated configuration) global:", &
     894           54 :          efficiency(0), " sub tree(s): ", efficiency(1:)
     895           14 :       DEALLOCATE (efficiency)
     896           14 :       IF (tmc_env%tmc_comp_set%group_cc_nr > 0) THEN
     897              :          WRITE (tmc_env%m_env%io_unit, FMT="(A,1000F5.2)") &
     898            0 :             " (MC elements/created configuration)          :", &
     899            0 :             tmc_env%m_env%result_count(:)/REAL(nr_of_job(3), KIND=dp)
     900              :       END IF
     901              :       WRITE (tmc_env%m_env%io_unit, FMT="(A,1000F5.2)") &
     902           14 :          " (MC elements/energy calculated configuration):", &
     903           68 :          tmc_env%m_env%result_count(:)/REAL(nr_of_job(4), KIND=dp)
     904           14 :       IF (tmc_env%params%NMC_inp_file /= "") THEN
     905              :          WRITE (tmc_env%m_env%io_unit, *) &
     906            5 :             "Amount of canceled elements (E/cc):", &
     907           10 :             tmc_env%m_env%count_cancel_ener, tmc_env%m_env%count_cancel_NMC
     908              :          WRITE (tmc_env%m_env%io_unit, *) &
     909            5 :             " reactivated E  ", reactivation_ener_count
     910              :          WRITE (tmc_env%m_env%io_unit, *) &
     911            5 :             " reactivated cc ", reactivation_cc_count
     912              :       END IF
     913              :       WRITE (tmc_env%m_env%io_unit, FMT="(A,F10.2)") &
     914           14 :          " computing time of one Markov chain element ", &
     915              :          (m_walltime() - run_time_start)/REAL(tmc_env%m_env%result_count(0) - &
     916           28 :                                               restarted_elem_nr, KIND=dp)
     917           14 :       WRITE (tmc_env%m_env%io_unit, FMT="(A,F10.2)") " TMC run time[s]: ", m_walltime() - run_time_start
     918           14 :       WRITE (tmc_env%m_env%io_unit, FMT="(/,T2,A)") REPEAT("=", 79)
     919              : 
     920              :       !-- FINALIZE
     921           14 :       WRITE (tmc_env%m_env%io_unit, *) "stopping workers"
     922              :       CALL stop_whole_group(para_env=tmc_env%tmc_comp_set%para_env_m_w, &
     923              :                             worker_info=worker_info, &
     924           14 :                             tmc_params=tmc_env%params)
     925           14 :       DEALLOCATE (worker_info)
     926              :       CALL stop_whole_group(para_env=tmc_env%tmc_comp_set%para_env_m_ana, &
     927              :                             worker_info=ana_worker_info, &
     928           14 :                             tmc_params=tmc_env%params)
     929           14 :       DEALLOCATE (ana_worker_info)
     930              : 
     931              :       !-- deallocating everything in tree module
     932           14 :       CALL finalize_trees(tmc_env=tmc_env)
     933              : 
     934           14 :       CALL free_cancelation_list(tmc_env%m_env%cancelation_list)
     935              : 
     936              :       ! -- write final configuration
     937           14 :       IF (tmc_env%params%DRAW_TREE) THEN
     938            1 :          CALL finalize_draw_tree(tmc_params=tmc_env%params)
     939              :       END IF
     940              : 
     941           14 :       WRITE (tmc_env%m_env%io_unit, *) "TMC master: all work done."
     942              : 
     943              :       ! end the timing
     944           14 :       CALL timestop(handle)
     945              : 
     946           28 :    END SUBROUTINE do_tmc_master
     947              : 
     948              : ! **************************************************************************************************
     949              : !> \brief routine sets the walltime delay, to the maximum calculation time
     950              : !>        hence the program can stop with a proper finailze
     951              : !> \param time actual calculation time
     952              : !> \param walltime_delay the actual biggest calculation time
     953              : !> \author Mandes 12.2012
     954              : ! **************************************************************************************************
     955         4380 :    SUBROUTINE set_walltime_delay(time, walltime_delay)
     956              :       REAL(KIND=dp)                                      :: time
     957              :       INTEGER                                            :: walltime_delay
     958              : 
     959         4380 :       CPASSERT(time >= 0.0_dp)
     960              : 
     961         4380 :       IF (time > walltime_delay) THEN
     962           14 :          walltime_delay = INT(time) + 1
     963              :       END IF
     964         4380 :    END SUBROUTINE set_walltime_delay
     965              : 
     966              : END MODULE tmc_master
        

Generated by: LCOV version 2.0-1