|
| 1 | +! This module define the interface bewteen GEOS and gigatraj |
| 2 | +! The functions are defined in gigatraj |
| 3 | + |
| 4 | +module GEOS_Giga_InterOpMod |
| 5 | + use, intrinsic :: iso_c_binding, only : c_double, c_int, c_ptr, c_null_char, c_associated |
| 6 | + use, intrinsic :: iso_c_binding, only : c_loc, c_null_ptr |
| 7 | + use mpi |
| 8 | + implicit none |
| 9 | + private |
| 10 | + |
| 11 | + public :: initMetGEOSDistributedLatLonData |
| 12 | + public :: initMetGEOSDistributedCubedData |
| 13 | + public :: updateFields |
| 14 | + public :: RK4_advance |
| 15 | + public :: setData |
| 16 | + public :: getData |
| 17 | + public :: getData2d |
| 18 | + |
| 19 | + public :: test_Field3D |
| 20 | + public :: test_dataflow |
| 21 | + public :: test_metData |
| 22 | + |
| 23 | + interface |
| 24 | + |
| 25 | + function initMetGEOSDistributedCubedData(comm, ijToRank, Ig, lev, i1, i2, j1, j2, nzs, lons_ptr, lats_ptr, eta_ptr, ctime_ptr) result (metdata_ptr) bind(C, name="initGigaGridDistributedCubedData") |
| 26 | + import :: c_int, c_ptr |
| 27 | + implicit none |
| 28 | + integer(c_int), intent(in), value :: comm, Ig, lev, i1,i2,j1,j2, nzs |
| 29 | + type(c_ptr), intent(in), value :: ijToRank, lons_ptr, lats_ptr, eta_ptr, ctime_ptr |
| 30 | + type(c_ptr) :: metdata_ptr |
| 31 | + end function |
| 32 | + |
| 33 | + function initMetGEOSDistributedLatLonData(comm, ijToRank, Ig, Jg,lev, nlon_local, nlat_local, nzs, lons_ptr, lats_ptr, eta_ptr, ctime_ptr) result (metdata_ptr) bind(C, name="initGigaGridDistributedLatLonData") |
| 34 | + import :: c_int, c_ptr |
| 35 | + implicit none |
| 36 | + integer(c_int), intent(in), value :: comm, Ig, Jg, lev, nlon_local, nlat_local, nzs |
| 37 | + type(c_ptr), intent(in), value :: ijToRank, lons_ptr, lats_ptr, eta_ptr, ctime_ptr |
| 38 | + type(c_ptr) :: metdata_ptr |
| 39 | + end function |
| 40 | + |
| 41 | + subroutine updateFields( metSrc_ptr, ctime_ptr, u_ptr, v_ptr, w_ptr, p_ptr) bind(C, name="updateFields") |
| 42 | + import :: c_ptr |
| 43 | + implicit none |
| 44 | + type(c_ptr), intent(in), value :: metSrc_ptr, ctime_ptr, u_ptr, v_ptr, w_ptr, p_ptr |
| 45 | + end subroutine |
| 46 | + |
| 47 | + subroutine RK4_advance(metsrc_ptr, ctime_ptr, dt, n, lons_ptr, lats_ptr, levs_ptr) bind( C, name='RK4_advance') |
| 48 | + import :: c_ptr, c_int, c_double |
| 49 | + type(c_ptr), intent(in), value :: metsrc_ptr |
| 50 | + real(c_double), intent(in), value :: dt |
| 51 | + integer(c_int), intent(in), value :: n |
| 52 | + type(c_ptr), intent(in), value :: ctime_ptr, lons_ptr, lats_ptr, levs_ptr |
| 53 | + end subroutine |
| 54 | + |
| 55 | + subroutine test_Field3d(obj_ptr) bind(C, name="test_Field3D") |
| 56 | + import :: c_ptr |
| 57 | + implicit none |
| 58 | + type(c_ptr), intent(in), value :: obj_ptr |
| 59 | + end subroutine |
| 60 | + |
| 61 | + subroutine test_metData(obj_ptr, time, n, lons_ptr, lats_ptr, levs_ptr, u_ptr, v_ptr, w_ptr) bind(C, name="test_metData") |
| 62 | + import :: c_ptr,c_int, c_double |
| 63 | + type(c_ptr), intent(in), value :: obj_ptr |
| 64 | + real(c_double), intent(in), value :: time |
| 65 | + integer(c_int), intent(in), value :: n |
| 66 | + type(c_ptr), intent(in), value :: lons_ptr, lats_ptr, levs_ptr, u_ptr, v_ptr, w_ptr |
| 67 | + end subroutine |
| 68 | + |
| 69 | + subroutine setData ( metSrc_ptr, ctime, quantity_ptr, data_ptr) bind(C, name="setData") |
| 70 | + import :: c_ptr |
| 71 | + type(c_ptr), intent(in), value :: metSrc_ptr, ctime, quantity_ptr, data_ptr |
| 72 | + end subroutine setData |
| 73 | + |
| 74 | + subroutine getData ( metSrc_ptr, ctime, quantity_ptr, n, lons_ptr, lats_ptr, levs_ptr, values_ptr) bind(C, name="getData") |
| 75 | + import :: c_ptr, c_int |
| 76 | + integer(c_int), intent(in), value :: n |
| 77 | + type(c_ptr), intent(in), value :: metSrc_ptr, ctime, quantity_ptr, lons_ptr, lats_ptr, levs_ptr, values_ptr |
| 78 | + end subroutine getData |
| 79 | + |
| 80 | + subroutine getData2d ( metSrc_ptr, ctime, quantity_ptr, n, lons_ptr, lats_ptr, values_ptr) bind(C, name="getData2d") |
| 81 | + import :: c_ptr, c_int |
| 82 | + integer(c_int), intent(in), value :: n |
| 83 | + type(c_ptr), intent(in), value :: metSrc_ptr, ctime, quantity_ptr, lons_ptr, lats_ptr, values_ptr |
| 84 | + end subroutine getData2d |
| 85 | + end interface |
| 86 | + |
| 87 | +contains |
| 88 | + |
| 89 | + subroutine test_dataflow(num_parcels, lons, lats, zs, CellToRank, DIMS, comm) |
| 90 | + integer :: num_parcels, comm, DIMS(3) |
| 91 | + real, dimension(:), intent(in) :: lons, lats,zs |
| 92 | + integer, dimension(:,:), intent(in) :: CellToRank |
| 93 | + |
| 94 | + integer :: i, npes, ierror, rank, my_rank |
| 95 | + real :: dlon, dlat |
| 96 | + real, allocatable :: lons_positive(:) |
| 97 | + |
| 98 | + real, allocatable :: lons_send(:), lats_send(:), zs_send(:) |
| 99 | + real, allocatable :: lons_recv(:), lats_recv(:), zs_recv(:) |
| 100 | + real, allocatable :: U_recv(:), U_send(:) |
| 101 | + real, allocatable :: U(:), V(:), W(:), pos(:) |
| 102 | + |
| 103 | + integer, allocatable :: counts_send(:),counts_recv(:), II(:), JJ(:), ranks(:) |
| 104 | + integer, allocatable :: disp_send(:), disp_recv(:), tmp_position(:) |
| 105 | + |
| 106 | + dlon = 360.0 / DIMS(1) |
| 107 | + dlat = 180.0 / DIMS(2) |
| 108 | + |
| 109 | + lons_positive = lons |
| 110 | + where (lons_positive < 0) lons_positive=lons_positive + 360.0 |
| 111 | + II = min( max(ceiling (lons_positive/dlon),1), DIMS(1)) |
| 112 | + JJ = min( max(ceiling ((lats + 90.0)/dlat),1), DIMS(2)) |
| 113 | + |
| 114 | + call MPI_Comm_size(comm, npes, ierror) |
| 115 | + call MPI_Comm_rank(comm, my_rank, ierror) |
| 116 | + |
| 117 | + allocate(ranks(num_parcels)) |
| 118 | + allocate(counts_send(npes)) |
| 119 | + allocate(counts_recv(npes)) |
| 120 | + allocate(disp_send(npes)) |
| 121 | + allocate(disp_recv(npes)) |
| 122 | + |
| 123 | + do i = 1, num_parcels |
| 124 | + ranks(i) = CellToRank(II(i), JJ(i)) |
| 125 | + enddo |
| 126 | + |
| 127 | +!-- ------------------- |
| 128 | +!step 4) Pack the location data and send them to where the metData sit |
| 129 | +!-- ------------------- |
| 130 | + |
| 131 | + do rank = 0, npes-1 |
| 132 | + counts_send(rank+1) = count(ranks == rank) |
| 133 | + enddo |
| 134 | + |
| 135 | + call MPI_AllToALL(counts_send, 1, MPI_INTEGER, counts_recv, 1, MPI_INTEGER, comm, ierror) |
| 136 | + |
| 137 | + disp_send = 0 |
| 138 | + do rank = 1, npes-1 |
| 139 | + disp_send(rank+1) = disp_send(rank)+ counts_send(rank) |
| 140 | + enddo |
| 141 | + disp_recv = 0 |
| 142 | + do rank = 1, npes-1 |
| 143 | + disp_recv(rank+1) = disp_recv(rank)+ counts_recv(rank) |
| 144 | + enddo |
| 145 | + |
| 146 | + |
| 147 | + ! re-arranged lats lons, and ids |
| 148 | + tmp_position = disp_send |
| 149 | + allocate(lons_send(num_parcels)) |
| 150 | + allocate(lons_recv(sum(counts_recv))) |
| 151 | + allocate(lats_send(num_parcels)) |
| 152 | + allocate(lats_recv(sum(counts_recv))) |
| 153 | + allocate(zs_send(num_parcels)) |
| 154 | + allocate(zs_recv(sum(counts_recv))) |
| 155 | + |
| 156 | + allocate(pos(num_parcels)) |
| 157 | + do i = 1, num_parcels |
| 158 | + rank = ranks(i) |
| 159 | + pos(i) = tmp_position(rank+1) +1 |
| 160 | + lons_send(pos(i)) = lons(i) |
| 161 | + lats_send(pos(i)) = lats(i) |
| 162 | + zs_send(pos(i)) = zs(i) |
| 163 | + tmp_position(rank+1) = tmp_position(rank+1) + 1 |
| 164 | + enddo |
| 165 | + |
| 166 | + call MPI_AllToALLv(lons_send, counts_send, disp_send, MPI_REAL, lons_recv, counts_recv, disp_recv, MPI_REAL, comm, ierror) |
| 167 | + call MPI_AllToALLv(lats_send, counts_send, disp_send, MPI_REAL, lats_recv, counts_recv, disp_recv, MPI_REAL, comm, ierror) |
| 168 | + call MPI_AllToALLv(zs_send, counts_send, disp_send, MPI_REAL, zs_recv, counts_recv, disp_recv, MPI_REAL, comm, ierror) |
| 169 | +!-- ------------------- |
| 170 | +!step 5) Interpolate the data ( horiontally and vertically) and send back where they are from |
| 171 | +!-- ------------------- |
| 172 | + allocate(U_recv(sum(counts_recv)), source = my_rank*1.0) |
| 173 | + allocate(U_send(num_parcels), source = -1.0) |
| 174 | + ! |
| 175 | + ! Horizontal and vertical interpolator here |
| 176 | + ! |
| 177 | + call MPI_AllToALLv(U_recv, counts_recv, disp_recv, MPI_REAL, U_send, counts_send, disp_send, MPI_REAL, comm, ierror) |
| 178 | + |
| 179 | +!--------------------- |
| 180 | +!step 6) Rearrange data ( not necessary if ids was rearranged ins step 4) |
| 181 | +!--------------------- |
| 182 | + |
| 183 | + allocate(U(num_parcels)) |
| 184 | + allocate(V(num_parcels)) |
| 185 | + allocate(W(num_parcels)) |
| 186 | + U(:) = U_send(pos(:)) |
| 187 | + |
| 188 | + end subroutine |
| 189 | + |
| 190 | +end module GEOS_Giga_InterOpMod |
0 commit comments