44 USE omp_lib,
ONLY: omp_get_max_threads,&
61 USE iso_fortran_env,
ONLY: compiler_options,&
69#include "../base/base_uses.f90"
73 CHARACTER(LEN=default_path_length) :: input_file_name, output_file_name, &
75 CHARACTER(LEN=default_path_length), &
76 DIMENSION(:, :),
ALLOCATABLE :: initial_variables, initial_variables_tmp
77 CHARACTER(LEN=:),
ALLOCATABLE :: compiler_options_string
78 INTEGER :: cpuid, cpuid_static, output_unit, l, i, var_set_sep, inp_var_idx
79 INTEGER :: ierr, i_arg
80 LOGICAL :: check, usage, echo_input, command_line_error
81 LOGICAL :: run_it, force_run, has_input, xml, print_version, print_license, shell_mode
84 NULLIFY (input_declaration)
97 print_version = .false.
98 print_license = .false.
99 command_line_error = .false.
101 input_file_name =
"Missing input file name"
102 output_file_name =
"__STD_OUT__"
103 ALLOCATE (initial_variables(2, 1:0))
106 CALL get_command_argument(number=0,
VALUE=command, status=ierr)
108 l = len_trim(command)
110 IF (command(i:i) ==
"/" .OR. command(i:i) ==
"\")
EXIT
112 command = command(i + 1:l)
116 DO WHILE (i_arg < command_argument_count())
118 CALL get_command_argument(number=i_arg,
VALUE=arg_att, status=ierr)
120 SELECT CASE (arg_att)
122 IF (output_file_name ==
"__STD_OUT__")
THEN
125 CALL get_command_argument(number=i_arg,
VALUE=arg_att, status=ierr)
127 IF (arg_att(1:1) ==
"-")
THEN
128 WRITE (output_unit,
"(/,T2,A)") &
129 "ERROR: The output file name "//trim(arg_att)//
" starts with -"
130 command_line_error = .true.
132 output_file_name = arg_att
133 CALL open_file(file_name=output_file_name, &
134 file_status=
"UNKNOWN", &
135 file_action=
"WRITE", &
136 file_position=
"APPEND", &
137 skip_get_unit_number=.true., &
138 unit_number=output_unit)
142 WRITE (output_unit,
"(/,T2,A)") &
143 "ERROR: The command line flag -o has been specified multiple times"
144 command_line_error = .true.
150 IF (command(1:10) ==
"cp2k_shell")
THEN
153 ELSE IF (command_argument_count() < 1)
THEN
154 WRITE (output_unit,
"(/,T2,A)") &
155 "ERROR: At least one command line argument must be specified"
156 command_line_error = .true.
160 l = len_trim(command)
162 IF (command(l - 4:l) ==
".sopt" .OR. command(l - 4:l) ==
".popt")
THEN
163 CALL omp_set_num_threads(1)
168 IF (omp_get_max_threads() > 1)
THEN
170 CHARACTER(len=default_path_length) :: env_var
171 INTEGER :: veclib_max_threads, ierr
172 CALL get_environment_variable(
"VECLIB_MAXIMUM_THREADS", env_var, status=ierr)
173 veclib_max_threads = 0
175 READ (env_var, *) veclib_max_threads
177 IF (ierr == 1 .OR. (ierr == 0 .AND. veclib_max_threads > 1))
THEN
178 CALL cp_warn(__location__, &
179 "macOS' Accelerate framework has its own threading enabled which may interfere"// &
180 " with the OpenMP threading. You can disable the Accelerate threading by setting"// &
181 " the environment variable VECLIB_MAXIMUM_THREADS=1")
188 arg_loop:
DO WHILE (i_arg < command_argument_count())
190 CALL get_command_argument(i_arg, arg_att, status=ierr)
192 SELECT CASE (arg_att)
193 CASE (
"--check",
"-c")
197 CASE (
"--echo",
"-e")
201 CASE (
"-v",
"--version")
202 print_version = .true.
205 print_license = .true.
209 CASE (
"--shell",
"-s")
212 CASE (
"-help",
"--help",
"-h")
217 CALL get_command_argument(i_arg, arg_att, status=ierr)
220 IF (.NOT. arg_att(1:1) ==
"-")
THEN
221 input_file_name = arg_att
224 WRITE (output_unit,
"(/,T2,A)") &
225 "ERROR: The input file name "//trim(arg_att)//
" starts with -"
226 command_line_error = .true.
231 CALL get_command_argument(i_arg, arg_att, status=ierr)
234 var_set_sep = index(arg_att,
'=')
236 IF (var_set_sep < 2)
THEN
237 WRITE (output_unit,
"(/,T2,A)")
"ERROR: Invalid initializer for preprocessor variable: "//trim(arg_att)
238 command_line_error = .true.
242 DO inp_var_idx = 1,
SIZE(initial_variables, 2)
244 IF (trim(initial_variables(1, inp_var_idx)) == arg_att(:var_set_sep - 1))
THEN
249 IF (inp_var_idx >
SIZE(initial_variables, 2))
THEN
251 ALLOCATE (initial_variables_tmp(2,
SIZE(initial_variables, 2) + 1))
252 initial_variables_tmp(:, 1:
SIZE(initial_variables, 2)) = initial_variables
253 CALL move_alloc(initial_variables_tmp, initial_variables)
256 initial_variables(1, inp_var_idx) = arg_att(:var_set_sep - 1)
257 initial_variables(2, inp_var_idx) = arg_att(var_set_sep + 1:)
261 CALL get_command_argument(i_arg, arg_att, status=ierr)
263 IF (arg_att(1:1) ==
"-")
EXIT arg_loop
270 IF ((.NOT. has_input) .AND. &
271 (i_arg == command_argument_count()) .AND. &
272 (.NOT. arg_att(1:1) ==
"-"))
THEN
273 input_file_name = arg_att
275 ELSE IF (has_input .AND. &
276 (.NOT. arg_att(1:1) ==
"-"))
THEN
277 WRITE (output_unit,
"(/,T2,A)") &
278 "Error: Tried to specify two input files"
279 command_line_error = .true.
285 IF ((run_it .OR. force_run .OR. check .OR. echo_input) .AND. &
286 (.NOT. has_input) .AND. (.NOT. command_line_error))
THEN
287 WRITE (unit=output_unit, fmt=
"(/,T2,A)") &
288 "ERROR: An input file name is required"
289 command_line_error = .true.
292 CALL init_cp2k(init_mpi=.true., ierr=ierr)
297 IF (usage .OR. command_line_error)
THEN
298 IF (default_para_env%is_source())
THEN
299 l = len_trim(command)
300 WRITE (unit=output_unit, fmt=
"(/,(T2,A))") &
301 trim(command)//
" [-c|--check] [-e|--echo] [-h|--help]", &
302 repeat(
" ", l)//
" [-i] <input_file>", &
303 repeat(
" ", l)//
" [-mpi-mapping|--mpi-mapping] <method>", &
304 repeat(
" ", l)//
" [-o] <output_file>", &
305 repeat(
" ", l)//
" [-r|-run] [-s|--shell] [--xml]"
306 WRITE (unit=output_unit, fmt=
"(/,T2,A,/,/,T2,A,/,/,T2,A,/,/,(T3,A))") &
307 "starts the CP2K program, see <https://www.cp2k.org/>", &
308 "The easiest way is "//trim(command)//
" <input_file>", &
309 "The following options can be used:", &
310 "-i <input_file> : provides an input file name, if it is the last", &
311 " argument, the -i flag is not needed", &
312 "-o <output_file> : provides an output file name [default: screen]"
313 WRITE (unit=output_unit, fmt=
"(/,T2,A,/,/,(T3,A))") &
314 "These switches skip the simulation, unless [-r|-run] is specified:", &
315 "--check, -c : performs a syntax check of the <input_file>", &
316 "--echo, -e : echoes the <input_file>, and make all defaults explicit", &
317 " The input is also checked, but only a failure is reported", &
318 "--help, -h : writes this message", &
319 "--license : prints the CP2K license", &
320 "--mpi-mapping : applies a given MPI reordering to CP2K", &
321 "--run, -r : forces a CP2K run regardless of other specified flags", &
322 "--shell, -s : start interactive shell mode", &
323 "--version, -v : prints the CP2K version and the revision number", &
324 "--xml : dumps the whole CP2K input structure as a XML file", &
325 " xml2htm generates a HTML manual from this XML file", &
326 "--set, -E name=value : set the initial value of a preprocessor value", &
331 IF (.NOT. command_line_error)
THEN
334 IF (print_version)
THEN
335 IF (default_para_env%is_source())
THEN
336 WRITE (output_unit,
"(T2,A)") cp2k_version, &
337 "Source code revision "//trim(compile_revision), &
339 compiler_options_string = compiler_options()
340 WRITE (output_unit,
"(T2,A,A)")
"compiler: ", compiler_version()
343 IF ((cpuid > 0) .OR. (cpuid_static > 0))
THEN
344 WRITE (output_unit,
"(T2,A,1X,I6,1X,A)") &
345 "compiler target: cpuid", cpuid_static,
"("//trim(m_cpuid_name(cpuid_static))//
")"
346 IF (cpuid /= cpuid_static)
THEN
347 WRITE (output_unit,
"(T2,A,1X,I6,1X,A)") &
348 "current: cpuid", cpuid,
"("//trim(m_cpuid_name(cpuid))//
")"
351 IF (m_cpuid_vlen(cpuid_static) < m_cpuid_vlen(cpuid))
THEN
352 WRITE (output_unit,
"(T2,A)") &
353 "WARNING: The compiler target used to build this binary does not "// &
354 "enable all instruction-set extensions available on this CPU. "// &
355 "Consider reconfiguring and rebuilding CP2K for this target system "// &
358 WRITE (output_unit,
"(T2,A)")
"compiler options:"
359 DO i = 0, (len(compiler_options_string) - 1)/68
360 WRITE (output_unit,
"(T4,A)") &
361 compiler_options_string(i*68 + 1:min(len(compiler_options_string), (i + 1)*68))
363 DEALLOCATE (compiler_options_string)
368 IF (print_license)
THEN
369 IF (default_para_env%is_source())
THEN
370 CALL print_cp2k_license(output_unit)
375 IF (default_para_env%is_source())
THEN
376 CALL write_xml_file()
380 CALL create_cp2k_root_section(input_declaration)
383 CALL check_input(input_declaration, input_file_name, output_file_name, &
384 echo_input=echo_input, ierr=ierr, initial_variables=initial_variables)
385 IF (default_para_env%is_source())
THEN
387 IF (.NOT. echo_input)
THEN
388 WRITE (output_unit,
"(A)")
"SUCCESS, the input could be parsed correctly."
389 WRITE (output_unit,
"(A)")
" This does not guarantee that this input is meaningful"
390 WRITE (output_unit,
"(A)")
" or will run successfully"
393 WRITE (output_unit,
"(A)")
"ERROR, the input could *NOT* be parsed correctly."
394 WRITE (output_unit,
"(A)")
" Please, check and correct it"
400 CALL launch_cp2k_shell(input_declaration)
403 IF (run_it .OR. force_run)
THEN
404 CALL run_input(input_declaration, input_file_name, output_file_name, initial_variables)
407 CALL section_release(input_declaration)
410 WRITE (unit=output_unit, fmt=
"(/,A)")
"initial setup (MPI ?) error"
414 CALL finalize_cp2k(finalize_mpi=.true., ierr=ierr)
415 DEALLOCATE (initial_variables)
program cp2k
Main program of CP2K.
Target architecture or instruction set extension according to compiler target flags.
int m_cpuid_static(void)
This routine determines the CPUID according to the given compiler flags (expected to be similar to Fo...
some minimal info about CP2K, including its version and license
subroutine, public print_cp2k_license(iunit)
...
character(len= *), parameter, public compile_revision
character(len=10 *default_string_length) function, public cp2k_flags()
list all compile time options that influence the capabilities of cp2k. All new flags should be added ...
character(len= *), parameter, public cp2k_version
subroutine, public run_input(input_declaration, input_file_path, output_file_path, initial_variables, mpi_comm)
runs the given input
subroutine, public write_xml_file()
...
Interactive shell of CP2K.
subroutine, public launch_cp2k_shell(input_declaration)
Launch the interactive CP2K shell.
Utility routines to open and close files. Tracking of preconnections.
subroutine, public open_file(file_name, file_status, file_form, file_action, file_position, file_pad, unit_number, debug, skip_get_unit_number, file_access)
Opens the requested file using a free unit number.
interface to use cp2k as library
type(mp_para_env_type), pointer, save, public default_para_env
subroutine, public init_cp2k(init_mpi, ierr, mpi_comm)
initializes cp2k, needs to be called once before using any of the other functions when using cp2k as ...
subroutine, public finalize_cp2k(finalize_mpi, ierr)
cleanup after you have finished using this interface
subroutine, public check_input(input_declaration, input_file_path, output_file_path, echo_input, mpi_comm, initial_variables, ierr)
performs a check of the input
Defines the basic variable types.
integer, parameter, public default_path_length
Machine interface based on Fortran 2003 and POSIX.
integer, parameter, public default_output_unit
pure integer function, public m_cpuid()
Target architecture or instruction set extension according to CPU-check at runtime.
pure integer function, public m_cpuid_vlen(cpuid, typesize)
Determine vector-length for a given CPUID.
pure character(len=default_string_length) function, public m_cpuid_name(cpuid)
Determine name of target architecture for a given CPUID.