process_halos_array_m.f90 3.79 KB
Newer Older
1
2
3
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
module process_halos_array_m

  use common_shared_variables_m
  use iso_fortran_env, only : ERROR_UNIT, OUTPUT_UNIT

  implicit none

contains
!------------------------------------------------------------------------------------------------------------------------------------
  subroutine Process_halos_array(halos_array)

    use fortran_hdf5_constants_m
    use fortran_hdf5_manage_files_m
    use fortran_hdf5_manage_groups_m
    use halo_post_functions_m
    use halos_array_m
    use hdf5
    use omp_lib

    type(halos_array_t), intent(inout) :: halos_array

    integer :: current_file
    integer(HID_T) :: input_file_id
    character(H5_FILENAME_LEN) :: input_file_name
    character(H5_STR_LEN) :: group_name
    integer(HID_T) :: group_id
    integer :: ihalo
    integer :: thread_id
    type(halos_array_t) :: halos_array_init
    integer(HID_T) :: output_data_id
    integer(HID_T) :: output_file_id
    character(H5_FILENAME_LEN) :: output_file_name
    real, allocatable, dimension(:,:) :: position_com

#ifdef DEBUG
    write(ERROR_UNIT,'(a,i0)') 'Process_array begins on process', mpi_process%rank
#endif

    allocate(position_com(3,halos_array%first_halo_index:halos_array%last_halo_index))

    ! open output file
    output_file_name = 'halo_post_output_00000.h5'
    write(output_file_name(len(trim(output_file_name))-7:len(trim(output_file_name))-3),'(i5.5)') &
          mpi_process%rank
    call hdf5_create_file(output_file_name, output_file_id)
    group_name = 'data'
    call hdf5_create_group(output_file_id, group_name, output_data_id)

!$omp parallel default(none) shared(halos_array,position_com,mpi_process, output_data_id) &
!$omp private(thread_id, ihalo,current_file,input_file_id,input_file_name,group_id,group_name)

    thread_id = omp_get_thread_num()

!$omp master
      current_file = halos_array%parameters%halo_file_ids(halos_array%first_halo_index)
      input_file_name = trim(halos_array%parameters%data_directory)//trim(halos_array%parameters%file_names(current_file))
      call hdf5_open_file(input_file_name, input_file_id)
      group_name = 'data'
      call hdf5_open_group(input_file_id, group_name, group_id)
      do ihalo = halos_array%first_halo_index, halos_array%last_halo_index
        if (halos_array%parameters%halo_file_ids(ihalo) /= current_file) then
          call hdf5_close_group(group_id)
          call hdf5_close_file(input_file_id)
          current_file = halos_array%parameters%halo_file_ids(ihalo)
          input_file_name = trim(halos_array%parameters%data_directory)//trim(halos_array%parameters%file_names(current_file))
          call hdf5_open_file(input_file_name, input_file_id)
          group_name = 'data'
          call hdf5_open_group(input_file_id, group_name, group_id)
        end if
        call halos_array%halos(ihalo)%Read_hdf5(group_id, halos_array%properties)
!$omp task untied 
        call compute_center_of_mass(halos_array%halos(ihalo), position_com(:,ihalo))
!        lock_halo(ihalo) = 1
!$omp task untied if(thread_id == 0)
        write(*,*) 'COM: ',thread_id, mpi_process%rank, ihalo, position_com(:,ihalo)
!        call halos_array%halos(ihalo)%Write_hdf5(output_data_id, halos_array%properties)
!$omp end task
!$omp end task  
        write(*,*) 'MASTER: ', ihalo, halos_array%halos(ihalo)%particles_number
        call halos_array%halos(ihalo)%Write_hdf5(output_data_id, halos_array%properties)
      end do
      call hdf5_close_group(group_id)
      call hdf5_close_file(input_file_id)
!$omp end master
!$omp end parallel

!      call halos_array%halos(halos_array%first_halo_index)%Write_hdf5(output_data_id, halos_array%properties)
      
    call hdf5_close_file(output_file_id)

#ifdef DEBUG
    write(ERROR_UNIT,'(a,i0)') 'Process_halos_array ends on process', mpi_process%rank
#endif

  end subroutine Process_halos_array

end module process_halos_array_m