program test_external_file_backend use fem_precision use fem_error use fem_control use fem_sparse_csr use fem_linear_solver_ext_audit use fem_linear_solver_ext_io use fem_linear_solver_ext_handoff use fem_linear_solver_ext_file use fem_linsol_ext_file_cmd use fem_linsol_ext_file_report use fem_linear_solver_cg implicit none real(rk), parameter :: TOL = 1.0e-10_rk call cleanup_files() call check_config_and_names() call check_command_template() call check_prepare_finish_manual() call check_finish_cleanup_policy() call check_solve_waiting() call check_auto_run_without_command() call check_dispatch_file_backend() call check_error_paths() call cleanup_files() contains subroutine check_config_and_names() character(len=96) :: prefix character(len=96) :: command character(len=32) :: name integer :: auto_run integer :: keep_files integer :: index_base integer :: storage integer :: ierr integer :: backend integer :: available integer :: capabilities call ext_file_configure('ext_file_cfg', '', 0, 1, EXT_IO_INDEX_ZERO, & EXT_IO_STORAGE_GENERAL, ierr) call require_success(ierr, 10) call ext_file_get_config(prefix, command, auto_run, keep_files, & index_base, storage, ierr) call require_success(ierr, 11) if (trim(prefix) .ne. 'ext_file_cfg') call fail(12) if (len_trim(command) .ne. 0) call fail(13) if (auto_run .ne. 0 .or. keep_files .ne. 1) call fail(14) if (index_base .ne. EXT_IO_INDEX_ZERO) call fail(15) if (storage .ne. EXT_IO_STORAGE_GENERAL) call fail(16) call ext_file_status_name(EXT_FILE_READY, name, ierr) call require_success(ierr, 17) if (trim(name) .ne. 'ready') call fail(18) call ext_file_status_name(EXT_FILE_WAITING, name, ierr) call require_success(ierr, 19) if (trim(name) .ne. 'waiting') call fail(20) call linear_external_backend_name(LS_EXT_BACKEND_FILE, name, ierr) call require_success(ierr, 21) if (trim(name) .ne. 'file_exchange') call fail(22) call linear_solver_config_external(LS_EXT_BACKEND_FILE, ierr) call require_success(ierr, 23) call linear_solver_get_external_info(backend, available, capabilities, ierr) call require_success(ierr, 24) if (backend .ne. LS_EXT_BACKEND_FILE) call fail(25) if (available .ne. 1) call fail(26) if (iand(capabilities, LS_EXT_CAP_GENERAL) .eq. 0) call fail(27) call linear_solver_config_external(LS_EXT_BACKEND_REF_DIRECT, ierr) call require_success(ierr, 28) end subroutine check_config_and_names subroutine check_command_template() type(csr_matrix) :: a type(ext_file_state) :: state real(rk) :: rhs(3) real(rk) :: x(3) integer :: ierr integer :: found character(len=EXT_FILE_CMD_LEN) :: command character(len=EXT_FILE_CMD_LEN) :: quoted call cleanup_files() call build_spd(a, ierr) call require_success(ierr, 30) rhs = (/ ZERO, ZERO, 4.0_rk /) x = ZERO call ext_file_configure_named('ext_file_cmd', 'cmd_backend', & 'solve --matrix {matrix} --rhs {rhs:q} --solution {solution} ' // & '--manifest {manifest:q} --nrow {nrow} --tol {tol} ' // & '--storage {storage_name} {{literal}}', 0, 1, EXT_IO_INDEX_ZERO, & EXT_IO_STORAGE_GENERAL, ierr) call require_success(ierr, 31) call ext_file_prepare(a, rhs, x, 20, TOL, state, ierr) call require_success(ierr, 32) call ext_file_build_command(state, command, ierr) call require_success(ierr, 33) if (index(command, '--matrix ext_file_cmd_matrix.mtx') .le. 0) & call fail(34) if (index(command, '--rhs "ext_file_cmd_rhs.vec"') .le. 0) call fail(35) if (index(command, '--solution ext_file_cmd_solution.vec') .le. 0) & call fail(36) if (index(command, '--manifest "ext_file_cmd_manifest.txt"') .le. 0) & call fail(37) if (index(command, '--nrow 3') .le. 0) call fail(38) if (index(command, '--storage general') .le. 0) call fail(39) if (index(command, '{literal}') .le. 0) call fail(30) call ext_file_cmd_has_token(state%config%command, 'matrix', found, ierr) call require_success(ierr, 331) if (found .ne. 1) call fail(332) call ext_file_cmd_has_token(state%config%command, 'csr', found, ierr) call require_success(ierr, 333) if (found .ne. 0) call fail(334) call ext_file_cmd_quote('path with spaces.vec', quoted, ierr) call require_success(ierr, 335) if (trim(quoted) .ne. '"path with spaces.vec"') call fail(336) state%config%command = 'bad {unknown_token}' call ext_file_build_command(state, command, ierr) if (ierr .eq. FEM_SUCCESS) call fail(337) state%config%command = 'bad {matrix' call ext_file_build_command(state, command, ierr) if (ierr .eq. FEM_SUCCESS) call fail(338) call csr_free(a, ierr) call require_success(ierr, 339) call cleanup_files() end subroutine check_command_template subroutine check_prepare_finish_manual() type(csr_matrix) :: a type(ext_file_state) :: state real(rk) :: rhs(3) real(rk) :: x(3) real(rk) :: sol(3) integer :: ierr integer :: iter integer :: status real(rk) :: residual integer :: prepared integer :: ran integer :: command_exit integer :: finish_status integer :: handoff_status integer :: audit_status integer :: nrow integer :: ncol integer :: nnz real(rk) :: relative real(rk) :: backward character(len=128) :: matrix_file character(len=128) :: rhs_file character(len=128) :: initial_file character(len=128) :: solution_file character(len=128) :: csr_file character(len=384) :: line call cleanup_files() call build_spd(a, ierr) call require_success(ierr, 40) rhs = (/ ZERO, ZERO, 4.0_rk /) x = ZERO sol = (/ ONE, 2.0_rk, 3.0_rk /) call ext_file_configure_named('ext_file_manual', 'manual_file_backend', & '', 0, 1, EXT_IO_INDEX_ZERO, EXT_IO_STORAGE_GENERAL, ierr) call require_success(ierr, 41) call ext_file_prepare(a, rhs, x, 20, TOL, state, ierr) call require_success(ierr, 42) if (state%prepared .ne. 1) call fail(43) if (trim(state%package%manifest%backend) .ne. 'manual_file_backend') & call fail(76) call ext_file_state_files(state, matrix_file, rhs_file, initial_file, & solution_file, csr_file, ierr) call require_success(ierr, 44) call require_file_lines(matrix_file, 10, 45) call require_file_lines(rhs_file, 6, 46) call require_file_lines(initial_file, 6, 47) call require_file_lines(csr_file, 23, 48) call require_file_lines('ext_file_manual_manifest.txt', 14, 49) call ext_io_write_vector(solution_file, sol, 'solution', ierr) call require_success(ierr, 50) call ext_file_finish(state, a, rhs, x, iter, residual, status, ierr) call require_success(ierr, 51) if (status .ne. LS_STATUS_CONVERGED) call fail(52) if (iter .ne. 1) call fail(53) if (residual .gt. TOL) call fail(54) call check_close(x(1), ONE, 55) call check_close(x(2), 2.0_rk, 56) call check_close(x(3), 3.0_rk, 57) call ext_file_state_summary(state, prepared, ran, command_exit, & finish_status, residual, relative, & backward, ierr) call require_success(ierr, 58) if (prepared .ne. 1 .or. ran .ne. 0) call fail(59) if (finish_status .ne. EXT_FILE_READY) call fail(60) if (relative .gt. TOL .or. backward .gt. TOL) call fail(61) call ext_file_report_req_export(state, ierr) call require_success(ierr, 63) call ext_file_report_require_ready(state, ierr) call require_success(ierr, 64) call ext_file_report_header(line, ierr) call require_success(ierr, 65) if (index(line, 'prepared,command_ran') .ne. 1) call fail(66) call ext_file_report_csv(state, line, ierr) call require_success(ierr, 67) if (index(line, '1,0,0,1') .ne. 1) call fail(68) call ext_file_report_values(state, prepared, ran, command_exit, & finish_status, handoff_status, audit_status, nrow, ncol, nnz, & residual, relative, backward, ierr) call require_success(ierr, 77) if (handoff_status .ne. EXT_HANDOFF_AUDITED) call fail(78) if (audit_status .ne. EXT_AUDIT_PASS) call fail(79) if (nrow .ne. 3 .or. ncol .ne. 3 .or. nnz .ne. 7) call fail(80) call ext_file_report_line(state, line, ierr) call require_success(ierr, 69) if (index(line, 'file_status=ready') .le. 0) call fail(70) call ext_file_report_files_line(state, line, ierr) call require_success(ierr, 71) if (index(line, 'ext_file_manual_matrix.mtx') .le. 0) call fail(72) call ext_file_report_manifest_line(state, line, ierr) call require_success(ierr, 81) if (index(line, 'manual_file_backend') .le. 0) call fail(82) call ext_file_report_command_line(state, line, ierr) call require_success(ierr, 83) if (index(line, 'command_ran=0') .ne. 1) call fail(84) call ext_file_report_write('ext_file_manual_report.txt', state, ierr) call require_success(ierr, 73) call require_file_lines('ext_file_manual_report.txt', 6, 74) call csr_free(a, ierr) call require_success(ierr, 75) call cleanup_files() end subroutine check_prepare_finish_manual subroutine check_finish_cleanup_policy() type(csr_matrix) :: a type(ext_file_state) :: state real(rk) :: rhs(3) real(rk) :: x(3) real(rk) :: sol(3) integer :: ierr integer :: iter integer :: status real(rk) :: residual call cleanup_files() call build_spd(a, ierr) call require_success(ierr, 80) rhs = (/ ZERO, ZERO, 4.0_rk /) x = ZERO sol = (/ ONE, 2.0_rk, 3.0_rk /) call ext_file_configure('ext_file_clean', '', 0, 0, & EXT_IO_INDEX_ZERO, EXT_IO_STORAGE_GENERAL, ierr) call require_success(ierr, 81) call ext_file_prepare(a, rhs, x, 20, TOL, state, ierr) call require_success(ierr, 82) call ext_io_write_vector(trim(state%package%manifest%solution_file), sol, & 'solution', ierr) call require_success(ierr, 83) call ext_file_finish(state, a, rhs, x, iter, residual, status, ierr) call require_success(ierr, 84) if (status .ne. LS_STATUS_CONVERGED) call fail(85) call require_missing('ext_file_clean_matrix.mtx', 86) call require_missing('ext_file_clean_rhs.vec', 87) call require_missing('ext_file_clean_solution.vec', 88) call require_missing('ext_file_clean_manifest.txt', 89) call csr_free(a, ierr) call require_success(ierr, 90) call cleanup_files() end subroutine check_finish_cleanup_policy subroutine check_solve_waiting() type(csr_matrix) :: a type(ext_file_state) :: state real(rk) :: rhs(3) real(rk) :: x(3) integer :: ierr integer :: iter integer :: status real(rk) :: residual call cleanup_files() call build_spd(a, ierr) call require_success(ierr, 110) rhs = (/ ZERO, ZERO, 4.0_rk /) x = ZERO call ext_file_configure('ext_file_wait', '', 0, 1, EXT_IO_INDEX_ZERO, & EXT_IO_STORAGE_GENERAL, ierr) call require_success(ierr, 111) call ext_file_solve(a, rhs, x, 20, TOL, iter, residual, status, state, & ierr) if (ierr .eq. FEM_SUCCESS) call fail(112) if (status .ne. LS_STATUS_UNSUPPORTED) call fail(113) if (state%prepared .ne. 1) call fail(114) if (state%finish_status .ne. EXT_FILE_WAITING) call fail(115) call require_file_lines('ext_file_wait_matrix.mtx', 10, 116) call require_file_lines('ext_file_wait_manifest.txt', 14, 117) call csr_free(a, ierr) call require_success(ierr, 118) call cleanup_files() end subroutine check_solve_waiting subroutine check_auto_run_without_command() type(csr_matrix) :: a type(ext_file_state) :: state real(rk) :: rhs(3) real(rk) :: x(3) integer :: ierr integer :: iter integer :: status real(rk) :: residual call cleanup_files() call build_spd(a, ierr) call require_success(ierr, 130) rhs = (/ ZERO, ZERO, 4.0_rk /) x = ZERO call ext_file_configure('ext_file_autorun', '', 1, 1, & EXT_IO_INDEX_ZERO, EXT_IO_STORAGE_GENERAL, ierr) call require_success(ierr, 131) call ext_file_solve(a, rhs, x, 20, TOL, iter, residual, status, state, & ierr) if (ierr .eq. FEM_SUCCESS) call fail(132) if (status .ne. LS_STATUS_FAILED) call fail(133) if (state%command_ran .ne. 0) call fail(134) if (state%finish_status .ne. EXT_FILE_WAITING) call fail(135) call csr_free(a, ierr) call require_success(ierr, 136) call cleanup_files() end subroutine check_auto_run_without_command subroutine check_dispatch_file_backend() type(csr_matrix) :: a real(rk) :: rhs(3) real(rk) :: x(3) integer :: ierr integer :: iter integer :: status integer :: prepared integer :: ran integer :: command_exit integer :: finish_status real(rk) :: residual real(rk) :: relative real(rk) :: backward integer :: method integer :: preconditioner character(len=128) :: matrix_file character(len=128) :: rhs_file character(len=128) :: initial_file character(len=128) :: solution_file character(len=128) :: csr_file character(len=128) :: manifest_file call cleanup_files() call build_spd(a, ierr) call require_success(ierr, 150) rhs = (/ ZERO, ZERO, 4.0_rk /) x = ZERO call linear_external_configure_file('ext_file_dispatch', '', 0, 1, & EXT_IO_INDEX_ZERO, EXT_IO_STORAGE_GENERAL, ierr) call require_success(ierr, 151) call linear_solver_config_external(LS_EXT_BACKEND_FILE, ierr) call require_success(ierr, 152) call linear_solver_configure(LSOLVER_EXTERNAL, LPRECOND_JACOBI, ierr) call require_success(ierr, 153) call linear_solver_solve(a, rhs, x, 20, TOL, iter, residual, ierr) if (ierr .eq. FEM_SUCCESS) call fail(154) call linear_solver_get_stats(method, preconditioner, iter, residual, & status, ierr) call require_success(ierr, 155) if (method .ne. LSOLVER_EXTERNAL) call fail(156) if (status .ne. LS_STATUS_UNSUPPORTED) call fail(157) call linear_external_get_file_diag(prepared, ran, command_exit, & finish_status, residual, relative, backward, ierr) call require_success(ierr, 158) if (prepared .ne. 1 .or. ran .ne. 0) call fail(159) if (finish_status .ne. EXT_FILE_WAITING) call fail(160) call linear_external_get_file_paths(matrix_file, rhs_file, initial_file, & solution_file, csr_file, manifest_file, ierr) call require_success(ierr, 166) if (trim(matrix_file) .ne. 'ext_file_dispatch_matrix.mtx') call fail(167) if (trim(rhs_file) .ne. 'ext_file_dispatch_rhs.vec') call fail(168) if (trim(initial_file) .ne. 'ext_file_dispatch_initial.vec') & call fail(169) if (trim(solution_file) .ne. 'ext_file_dispatch_solution.vec') & call fail(170) if (trim(csr_file) .ne. 'ext_file_dispatch_csr.txt') call fail(171) if (trim(manifest_file) .ne. 'ext_file_dispatch_manifest.txt') & call fail(172) call require_file_lines('ext_file_dispatch_matrix.mtx', 10, 161) call require_file_lines('ext_file_dispatch_manifest.txt', 14, 162) call linear_solver_config_external(LS_EXT_BACKEND_REF_DIRECT, ierr) call require_success(ierr, 163) call linear_solver_configure(LSOLVER_CG, LPRECOND_JACOBI, ierr) call require_success(ierr, 164) call csr_free(a, ierr) call require_success(ierr, 165) call cleanup_files() end subroutine check_dispatch_file_backend subroutine check_error_paths() type(csr_matrix) :: a type(ext_file_state) :: state type(ext_file_config) :: config real(rk) :: rhs(3) real(rk) :: x(3) real(rk) :: short_x(2) integer :: ierr integer :: iter integer :: status real(rk) :: residual character(len=8) :: small call build_spd(a, ierr) call require_success(ierr, 180) rhs = (/ ONE, ZERO, ONE /) x = ZERO short_x = ZERO call ext_file_configure('ext_file_bad', '', 0, 1, 99, & EXT_IO_STORAGE_GENERAL, ierr) if (ierr .eq. FEM_SUCCESS) call fail(181) call ext_file_config_reset(config) config%prefix = '' call ext_file_validate_config(config, ierr) if (ierr .eq. FEM_SUCCESS) call fail(182) call ext_file_status_name(99, small, ierr) if (ierr .eq. FEM_SUCCESS) call fail(183) call ext_file_configure('ext_file_missing', '', 0, 1, & EXT_IO_INDEX_ZERO, EXT_IO_STORAGE_GENERAL, ierr) call require_success(ierr, 184) call ext_file_finish(state, a, rhs, x, iter, residual, status, ierr) if (ierr .eq. FEM_SUCCESS) call fail(185) call ext_file_prepare(a, rhs, short_x, 20, TOL, state, ierr) if (ierr .eq. FEM_SUCCESS) call fail(186) call ext_file_prepare(a, rhs, x, 20, TOL, state, ierr) call require_success(ierr, 187) call ext_file_report_require_ready(state, ierr) if (ierr .eq. FEM_SUCCESS) call fail(191) call ext_file_finish(state, a, rhs, x, iter, residual, status, ierr) if (ierr .eq. FEM_SUCCESS) call fail(188) if (status .ne. LS_STATUS_UNSUPPORTED) call fail(189) call ext_file_report_write('', state, ierr) if (ierr .eq. FEM_SUCCESS) call fail(192) call csr_free(a, ierr) call require_success(ierr, 193) call cleanup_files() end subroutine check_error_paths subroutine build_spd(a, ierr) type(csr_matrix), intent(out) :: a integer, intent(out) :: ierr integer :: row_ptr(4) integer :: col_ind(7) call csr_init(a) row_ptr = (/ 1, 3, 6, 8 /) col_ind = (/ 1, 2, 1, 2, 3, 2, 3 /) call csr_create_pattern(a, 3, 3, row_ptr, col_ind, ierr) if (ierr .ne. FEM_SUCCESS) return call csr_set_value(a, 1, 1, 2.0_rk, ierr) if (ierr .ne. FEM_SUCCESS) return call csr_set_value(a, 1, 2, -ONE, ierr) if (ierr .ne. FEM_SUCCESS) return call csr_set_value(a, 2, 1, -ONE, ierr) if (ierr .ne. FEM_SUCCESS) return call csr_set_value(a, 2, 2, 2.0_rk, ierr) if (ierr .ne. FEM_SUCCESS) return call csr_set_value(a, 2, 3, -ONE, ierr) if (ierr .ne. FEM_SUCCESS) return call csr_set_value(a, 3, 2, -ONE, ierr) if (ierr .ne. FEM_SUCCESS) return call csr_set_value(a, 3, 3, 2.0_rk, ierr) end subroutine build_spd subroutine require_file_lines(filename, expected, code) character(len=*), intent(in) :: filename integer, intent(in) :: expected integer, intent(in) :: code character(len=256) :: line integer :: unit integer :: ios integer :: count unit = 90 open(unit=unit, file=trim(filename), status='old', action='read', & iostat=ios) if (ios .ne. 0) call fail(code) count = 0 do read(unit, '(A)', iostat=ios) line if (ios .lt. 0) exit if (ios .gt. 0) call fail(code + 1000) count = count + 1 end do close(unit) if (count .ne. expected) call fail(code + 2000) end subroutine require_file_lines subroutine require_missing(filename, code) character(len=*), intent(in) :: filename integer, intent(in) :: code integer :: unit integer :: ios unit = 91 open(unit=unit, file=trim(filename), status='old', action='read', & iostat=ios) if (ios .eq. 0) then close(unit) call fail(code) end if end subroutine require_missing subroutine check_close(actual, expected, code) real(rk), intent(in) :: actual real(rk), intent(in) :: expected integer, intent(in) :: code if (abs(actual - expected) .gt. 1.0e-8_rk) call fail(code) end subroutine check_close subroutine require_success(ierr, code) integer, intent(in) :: ierr integer, intent(in) :: code if (ierr .ne. FEM_SUCCESS) call fail(code) end subroutine require_success subroutine cleanup_files() call delete_file('ext_file_cfg_matrix.mtx') call delete_file('ext_file_cfg_rhs.vec') call delete_file('ext_file_cfg_initial.vec') call delete_file('ext_file_cfg_solution.vec') call delete_file('ext_file_cfg_csr.txt') call delete_file('ext_file_cfg_manifest.txt') call delete_file('ext_file_cmd_matrix.mtx') call delete_file('ext_file_cmd_rhs.vec') call delete_file('ext_file_cmd_initial.vec') call delete_file('ext_file_cmd_solution.vec') call delete_file('ext_file_cmd_csr.txt') call delete_file('ext_file_cmd_manifest.txt') call delete_file('ext_file_manual_matrix.mtx') call delete_file('ext_file_manual_rhs.vec') call delete_file('ext_file_manual_initial.vec') call delete_file('ext_file_manual_solution.vec') call delete_file('ext_file_manual_csr.txt') call delete_file('ext_file_manual_manifest.txt') call delete_file('ext_file_manual_report.txt') call delete_file('ext_file_clean_matrix.mtx') call delete_file('ext_file_clean_rhs.vec') call delete_file('ext_file_clean_initial.vec') call delete_file('ext_file_clean_solution.vec') call delete_file('ext_file_clean_csr.txt') call delete_file('ext_file_clean_manifest.txt') call delete_file('ext_file_wait_matrix.mtx') call delete_file('ext_file_wait_rhs.vec') call delete_file('ext_file_wait_initial.vec') call delete_file('ext_file_wait_solution.vec') call delete_file('ext_file_wait_csr.txt') call delete_file('ext_file_wait_manifest.txt') call delete_file('ext_file_autorun_matrix.mtx') call delete_file('ext_file_autorun_rhs.vec') call delete_file('ext_file_autorun_initial.vec') call delete_file('ext_file_autorun_solution.vec') call delete_file('ext_file_autorun_csr.txt') call delete_file('ext_file_autorun_manifest.txt') call delete_file('ext_file_dispatch_matrix.mtx') call delete_file('ext_file_dispatch_rhs.vec') call delete_file('ext_file_dispatch_initial.vec') call delete_file('ext_file_dispatch_solution.vec') call delete_file('ext_file_dispatch_csr.txt') call delete_file('ext_file_dispatch_manifest.txt') call delete_file('ext_file_missing_matrix.mtx') call delete_file('ext_file_missing_rhs.vec') call delete_file('ext_file_missing_initial.vec') call delete_file('ext_file_missing_solution.vec') call delete_file('ext_file_missing_csr.txt') call delete_file('ext_file_missing_manifest.txt') end subroutine cleanup_files subroutine delete_file(filename) character(len=*), intent(in) :: filename integer :: unit integer :: ios unit = 92 open(unit=unit, file=trim(filename), status='old', iostat=ios) if (ios .eq. 0) close(unit, status='delete') end subroutine delete_file subroutine fail(code) integer, intent(in) :: code call cleanup_files() write(*,*) 'test_external_file_backend failed:', code stop 1 end subroutine fail end program test_external_file_backend