Line data Source code
1 : !--------------------------------------------------------------------------------------------------! 2 : ! CP2K: A general program to perform molecular dynamics simulations ! 3 : ! Copyright 2000-2024 CP2K developers group <https://cp2k.org> ! 4 : ! ! 5 : ! SPDX-License-Identifier: GPL-2.0-or-later ! 6 : !--------------------------------------------------------------------------------------------------! 7 : 8 : ! ************************************************************************************************** 9 : !> \brief a module to allow simple buffering of read lines of a parser 10 : !> \author Teodoro Laino [tlaino] - University of Zurich 11 : !> \date 08.2008 12 : ! ************************************************************************************************** 13 : MODULE cp_parser_buffer_types 14 : 15 : USE kinds, ONLY: max_line_length 16 : #include "../base/base_uses.f90" 17 : 18 : IMPLICIT NONE 19 : PRIVATE 20 : 21 : ! **************************************************************************** 22 : !> \brief Buffer type for speeding-up the parsing in parallel 23 : !> \author Teodoro Laino [tlaino] - University of Zurich 24 : !> \date 08.2008 25 : ! ************************************************************************************************** 26 : TYPE buffer_type 27 : INTEGER :: size = 0, buffer_id = 0 28 : INTEGER :: present_line_number = 0, & 29 : last_line_number = 0, & 30 : istat = 0 31 : INTEGER, DIMENSION(:), POINTER :: input_line_numbers => NULL() 32 : CHARACTER(LEN=max_line_length), & 33 : DIMENSION(:), POINTER :: input_lines => NULL() 34 : TYPE(buffer_type), POINTER :: sub_buffer => NULL() 35 : END TYPE buffer_type 36 : 37 : PUBLIC :: buffer_type, create_buffer_type, release_buffer_type, copy_buffer_type, & 38 : initialize_sub_buffer, finalize_sub_buffer 39 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_parser_buffer_types' 40 : INTEGER, PARAMETER, PRIVATE :: buffer_size = 1000 41 : 42 : CONTAINS 43 : 44 : ! **************************************************************************** 45 : !> \brief Creates the parser buffer type 46 : !> \param buffer ... 47 : !> \date 08.2008 48 : !> \author Teodoro Laino [tlaino] - University of Zurich 49 : ! ************************************************************************************************** 50 103060 : SUBROUTINE create_buffer_type(buffer) 51 : TYPE(buffer_type), POINTER :: buffer 52 : 53 103060 : CPASSERT(.NOT. ASSOCIATED(buffer)) 54 103060 : ALLOCATE (buffer) 55 103060 : buffer%size = buffer_size 56 103060 : ALLOCATE (buffer%input_lines(buffer%size)) 57 103060 : ALLOCATE (buffer%input_line_numbers(buffer%size)) 58 103060 : buffer%present_line_number = buffer%size 59 103060 : buffer%last_line_number = buffer%size 60 103060 : END SUBROUTINE create_buffer_type 61 : 62 : ! **************************************************************************** 63 : !> \brief Releases the parser buffer type 64 : !> \param buffer ... 65 : !> \date 08.2008 66 : !> \author Teodoro Laino [tlaino] - University of Zurich 67 : ! ************************************************************************************************** 68 103060 : RECURSIVE SUBROUTINE release_buffer_type(buffer) 69 : TYPE(buffer_type), POINTER :: buffer 70 : 71 103060 : CPASSERT(ASSOCIATED(buffer)) 72 103060 : DEALLOCATE (buffer%input_lines) 73 103060 : DEALLOCATE (buffer%input_line_numbers) 74 103060 : IF (ASSOCIATED(buffer%sub_buffer)) THEN 75 0 : CALL release_buffer_type(buffer%sub_buffer) 76 : END IF 77 103060 : DEALLOCATE (buffer) 78 103060 : END SUBROUTINE release_buffer_type 79 : 80 : ! **************************************************************************** 81 : !> \brief Copies buffer types 82 : !> \param buffer_in ... 83 : !> \param buffer_out ... 84 : !> \param force ... 85 : !> \date 08.2008 86 : !> \author Teodoro Laino [tlaino] - University of Zurich 87 : ! ************************************************************************************************** 88 8809350 : RECURSIVE SUBROUTINE copy_buffer_type(buffer_in, buffer_out, force) 89 : TYPE(buffer_type), POINTER :: buffer_in, buffer_out 90 : LOGICAL, INTENT(IN), OPTIONAL :: force 91 : 92 : INTEGER :: i 93 : LOGICAL :: my_force 94 : 95 8809350 : CPASSERT(ASSOCIATED(buffer_in)) 96 8809350 : CPASSERT(ASSOCIATED(buffer_out)) 97 8809350 : CPASSERT(buffer_in%size == buffer_out%size) 98 8809350 : my_force = .FALSE. 99 8809350 : IF (PRESENT(force)) my_force = force 100 : ! Copy buffer structure 101 8809350 : buffer_out%present_line_number = buffer_in%present_line_number 102 8809350 : buffer_out%last_line_number = buffer_in%last_line_number 103 8809350 : buffer_out%istat = buffer_in%istat 104 : ! This part can be quite expensive.. we do it only when strictly necessary.. 105 8809350 : IF ((buffer_out%buffer_id /= buffer_in%buffer_id) .OR. (my_force)) THEN 106 28508 : buffer_out%buffer_id = buffer_in%buffer_id 107 57073016 : buffer_out%input_line_numbers = buffer_in%input_line_numbers 108 : ! Explicit loop: bypass a NAG bug.. 109 28536508 : DO i = 1, SIZE(buffer_in%input_lines) 110 37317350 : buffer_out%input_lines(i) = buffer_in%input_lines(i) 111 : END DO 112 : END IF 113 8809350 : IF (ASSOCIATED(buffer_in%sub_buffer) .AND. ASSOCIATED(buffer_out%sub_buffer)) THEN 114 0 : CALL copy_buffer_type(buffer_in%sub_buffer, buffer_out%sub_buffer, force) 115 : END IF 116 8809350 : END SUBROUTINE copy_buffer_type 117 : 118 : ! **************************************************************************** 119 : !> \brief Initializes sub buffer structure 120 : !> \param sub_buffer ... 121 : !> \param buffer ... 122 : !> \date 08.2008 123 : !> \author Teodoro Laino [tlaino] - University of Zurich 124 : ! ************************************************************************************************** 125 38 : SUBROUTINE initialize_sub_buffer(sub_buffer, buffer) 126 : TYPE(buffer_type), POINTER :: sub_buffer, buffer 127 : 128 38 : CPASSERT(ASSOCIATED(buffer)) 129 38 : CPASSERT(.NOT. ASSOCIATED(sub_buffer)) 130 38 : CALL create_buffer_type(sub_buffer) 131 38 : CALL copy_buffer_type(buffer, sub_buffer) 132 38 : sub_buffer%present_line_number = 0 133 38 : END SUBROUTINE initialize_sub_buffer 134 : 135 : ! **************************************************************************** 136 : !> \brief Finalizes sub buffer structure 137 : !> \param sub_buffer ... 138 : !> \param buffer ... 139 : !> \date 08.2008 140 : !> \author Teodoro Laino [tlaino] - University of Zurich 141 : ! ************************************************************************************************** 142 38 : SUBROUTINE finalize_sub_buffer(sub_buffer, buffer) 143 : TYPE(buffer_type), POINTER :: sub_buffer, buffer 144 : 145 38 : CPASSERT(ASSOCIATED(buffer)) 146 38 : CPASSERT(ASSOCIATED(sub_buffer)) 147 38 : CALL copy_buffer_type(sub_buffer, buffer) 148 38 : CALL release_buffer_type(sub_buffer) 149 38 : END SUBROUTINE finalize_sub_buffer 150 : 151 0 : END MODULE cp_parser_buffer_types