Implement CCLite 0.1.3 command block
This commit is contained in:
@@ -3,8 +3,8 @@ module ccl_version
|
||||
|
||||
integer, parameter :: CCL_VERSION_MAJOR = 0
|
||||
integer, parameter :: CCL_VERSION_MINOR = 1
|
||||
integer, parameter :: CCL_VERSION_PATCH = 2
|
||||
character(len=*), parameter :: CCL_VERSION_LABEL = "0.1.2"
|
||||
integer, parameter :: CCL_VERSION_PATCH = 3
|
||||
character(len=*), parameter :: CCL_VERSION_LABEL = "0.1.3"
|
||||
|
||||
contains
|
||||
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
module ccl_command_block
|
||||
use iso_c_binding, only: c_double, c_int32_t, c_int64_t, c_sizeof
|
||||
use ccl_errors, only: CCL_ERROR_IPC_BAD_HEADER, CCL_ERROR_IPC_TIMEOUT, &
|
||||
CCL_ERROR_WORKER_BAD_STATE, CCL_SUCCESS
|
||||
implicit none
|
||||
|
||||
integer(c_int64_t), parameter :: CCL_COMMAND_MAGIC = 1128350028_c_int64_t
|
||||
integer(c_int32_t), parameter :: CCL_COMMAND_ABI_VERSION = 1_c_int32_t
|
||||
integer(c_int32_t), parameter :: CCL_COMMAND_LAYOUT_ID = 1_c_int32_t
|
||||
|
||||
integer(c_int32_t), parameter :: CCL_SOLVER_ID_SPH = 1_c_int32_t
|
||||
integer(c_int32_t), parameter :: CCL_SOLVER_ID_FEM = 2_c_int32_t
|
||||
|
||||
integer(c_int32_t), parameter :: CCL_CMD_INIT = 1_c_int32_t
|
||||
integer(c_int32_t), parameter :: CCL_CMD_READ_INPUT = 2_c_int32_t
|
||||
integer(c_int32_t), parameter :: CCL_CMD_BUILD_MODEL = 3_c_int32_t
|
||||
integer(c_int32_t), parameter :: CCL_CMD_EXPORT_INTERFACE = 4_c_int32_t
|
||||
integer(c_int32_t), parameter :: CCL_CMD_IMPORT_LOADS = 5_c_int32_t
|
||||
integer(c_int32_t), parameter :: CCL_CMD_ADVANCE = 6_c_int32_t
|
||||
integer(c_int32_t), parameter :: CCL_CMD_WRITE_OUTPUT = 7_c_int32_t
|
||||
integer(c_int32_t), parameter :: CCL_CMD_STOP = 99_c_int32_t
|
||||
|
||||
integer(c_int32_t), parameter :: CCL_WORKER_STARTING = 1_c_int32_t
|
||||
integer(c_int32_t), parameter :: CCL_WORKER_READY = 2_c_int32_t
|
||||
integer(c_int32_t), parameter :: CCL_WORKER_BUSY = 3_c_int32_t
|
||||
integer(c_int32_t), parameter :: CCL_WORKER_DONE = 4_c_int32_t
|
||||
integer(c_int32_t), parameter :: CCL_WORKER_ERROR = 5_c_int32_t
|
||||
integer(c_int32_t), parameter :: CCL_WORKER_STOPPED = 6_c_int32_t
|
||||
|
||||
type, bind(C) :: CCL_Command_Block_Type
|
||||
integer(c_int64_t) :: magic
|
||||
integer(c_int32_t) :: abi_version
|
||||
integer(c_int32_t) :: layout_id
|
||||
integer(c_int32_t) :: solver_id
|
||||
integer(c_int32_t) :: command_id
|
||||
integer(c_int64_t) :: command_seq
|
||||
integer(c_int64_t) :: ack_seq
|
||||
integer(c_int64_t) :: step_id
|
||||
integer(c_int64_t) :: substep_id
|
||||
integer(c_int64_t) :: iteration_id
|
||||
integer(c_int64_t) :: command_flags
|
||||
integer(c_int64_t) :: heartbeat_seq
|
||||
integer(c_int32_t) :: worker_state
|
||||
integer(c_int32_t) :: error_code
|
||||
integer(c_int32_t) :: int_param(16)
|
||||
real(c_double) :: real_param(16)
|
||||
real(c_double) :: macro_dt
|
||||
end type CCL_Command_Block_Type
|
||||
|
||||
contains
|
||||
|
||||
subroutine ccl_command_init(block, solver_id)
|
||||
type(CCL_Command_Block_Type), intent(out) :: block
|
||||
integer(c_int32_t), intent(in) :: solver_id
|
||||
|
||||
block%magic = CCL_COMMAND_MAGIC
|
||||
block%abi_version = CCL_COMMAND_ABI_VERSION
|
||||
block%layout_id = CCL_COMMAND_LAYOUT_ID
|
||||
block%solver_id = solver_id
|
||||
block%command_id = 0_c_int32_t
|
||||
block%command_seq = 0_c_int64_t
|
||||
block%ack_seq = 0_c_int64_t
|
||||
block%step_id = 0_c_int64_t
|
||||
block%substep_id = 0_c_int64_t
|
||||
block%iteration_id = 0_c_int64_t
|
||||
block%command_flags = 0_c_int64_t
|
||||
block%heartbeat_seq = 0_c_int64_t
|
||||
block%worker_state = CCL_WORKER_STARTING
|
||||
block%error_code = CCL_SUCCESS
|
||||
block%int_param = 0_c_int32_t
|
||||
block%real_param = 0.0_c_double
|
||||
block%macro_dt = 0.0_c_double
|
||||
end subroutine ccl_command_init
|
||||
|
||||
subroutine ccl_command_validate(block, ierr)
|
||||
type(CCL_Command_Block_Type), intent(in) :: block
|
||||
integer, intent(out) :: ierr
|
||||
|
||||
ierr = CCL_SUCCESS
|
||||
if (block%magic /= CCL_COMMAND_MAGIC) then
|
||||
ierr = CCL_ERROR_IPC_BAD_HEADER
|
||||
return
|
||||
end if
|
||||
if (block%abi_version /= CCL_COMMAND_ABI_VERSION) then
|
||||
ierr = CCL_ERROR_IPC_BAD_HEADER
|
||||
return
|
||||
end if
|
||||
if (block%layout_id /= CCL_COMMAND_LAYOUT_ID) then
|
||||
ierr = CCL_ERROR_IPC_BAD_HEADER
|
||||
return
|
||||
end if
|
||||
if (.not. ccl_worker_state_is_valid(block%worker_state)) then
|
||||
ierr = CCL_ERROR_WORKER_BAD_STATE
|
||||
end if
|
||||
end subroutine ccl_command_validate
|
||||
|
||||
subroutine ccl_command_dispatch(block, command_id, step_id, macro_dt, ierr)
|
||||
type(CCL_Command_Block_Type), intent(inout) :: block
|
||||
integer(c_int32_t), intent(in) :: command_id
|
||||
integer(c_int64_t), intent(in) :: step_id
|
||||
real(c_double), intent(in) :: macro_dt
|
||||
integer, intent(out) :: ierr
|
||||
|
||||
call ccl_command_validate(block, ierr)
|
||||
if (ierr /= CCL_SUCCESS) return
|
||||
if (.not. ccl_command_id_is_valid(command_id)) then
|
||||
ierr = CCL_ERROR_IPC_BAD_HEADER
|
||||
return
|
||||
end if
|
||||
|
||||
block%command_id = command_id
|
||||
block%command_seq = block%command_seq + 1_c_int64_t
|
||||
block%step_id = step_id
|
||||
block%substep_id = 0_c_int64_t
|
||||
block%iteration_id = 0_c_int64_t
|
||||
block%macro_dt = macro_dt
|
||||
block%worker_state = CCL_WORKER_BUSY
|
||||
block%error_code = CCL_SUCCESS
|
||||
ierr = CCL_SUCCESS
|
||||
end subroutine ccl_command_dispatch
|
||||
|
||||
subroutine ccl_command_ack(block, worker_state, error_code, ierr)
|
||||
type(CCL_Command_Block_Type), intent(inout) :: block
|
||||
integer(c_int32_t), intent(in) :: worker_state
|
||||
integer(c_int32_t), intent(in) :: error_code
|
||||
integer, intent(out) :: ierr
|
||||
|
||||
call ccl_command_validate(block, ierr)
|
||||
if (ierr /= CCL_SUCCESS) return
|
||||
if (.not. ccl_worker_state_is_valid(worker_state)) then
|
||||
ierr = CCL_ERROR_WORKER_BAD_STATE
|
||||
return
|
||||
end if
|
||||
|
||||
block%ack_seq = block%command_seq
|
||||
block%worker_state = worker_state
|
||||
block%error_code = error_code
|
||||
ierr = CCL_SUCCESS
|
||||
end subroutine ccl_command_ack
|
||||
|
||||
subroutine ccl_command_check_ack(block, ierr)
|
||||
type(CCL_Command_Block_Type), intent(in) :: block
|
||||
integer, intent(out) :: ierr
|
||||
|
||||
call ccl_command_validate(block, ierr)
|
||||
if (ierr /= CCL_SUCCESS) return
|
||||
if (block%ack_seq /= block%command_seq) then
|
||||
ierr = CCL_ERROR_WORKER_BAD_STATE
|
||||
end if
|
||||
end subroutine ccl_command_check_ack
|
||||
|
||||
subroutine ccl_command_heartbeat(block)
|
||||
type(CCL_Command_Block_Type), intent(inout) :: block
|
||||
|
||||
block%heartbeat_seq = block%heartbeat_seq + 1_c_int64_t
|
||||
end subroutine ccl_command_heartbeat
|
||||
|
||||
subroutine ccl_command_check_timeout(block, previous_heartbeat, elapsed_ms, timeout_ms, ierr)
|
||||
type(CCL_Command_Block_Type), intent(in) :: block
|
||||
integer(c_int64_t), intent(in) :: previous_heartbeat
|
||||
integer, intent(in) :: elapsed_ms
|
||||
integer, intent(in) :: timeout_ms
|
||||
integer, intent(out) :: ierr
|
||||
|
||||
ierr = CCL_SUCCESS
|
||||
if (elapsed_ms >= timeout_ms .and. block%heartbeat_seq == previous_heartbeat) then
|
||||
ierr = CCL_ERROR_IPC_TIMEOUT
|
||||
end if
|
||||
end subroutine ccl_command_check_timeout
|
||||
|
||||
subroutine ccl_command_pack(src, dst, ierr)
|
||||
type(CCL_Command_Block_Type), intent(in) :: src
|
||||
type(CCL_Command_Block_Type), intent(out) :: dst
|
||||
integer, intent(out) :: ierr
|
||||
|
||||
call ccl_command_validate(src, ierr)
|
||||
if (ierr == CCL_SUCCESS) dst = src
|
||||
end subroutine ccl_command_pack
|
||||
|
||||
subroutine ccl_command_unpack(src, dst, ierr)
|
||||
type(CCL_Command_Block_Type), intent(in) :: src
|
||||
type(CCL_Command_Block_Type), intent(out) :: dst
|
||||
integer, intent(out) :: ierr
|
||||
|
||||
dst = src
|
||||
call ccl_command_validate(dst, ierr)
|
||||
end subroutine ccl_command_unpack
|
||||
|
||||
integer(c_int64_t) function ccl_command_block_size() result(byte_size)
|
||||
type(CCL_Command_Block_Type) :: block
|
||||
|
||||
call ccl_command_init(block, CCL_SOLVER_ID_SPH)
|
||||
byte_size = int(c_sizeof(block), c_int64_t)
|
||||
end function ccl_command_block_size
|
||||
|
||||
logical function ccl_command_id_is_valid(command_id) result(valid)
|
||||
integer(c_int32_t), intent(in) :: command_id
|
||||
|
||||
select case (command_id)
|
||||
case (CCL_CMD_INIT, CCL_CMD_READ_INPUT, CCL_CMD_BUILD_MODEL, CCL_CMD_EXPORT_INTERFACE, &
|
||||
CCL_CMD_IMPORT_LOADS, CCL_CMD_ADVANCE, CCL_CMD_WRITE_OUTPUT, CCL_CMD_STOP)
|
||||
valid = .true.
|
||||
case default
|
||||
valid = .false.
|
||||
end select
|
||||
end function ccl_command_id_is_valid
|
||||
|
||||
logical function ccl_worker_state_is_valid(worker_state) result(valid)
|
||||
integer(c_int32_t), intent(in) :: worker_state
|
||||
|
||||
select case (worker_state)
|
||||
case (CCL_WORKER_STARTING, CCL_WORKER_READY, CCL_WORKER_BUSY, CCL_WORKER_DONE, &
|
||||
CCL_WORKER_ERROR, CCL_WORKER_STOPPED)
|
||||
valid = .true.
|
||||
case default
|
||||
valid = .false.
|
||||
end select
|
||||
end function ccl_worker_state_is_valid
|
||||
|
||||
end module ccl_command_block
|
||||
Reference in New Issue
Block a user