| 1 | """ |
|---|
| 2 | The launch module handles launching of simulations as sub-processes, and |
|---|
| 3 | obtaining information about the platform(s) on which the simulations are run. |
|---|
| 4 | |
|---|
| 5 | Classes |
|---|
| 6 | ------- |
|---|
| 7 | |
|---|
| 8 | PlatformInformation - a container for platform information |
|---|
| 9 | SerialLaunchMode - handles launching local, serial simulations |
|---|
| 10 | DistributedLaunchMode - handles launching distributed simulations using MPI |
|---|
| 11 | """ |
|---|
| 12 | |
|---|
| 13 | import platform |
|---|
| 14 | import socket |
|---|
| 15 | import subprocess |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | class PlatformInformation(object): |
|---|
| 19 | |
|---|
| 20 | def __init__(self, **kwargs): |
|---|
| 21 | for k,v in kwargs.items(): |
|---|
| 22 | setattr(self, k, v) |
|---|
| 23 | #platform.mac_ver() |
|---|
| 24 | #platform.win32_ver() |
|---|
| 25 | #platform.dist() |
|---|
| 26 | #platform.libc_ver() |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | class LaunchMode(object): |
|---|
| 30 | """ |
|---|
| 31 | Launch serially or in parallel with MPI. |
|---|
| 32 | If MPI store configuration (which nodes, etc) |
|---|
| 33 | """ |
|---|
| 34 | |
|---|
| 35 | def get_state(self): |
|---|
| 36 | """ |
|---|
| 37 | Since each subclass has different attributes, we provide this method |
|---|
| 38 | as a standard way of obtaining these attributes, for database storage, |
|---|
| 39 | etc. Returns a dict. |
|---|
| 40 | """ |
|---|
| 41 | return {} |
|---|
| 42 | |
|---|
| 43 | def pre_run(self, executable): |
|---|
| 44 | #Run tasks before the simulation proper, e.g. nrnivmodl |
|---|
| 45 | #should get the tasks to run from the Executable |
|---|
| 46 | pass |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | class SerialLaunchMode(LaunchMode): |
|---|
| 50 | |
|---|
| 51 | def __init__(self): |
|---|
| 52 | LaunchMode.__init__(self) |
|---|
| 53 | |
|---|
| 54 | def __str__(self): |
|---|
| 55 | return "serial" |
|---|
| 56 | |
|---|
| 57 | def run(self, executable, main_file, parameter_file): |
|---|
| 58 | cmd = "%s %s %s" % (executable.default_executable_name, main_file, parameter_file) |
|---|
| 59 | print "Sumatra is running the following command:", cmd |
|---|
| 60 | #p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) |
|---|
| 61 | p = subprocess.Popen(cmd, shell=True, stdout=None, stderr=None, close_fds=True) |
|---|
| 62 | result = p.wait() |
|---|
| 63 | #self.errors = p.stderr.read() |
|---|
| 64 | #self.output = p.stdout.read() |
|---|
| 65 | #sys.stdout.write(self.output) |
|---|
| 66 | #sys.stderr.write(self.errors) |
|---|
| 67 | return result |
|---|
| 68 | |
|---|
| 69 | def get_platform_information(self): |
|---|
| 70 | network_name = platform.node() |
|---|
| 71 | bits, linkage = platform.architecture() |
|---|
| 72 | return [PlatformInformation(architecture_bits=bits, |
|---|
| 73 | architecture_linkage=linkage, |
|---|
| 74 | machine=platform.machine(), |
|---|
| 75 | network_name=network_name, |
|---|
| 76 | ip_addr=socket.gethostbyname(network_name), |
|---|
| 77 | processor=platform.processor(), |
|---|
| 78 | release=platform.release(), |
|---|
| 79 | system_name=platform.system(), |
|---|
| 80 | version=platform.version())] |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | class DistributedLaunchMode(LaunchMode): |
|---|
| 84 | |
|---|
| 85 | def __init__(self, mpirun, hosts, n): |
|---|
| 86 | LaunchMode.__init__(self) |
|---|
| 87 | self.mpirun = os.path.abspath(mpirun) |
|---|
| 88 | self.hosts = hosts |
|---|
| 89 | self.n = n |
|---|
| 90 | self.mpi_info = {} |
|---|
| 91 | |
|---|
| 92 | def run(self, executable, main_file, parameter_file): |
|---|
| 93 | cmd = "%s -np %d -host %s %s %s %s" % (self.mpirun, |
|---|
| 94 | self.n, |
|---|
| 95 | ",".join(hosts), |
|---|
| 96 | executable.default_executable_name, |
|---|
| 97 | main_file, |
|---|
| 98 | parameter_file) |
|---|
| 99 | print "Sumatra is running the following command:", cmd |
|---|
| 100 | p = subprocess.Popen(cmd, shell=True, stdout=None, stderr=None, close_fds=True) |
|---|
| 101 | result = p.wait() |
|---|
| 102 | return result |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|