Worker Resources Module

class resources.worker_resources.ResourceManager(num_workers, resources)

Bases: RSetResources

Provides methods for managing the assignment of resource sets to workers.

Parameters:
__init__(num_workers, resources)

Initializes a new ResourceManager instance

Instantiates the numpy structured array that holds information for each resource set.

Parameters:
  • num_workers (int) – The number of workers

  • resources (Resources) – A Resources object containing global nodelist and intranode information

Return type:

None

assign_rsets(rset_team, worker_id)

Mark the resource sets given by rset_team as assigned to worker_id

free_rsets(worker=None)

Free up assigned resource sets

static get_index_list(num_workers, num_rsets, zero_resource_list)

Map WorkerID to index into a nodelist

Parameters:
  • num_workers (int) –

  • num_rsets (int) –

  • zero_resource_list (List[int | Any]) –

Return type:

List[int | None]

class resources.worker_resources.WorkerResources(num_workers, resources, workerID)

Bases: RSetResources

Provide system resources per worker to libEnsemble and executor.

Object Attributes:

Some of these attributes may be updated as the ensemble progresses.

rsets below is used to abbreviate resource sets.

Variables:
  • workerID (int) – workerID for this worker.

  • local_nodelist (list) – A list of all nodes assigned to this worker.

  • rset_team (list) – List of rset IDs currently assigned to this worker.

  • num_rsets (int) – The number of resource sets assigned to this worker.

  • slots (dict) – A dictionary with a list of slot IDs for each node.

  • even_slots (bool) – True if each node has the same number of slots.

  • matching_slots (bool) – True if each node has matching slot IDs.

  • slot_count (int) – The number of slots per node if even_slots is True, else None.

  • slots_on_node (list) – A list of slots IDs if matching_slots is True, else None.

  • local_node_count (int) – The number of nodes available to this worker (rounded up to whole number).

  • rsets_per_node (int) – The number of rsets per node (if a rset > 1 node, will be 1).

The worker_resources attributes can be queried, and convenience functions called, via the resources class attribute. For example:

With resources imported:

from libensemble.resources.resources import Resources

A user function (sim/gen) may do:

resources = Resources.resources.worker_resources
num_nodes = resources.local_node_count
cores_per_node = resources.slot_count  # One CPU per GPU
resources.set_env_to_slots("CUDA_VISIBLE_DEVICES")  # Use convenience function.

Note that slots are resource sets enumerated on a node (starting with zero). If a resource set has more than one node, then each node is considered to have slot zero.

If even_slots is True, then the attributes slot_count will give the number of slots on each node. If matching_slots is True, then slots_on_node will give the slot IDs for all nodes. These can be used for simplicity; otherwise, the slots dictionary can be used to get information for each node.

__init__(num_workers, resources, workerID)

Initializes a new WorkerResources instance

Determines the compute resources available for current worker, including node list and cores/hardware threads available within nodes.

Parameters:
  • num_workers (int) – The number of workers

  • resources (Resources) – A Resources object containing global nodelist and intranode information

  • workerID (int) – workerID of current process

get_slots_as_string(multiplier=1, delimiter=',', limit=None)

Returns list of slots as a string

Parameters:
  • multiplier – Optional int. Assume this many items per slot.

  • delimiter – Optional int. Delimiter for output string.

  • limit – Optional int. Maximum slots (truncate list after this many slots).

set_env_to_slots(env_var, multiplier=1, delimiter=',')

Sets the given environment variable to slots

Parameters:
  • env_var – String. Name of environment variable to set.

  • multiplier – Optional int. Assume this many items per slot.

  • delimiter – Optional int. Delimiter for output string.

Example usage in a sim function:

With resources imported:

from libensemble.resources.resources import Resources

Obtain worker resources:

resources = Resources.resources.worker_resources
resources.set_env_to_slots("CUDA_VISIBLE_DEVICES")
set_env_to_gpus(env_var=None, delimiter=',')

Sets the given environment variable to GPUs

Parameters:
  • env_var – String. Name of environment variable to set.

  • delimiter – Optional int. Delimiter for output string.

Example usage in a sim function:

With resources imported:

from libensemble.resources.resources import Resources

Obtain worker resources:

resources = Resources.resources.worker_resources
resources.set_env_to_gpus("CUDA_VISIBLE_DEVICES")
doihave_gpus()

Are this workers current resource sets GPU rsets

set_rset_team(rset_team)

Update worker team and local attributes

Updates: rset_team

local_nodelist slots (dictionary with list of partitions for each node) slot_count - number of slots on each node local_node_count

Parameters:

rset_team (List[int]) –

Return type:

None

set_gen_procs_gpus(libE_info)

Add gen supplied procs and gpus

set_slot_count()

Sets attributes even_slots and matching_slots.

Also sets slot_count if even_slots (else None) and sets slots_on_node if matching_slots (else None).

Return type:

None

static get_local_nodelist(workerID, rset_team, split_list, rsets_per_node)

Returns the list of nodes available to the given worker and the slot dictionary

Parameters:
  • workerID (int) –

  • rset_team (List[int]) –

  • split_list (List[List[str]]) –

  • rsets_per_node (int) –

Return type:

Tuple[List[str], Dict[str, List[int]]]