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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//! 异步执行 静态有向无环图 的运行节点
#![feature(associated_type_bounds)]
#![feature(test)]
extern crate test;

use core::hash::Hash;
use flume::{bounded, Receiver, Sender};
use pi_async::prelude::AsyncRuntime;
use pi_futures::BoxFuture;
use pi_graph::{DirectedGraph, DirectedGraphNode};
use pi_share::{Share, ThreadSend, ThreadSync};
use std::fmt::Debug;
use std::io::{Error, ErrorKind, Result};
use std::marker::PhantomData;

/// 同步执行节点
pub trait Runner {
    type Context: Sync;

    fn run(self, context: &'static Self::Context);
}

/// 可运行节点
pub trait Runnble {
    type Context: Sync;

    type R: Runner<Context = Self::Context> + ThreadSend + 'static;

    /// 判断是否同步运行, None表示不是可运行节点,true表示同步运行, false表示异步运行
    fn is_sync(&self) -> Option<bool>;
    /// 获得需要执行的同步函数
    fn get_sync(&self) -> Self::R;
    /// 获得需要执行的异步块
    fn get_async(&self) -> BoxFuture<'static, Result<()>>;
}

/// 异步图执行
pub async fn async_graph<
    Context: Sync,
    A: AsyncRuntime<()>,
    K: Hash + Eq + Sized + Clone + Debug + ThreadSync + 'static,
    R: Runnble<Context = Context> + ThreadSync + 'static,
    G: DirectedGraph<K, R, Node: ThreadSend + 'static> + ThreadSync + 'static,
>(
    rt: A,
    graph: Share<G>,
    context: &'static Context,
) -> Result<()> {
    // 获得图的to节点的数量
    let mut count = graph.to_len();
    if count == 0 {
        return Ok(());
    }
    let (producor, consumer) = bounded(count);
    for k in graph.from() {
        let an = AsyncGraphNode::new(graph.clone(), k.clone(), producor.clone());
        let end_r = an.exec(rt.clone(), graph.get(k).unwrap(), context);
        // 减去立即执行完毕的数量
        count -= end_r.unwrap();
    }
    // println!("wait count:{}", count);
    let r = AsyncGraphResult { count, consumer };
    r.reduce().await
}

/// 异步结果
pub struct AsyncGraphResult {
    count: usize,                      //派发的任务数量
    consumer: Receiver<Result<usize>>, //异步返回值接收器
}
/*
* 异步结果方法
*/
impl AsyncGraphResult {
    /// 归并所有派发的任务
    pub async fn reduce(mut self) -> Result<()> {
        loop {
            match self.consumer.recv_async().await {
                Err(e) => {
                    //接收错误,则立即返回
                    return Err(Error::new(
                        ErrorKind::Other,
                        format!("graph result failed, reason: {:?}", e),
                    ));
                }
                Ok(r) => match r {
                    Ok(count) => {
                        //接收成功,则检查是否全部任务都完毕
                        self.count -= count;
                        if self.count == 0 {
                            return Ok(());
                        }
                    }
                    Err(e) => {
                        return Err(Error::new(
                            ErrorKind::Other,
                            format!("graph node failed, reason: {:?}", e),
                        ))
                    }
                },
            }
        }
    }
}
/// 异步图节点执行
pub struct AsyncGraphNode<
    Context: Sync,
    K: Hash + Eq + Sized + Debug + ThreadSync + 'static,
    R: Runnble<Context = Context>,
    G: DirectedGraph<K, R, Node: ThreadSend + 'static> + ThreadSync + 'static,
> {
    graph: Share<G>,
    key: K,
    producor: Sender<Result<usize>>, //异步返回值生成器
    _k: PhantomData<R>,
}

impl<
        Context: Sync,
        K: Hash + Eq + Sized + Debug + ThreadSync + 'static,
        R: Runnble<Context = Context>,
        G: DirectedGraph<K, R, Node: ThreadSend + 'static> + ThreadSync + 'static,
    > AsyncGraphNode<Context, K, R, G>
{
    pub fn new(graph: Share<G>, key: K, producor: Sender<Result<usize>>) -> Self {
        AsyncGraphNode {
            graph,
            key,
            producor,
            _k: PhantomData,
        }
    }
}
unsafe impl<
        Context: Sync,
        K: Hash + Eq + Sized + Clone + Debug + ThreadSync + 'static,
        R: Runnble<Context = Context>,
        G: DirectedGraph<K, R, Node: ThreadSend + 'static> + ThreadSync + 'static,
    > Send for AsyncGraphNode<Context, K, R, G>
{
}

impl<
        Context: Sync,
        K: Hash + Eq + Sized + Clone + Debug + ThreadSync + 'static,
        R: Runnble<Context = Context> + 'static,
        G: DirectedGraph<K, R, Node: ThreadSend + 'static> + ThreadSync + 'static,
    > AsyncGraphNode<Context, K, R, G>
{
    /// 执行指定异步图节点到指定的运行时,并返回任务同步情况下的结束数量
    pub fn exec<A: AsyncRuntime<()>>(
        self,
        rt: A,
        node: &G::Node,
        context: &'static Context,
    ) -> Result<usize> {
        match node.value().is_sync() {
            None => {
                // 该节点为空节点
                return self.exec_next(rt, node, context);
            }
            Some(true) => {
                // 同步节点
                let r = node.value().get_sync();
                rt.clone().spawn(rt.alloc(), async move {
                    // 执行同步任务
                    r.run(context);

                    self.exec_async(rt, context).await;
                })?;
            }
            _ => {
                let f = node.value().get_async();
                rt.clone().spawn(rt.alloc(), async move {
                    // 执行异步任务
                    if let Err(e) = f.await {
                        let _ = self.producor.into_send_async(Err(e)).await;
                        return;
                    }
                    self.exec_async(rt, context).await;
                })?;
            }
        }
        Ok(0)
    }
    /// 递归的异步执行
    async fn exec_async<A: AsyncRuntime<()>>(self, rt: A, context: &'static Context) {
        // 获取同步执行exec_next的结果, 为了不让node引用穿过await,显示声明它的生命周期
        let r = {
            let node = self.graph.get(&self.key).unwrap();
            self.exec_next(rt, node, context)
        };
        if let Ok(0) = r {
            return;
        }
        let _ = self.producor.into_send_async(r).await;
    }

    /// 递归的同步执行
    fn exec_next<A: AsyncRuntime<()>>(
        &self,
        rt: A,
        node: &G::Node,
        context: &'static Context,
    ) -> Result<usize> {
        // 没有后续的节点,则返回结束的数量1
        if node.to_len() == 0 {
            return Ok(1);
        }
        let mut sync_count = 0; // 记录同步返回结束的数量
        for k in node.to() {
            let n = self.graph.get(k).unwrap();
            // println!("node: {:?}, count: {} from: {}", n.key(), n.load_count(), n.from_len());
            // 将所有的to节点的计数加1,如果计数为from_len, 则表示全部的依赖都就绪
            if n.add_count(1) + 1 != n.from_len() {
                //println!("node1: {:?}, count: {} ", n.key(), n.load_count());
                continue;
            }
            // 将状态置为0,创建新的AsyncGraphNode并执行
            n.set_count(0);

            let an = AsyncGraphNode::new(self.graph.clone(), k.clone(), self.producor.clone());

            sync_count += an.exec(rt.clone(), n, context)?;
        }

        Ok(sync_count)
    }
}

pub trait RunFactory {
    type R: Runner;
    fn create(&self) -> Self::R;
}

pub trait AsyncNode: Fn() -> BoxFuture<'static, Result<()>> + ThreadSync + 'static {}
impl<T: Fn() -> BoxFuture<'static, Result<()>> + ThreadSync + 'static> AsyncNode for T {}

pub enum ExecNode<Run: Runner, Fac: RunFactory<R = Run>> {
    None,
    Sync(Fac),
    Async(Box<dyn AsyncNode>),
}

impl<Run: Runner + ThreadSync + 'static, Fac: RunFactory<R = Run>> Runnble for ExecNode<Run, Fac> {
    type R = Run;
    type Context = Run::Context;

