1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use crate::active_messaging::registered_active_message::AmId;
use crate::active_messaging::*;

pub(crate) mod simple_batcher;
use simple_batcher::SimpleBatcher;

pub(crate) mod team_am_batcher;
use team_am_batcher::TeamAmBatcher;

use async_trait::async_trait;

#[derive(Clone)]
enum LamellarData {
    Am(LamellarArcAm, AmId),
    Return(LamellarArcAm, AmId),
    Data(LamellarResultArc, Vec<RemotePtr>, usize),
    Unit,
}

impl std::fmt::Debug for LamellarData {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LamellarData::Am(_, _) => write!(f, "Am"),
            LamellarData::Return(_, _) => write!(f, "Return"),
            LamellarData::Data(_, _, _) => write!(f, "Data"),
            LamellarData::Unit => write!(f, "Unit"),
        }
    }
}

#[async_trait]
pub(crate) trait Batcher {
    fn add_remote_am_to_batch(
        &self,
        req_data: ReqMetaData,
        am: LamellarArcAm,
        am_id: AmId,
        am_size: usize,
        scheduler: &(impl SchedulerQueue + Sync + std::fmt::Debug),
        stall_mark: usize,
    );
    fn add_return_am_to_batch(
        &self,
        req_data: ReqMetaData,
        am: LamellarArcAm,
        am_id: AmId,
        am_size: usize,
        scheduler: &(impl SchedulerQueue + Sync + std::fmt::Debug),
        stall_mark: usize,
    );
    fn add_data_am_to_batch(
        &self,
        req_data: ReqMetaData,
        data: LamellarResultArc,
        data_size: usize,
        scheduler: &(impl SchedulerQueue + Sync + std::fmt::Debug),
        stall_mark: usize,
    );
    fn add_unit_am_to_batch(
        &self,
        req_data: ReqMetaData,
        scheduler: &(impl SchedulerQueue + Sync + std::fmt::Debug),
        stall_mark: usize,
    );

    async fn exec_batched_msg(
        &self,
        msg: Msg,
        ser_data: SerializedData,
        lamellae: Arc<Lamellae>,
        scheduler: &(impl SchedulerQueue + Sync + std::fmt::Debug),
        ame: &RegisteredActiveMessages,
    );
}

#[derive(Debug)]
pub(crate) enum BatcherType {
    Simple(SimpleBatcher),
    TeamAm(TeamAmBatcher),
}

#[async_trait]
impl Batcher for BatcherType {
    #[tracing::instrument(skip_all)]
    fn add_remote_am_to_batch(
        &self,
        req_data: ReqMetaData,
        am: LamellarArcAm,
        am_id: AmId,
        am_size: usize,
        scheduler: &(impl SchedulerQueue + Sync + std::fmt::Debug),
        stall_mark: usize,
    ) {
        match self {
            BatcherType::Simple(batcher) => {
                batcher.add_remote_am_to_batch(req_data, am, am_id, am_size, scheduler, stall_mark)
            }
            BatcherType::TeamAm(batcher) => {
                batcher.add_remote_am_to_batch(req_data, am, am_id, am_size, scheduler, stall_mark)
            }
        }
    }
    #[tracing::instrument(skip_all)]
    fn add_return_am_to_batch(
        &self,
        req_data: ReqMetaData,
        am: LamellarArcAm,
        am_id: AmId,
        am_size: usize,
        scheduler: &(impl SchedulerQueue + Sync + std::fmt::Debug),
        stall_mark: usize,
    ) {
        match self {
            BatcherType::Simple(batcher) => {
                batcher.add_return_am_to_batch(req_data, am, am_id, am_size, scheduler, stall_mark)
            }
            BatcherType::TeamAm(batcher) => {
                batcher.add_return_am_to_batch(req_data, am, am_id, am_size, scheduler, stall_mark)
            }
        }
    }
    #[tracing::instrument(skip_all)]
    fn add_data_am_to_batch(
        &self,
        req_data: ReqMetaData,
        data: LamellarResultArc,
        data_size: usize,
        scheduler: &(impl SchedulerQueue + Sync + std::fmt::Debug),
        stall_mark: usize,
    ) {
        match self {
            BatcherType::Simple(batcher) => {
                batcher.add_data_am_to_batch(req_data, data, data_size, scheduler, stall_mark)
            }
            BatcherType::TeamAm(batcher) => {
                batcher.add_data_am_to_batch(req_data, data, data_size, scheduler, stall_mark)
            }
        }
    }
    #[tracing::instrument(skip_all)]
    fn add_unit_am_to_batch(
        &self,
        req_data: ReqMetaData,
        scheduler: &(impl SchedulerQueue + Sync + std::fmt::Debug),
        stall_mark: usize,
    ) {
        match self {
            BatcherType::Simple(batcher) => {
                batcher.add_unit_am_to_batch(req_data, scheduler, stall_mark)
            }
            BatcherType::TeamAm(batcher) => {
                batcher.add_unit_am_to_batch(req_data, scheduler, stall_mark)
            }
        }
    }
    #[tracing::instrument(skip_all)]
    async fn exec_batched_msg(
        &self,
        msg: Msg,
        ser_data: SerializedData,
        lamellae: Arc<Lamellae>,
        scheduler: &(impl SchedulerQueue + Sync + std::fmt::Debug),
        ame: &RegisteredActiveMessages,
    ) {
        match self {
            BatcherType::Simple(batcher) => {
                batcher
                    .exec_batched_msg(msg, ser_data, lamellae, scheduler, ame)
                    .await;
            }
            BatcherType::TeamAm(batcher) => {
                batcher
                    .exec_batched_msg(msg, ser_data, lamellae, scheduler, ame)
                    .await;
            }
        }
    }
}