subroutine tem_load_weights( me, weights, success )
! -------------------------------------------------------------------- !
type(treelmesh_type), intent(in) :: me
real(kind=rk), intent(out) :: weights(me%nElems)
logical, intent(out) :: success
! -------------------------------------------------------------------- !
integer(kind=MPI_OFFSET_KIND) :: displacement
integer(kind=MPI_OFFSET_KIND) :: filebytes
integer :: fh, ftype, iError, typesize
integer :: iostatus( MPI_STATUS_SIZE )
integer :: level
integer :: iElem
logical :: ex
character(len=PathLen) :: filename
character(len=4) :: EndianSuffix
! -------------------------------------------------------------------- !
EndianSuffix = tem_create_endianSuffix()
if (trim(me%weights_file) /= '') then
filename = trim(me%weights_file)//EndianSuffix
! check if a corresponding weight file exists
if (me%global%myPart == 0) then
inquire(file=trim(filename), exist=ex)
end if
call MPI_Bcast(ex, 1, MPI_LOGICAL, 0, me%global%comm, iError)
else
ex = .false.
end if
if (ex) then
! Found a weights file, which is used to read a weight for each
! element.
write(logUnit(3),*) 'Loading Weights from file: '//trim(filename)
! Open the binary file for MPI I/O (Write)
call MPI_File_open( me%global%comm, trim(filename), &
& MPI_MODE_RDONLY, MPI_INFO_NULL, fh, iError )
call check_mpi_error(iError,'file_open in load_weights')
! Create a MPI Subarray as ftype for file view
call MPI_Type_contiguous( me%nElems, rk_mpi , ftype, iError )
call check_mpi_error(iError,'type ftype in load_weights')
call MPI_Type_commit( ftype, iError )
call check_mpi_error(iError,'commit ftype in load_weights')
!get size of etype
call MPI_Type_size(rk_mpi, typesize, iError )
call check_mpi_error(iError,'typesize in load_weights')
call MPI_File_get_size(fh, filebytes, iError)
! Check whether the weight file has as many values as there are
! elements in the mesh.
if (filebytes /= typesize*me%global%nElems) then
write(logunit(3),*) 'Mesh file has wrong number of elements!'
write(logunit(3),*) 'NO BALANCING!'
success = .false.
else
! File has correct number of elements matching the mesh.
! calculate displacement
displacement = me%elemoffset * typesize * 1_MPI_OFFSET_KIND
! Set the view for each process on the file above
call MPI_File_set_view( fh, displacement, rk_mpi, ftype, &
& "native", MPI_INFO_NULL, iError )
call check_mpi_error(iError,'set_view in load_weights')
! Read data from the file
call MPI_File_read_all( fh, weights, me%nElems, rk_mpi, iostatus, &
& iError )
call check_mpi_error(iError,'read_all in load_weights')
success = .true.
end if
!Free the MPI_Datatypes which were created and close the file
call MPI_Type_free(ftype, iError)
call check_mpi_error(iError,'free ftype in load_weights')
call MPI_File_close(fh, iError)
call check_mpi_error(iError,'close file in load_weights')
else if (maxval(me%levelweight) > 0.0_rk) then
write(logunit(3),*) 'Using levelwise weights.'
do iElem=1,me%nElems
level = tem_levelOf( me%treeID(iElem) )
weights(iElem) = me%levelWeight(level)
end do
success = .true.
else
write(logunit(3),*) 'NO BALANCING!'
success = .false.
end if
write(logUnit(3),*) 'Done loading weights.'
end subroutine tem_load_weights