    fn is_sync(&self) -> Option<bool> {
        match self {
            ExecNode::None => None,
            ExecNode::Sync(_) => Some(true),
            _ => Some(false),
        }
    }
    /// 获得需要执行的同步函数
    fn get_sync(&self) -> Self::R {
        match self {
            ExecNode::Sync(r) => r.create(),
            _ => panic!(),
        }
    }
    /// 获得需要执行的异步块
    fn get_async(&self) -> BoxFuture<'static, Result<()>> {
        match self {
            ExecNode::Async(f) => f(),
            _ => panic!(),
        }
    }
}

#[test]
fn test_graph() {
    use futures::FutureExt;
    use pi_async::prelude::multi_thread::{MultiTaskRuntimeBuilder, StealableTaskPool};
    use pi_graph::NGraphBuilder;
    use std::time::Duration;

    struct A(usize);

    impl Runner for A {
        type Context = ();
        fn run(self, _: &'static Self::Context) {
            println!("A id:{}", self.0);
        }
    }

    struct B(usize);
    impl RunFactory for B {
        type R = A;
        fn create(&self) -> A {
            A(self.0)
        }
    }
    fn syn(id: usize) -> ExecNode<A, B> {
        ExecNode::Sync(B(id))
    }
    fn asyn(id: usize) -> ExecNode<A, B> {
        let f = move || -> BoxFuture<'static, Result<()>> {
            async move {
                println!("async id:{}", id);
                Ok(())
            }
            .boxed()
        };
        ExecNode::Async(Box::new(f))
    }

    let pool = MultiTaskRuntimeBuilder::<(), StealableTaskPool<()>>::default();
    let rt0 = pool.build();
    let rt1 = rt0.clone();
    let graph = NGraphBuilder::new()
        .node(1, asyn(1))
        .node(2, asyn(2))
        .node(3, syn(3))
        .node(4, asyn(4))
        .node(5, asyn(5))
        .node(6, asyn(6))
        .node(7, asyn(7))
        .node(8, asyn(8))
        .node(9, asyn(9))
        .node(10, ExecNode::None)
        .node(11, syn(11))
        .edge(1, 4)
        .edge(2, 4)
        .edge(2, 5)
        .edge(3, 5)
        .edge(4, 6)
        .edge(4, 7)
        .edge(5, 8)
        .edge(9, 10)
        .edge(10, 11)
        .build()
        .unwrap();

    let _ = rt0.spawn(rt0.alloc(), async move {
        let ag = Share::new(graph);
        let _: _ = async_graph(rt1, ag, &()).await;
        println!("ok");
    });
    std::thread::sleep(Duration::from_millis(5000));
}

#[test]
fn test() {
    use pi_async::prelude::multi_thread::{MultiTaskRuntimeBuilder, StealableTaskPool};
    use std::time::Duration;

    let pool = MultiTaskRuntimeBuilder::<(), StealableTaskPool<()>>::default();
    let rt0 = pool.build();
    let rt1 = rt0.clone();
    let _ = rt0.spawn(rt0.alloc(), async move {
        let mut map_reduce = rt1.map_reduce(10);
        let rt2 = rt1.clone();
        let rt3 = rt1.clone();
        let _ = map_reduce.map(rt1.clone(), async move {
            rt1.timeout(300).await;
            println!("1111");
            Ok(1)
        });

        let _ = map_reduce.map(rt2.clone(), async move {
            rt2.timeout(1000).await;
            println!("2222");
            Ok(2)
        });
        let _ = map_reduce.map(rt3.clone(), async move {
            rt3.timeout(600).await;
            println!("3333");
            Ok(3)
        });
        for r in map_reduce.reduce(true).await.unwrap() {
            println!("r: {:?}", r);
        }
    });
    std::thread::sleep(Duration::from_millis(5000));
}