Implement CCLite 0.1.3 command block
This commit is contained in:
+8
-1
@@ -1,6 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
project(CCLite VERSION 0.1.2 LANGUAGES C Fortran)
|
||||
project(CCLite VERSION 0.1.3 LANGUAGES C Fortran)
|
||||
|
||||
set(CMAKE_Fortran_STANDARD 2018)
|
||||
set(CMAKE_Fortran_STANDARD_REQUIRED ON)
|
||||
@@ -14,6 +14,7 @@ set(CCL_COMMON_SOURCES
|
||||
src/case/ccl_string_utils.f90
|
||||
src/case/ccl_case_types.f90
|
||||
src/case/ccl_inp_parser.f90
|
||||
src/interface_data/ccl_command_block.f90
|
||||
)
|
||||
|
||||
add_library(ccl_common STATIC ${CCL_COMMON_SOURCES})
|
||||
@@ -51,6 +52,11 @@ add_executable(test_ipc_lifecycle
|
||||
)
|
||||
target_link_libraries(test_ipc_lifecycle PRIVATE ccl_ipc ccl_common)
|
||||
|
||||
add_executable(test_command_block
|
||||
tests/unit/test_command_block.f90
|
||||
)
|
||||
target_link_libraries(test_command_block PRIVATE ccl_common)
|
||||
|
||||
add_test(NAME smoke_cclite_version COMMAND cclite --version)
|
||||
add_test(NAME smoke_cclite_help COMMAND cclite --help)
|
||||
add_test(NAME smoke_mock_sph_worker_version COMMAND mock_sph_worker --version)
|
||||
@@ -71,3 +77,4 @@ add_test(NAME check_invalid_ipc_type
|
||||
set_tests_properties(check_invalid_ipc_type PROPERTIES WILL_FAIL TRUE)
|
||||
|
||||
add_test(NAME ipc_lifecycle COMMAND test_ipc_lifecycle)
|
||||
add_test(NAME command_block_semantics COMMAND test_command_block)
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,102 @@
|
||||
program test_command_block
|
||||
use iso_c_binding, only: c_double, c_int32_t, c_int64_t
|
||||
use ccl_command_block, only: CCL_CMD_ADVANCE, CCL_CMD_INIT, CCL_COMMAND_MAGIC, &
|
||||
CCL_Command_Block_Type, CCL_SOLVER_ID_SPH, CCL_WORKER_DONE, CCL_WORKER_READY, &
|
||||
ccl_command_ack, ccl_command_block_size, ccl_command_check_ack, &
|
||||
ccl_command_check_timeout, ccl_command_dispatch, ccl_command_heartbeat, &
|
||||
ccl_command_init, ccl_command_pack, ccl_command_unpack, ccl_command_validate
|
||||
use ccl_errors, only: CCL_ERROR_IPC_BAD_HEADER, CCL_ERROR_IPC_TIMEOUT, &
|
||||
CCL_ERROR_WORKER_BAD_STATE, CCL_SUCCESS
|
||||
implicit none
|
||||
|
||||
integer :: ierr
|
||||
integer(c_int64_t) :: heartbeat
|
||||
type(CCL_Command_Block_Type) :: block
|
||||
type(CCL_Command_Block_Type) :: packed
|
||||
type(CCL_Command_Block_Type) :: unpacked
|
||||
|
||||
call ccl_command_init(block, CCL_SOLVER_ID_SPH)
|
||||
call ccl_command_validate(block, ierr)
|
||||
call require_equal(ierr, CCL_SUCCESS, "fresh command block validates")
|
||||
call require_true(ccl_command_block_size() > 0_c_int64_t, "command block has positive byte size")
|
||||
|
||||
call ccl_command_dispatch(block, CCL_CMD_INIT, 1_c_int64_t, 1.0d-6, ierr)
|
||||
call require_equal(ierr, CCL_SUCCESS, "dispatch INIT")
|
||||
call require_equal_int64(block%command_seq, 1_c_int64_t, "command seq increments")
|
||||
call require_equal(block%command_id, CCL_CMD_INIT, "command id set")
|
||||
call require_equal_int64(block%substep_id, 0_c_int64_t, "substep id reserved as zero")
|
||||
call require_equal_int64(block%iteration_id, 0_c_int64_t, "iteration id reserved as zero")
|
||||
|
||||
call ccl_command_check_ack(block, ierr)
|
||||
call require_equal(ierr, CCL_ERROR_WORKER_BAD_STATE, "unacked command is not complete")
|
||||
|
||||
call ccl_command_heartbeat(block)
|
||||
heartbeat = block%heartbeat_seq
|
||||
call ccl_command_check_timeout(block, heartbeat, 10, 5, ierr)
|
||||
call require_equal(ierr, CCL_ERROR_IPC_TIMEOUT, "unchanged heartbeat times out")
|
||||
call ccl_command_heartbeat(block)
|
||||
call ccl_command_check_timeout(block, heartbeat, 10, 5, ierr)
|
||||
call require_equal(ierr, CCL_SUCCESS, "changed heartbeat avoids timeout")
|
||||
|
||||
call ccl_command_ack(block, CCL_WORKER_DONE, 0_c_int32_t, ierr)
|
||||
call require_equal(ierr, CCL_SUCCESS, "ack DONE")
|
||||
call ccl_command_check_ack(block, ierr)
|
||||
call require_equal(ierr, CCL_SUCCESS, "ack seq matches command seq")
|
||||
|
||||
call ccl_command_dispatch(block, CCL_CMD_ADVANCE, 2_c_int64_t, 2.0d-6, ierr)
|
||||
call require_equal(ierr, CCL_SUCCESS, "dispatch ADVANCE")
|
||||
call ccl_command_ack(block, CCL_WORKER_READY, 0_c_int32_t, ierr)
|
||||
call require_equal(ierr, CCL_SUCCESS, "ack READY")
|
||||
|
||||
call ccl_command_pack(block, packed, ierr)
|
||||
call require_equal(ierr, CCL_SUCCESS, "pack command block")
|
||||
call ccl_command_unpack(packed, unpacked, ierr)
|
||||
call require_equal(ierr, CCL_SUCCESS, "unpack command block")
|
||||
call require_equal_int64(unpacked%command_seq, block%command_seq, "unpacked seq matches")
|
||||
call require_equal_int64(unpacked%ack_seq, block%ack_seq, "unpacked ack matches")
|
||||
call require_true(abs(unpacked%macro_dt - 2.0_c_double * 1.0d-6) < 1.0d-14, "macro dt preserved")
|
||||
|
||||
block%magic = CCL_COMMAND_MAGIC + 1_c_int64_t
|
||||
call ccl_command_validate(block, ierr)
|
||||
call require_equal(ierr, CCL_ERROR_IPC_BAD_HEADER, "bad magic rejected")
|
||||
|
||||
call ccl_command_init(block, CCL_SOLVER_ID_SPH)
|
||||
block%worker_state = 999_c_int32_t
|
||||
call ccl_command_validate(block, ierr)
|
||||
call require_equal(ierr, CCL_ERROR_WORKER_BAD_STATE, "bad worker state rejected")
|
||||
|
||||
contains
|
||||
|
||||
subroutine require_equal(actual, expected, message)
|
||||
integer, intent(in) :: actual
|
||||
integer, intent(in) :: expected
|
||||
character(len=*), intent(in) :: message
|
||||
|
||||
if (actual /= expected) then
|
||||
print '(a,": expected ",i0," got ",i0)', trim(message), expected, actual
|
||||
stop 1
|
||||
end if
|
||||
end subroutine require_equal
|
||||
|
||||
subroutine require_equal_int64(actual, expected, message)
|
||||
integer(c_int64_t), intent(in) :: actual
|
||||
integer(c_int64_t), intent(in) :: expected
|
||||
character(len=*), intent(in) :: message
|
||||
|
||||
if (actual /= expected) then
|
||||
print '(a,": int64 mismatch")', trim(message)
|
||||
stop 1
|
||||
end if
|
||||
end subroutine require_equal_int64
|
||||
|
||||
subroutine require_true(condition, message)
|
||||
logical, intent(in) :: condition
|
||||
character(len=*), intent(in) :: message
|
||||
|
||||
if (.not. condition) then
|
||||
print '(a)', trim(message)
|
||||
stop 1
|
||||
end if
|
||||
end subroutine require_true
|
||||
|
||||
end program test_command_block
|
||||
@@ -123,31 +123,31 @@
|
||||
|
||||
目标:实现命令共享块、seq/ack 和 worker 状态机基础。
|
||||
|
||||
- [ ] T0131 定义 command id 枚举。
|
||||
- [x] T0131 定义 command id 枚举。
|
||||
完成判据:支持 `INIT`、`READ_INPUT`、`BUILD_MODEL`、`EXPORT_INTERFACE`、`ADVANCE`、`WRITE_OUTPUT`、`STOP`。
|
||||
|
||||
- [ ] T0132 定义 worker state 枚举。
|
||||
- [x] T0132 定义 worker state 枚举。
|
||||
完成判据:支持 `STARTING`、`READY`、`BUSY`、`DONE`、`ERROR`、`STOPPED`。
|
||||
|
||||
- [ ] T0133 定义 command block 逻辑类型。
|
||||
- [x] T0133 定义 command block 逻辑类型。
|
||||
完成判据:字段覆盖 05 号文档要求,`substep_id` 和 `iteration_id` 在 0.1.x 写 0。
|
||||
|
||||
- [ ] T0134 实现 command block pack/unpack。
|
||||
- [x] T0134 实现 command block pack/unpack。
|
||||
完成判据:二进制布局可稳定写入共享内存并读回。
|
||||
|
||||
- [ ] T0135 实现 magic/version/layout 校验。
|
||||
- [x] T0135 实现 magic/version/layout 校验。
|
||||
完成判据:非法 magic、ABI 或 layout id 能在初始化阶段拒绝。
|
||||
|
||||
- [ ] T0136 实现 command seq/ack 配对。
|
||||
- [x] T0136 实现 command seq/ack 配对。
|
||||
完成判据:每条命令都有递增 seq,worker 回写对应 ack。
|
||||
|
||||
- [ ] T0137 实现 heartbeat 更新。
|
||||
- [x] T0137 实现 heartbeat 更新。
|
||||
完成判据:worker 空闲或执行命令时能更新 `heartbeat_seq`。
|
||||
|
||||
- [ ] T0138 实现 command timeout 检测。
|
||||
- [x] T0138 实现 command timeout 检测。
|
||||
完成判据:worker 不响应时 CCLite 返回超时错误,不永久等待。
|
||||
|
||||
- [ ] T0139 增加 command block 单元测试。
|
||||
- [x] T0139 增加 command block 单元测试。
|
||||
完成判据:覆盖字段读写、seq/ack、版本校验、非法状态和 timeout。
|
||||
|
||||
# 6. 0.1.4 Data Block 与 AoR v1
|
||||
|
||||
Reference in New Issue
Block a user