Skip to content

Commit 650b38d

Browse files
committed
Documenting the core APIs.
1 parent 8e9eee7 commit 650b38d

File tree

3 files changed

+224
-0
lines changed

3 files changed

+224
-0
lines changed

src/algorithm/core.jl

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,76 @@
11
export DistributedAlgorithm, on_exchange_message, CoordinatedDistributedAlgorithm, start_optimization, Coordinator
22

3+
"""
4+
DistributedAlgorithm
5+
6+
An abstract type representing the base for distributed optimization algorithms.
7+
Subtypes should implement specific distributed algorithm logic for resource optimization.
8+
"""
39
abstract type DistributedAlgorithm end
410

11+
12+
"""
13+
on_exchange_message(algorithm::DistributedAlgorithm, carrier::Carrier, message_data::Any, meta::Any)
14+
15+
Handles an incoming exchange message within the distributed algorithm framework.
16+
17+
# Arguments
18+
- `algorithm::DistributedAlgorithm`: The distributed algorithm instance managing the exchange.
19+
- `carrier::Carrier`: The communication carrier responsible for message delivery.
20+
- `message_data::Any`: The data contained in the received message.
21+
- `meta::Any`: Additional metadata associated with the message.
22+
23+
# Description
24+
Processes the received exchange message, updating the algorithm's state or triggering appropriate actions based on the message content and metadata.
25+
26+
# Returns
27+
May return a result or perform side effects depending on the implementation.
28+
"""
529
function on_exchange_message(algorithm::DistributedAlgorithm, carrier::Carrier, message_data::Any, meta::Any)
630
throw("NotImplementedOrWrongArgumentTypes")
731
end
832

33+
34+
"""
35+
Coordinator
36+
37+
An abstract type representing the coordinator in distributed resource optimization algorithms.
38+
Concrete implementations of this type are responsible for managing and coordinating the optimization process across distributed resources.
39+
"""
940
abstract type Coordinator end
1041

42+
43+
"""
44+
start_optimization(coordinator::Coordinator, carrier::Carrier, message_data::Any, meta::Any)
45+
46+
Initiates the optimization process using the provided `coordinator` and `carrier` objects.
47+
The function takes arbitrary `message_data` and `meta` information to configure or inform the optimization run.
48+
49+
# Arguments
50+
- `coordinator::Coordinator`: The main coordinator responsible for managing the optimization workflow.
51+
- `carrier::Carrier`: The carrier object that facilitates communication or data transfer during optimization.
52+
- `message_data::Any`: Additional data or messages required for the optimization process.
53+
- `meta::Any`: Communication Metadata or supplementary information relevant to the optimization.
54+
55+
# Returns
56+
- The result of the optimization process, which may vary depending on the implementation.
57+
58+
# Notes
59+
- Ensure that `coordinator` and `carrier` are properly initialized before calling this function.
60+
- The types of `message_data` and `meta` are flexible to accommodate various use cases.
61+
"""
1162
function start_optimization(coordinator::Coordinator, carrier::Carrier, message_data::Any, meta::Any)
1263
throw("NotImplementedOrWrongArgumentTypes")
1364
end
1465

66+
67+
"""
68+
CoordinatedDistributedAlgorithm
69+
70+
A struct representing the core of a coordinated distributed optimization algorithm.
71+
This type encapsulates the necessary data and methods for coordinating multiple agents
72+
or processes in a distributed resource optimization setting.
73+
"""
1574
struct CoordinatedDistributedAlgorithm
1675
distributed_algo::Vector{DistributedAlgorithm}
1776
coordinator::Coordinator

src/carrier/core.jl

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,138 @@
11
export Carrier, send_to_other, reply_to_other, send_and_wait_for_answers, send_awaitable, schedule_using, others
22

3+
"""
4+
Carrier
5+
6+
An abstract type representing a generic data/communication carrier in the system.
7+
Concrete subtypes should implement specific carrier behaviors and properties.
8+
"""
39
abstract type Carrier end
410

