Skip to content

Commit 3b708f0

Browse files
committed
Cleaned a lot the cluster interface. Now it should be much easier to rewrite it in a new way
1 parent 4bf8812 commit 3b708f0

1 file changed

Lines changed: 181 additions & 89 deletions

File tree

Modules/Cluster.py

Lines changed: 181 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def __init__(self, hostname=None, pwd=None, extra_options="", workdir = "",
162162
self.workdir=r""
163163
self.submit_command="sbatch --wait"
164164
self.submit_name="SBATCH"
165-
self.terminal="#!/bin/bash"
165+
self.terminal="/bin/bash"
166166
self.v_nodes="-N "
167167
self.use_nodes = True
168168
self.v_cpu="-n "
@@ -384,7 +384,182 @@ def CheckCommunication(self):
384384
return False
385385

386386
return True
387-
387+
388+
def create_submission_script(self, labels):
389+
"""
390+
CREATE THE SUBMISSION SCRIPT
391+
===========================================
392+
393+
This is a function that is general and does not depend on the specific
394+
calculator. It is usefull to create the header of the submission script.
395+
396+
Parameters
397+
----------
398+
labels : list
399+
It is a list of the labels of the calculations to be done.
400+
401+
Returns
402+
-------
403+
submission_header : string
404+
The text of the submission header.
405+
"""
406+
407+
# prepare the submission script
408+
submission = "#!" + self.terminal + "\n"
409+
410+
411+
412+
# Add the submission options
413+
if self.use_nodes:
414+
submission += "#%s %s%d\n" % (self.submit_name, self.v_nodes, self.n_nodes)
415+
if self.use_cpu:
416+
submission += "#%s %s%d\n" % (self.submit_name, self.v_cpu, self.n_cpu)
417+
if self.use_time:
418+
submission += "#%s %s%s\n" % (self.submit_name, self.v_time, self.time)
419+
if self.use_account:
420+
submission += "#%s %s%s\n" % (self.submit_name, self.v_account, self.account_name)
421+
if self.use_memory:
422+
submission += "#%s %s%s\n" % (self.submit_name, self.v_memory, self.ram)
423+
if self.use_partition:
424+
submission += "#%s %s%s\n" % (self.submit_name, self.v_partition, self.partition_name)
425+
426+
# Append the additional parameters
427+
for add_parameter in self.custom_params:
428+
if self.custom_params[add_parameter] is None:
429+
submission += "#{} --{}\n".format(self.submit_name, add_parameter)
430+
else:
431+
submission += "#{} --{}={}\n".format(self.submit_name, add_parameter, self.custom_params[add_parameter])
432+
433+
434+
# Add the set -x option
435+
if self.add_set_minus_x:
436+
submission += "set -x\n"
437+
438+
# Add the loading of the modules
439+
submission += self.load_modules + "\n"
440+
441+
# Go to the working directory
442+
submission += "cd " + self.workdir + "\n"
443+
444+
# If any, apply the extra text before and after the calculation
445+
other_input = ""
446+
other_output = ""
447+
if (self.additional_script_parameters is not None):
448+
other_input, other_output = self.additional_script_parameters(labels)
449+
450+
submission += other_input
451+
452+
# Use the xargs trick
453+
#submission += "xargs -d " + r"'\n'" + " -L1 -P%d -a %s -- bash -c\n" % (n_togheder,
454+
for i, lbl in enumerate(labels):
455+
submission += self.get_execution_command(lbl)
456+
457+
458+
submission += other_output
459+
460+
return submission
461+
462+
def get_execution_command(self, label):
463+
"""
464+
GET THE EXECUTION COMMAND
465+
=========================
466+
467+
Return the command used in the submission script to actually execute the calculation.
468+
469+
Parameters
470+
----------
471+
label : string
472+
The label of the calculation
473+
474+
Returns
475+
-------
476+
commnad : string
477+
The command to be appended to the submission script
478+
"""
479+
480+
# Get the MPI command replacing NPROC
481+
new_mpicmd = self.mpi_cmd.replace("NPROC", str(self.n_cpu))
482+
483+
# Replace the NPOOL variable and the PREFIX in the binary
484+
binary = self.binary.replace("NPOOL", str(self.n_pool)).replace("PREFIX", label)
485+
486+
487+
tmt_str = ""
488+
if self.use_timeout:
489+
tmt_str = "timeout %d " % self.timeout
490+
return "%s%s %s\n" % (tmt_str, new_mpicmd, binary)
491+
492+
def prepare_input_file(self, structure, calc, label):
493+
"""
494+
PREPARE THE INPUT FILE
495+
======================
496+
497+
This is specific for quantum espresso and must be inherit and replaced for
498+
other calculators.
499+
500+
This crates the input file and copy it in the working directory.
501+
502+
Parameters
503+
----------
504+
structure : CellConstructor.Structure.Structure
505+
The atomic structure on which to run the calculation
506+
calc : the ASE or CellConstructor calculator.
507+
In this case, it works with quantum espresso
508+
label : string
509+
The unique name of this calculation
510+
"""
511+
512+
# Prepare the input file
513+
atm = structure.get_ase_atoms()
514+
atm.set_calculator(calc)
515+
ase.io.write("%s/%s.pwi"% (self.local_workdir, label),
516+
atm, **calc.parameters)
517+
518+
519+
520+
# First of all clean eventually input/output file of this very same calculation
521+
cmd = self.sshcmd + " %s 'rm -f %s/%s%s %s/%s%s'" % (self.hostname,
522+
self.workdir, label, ".pwi",
523+
self.workdir, label, ".pwo")
524+
self.ExecuteCMD(cmd, False)
525+
# cp_res = os.system(cmd + " > /dev/null")
526+
# if cp_res != 0:
527+
# print "Error while executing:", cmd
528+
# print "Return code:", cp_res
529+
# sys.stderr.write(cmd + ": exit with code " + str(cp_res) + "\n")
530+
#
531+
# Copy the file into the cluster
532+
cmd = self.scpcmd + " %s/%s%s %s:%s/" % (self.local_workdir, label,
533+
".pwi", self.hostname,
534+
self.workdir)
535+
cp_res = self.ExecuteCMD(cmd, False)
536+
if not cp_res:
537+
print ("Error while executing:", cmd)
538+
print ("Return code:", cp_res)
539+
sys.stderr.write(cmd + ": exit with code " + str(cp_res) + "\n")
540+
return cp_res
541+
#cp_res = os.system(cmd + " > /dev/null")
542+
543+
544+
def submit(self, script_location):
545+
"""
546+
SUBMIT THE CALCULATION
547+
======================
548+
549+
Submit the calculation
550+
"""
551+
552+
cmd = "{ssh} {host} '{submit_cmd} {script}'"
553+
if self.use_active_shell:
554+
cmd = "{ssh} {host} -t '{shell} --login -c \"{submit_cmd} {script}\"'".format(shell = self.terminal)
555+
556+
557+
cmd = cmd.format(ssh = self.sshcmd, host = self.hostname,
558+
submit_cmd = self.submit_name, script = script_location)
559+
560+
return cmd
561+
562+
388563
def batch_submission(self, list_of_structures, calc, indices,
389564
in_extension, out_extension,
390565
label = "ESP", n_togheder=1):
@@ -434,7 +609,6 @@ def batch_submission(self, list_of_structures, calc, indices,
434609
# Prepare the input atoms
435610
app_list = ""
436611
new_ncpu = self.n_cpu * n_togheder
437-
new_mpicmd = self.mpi_cmd.replace("NPROC", str(self.n_cpu))
438612
results = [None] * N_structs
439613
submitted = []
440614
submission_labels = []
@@ -443,44 +617,10 @@ def batch_submission(self, list_of_structures, calc, indices,
443617
lbl = label + "_" + str(indices[i])
444618
submission_labels.append(lbl)
445619

446-
atm = list_of_structures[i].get_ase_atoms()
447-
atm.set_calculator(calc)
448-
ase.io.write("%s/%s%s"% (self.local_workdir, lbl, in_extension),
449-
atm,**calc.parameters)
450-
451-
452-
# Add the file in the applist
453-
binary = self.binary.replace("NPOOL", str(self.n_pool)).replace("PREFIX", lbl)
454-
455-
456-
# First of all clean eventually input/output file of this very same calculation
457-
cmd = self.sshcmd + " %s 'rm -f %s/%s%s %s/%s%s'" % (self.hostname,
458-
self.workdir, lbl, in_extension,
459-
self.workdir, lbl, out_extension)
460-
self.ExecuteCMD(cmd, False)
461-
# cp_res = os.system(cmd + " > /dev/null")
462-
# if cp_res != 0:
463-
# print "Error while executing:", cmd
464-
# print "Return code:", cp_res
465-
# sys.stderr.write(cmd + ": exit with code " + str(cp_res) + "\n")
466-
#
467-
# Copy the file into the cluster
468-
cmd = self.scpcmd + " %s/%s%s %s:%s/" % (self.local_workdir, lbl,
469-
in_extension, self.hostname,
470-
self.workdir)
471-
cp_res = self.ExecuteCMD(cmd, False)
472-
473-
#cp_res = os.system(cmd + " > /dev/null")
474-
if not cp_res:
475-
print ("Error while executing:", cmd)
476-
print ("Return code:", cp_res)
477-
sys.stderr.write(cmd + ": exit with code " + str(cp_res) + "\n")
620+
# Create the input file and copy it into the cluster
621+
if not self.prepare_input_file(list_of_structures[i], calc, lbl):
478622
continue
479-
480-
tmt_str = ""
481-
if self.use_timeout:
482-
tmt_str = "timeout %d " % self.timeout
483-
app_list += "%s%s %s\n" % (tmt_str, new_mpicmd, binary)
623+
484624
submitted.append(i)
485625

486626
# Save the app list and copy it to the destination
@@ -500,55 +640,7 @@ def batch_submission(self, list_of_structures, calc, indices,
500640
# sys.stderr.write(cmd + ": exit with code " + str(cp_res) + "\n")
501641
# return results #[None] * N_structs
502642

503-
504-
# prepare the submission script
505-
submission = self.terminal + "\n"
506-
507-
# Add the submission options
508-
if self.use_nodes:
509-
submission += "#%s %s%d\n" % (self.submit_name, self.v_nodes, self.n_nodes)
510-
if self.use_cpu:
511-
submission += "#%s %s%d\n" % (self.submit_name, self.v_cpu, new_ncpu)
512-
if self.use_time:
513-
submission += "#%s %s%s\n" % (self.submit_name, self.v_time, self.time)
514-
if self.use_account:
515-
submission += "#%s %s%s\n" % (self.submit_name, self.v_account, self.account_name)
516-
if self.use_memory:
517-
submission += "#%s %s%s\n" % (self.submit_name, self.v_memory, self.ram)
518-
if self.use_partition:
519-
submission += "#%s %s%s\n" % (self.submit_name, self.v_partition, self.partition_name)
520-
521-
# Append the additional parameters
522-
for add_parameter in self.custom_params:
523-
if self.custom_params[add_parameter] is None:
524-
submission += "#{} --{}\n".format(self.submit_name, add_parameter)
525-
else:
526-
submission += "#{} --{}={}\n".format(self.submit_name, add_parameter, self.custom_params[add_parameter])
527-
528-
529-
# Add the set -x option
530-
if self.add_set_minus_x:
531-
submission += "set -x\n"
532-
533-
# Add the loading of the modules
534-
submission += self.load_modules + "\n"
535-
536-
# Go to the working directory
537-
submission += "cd " + self.workdir + "\n"
538-
539-
# If any, apply the extra text before and after the calculation
540-
other_input = ""
541-
other_output = ""
542-
if self.additional_script_parameters is not None:
543-
other_input, other_output = self.additional_script_parameters(submission_labels)
544-
545-
submission += other_input
546-
547-
# Use the xargs trick
548-
#submission += "xargs -d " + r"'\n'" + " -L1 -P%d -a %s -- bash -c\n" % (n_togheder,
549-
submission += app_list
550-
551-
submission += other_output
643+
submission = self.create_submission_script(submission_labels)
552644

553645
# Copy the submission script
554646
sub_fpath = "%s/%s.sh" % (self.local_workdir, label + "_" + str(indices[0]))

0 commit comments

Comments
 (0)