def num_threads(self): if not _hoomd.is_TBB_available(): msg.warning( "HOOMD was compiled without thread support, returning None\n") return None else: return self._get_exec_conf().getNumThreads()
def __init__(self, communicator, notice_level, msg_file, shared_msg_file): # metadata stuff hoomd.meta._metadata.__init__(self) self.metadata_fields = ['gpu_ids', 'mode', 'num_ranks'] if _hoomd.is_TBB_available(): self.metadata_fields.append('num_threads') # check shared_msg_file if shared_msg_file is not None: if not _hoomd.is_MPI_available(): raise RuntimeError( "Shared log files are only available in MPI builds.\n") # MPI communicator if communicator is None: self._comm = hoomd.comm.Communicator() else: self._comm = communicator # c++ messenger object self.cpp_msg = _create_messenger(self.comm.cpp_mpi_conf, notice_level, msg_file, shared_msg_file) # output the version info on initialization self.cpp_msg.notice(1, _hoomd.output_version_info()) # c++ execution configuration mirror class self.cpp_exec_conf = None # name of the message file self._msg_file = msg_file
def __init__(self): hoomd.meta._metadata.__init__(self) self.metadata_fields = [ 'hostname', 'gpu', 'mode', 'num_ranks', 'username', 'wallclocktime', 'cputime', 'job_id', 'job_name' ] if _hoomd.is_TBB_available(): self.metadata_fields.append('num_threads')
def __init__(self): hoomd.meta._metadata.__init__(self) self.metadata_fields = [ 'hostname', 'gpu', 'mode', 'num_ranks', 'username', 'wallclocktime', 'cputime', 'job_id', 'job_name' ] if _hoomd.is_TBB_available(): self.metadata_fields.append('num_threads')
def _create_exec_conf(): global exec_conf, options, msg # use a cached execution configuration if available if exec_conf is not None: return exec_conf mpi_available = _hoomd.is_MPI_available() # error out on nyx/flux if the auto mode is set if options.mode == 'auto': host = _get_proc_name() if "flux" in host or "nyx" in host: msg.error( "--mode=gpu or --mode=cpu must be specified on nyx/flux\n") raise RuntimeError("Error initializing") exec_mode = _hoomd.ExecutionConfiguration.executionMode.AUTO elif options.mode == "cpu": exec_mode = _hoomd.ExecutionConfiguration.executionMode.CPU elif options.mode == "gpu": exec_mode = _hoomd.ExecutionConfiguration.executionMode.GPU else: raise RuntimeError("Invalid mode") # convert None options to defaults if options.gpu is None: gpu_id = -1 else: gpu_id = int(options.gpu) if options.nrank is None: nrank = 0 else: nrank = int(options.nrank) # create the specified configuration exec_conf = _hoomd.ExecutionConfiguration(exec_mode, gpu_id, options.min_cpu, options.ignore_display, msg, nrank) # if gpu_error_checking is set, enable it on the GPU if options.gpu_error_checking: exec_conf.setCUDAErrorChecking(True) if _hoomd.is_TBB_available(): # set the number of TBB threads as necessary if options.nthreads != None: exec_conf.setNumThreads(options.nthreads) exec_conf = exec_conf return exec_conf
def set_num_threads(num_threads): R""" Set the number of CPU (TBB) threads HOOMD uses Args: num_threads (int): The number of threads Note: Overrides ``--nthreads`` on the command line. """ if not _hoomd.is_TBB_available(): msg.warning("HOOMD was compiled without thread support, ignoring request to set number of threads.\n"); else: hoomd.context.exec_conf.setNumThreads(int(num_threads));
def set_num_threads(num_threads): R""" Set the number of CPU (TBB) threads HOOMD uses Args: num_threads (int): The number of threads Note: Overrides ``--nthreads`` on the command line. """ if not _hoomd.is_TBB_available(): msg.warning( "HOOMD was compiled without thread support, ignoring request to set number of threads.\n" ) else: hoomd.context.exec_conf.setNumThreads(int(num_threads))
def _create_exec_conf(mpi_config, msg, options): global exec_conf # use a cached execution configuration if available if exec_conf is not None: return exec_conf if options.mode == 'auto': exec_mode = _hoomd.ExecutionConfiguration.executionMode.AUTO; elif options.mode == "cpu": exec_mode = _hoomd.ExecutionConfiguration.executionMode.CPU; elif options.mode == "gpu": exec_mode = _hoomd.ExecutionConfiguration.executionMode.GPU; else: raise RuntimeError("Invalid mode"); # convert None options to defaults if options.gpu is None: gpu_id = []; else: gpu_id = options.gpu; gpu_vec = _hoomd.std_vector_int() for gpuid in gpu_id: gpu_vec.append(gpuid) # create the specified configuration exec_conf = _hoomd.ExecutionConfiguration(exec_mode, gpu_vec, options.min_cpu, options.ignore_display, mpi_conf, msg); # if gpu_error_checking is set, enable it on the GPU if options.gpu_error_checking: exec_conf.setCUDAErrorChecking(True); if _hoomd.is_TBB_available(): # set the number of TBB threads as necessary if options.nthreads != None: exec_conf.setNumThreads(options.nthreads) exec_conf = exec_conf; return exec_conf;
def _create_exec_conf(mpi_comm): global exec_conf, options, msg # use a cached execution configuration if available if exec_conf is not None: return exec_conf mpi_available = _hoomd.is_MPI_available() if options.mode == 'auto': exec_mode = _hoomd.ExecutionConfiguration.executionMode.AUTO elif options.mode == "cpu": exec_mode = _hoomd.ExecutionConfiguration.executionMode.CPU elif options.mode == "gpu": exec_mode = _hoomd.ExecutionConfiguration.executionMode.GPU else: raise RuntimeError("Invalid mode") # convert None options to defaults if options.gpu is None: gpu_id = -1 else: gpu_id = int(options.gpu) if options.nrank is None: nrank = 0 else: nrank = int(options.nrank) # create the specified configuration if mpi_comm is None: exec_conf = _hoomd.ExecutionConfiguration(exec_mode, gpu_id, options.min_cpu, options.ignore_display, msg, nrank) else: if not mpi_available: msg.error( "mpi_comm provided, but MPI support was disabled at compile time\n" ) raise RuntimeError("mpi_comm is not supported in serial builds") handled = False # pass in pointer to MPI_Comm object provided by mpi4py try: import mpi4py if isinstance(mpi_comm, mpi4py.MPI.Comm): addr = mpi4py.MPI._addressof(mpi_comm) exec_conf = _hoomd.ExecutionConfiguration._make_exec_conf_mpi_comm( exec_mode, gpu_id, options.min_cpu, options.ignore_display, msg, nrank, addr) handled = True except ImportError: # silently ignore when mpi4py is missing pass # undocumented case: handle plain integers as pointers to MPI_Comm objects if not handled and isinstance(mpi_comm, int): exec_conf = _hoomd.ExecutionConfiguration._make_exec_conf_mpi_comm( exec_mode, gpu_id, options.min_cpu, options.ignore_display, msg, nrank, mpi_comm) handled = True if not handled: msg.error("unknown mpi_comm object: {}.\n".format(mpi_comm)) raise RuntimeError("Invalid mpi_comm object") # if gpu_error_checking is set, enable it on the GPU if options.gpu_error_checking: exec_conf.setCUDAErrorChecking(True) if _hoomd.is_TBB_available(): # set the number of TBB threads as necessary if options.nthreads != None: exec_conf.setNumThreads(options.nthreads) exec_conf = exec_conf return exec_conf
def _parse_command_line(arg_string=None): parser = OptionParser() parser.add_option("--mode", dest="mode", help="Execution mode (cpu or gpu)", default='auto') parser.add_option("--gpu", dest="gpu", help="GPU on which to execute") parser.add_option("--gpu_error_checking", dest="gpu_error_checking", action="store_true", default=False, help="Enable error checking on the GPU") parser.add_option( "--minimize-cpu-usage", dest="min_cpu", action="store_true", default=False, help= "Enable to keep the CPU usage of HOOMD to a bare minimum (will degrade overall performance somewhat)" ) parser.add_option("--ignore-display-gpu", dest="ignore_display", action="store_true", default=False, help="Attempt to avoid running on the display GPU") parser.add_option("--notice-level", dest="notice_level", help="Minimum level of notice messages to print") parser.add_option("--msg-file", dest="msg_file", help="Name of file to write messages to") parser.add_option( "--shared-msg-file", dest="shared_msg_file", help= "(MPI only) Name of shared file to write message to (append partition #)" ) parser.add_option("--nrank", dest="nrank", help="(MPI) Number of ranks to include in a partition") parser.add_option("--nx", dest="nx", help="(MPI) Number of domains along the x-direction") parser.add_option("--ny", dest="ny", help="(MPI) Number of domains along the y-direction") parser.add_option("--nz", dest="nz", help="(MPI) Number of domains along the z-direction") parser.add_option( "--linear", dest="linear", action="store_true", default=False, help="(MPI only) Force a slab (1D) decomposition along the z-direction" ) parser.add_option( "--onelevel", dest="onelevel", action="store_true", default=False, help="(MPI only) Disable two-level (node-local) decomposition") parser.add_option("--single-mpi", dest="single_mpi", action="store_true", help="Allow single-threaded HOOMD builds in MPI jobs") parser.add_option("--user", dest="user", help="User options") parser.add_option("--nthreads", dest="nthreads", help="Number of TBB threads") input_args = None if arg_string is not None: input_args = shlex.split(arg_string) (cmd_options, args) = parser.parse_args(args=input_args) # chedk for valid mode setting if cmd_options.mode is not None: if not (cmd_options.mode == "cpu" or cmd_options.mode == "gpu" or cmd_options.mode == "auto"): parser.error("--mode must be either cpu, gpu, or auto") # check for sane options if cmd_options.mode == "cpu" and (cmd_options.gpu is not None): parser.error("--mode=cpu cannot be specified along with --gpu") # set the mode to gpu if the gpu # was set if cmd_options.gpu is not None and cmd_options.mode == 'auto': cmd_options.mode = "gpu" # convert gpu to an integer if cmd_options.gpu is not None: try: cmd_options.gpu = int(cmd_options.gpu) except ValueError: parser.error('--gpu must be an integer') # convert notice_level to an integer if cmd_options.notice_level is not None: try: cmd_options.notice_level = int(cmd_options.notice_level) except ValueError: parser.error('--notice-level must be an integer') # Convert nx to an integer if cmd_options.nx is not None: if not _hoomd.is_MPI_available(): hoomd.context.msg.error( "The --nx option is only avaible in MPI builds.\n") raise RuntimeError('Error setting option') try: cmd_options.nx = int(cmd_options.nx) except ValueError: parser.error('--nx must be an integer') # Convert ny to an integer if cmd_options.ny is not None: if not _hoomd.is_MPI_available(): hoomd.context.msg.error( "The --ny option is only avaible in MPI builds.\n") raise RuntimeError('Error setting option') try: cmd_options.ny = int(cmd_options.ny) except ValueError: parser.error('--ny must be an integer') # Convert nz to an integer if cmd_options.nz is not None: if not _hoomd.is_MPI_available(): hoomd.context.msg.error( "The --nz option is only avaible in MPI builds.\n") raise RuntimeError('Error setting option') try: cmd_options.nz = int(cmd_options.nz) except ValueError: parser.error('--nz must be an integer') # Convert nthreads to an integer if cmd_options.nthreads is not None: if not _hoomd.is_TBB_available(): hoomd.context.msg.error( "The --nthreads option is only avaible in TBB-enabled builds.\n" ) raise RuntimeError('Error setting option') try: cmd_options.nthreads = int(cmd_options.nthreads) except ValueError: parser.error('--nthreads must be an integer') # copy command line options over to global options hoomd.context.options.mode = cmd_options.mode hoomd.context.options.gpu = cmd_options.gpu hoomd.context.options.gpu_error_checking = cmd_options.gpu_error_checking hoomd.context.options.min_cpu = cmd_options.min_cpu hoomd.context.options.ignore_display = cmd_options.ignore_display hoomd.context.options.nx = cmd_options.nx hoomd.context.options.ny = cmd_options.ny hoomd.context.options.nz = cmd_options.nz hoomd.context.options.linear = cmd_options.linear hoomd.context.options.onelevel = cmd_options.onelevel hoomd.context.options.single_mpi = cmd_options.single_mpi hoomd.context.options.nthreads = cmd_options.nthreads if cmd_options.notice_level is not None: hoomd.context.options.notice_level = cmd_options.notice_level hoomd.context.msg.setNoticeLevel(hoomd.context.options.notice_level) if cmd_options.msg_file is not None: hoomd.context.options.msg_file = cmd_options.msg_file hoomd.context.msg.openFile(hoomd.context.options.msg_file) if cmd_options.shared_msg_file is not None: if not _hoomd.is_MPI_available(): hoomd.context.msg.error( "Shared log files are only available in MPI builds.\n") raise RuntimeError('Error setting option') hoomd.context.options.shared_msg_file = cmd_options.shared_msg_file hoomd.context.msg.setSharedFile(hoomd.context.options.shared_msg_file) if cmd_options.nrank is not None: if not _hoomd.is_MPI_available(): hoomd.context.msg.error( "The --nrank option is only avaible in MPI builds.\n") raise RuntimeError('Error setting option') # check validity nrank = int(cmd_options.nrank) if (_hoomd.ExecutionConfiguration.getNRanksGlobal() % nrank): hoomd.context.msg.error( "Total number of ranks is not a multiple of --nrank\n") raise RuntimeError('Error checking option') hoomd.context.options.nrank = nrank if cmd_options.user is not None: hoomd.context.options.user = shlex.split(cmd_options.user)
def num_threads(self): if not _hoomd.is_TBB_available(): msg.warning("HOOMD was compiled without thread support, returning None\n"); return None else: return self._get_exec_conf().getNumThreads();
def _create_exec_conf(mpi_comm): global exec_conf, options, msg # use a cached execution configuration if available if exec_conf is not None: return exec_conf mpi_available = _hoomd.is_MPI_available(); if options.mode == 'auto': exec_mode = _hoomd.ExecutionConfiguration.executionMode.AUTO; elif options.mode == "cpu": exec_mode = _hoomd.ExecutionConfiguration.executionMode.CPU; elif options.mode == "gpu": exec_mode = _hoomd.ExecutionConfiguration.executionMode.GPU; else: raise RuntimeError("Invalid mode"); # convert None options to defaults if options.gpu is None: gpu_id = []; else: gpu_id = options.gpu; if options.nrank is None: nrank = 0; else: nrank = int(options.nrank); gpu_vec = _hoomd.std_vector_int() for gpuid in gpu_id: gpu_vec.append(gpuid) # create the specified configuration if mpi_comm is None: exec_conf = _hoomd.ExecutionConfiguration(exec_mode, gpu_vec, options.min_cpu, options.ignore_display, msg, nrank); else: if not mpi_available: msg.error("mpi_comm provided, but MPI support was disabled at compile time\n"); raise RuntimeError("mpi_comm is not supported in serial builds"); handled = False; # pass in pointer to MPI_Comm object provided by mpi4py try: import mpi4py if isinstance(mpi_comm, mpi4py.MPI.Comm): addr = mpi4py.MPI._addressof(mpi_comm); exec_conf = _hoomd.ExecutionConfiguration._make_exec_conf_mpi_comm(exec_mode, gpu_vec, options.min_cpu, options.ignore_display, msg, nrank, addr); handled = True except ImportError: # silently ignore when mpi4py is missing pass # undocumented case: handle plain integers as pointers to MPI_Comm objects if not handled and isinstance(mpi_comm, int): exec_conf = _hoomd.ExecutionConfiguration._make_exec_conf_mpi_comm(exec_mode, gpu_vec, options.min_cpu, options.ignore_display, msg, nrank, mpi_comm); handled = True if not handled: msg.error("unknown mpi_comm object: {}.\n".format(mpi_comm)); raise RuntimeError("Invalid mpi_comm object"); # if gpu_error_checking is set, enable it on the GPU if options.gpu_error_checking: exec_conf.setCUDAErrorChecking(True); if _hoomd.is_TBB_available(): # set the number of TBB threads as necessary if options.nthreads != None: exec_conf.setNumThreads(options.nthreads) exec_conf = exec_conf; return exec_conf;
# -*- coding: iso-8859-1 -*- # Maintainer: joaander from hoomd import * import hoomd; from hoomd import _hoomd if _hoomd.is_TBB_available(): # test the command line option context.initialize("--nthreads=4") import unittest import os # unit tests for options class option_tests (unittest.TestCase): def setUp(self): print # tests that mode settings work properly def test_nthreads(self): self.assertEqual(hoomd.context.ExecutionContext().num_threads, 4); option.set_num_threads(2) self.assertEqual(hoomd.context.ExecutionContext().num_threads, 2); def tearDown(self): pass; if __name__ == '__main__': unittest.main(argv = ['test.py', '-v'])
def num_threads(self): if not _hoomd.is_TBB_available(): return 1 else: return self.cpp_exec_conf.getNumThreads()
def _parse_command_line(arg_string=None): parser = OptionParser(); parser.add_option("--mode", dest="mode", help="Execution mode (cpu or gpu)", default='auto'); parser.add_option("--gpu", dest="gpu", help="GPU or comma-separated list of GPUs on which to execute"); parser.add_option("--gpu_error_checking", dest="gpu_error_checking", action="store_true", default=False, help="Enable error checking on the GPU"); parser.add_option("--minimize-cpu-usage", dest="min_cpu", action="store_true", default=False, help="Enable to keep the CPU usage of HOOMD to a bare minimum (will degrade overall performance somewhat)"); parser.add_option("--ignore-display-gpu", dest="ignore_display", action="store_true", default=False, help="Attempt to avoid running on the display GPU"); parser.add_option("--notice-level", dest="notice_level", help="Minimum level of notice messages to print"); parser.add_option("--msg-file", dest="msg_file", help="Name of file to write messages to"); parser.add_option("--shared-msg-file", dest="shared_msg_file", help="(MPI only) Name of shared file to write message to (append partition #)"); parser.add_option("--nrank", dest="nrank", help="(MPI) Number of ranks to include in a partition"); parser.add_option("--nx", dest="nx", help="(MPI) Number of domains along the x-direction"); parser.add_option("--ny", dest="ny", help="(MPI) Number of domains along the y-direction"); parser.add_option("--nz", dest="nz", help="(MPI) Number of domains along the z-direction"); parser.add_option("--linear", dest="linear", action="store_true", default=False, help="(MPI only) Force a slab (1D) decomposition along the z-direction"); parser.add_option("--onelevel", dest="onelevel", action="store_true", default=False, help="(MPI only) Disable two-level (node-local) decomposition"); parser.add_option("--single-mpi", dest="single_mpi", action="store_true", help="Allow single-threaded HOOMD builds in MPI jobs"); parser.add_option("--user", dest="user", help="User options"); parser.add_option("--nthreads", dest="nthreads", help="Number of TBB threads"); input_args = None; if arg_string is not None: input_args = shlex.split(arg_string); (cmd_options, args) = parser.parse_args(args=input_args); # check for valid mode setting if cmd_options.mode is not None: if not (cmd_options.mode == "cpu" or cmd_options.mode == "gpu" or cmd_options.mode == "auto"): parser.error("--mode must be either cpu, gpu, or auto"); # check for sane options if cmd_options.mode == "cpu" and (cmd_options.gpu is not None): parser.error("--mode=cpu cannot be specified along with --gpu") # set the mode to gpu if the gpu # was set if cmd_options.gpu is not None and cmd_options.mode == 'auto': cmd_options.mode = "gpu" # convert gpu to an integer if cmd_options.gpu is not None: try: cmd_options.gpu = [int(gpu) for gpu in str(cmd_options.gpu).split(',')] except ValueError: parser.error('--gpu must be an integer or comma-separated list of integers') # convert notice_level to an integer if cmd_options.notice_level is not None: try: cmd_options.notice_level = int(cmd_options.notice_level); except ValueError: parser.error('--notice-level must be an integer') # Convert nx to an integer if cmd_options.nx is not None: if not _hoomd.is_MPI_available(): hoomd.context.msg.error("The --nx option is only available in MPI builds.\n"); raise RuntimeError('Error setting option'); try: cmd_options.nx = int(cmd_options.nx); except ValueError: parser.error('--nx must be an integer') # Convert ny to an integer if cmd_options.ny is not None: if not _hoomd.is_MPI_available(): hoomd.context.msg.error("The --ny option is only available in MPI builds.\n"); raise RuntimeError('Error setting option'); try: cmd_options.ny = int(cmd_options.ny); except ValueError: parser.error('--ny must be an integer') # Convert nz to an integer if cmd_options.nz is not None: if not _hoomd.is_MPI_available(): hoomd.context.msg.error("The --nz option is only available in MPI builds.\n"); raise RuntimeError('Error setting option'); try: cmd_options.nz = int(cmd_options.nz); except ValueError: parser.error('--nz must be an integer') # Convert nthreads to an integer if cmd_options.nthreads is not None: if not _hoomd.is_TBB_available(): hoomd.context.msg.error("The --nthreads option is only available in TBB-enabled builds.\n"); raise RuntimeError('Error setting option'); try: cmd_options.nthreads = int(cmd_options.nthreads); except ValueError: parser.error('--nthreads must be an integer') # copy command line options over to global options hoomd.context.options.mode = cmd_options.mode; hoomd.context.options.gpu = cmd_options.gpu; hoomd.context.options.gpu_error_checking = cmd_options.gpu_error_checking; hoomd.context.options.min_cpu = cmd_options.min_cpu; hoomd.context.options.ignore_display = cmd_options.ignore_display; hoomd.context.options.nx = cmd_options.nx; hoomd.context.options.ny = cmd_options.ny; hoomd.context.options.nz = cmd_options.nz; hoomd.context.options.linear = cmd_options.linear hoomd.context.options.onelevel = cmd_options.onelevel hoomd.context.options.single_mpi = cmd_options.single_mpi hoomd.context.options.nthreads = cmd_options.nthreads if cmd_options.notice_level is not None: hoomd.context.options.notice_level = cmd_options.notice_level; hoomd.context.msg.setNoticeLevel(hoomd.context.options.notice_level); if cmd_options.msg_file is not None: hoomd.context.options.msg_file = cmd_options.msg_file; hoomd.context.msg.openFile(hoomd.context.options.msg_file); if cmd_options.shared_msg_file is not None: if not _hoomd.is_MPI_available(): hoomd.context.msg.error("Shared log files are only available in MPI builds.\n"); raise RuntimeError('Error setting option'); hoomd.context.options.shared_msg_file = cmd_options.shared_msg_file; hoomd.context.msg.setSharedFile(hoomd.context.options.shared_msg_file); if cmd_options.nrank is not None: if not _hoomd.is_MPI_available(): hoomd.context.msg.error("The --nrank option is only available in MPI builds.\n"); raise RuntimeError('Error setting option'); # check validity nrank = int(cmd_options.nrank) if (_hoomd.ExecutionConfiguration.getNRanksGlobal() % nrank): hoomd.context.msg.error("Total number of ranks is not a multiple of --nrank\n"); raise RuntimeError('Error checking option'); hoomd.context.options.nrank = nrank if cmd_options.user is not None: hoomd.context.options.user = shlex.split(cmd_options.user);