Implement CCLite 0.1.5 empty coupling

This commit is contained in:
Codex
2026-07-05 17:04:19 +08:00
parent 69ae2a1c9c
commit a9f40635b6
10 changed files with 1146 additions and 20 deletions
+332 -2
View File
@@ -1,9 +1,57 @@
module ccl_mock_worker
use ccl_errors, only: CCL_SUCCESS
use iso_c_binding, only: c_f_pointer, c_int32_t, c_int64_t, c_size_t
use ccl_command_block, only: CCL_CMD_ADVANCE, CCL_CMD_BUILD_MODEL, CCL_CMD_EXPORT_INTERFACE, &
CCL_CMD_IMPORT_LOADS, CCL_CMD_INIT, CCL_CMD_READ_INPUT, CCL_CMD_STOP, CCL_CMD_WRITE_OUTPUT, &
CCL_Command_Block_Type, CCL_SOLVER_ID_FEM, CCL_SOLVER_ID_SPH, CCL_WORKER_DONE, &
CCL_WORKER_ERROR, CCL_WORKER_READY, CCL_WORKER_STOPPED, ccl_command_ack, &
ccl_command_block_size, ccl_command_heartbeat
use ccl_data_block, only: CCL_BUFFER_CONSUMED, CCL_BUFFER_READY, CCL_BUFFER_READING, &
CCL_BUFFER_WRITING, CCL_Data_Block_Header_Type, CCL_ROLE_CCL_TO_FEM_LOAD, &
CCL_ROLE_CCL_TO_SPH_FORCE, CCL_ROLE_FEM_TO_CCL_FACET, CCL_ROLE_SPH_TO_CCL_PARTICLE, &
ccl_data_header_init, ccl_data_header_set_count, ccl_data_header_set_state, &
ccl_data_header_size, ccl_data_header_validate, ccl_data_record_size_for_role
use ccl_errors, only: CCL_ERROR_INPUT_INVALID_VALUE, CCL_ERROR_IPC_BAD_HEADER, &
CCL_ERROR_IPC_TIMEOUT, CCL_SUCCESS
use ccl_ipc, only: CCL_Named_Event, CCL_Shared_Memory, ccl_event_close, ccl_event_open, &
ccl_event_reset, ccl_event_signal, ccl_event_wait, ccl_shm_close, ccl_shm_open
use ccl_log, only: ccl_log_info
use ccl_version, only: CCL_VERSION_LABEL
implicit none
integer(c_int32_t), parameter :: CCL_SOLVER_ID_CCL = 3_c_int32_t
integer(c_int32_t), parameter :: CCL_EXPORT_RECORD_CAPACITY = 4_c_int32_t
integer(c_int32_t), parameter :: CCL_EMPTY_LOAD_CAPACITY = 0_c_int32_t
type :: CCL_Mock_Worker_Config
logical :: service_mode = .false.
character(len=16) :: solver_name = ""
character(len=256) :: prefix = ""
character(len=256) :: input_file = ""
integer :: timeout_ms = 5000
end type CCL_Mock_Worker_Config
type :: CCL_Mock_Worker_Runtime
character(len=16) :: solver_name = ""
character(len=256) :: prefix = ""
character(len=256) :: command_name = ""
character(len=256) :: command_event_name = ""
character(len=256) :: ack_event_name = ""
character(len=256) :: export_name = ""
character(len=256) :: load_name = ""
integer(c_int32_t) :: solver_id = 0_c_int32_t
integer(c_int32_t) :: export_role = 0_c_int32_t
integer(c_int32_t) :: load_role = 0_c_int32_t
integer :: timeout_ms = 5000
type(CCL_Shared_Memory) :: command_shm
type(CCL_Shared_Memory) :: export_shm
type(CCL_Shared_Memory) :: load_shm
type(CCL_Named_Event) :: command_event
type(CCL_Named_Event) :: ack_event
type(CCL_Command_Block_Type), pointer :: command => null()
type(CCL_Data_Block_Header_Type), pointer :: export_header => null()
type(CCL_Data_Block_Header_Type), pointer :: load_header => null()
end type CCL_Mock_Worker_Runtime
contains
integer function run_mock_worker(worker_name, solver_id) result(ierr)
@@ -11,6 +59,7 @@ contains
character(len=*), intent(in) :: solver_id
integer :: argc
character(len=128) :: arg
type(CCL_Mock_Worker_Config) :: config
argc = command_argument_count()
if (argc >= 1) then
@@ -28,6 +77,12 @@ contains
end select
end if
call parse_worker_args(solver_id, config)
if (config%service_mode) then
ierr = run_worker_service(worker_name, config)
return
end if
call ccl_log_info(worker_name, "mock worker started")
call ccl_log_info(worker_name, "solver_id=" // trim(solver_id))
call ccl_log_info(worker_name, "0.1.0 skeleton exits without IPC attach")
@@ -41,7 +96,282 @@ contains
print '(a)', "Usage:"
print '(a)', " " // trim(worker_name) // " --help"
print '(a)', " " // trim(worker_name) // " --version"
print '(a)', " " // trim(worker_name) // " --cclite-worker --solver-id <SPH|FEM>"
print '(a)', " " // trim(worker_name) // " --cclite-worker --solver-id <SPH|FEM> --ipc-prefix <name>"
end subroutine print_worker_help
subroutine parse_worker_args(default_solver_id, config)
character(len=*), intent(in) :: default_solver_id
type(CCL_Mock_Worker_Config), intent(out) :: config
integer :: argc
integer :: i
integer :: ios
character(len=256) :: arg
config = CCL_Mock_Worker_Config()
config%solver_name = trim(default_solver_id)
argc = command_argument_count()
i = 1
do while (i <= argc)
call get_command_argument(i, arg)
arg = adjustl(arg)
select case (trim(arg))
case ("--cclite-worker")
config%service_mode = .true.
case ("--solver-id")
if (i < argc) then
i = i + 1
call get_command_argument(i, config%solver_name)
config%solver_name = adjustl(config%solver_name)
end if
case ("--ipc-prefix")
if (i < argc) then
i = i + 1
call get_command_argument(i, config%prefix)
config%prefix = adjustl(config%prefix)
end if
case ("--timeout-ms")
if (i < argc) then
i = i + 1
call get_command_argument(i, arg)
read(arg, *, iostat=ios) config%timeout_ms
if (ios /= 0) config%timeout_ms = 5000
end if
case ("--solver-input")
if (i < argc) then
i = i + 1
call get_command_argument(i, config%input_file)
config%input_file = adjustl(config%input_file)
end if
end select
i = i + 1
end do
end subroutine parse_worker_args
integer function run_worker_service(worker_name, config) result(ierr)
character(len=*), intent(in) :: worker_name
type(CCL_Mock_Worker_Config), intent(in) :: config
type(CCL_Mock_Worker_Runtime) :: runtime
integer :: close_ierr
call ccl_log_info(worker_name, "mock worker entering CCLite service mode")
call init_runtime(config, runtime, ierr)
if (ierr /= CCL_SUCCESS) goto 900
call open_runtime_resources(runtime, ierr)
if (ierr /= CCL_SUCCESS) goto 900
call signal_worker_ready(runtime, ierr)
if (ierr /= CCL_SUCCESS) goto 900
call command_loop(runtime, ierr)
900 continue
call close_runtime(runtime, close_ierr)
end function run_worker_service
subroutine init_runtime(config, runtime, ierr)
type(CCL_Mock_Worker_Config), intent(in) :: config
type(CCL_Mock_Worker_Runtime), intent(out) :: runtime
integer, intent(out) :: ierr
ierr = CCL_SUCCESS
runtime%solver_name = trim(config%solver_name)
runtime%prefix = trim(config%prefix)
runtime%timeout_ms = config%timeout_ms
if (len_trim(runtime%prefix) == 0) then
ierr = CCL_ERROR_INPUT_INVALID_VALUE
return
end if
select case (trim(runtime%solver_name))
case ("SPH")
runtime%solver_id = CCL_SOLVER_ID_SPH
runtime%export_role = CCL_ROLE_SPH_TO_CCL_PARTICLE
runtime%load_role = CCL_ROLE_CCL_TO_SPH_FORCE
call make_name(runtime%prefix, "CCL_COMMAND_SPH", runtime%command_name)
call make_name(runtime%prefix, "EVENT_COMMAND_SPH", runtime%command_event_name)
call make_name(runtime%prefix, "EVENT_ACK_SPH", runtime%ack_event_name)
call make_name(runtime%prefix, "SPH_TO_CCL_PARTICLE", runtime%export_name)
call make_name(runtime%prefix, "CCL_TO_SPH_FORCE", runtime%load_name)
case ("FEM")
runtime%solver_id = CCL_SOLVER_ID_FEM
runtime%export_role = CCL_ROLE_FEM_TO_CCL_FACET
runtime%load_role = CCL_ROLE_CCL_TO_FEM_LOAD
call make_name(runtime%prefix, "CCL_COMMAND_FEM", runtime%command_name)
call make_name(runtime%prefix, "EVENT_COMMAND_FEM", runtime%command_event_name)
call make_name(runtime%prefix, "EVENT_ACK_FEM", runtime%ack_event_name)
call make_name(runtime%prefix, "FEM_TO_CCL_FACET", runtime%export_name)
call make_name(runtime%prefix, "CCL_TO_FEM_LOAD", runtime%load_name)
case default
ierr = CCL_ERROR_INPUT_INVALID_VALUE
end select
end subroutine init_runtime
subroutine open_runtime_resources(runtime, ierr)
type(CCL_Mock_Worker_Runtime), intent(inout) :: runtime
integer, intent(out) :: ierr
integer(c_size_t) :: command_bytes
integer(c_size_t) :: export_bytes
integer(c_size_t) :: load_bytes
command_bytes = int(ccl_command_block_size(), c_size_t)
export_bytes = data_block_bytes(runtime%export_role, CCL_EXPORT_RECORD_CAPACITY)
load_bytes = data_block_bytes(runtime%load_role, CCL_EMPTY_LOAD_CAPACITY)
call ccl_shm_open(runtime%command_name, command_bytes, runtime%command_shm, ierr)
if (ierr /= CCL_SUCCESS) return
call c_f_pointer(runtime%command_shm%payload, runtime%command)
call ccl_shm_open(runtime%export_name, export_bytes, runtime%export_shm, ierr)
if (ierr /= CCL_SUCCESS) return
call c_f_pointer(runtime%export_shm%payload, runtime%export_header)
call ccl_shm_open(runtime%load_name, load_bytes, runtime%load_shm, ierr)
if (ierr /= CCL_SUCCESS) return
call c_f_pointer(runtime%load_shm%payload, runtime%load_header)
call ccl_event_open(runtime%command_event_name, runtime%command_event, ierr)
if (ierr /= CCL_SUCCESS) return
call ccl_event_open(runtime%ack_event_name, runtime%ack_event, ierr)
end subroutine open_runtime_resources
subroutine signal_worker_ready(runtime, ierr)
type(CCL_Mock_Worker_Runtime), intent(inout) :: runtime
integer, intent(out) :: ierr
call ccl_command_heartbeat(runtime%command)
call ccl_command_ack(runtime%command, CCL_WORKER_READY, CCL_SUCCESS, ierr)
if (ierr /= CCL_SUCCESS) return
call ccl_event_signal(runtime%ack_event, ierr)
end subroutine signal_worker_ready
subroutine command_loop(runtime, ierr)
type(CCL_Mock_Worker_Runtime), intent(inout) :: runtime
integer, intent(out) :: ierr
integer(c_int64_t) :: last_seq
integer :: ack_ierr
logical :: should_stop
ierr = CCL_SUCCESS
last_seq = runtime%command%command_seq
do
call ccl_event_wait(runtime%command_event, runtime%timeout_ms, ierr)
if (ierr == CCL_ERROR_IPC_TIMEOUT) then
call ccl_command_heartbeat(runtime%command)
ierr = CCL_SUCCESS
cycle
end if
if (ierr /= CCL_SUCCESS) return
call ccl_event_reset(runtime%command_event, ierr)
if (ierr /= CCL_SUCCESS) return
if (runtime%command%command_seq == last_seq) cycle
last_seq = runtime%command%command_seq
should_stop = .false.
call handle_command(runtime, should_stop, ierr)
if (ierr == CCL_SUCCESS) then
if (should_stop) then
call ccl_command_ack(runtime%command, CCL_WORKER_STOPPED, CCL_SUCCESS, ierr)
else
call ccl_command_ack(runtime%command, CCL_WORKER_DONE, CCL_SUCCESS, ierr)
end if
else
ack_ierr = ierr
call ccl_command_ack(runtime%command, CCL_WORKER_ERROR, ack_ierr, ierr)
end if
if (ierr /= CCL_SUCCESS) return
call ccl_event_signal(runtime%ack_event, ierr)
if (ierr /= CCL_SUCCESS) return
if (should_stop) exit
end do
end subroutine command_loop
subroutine handle_command(runtime, should_stop, ierr)
type(CCL_Mock_Worker_Runtime), intent(inout) :: runtime
logical, intent(out) :: should_stop
integer, intent(out) :: ierr
ierr = CCL_SUCCESS
should_stop = .false.
call ccl_command_heartbeat(runtime%command)
select case (runtime%command%command_id)
case (CCL_CMD_INIT, CCL_CMD_READ_INPUT, CCL_CMD_BUILD_MODEL, CCL_CMD_ADVANCE, CCL_CMD_WRITE_OUTPUT)
ierr = CCL_SUCCESS
case (CCL_CMD_EXPORT_INTERFACE)
call export_empty_block(runtime, ierr)
case (CCL_CMD_IMPORT_LOADS)
call consume_empty_load(runtime, ierr)
case (CCL_CMD_STOP)
should_stop = .true.
case default
ierr = CCL_ERROR_IPC_BAD_HEADER
end select
end subroutine handle_command
subroutine export_empty_block(runtime, ierr)
type(CCL_Mock_Worker_Runtime), intent(inout) :: runtime
integer, intent(out) :: ierr
call ccl_data_header_init(runtime%export_header, runtime%export_role, runtime%solver_id, &
CCL_SOLVER_ID_CCL, CCL_EXPORT_RECORD_CAPACITY)
runtime%export_header%step_id = int(runtime%command%step_id, c_int32_t)
call ccl_data_header_set_state(runtime%export_header, CCL_BUFFER_WRITING, ierr)
if (ierr /= CCL_SUCCESS) return
call ccl_data_header_set_count(runtime%export_header, 0_c_int32_t, ierr)
if (ierr /= CCL_SUCCESS) return
call ccl_data_header_set_state(runtime%export_header, CCL_BUFFER_READY, ierr)
end subroutine export_empty_block
subroutine consume_empty_load(runtime, ierr)
type(CCL_Mock_Worker_Runtime), intent(inout) :: runtime
integer, intent(out) :: ierr
call ccl_data_header_validate(runtime%load_header, ierr)
if (ierr /= CCL_SUCCESS) return
if (runtime%load_header%state /= CCL_BUFFER_READY) then
ierr = CCL_ERROR_IPC_BAD_HEADER
return
end if
call ccl_data_header_set_state(runtime%load_header, CCL_BUFFER_READING, ierr)
if (ierr /= CCL_SUCCESS) return
call ccl_data_header_set_state(runtime%load_header, CCL_BUFFER_CONSUMED, ierr)
end subroutine consume_empty_load
subroutine close_runtime(runtime, ierr)
type(CCL_Mock_Worker_Runtime), intent(inout) :: runtime
integer, intent(out) :: ierr
integer :: local_ierr
ierr = CCL_SUCCESS
call ccl_event_close(runtime%ack_event, local_ierr)
if (ierr == CCL_SUCCESS) ierr = local_ierr
call ccl_event_close(runtime%command_event, local_ierr)
if (ierr == CCL_SUCCESS) ierr = local_ierr
call ccl_shm_close(runtime%load_shm, local_ierr)
if (ierr == CCL_SUCCESS) ierr = local_ierr
call ccl_shm_close(runtime%export_shm, local_ierr)
if (ierr == CCL_SUCCESS) ierr = local_ierr
call ccl_shm_close(runtime%command_shm, local_ierr)
if (ierr == CCL_SUCCESS) ierr = local_ierr
nullify(runtime%command)
nullify(runtime%export_header)
nullify(runtime%load_header)
end subroutine close_runtime
integer(c_size_t) function data_block_bytes(block_role, capacity) result(byte_size)
integer(c_int32_t), intent(in) :: block_role
integer(c_int32_t), intent(in) :: capacity
byte_size = int(ccl_data_header_size(), c_size_t) + &
int(ccl_data_record_size_for_role(block_role), c_size_t) * int(capacity, c_size_t)
end function data_block_bytes
subroutine make_name(prefix, role, name)
character(len=*), intent(in) :: prefix
character(len=*), intent(in) :: role
character(len=*), intent(out) :: name
write(name, '(a,"_",a)') trim(prefix), trim(role)
end subroutine make_name
end module ccl_mock_worker