Example #1
0
def master(group_name: str, is_immediate: bool = False):
    """
    The main master logic includes initialize proxy and allocate jobs to workers.

    Args:
        group_name (str): Identifier for the group of all communication components,
        is_immediate (bool): If True, it will be an async mode; otherwise, it will be an sync mode.
            Async Mode: The proxy only returns the session id for sending messages. Based on the local task priority,
                        you can do something with high priority before receiving replied messages from peers.
            Sync Mode: It will block until the proxy returns all the replied messages.
    """
    proxy = Proxy(group_name=group_name,
                  component_type="master",
                  expected_peers={"worker": 1})

    random_integer_list = np.random.randint(0, 100, 5)
    print(f"generate random integer list: {random_integer_list}.")

    for peer in proxy.peers_name["worker"]:
        message = SessionMessage(tag="sum",
                                 source=proxy.name,
                                 destination=peer,
                                 payload=random_integer_list,
                                 session_type=SessionType.TASK)
        if is_immediate:
            session_id = proxy.isend(message)
            # Do some tasks with higher priority here.
            replied_msgs = proxy.receive_by_id(session_id, timeout=-1)
        else:
            replied_msgs = proxy.send(message, timeout=-1)

        for msg in replied_msgs:
            print(
                f"{proxy.name} receive {msg.source}, replied payload is {msg.payload}."
            )
Example #2
0
def master(group_name: str, worker_num: int, is_immediate: bool = False):
    """
    The main master logic includes initialize proxy and allocate jobs to workers.

    Args:
        group_name (str): Identifier for the group of all communication components,
        worker_num (int): The number of workers,
        is_immediate (bool): If True, it will be an async mode; otherwise, it will be an sync mode.
            Async Mode: The proxy only returns the session id for sending messages. Based on the local task priority,
                        you can do something with high priority before receiving replied messages from peers.
            Sync Mode: It will block until the proxy returns all the replied messages.
    """
    proxy = Proxy(group_name=group_name,
                  component_type="master",
                  expected_peers={"worker": worker_num})

    if is_immediate:
        session_ids = proxy.ibroadcast(tag="INC",
                                       session_type=SessionType.NOTIFICATION)
        # do some tasks with higher priority here.
        replied_msgs = proxy.receive_by_id(session_ids)
    else:
        replied_msgs = proxy.broadcast(tag="INC",
                                       session_type=SessionType.NOTIFICATION)

    for msg in replied_msgs:
        print(
            f"{proxy.component_name} get receive notification from {msg.source} with message session stage "
            + f"{msg.session_stage}.")
Example #3
0
def master(group_name: str,
           sum_worker_number: int,
           multiply_worker_number: int,
           is_immediate: bool = False):
    """
    The main master logic includes initialize proxy and allocate jobs to workers.

    Args:
        group_name (str): Identifier for the group of all communication components,
        sum_worker_number (int): The number of sum workers,
        multiply_worker_number (int): The number of multiply workers,
        is_immediate (bool): If True, it will be an async mode; otherwise, it will be an sync mode.
            Async Mode: The proxy only returns the session id for sending messages. Based on the local task priority,
                        you can do something with high priority before receiving replied messages from peers.
            Sync Mode: It will block until the proxy returns all the replied messages.
    """
    proxy = Proxy(group_name=group_name,
                  component_type="master",
                  expected_peers={
                      "sum_worker": sum_worker_number,
                      "multiply_worker": multiply_worker_number
                  })

    sum_list = np.random.randint(0, 10, 100)
    multiple_list = np.random.randint(1, 10, 20)
    print("Generate random sum/multiple list with length 100.")

    # Assign sum tasks for summation workers.
    destination_payload_list = []
    for idx, peer in enumerate(proxy.peers_name["sum_worker"]):
        data_length_per_peer = int(
            len(sum_list) / len(proxy.peers_name["sum_worker"]))
        destination_payload_list.append(
            (peer, sum_list[idx * data_length_per_peer:(idx + 1) *
                            data_length_per_peer]))

    # Assign multiply tasks for multiplication workers.
    for idx, peer in enumerate(proxy.peers_name["multiply_worker"]):
        data_length_per_peer = int(
            len(multiple_list) / len(proxy.peers_name["multiply_worker"]))
        destination_payload_list.append(
            (peer, multiple_list[idx * data_length_per_peer:(idx + 1) *
                                 data_length_per_peer]))

    if is_immediate:
        session_ids = proxy.iscatter(
            tag="job",
            session_type=SessionType.TASK,
            destination_payload_list=destination_payload_list)
        # Do some tasks with higher priority here.
        replied_msgs = proxy.receive_by_id(session_ids, timeout=-1)
    else:
        replied_msgs = proxy.scatter(
            tag="job",
            session_type=SessionType.TASK,
            destination_payload_list=destination_payload_list,
            timeout=-1)

    sum_result, multiply_result = 0, 1
    for msg in replied_msgs:
        if msg.tag == "sum":
            print(
                f"{proxy.name} receive message from {msg.source} with the sum result {msg.payload}."
            )
            sum_result += msg.payload
        elif msg.tag == "multiply":
            print(
                f"{proxy.name} receive message from {msg.source} with the multiply result {msg.payload}."
            )
            multiply_result *= msg.payload

    # Check task result correction.
    assert (sum(sum_list) == sum_result)
    assert (np.prod(multiple_list) == multiply_result)