11+
"""
12+
send_to_other(carrier::Carrier, content_data::Any, receiver::Any)
13+
14+
Sends `content_data` using the specified `carrier` to the given `receiver`.
15+
16+
# Arguments
17+
- `carrier::Carrier`: The carrier object responsible for sending the data.
18+
- `content_data::Any`: The data to be sent.
19+
- `receiver::Any`: The target receiver of the data.
20+
21+
# Returns
22+
- The result of the send operation, which may vary depending on the implementation.
23+
24+
"""
525
function send_to_other(carrier::Carrier, content_data::Any, receiver::Any)
626
throw("NotImplemented")
727
end
28+
29+
30+
"""
31+
reply_to_other(carrier::Carrier, content_data::Any, meta::Any)
32+
33+
Reply using the given `carrier` to another entity, using the provided `content_data` and
34+
`meta` information.
35+
36+
# Arguments
37+
- `carrier::Carrier`: The carrier object responsible for communication.
38+
- `content_data::Any`: The data to be sent in the reply.
39+
- `meta::Any`: Additional metadata associated with the reply.
40+
41+
# Returns
42+
- The result of the reply operation, depending on the implementation.
43+
44+
# Notes
45+
- The specific behavior depends on the implementation details of the `Carrier` type.
46+
"""
847
function reply_to_other(carrier::Carrier, content_data::Any, meta::Any)
948
throw("NotImplemented")
1049
end
50+
51+
52+
"""
53+
send_and_wait_for_answers(carrier::Carrier, content_data::Any, receivers::Any)
54+
55+
Sends `content_data` using `carrier` to the given `receivers` and waits for their responses.
56+
57+
# Arguments
58+
- `carrier::Carrier`: The carrier object responsible for sending the data.
59+
- `content_data::Any`: The data to be sent to the receivers.
60+
- `receivers::Any`: The target receivers to which the data will be sent.
61+
62+
# Returns
63+
Returns the responses received from the receivers after sending the data.
64+
65+
# Notes
66+
This function blocks execution until all expected answers are received from the receivers.
67+
"""
1168
function send_and_wait_for_answers(carrier::Carrier, content_data::Any, receivers::Any)
1269
throw("NotImplemented")
1370
end
71+
72+
73+
"""
74+
send_awaitable(carrier::Carrier, content_data::Any, receiver::Any)
75+
76+
Sends an awaitable message using the specified `carrier` to the given `receiver` with the provided `content_data`.
77+
78+
# Arguments
79+
- `carrier::Carrier`: The carrier object responsible for message transmission.
80+
- `content_data::Any`: The data to be sent in the message.
81+
- `receiver::Any`: The target recipient of the message.
82+
83+
# Returns
84+
Returns an awaitable object or handle that can be used to track the completion or response of the sent message.
85+
"""
1486
function send_awaitable(carrier::Carrier, content_data::Any, receiver::Any)
1587
throw("NotImplemented $carrier $content_data $receiver")
1688
end
89+
90+
"""
91+
Base.wait(carrier::Carrier, waitable::Any)
92+
93+
Waits for the specified `waitable` object to become ready or complete within the context of the given `carrier`.
94+
This function integrates with the `Carrier`'s scheduling or resource management logic to handle asynchronous or blocking operations.
95+
96+
# Arguments
97+
- `carrier::Carrier`: The carrier instance managing the waiting operation.
98+
- `waitable::Any`: The object or resource to wait for. This can be any type that supports waiting semantics.
99+
100+
# Notes
101+
The function blocks until the `waitable` is ready or the operation is complete.
102+
"""
17103
function Base.wait(carrier::Carrier, waitable::Any)
18104
throw("NotImplemented")
19105
end
106+
107+
"""
108+
schedule_using(carrier::Carrier, to_be_scheduled::Function, delay_s::Float64)
109+
110+
Schedules the execution of the provided function `to_be_scheduled` using the specified `carrier` after a delay of `delay_s` seconds.
111+
112+
# Arguments
113+
- `carrier::Carrier`: The carrier object responsible for scheduling.
114+
- `to_be_scheduled::Function`: The function to be executed after the delay.
115+
- `delay_s::Float64`: The delay in seconds before executing the function.
116+
117+
# Returns
118+
- Implementation-dependent. Typically, returns a handle or status indicating the scheduled task.
119+
"""
20120
function schedule_using(carrier::Carrier, to_be_scheduled::Function, delay_s::Float64)
21121
throw("NotImplemented")
22122
end
123+
124+
"""
125+
others(carrier::Carrier, participant_id::String)
126+
127+
Returns a collection of participants in the given `carrier` excluding the participant with the specified `participant_id`.
128+
129+
# Arguments
130+
- `carrier::Carrier`: The carrier object containing participants.
131+
- `participant_id::String`: The identifier of the participant to exclude.
132+
133+
# Returns
134+
- A collection (e.g., array or set) of participants other than the one with `participant_id`.
135+
"""
23136
function others(carrier::Carrier, participant_id::String)
24137
throw("NotImplemented")
25138
end

