Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Delete now useless xbt_dynar_three_way_partition().
[simgrid.git] / src / xbt / parmap.cpp
1 /* Copyright (c) 2004-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "xbt/parmap.h"
7 #include "xbt/log.h"
8 #include "xbt/parmap.hpp"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_parmap, xbt, "parmap: parallel map");
11
12 /**
13  * \brief Parallel map structure
14  */
15 typedef struct s_xbt_parmap {
16   simgrid::xbt::Parmap<void*> p;
17 } s_xbt_parmap_t;
18
19 /**
20  * \brief Creates a parallel map object
21  * \param num_workers number of worker threads to create
22  * \param mode how to synchronize the worker threads
23  * \return the parmap created
24  */
25 xbt_parmap_t xbt_parmap_new(unsigned int num_workers, e_xbt_parmap_mode_t mode)
26 {
27   return reinterpret_cast<xbt_parmap_t>(new simgrid::xbt::Parmap<void*>(num_workers, mode));
28 }
29
30 /**
31  * \brief Destroys a parmap
32  * \param parmap the parmap to destroy
33  */
34 void xbt_parmap_destroy(xbt_parmap_t parmap)
35 {
36   delete parmap;
37 }
38
39 /**
40  * \brief Applies a list of tasks in parallel.
41  * \param parmap a parallel map object
42  * \param fun the function to call in parallel
43  * \param data each element of this dynar will be passed as an argument to fun
44  */
45 void xbt_parmap_apply(xbt_parmap_t parmap, void_f_pvoid_t fun, xbt_dynar_t data)
46 {
47   if (!xbt_dynar_is_empty(data)) {
48     void** pdata = (void**)xbt_dynar_get_ptr(data, 0);
49     std::vector<void*> vdata(pdata, pdata + xbt_dynar_length(data));
50     parmap->p.apply(fun, vdata);
51   }
52 }
53
54 /**
55  * \brief Returns a next task to process.
56  *
57  * Worker threads call this function to get more work.
58  *
59  * \return the next task to process, or nullptr if there is no more work
60  */
61 void* xbt_parmap_next(xbt_parmap_t parmap)
62 {
63   try {
64     return parmap->p.next();
65   } catch (std::out_of_range) {
66     return nullptr;
67   }
68 }