src/carrier/mango.jl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ export DistributedOptimizationRole, MangoCarrier, CoordinatorRole, StartCoordina
22

33
using Mango
44

5+
"""
6+
MangoCarrier <: Carrier
7+
8+
A concrete implementation of the `Carrier` type representing a mango carrier.
9+
Extend this struct with relevant fields and methods to model the behavior and properties of a mango carrier in the distributed resource optimization context.
10+
"""
511
struct MangoCarrier <: Carrier
612
parent::Role
713
include_self::Bool
@@ -11,17 +17,42 @@ struct StartCoordinatedDistributedOptimization
1117
input::Any
1218
end
1319

20+
"""
21+
OptimizationFinishedMessage
22+
23+
A message struct indicating that an optimization process has completed.
24+
Typically used for signaling the end of an optimization routine in distributed or parallel computation contexts.
25+
"""
1426
struct OptimizationFinishedMessage
1527
result::Any
1628
end
1729

30+
"""
31+
CoordinatorRole
32+
33+
A role struct representing the coordinator in the distributed resource optimization system.
34+
Used to identify and manage coordinator-specific logic and responsibilities within the carrier module.
35+
"""
1836
@role struct CoordinatorRole
1937
coordinator::Coordinator
2038
carrier::Union{Nothing,MangoCarrier}
2139
tid::Symbol = :default
2240
task::Any = nothing
2341
end
2442

43+
"""
44+
CoordinatorRole(coordinator::Coordinator; include_self=false, tid::Symbol = :default)
45+
46+
Assigns the coordinator role to the specified `coordinator` object.
47+
48+
# Arguments
49+
- `coordinator::Coordinator`: The coordinator instance to assign the role to.
50+
- `include_self::Bool=false`: If `true`, includes the coordinator itself in the role assignment.
51+
- `tid::Symbol=:default`: An optional identifier symbol for neighbor lookups.
52+
53+
# Returns
54+
Returns the configured coordinator role.
55+
"""
2556
function CoordinatorRole(coordinator::Coordinator; include_self=false, tid::Symbol = :default)
2657
role = CoordinatorRole(coordinator, nothing, tid=tid)
2758
role.carrier = MangoCarrier(role, include_self)
@@ -38,12 +69,33 @@ function Mango.handle_message(role::CoordinatorRole, message::StartCoordinatedDi
3869
end
3970
end
4071

72+
73+
"""
74+
DistributedOptimizationRole
75+
76+
A role struct used to define distributed optimization responsibilities within the system.
77+
Attach this role to entities that participate in distributed resource optimization processes.
78+
"""
4179
@role struct DistributedOptimizationRole
4280
algorithm::DistributedAlgorithm
4381
carrier::Union{Nothing,MangoCarrier}
4482
tid::Symbol = :default
4583
end
4684

85+
"""
86+
DistributedOptimizationRole(algorithm::DistributedAlgorithm; tid::Symbol = :default)
87+
88+
Creates and returns a distributed optimization role using the specified `algorithm`.
89+
An optional identifier `tid` can be provided, defaulting to `:default`.
90+
91+
# Arguments
92+
- `algorithm::DistributedAlgorithm`: The distributed optimization algorithm to be used.
93+
- `tid::Symbol`: (Optional) Identifier for the neighbor lookup. Defaults to `:default`.
94+
95+
# Returns
96+
A distributed optimization role configured with the given algorithm and an identifier
97+
to specify the addresses of other participants.
98+
"""
4799
function DistributedOptimizationRole(algorithm::DistributedAlgorithm; tid::Symbol = :default)
48100
role = DistributedOptimizationRole(algorithm, nothing, tid=tid)
49101
role.carrier = MangoCarrier(role, false)

0 commit comments

Comments
 (0)