summaryrefslogtreecommitdiff
path: root/wasserstein/include/dnn/parallel/tbb.h
blob: 20e886e8ce8fa6eb4a1c1ceff456a38c0fcc9024 (plain)
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
#ifndef HERA_WS_PARALLEL_H
#define HERA_WS_PARALLEL_H

#include <vector>

#include <boost/range.hpp>
#include <boost/bind/bind.hpp>
#include <boost/foreach.hpp>

#ifdef TBB

#include <tbb/tbb.h>
#include <tbb/concurrent_hash_map.h>
#include <tbb/scalable_allocator.h>

#include <boost/serialization/split_free.hpp>
#include <boost/serialization/collections_load_imp.hpp>
#include <boost/serialization/collections_save_imp.hpp>

using namespace boost::placeholders;

namespace hera
{
namespace ws
{
namespace dnn
{
    using tbb::mutex;
    using tbb::task_scheduler_init;
    using tbb::task_group;
    using tbb::task;

    template<class T>
    struct vector
    {
        typedef         tbb::concurrent_vector<T>               type;
    };

    template<class T>
    struct atomic
    {
        typedef         tbb::atomic<T>                          type;
        static T        compare_and_swap(type& v, T n, T o)     { return v.compare_and_swap(n,o); }
    };

    template<class Iterator, class F>
    void                do_foreach(Iterator begin, Iterator end, const F& f)            { tbb::parallel_do(begin, end, f); }

    template<class Range, class F>
    void                for_each_range_(const Range& r, const F& f)
    {
        for (typename Range::iterator cur = r.begin(); cur != r.end(); ++cur)
            f(*cur);
    }

    template<class F>
    void                for_each_range(size_t from, size_t to, const F& f)
    {
        //static tbb::affinity_partitioner ap;
        //tbb::parallel_for(c.range(), boost::bind(&for_each_range_<typename Container::range_type, F>, _1, f), ap);
        tbb::parallel_for(from, to, f);
    }

    template<class Container, class F>
    void                for_each_range(const Container& c, const F& f)
    {
        //static tbb::affinity_partitioner ap;
        //tbb::parallel_for(c.range(), boost::bind(&for_each_range_<typename Container::range_type, F>, _1, f), ap);
        tbb::parallel_for(c.range(), boost::bind(&for_each_range_<typename Container::const_range_type, F>, _1, f));
    }

    template<class Container, class F>
    void                for_each_range(Container& c, const F& f)
    {
        //static tbb::affinity_partitioner ap;
        //tbb::parallel_for(c.range(), boost::bind(&for_each_range_<typename Container::range_type, F>, _1, f), ap);
        tbb::parallel_for(c.range(), boost::bind(&for_each_range_<typename Container::range_type, F>, _1, f));
    }

    template<class ID, class NodePointer, class IDTraits, class Allocator>
    struct map_traits
    {
        typedef         tbb::concurrent_hash_map<ID, NodePointer, IDTraits, Allocator>              type;
        typedef         typename type::range_type                                                   range;
    };

    struct progress_timer
    {
                        progress_timer(): start(tbb::tick_count::now())                 {}
                        ~progress_timer()
                        { std::cout << (tbb::tick_count::now() - start).seconds() << " s" << std::endl; }

        tbb::tick_count start;
    };
} // dnn
} // ws
} // hera

// Serialization for tbb::concurrent_vector<...>
namespace boost
{
    namespace serialization
    {
        template<class Archive, class T, class A>
        void save(Archive& ar, const tbb::concurrent_vector<T,A>& v, const unsigned int file_version)
        { stl::save_collection(ar, v); }

        template<class Archive, class T, class A>
        void load(Archive& ar, tbb::concurrent_vector<T,A>& v, const unsigned int file_version)
        {
            stl::load_collection<Archive,
                                 tbb::concurrent_vector<T,A>,
                                 stl::archive_input_seq< Archive, tbb::concurrent_vector<T,A> >,
                                 stl::reserve_imp< tbb::concurrent_vector<T,A> >
                                >(ar, v);
        }

        template<class Archive, class T, class A>
        void serialize(Archive& ar, tbb::concurrent_vector<T,A>& v, const unsigned int file_version)
        { split_free(ar, v, file_version); }

        template<class Archive, class T>
        void save(Archive& ar, const tbb::atomic<T>& v, const unsigned int file_version)
        { T v_ = v; ar << v_; }

        template<class Archive, class T>
        void load(Archive& ar, tbb::atomic<T>& v, const unsigned int file_version)
        { T v_; ar >> v_; v = v_; }

        template<class Archive, class T>
        void serialize(Archive& ar, tbb::atomic<T>& v, const unsigned int file_version)
        { split_free(ar, v, file_version); }
    }
}

#else

#include <algorithm>
#include <map>

namespace hera
{
namespace ws
{
namespace dnn
{
    template<class T>
    struct vector
    {
        typedef         ::std::vector<T>                        type;
    };

    template<class T>
    struct atomic
    {
        typedef         T                                       type;
        static T        compare_and_swap(type& v, T n, T o)     { if (v != o) return v; v = n; return o; }
    };

    template<class Iterator, class F>
    void                do_foreach(Iterator begin, Iterator end, const F& f)    { std::for_each(begin, end, f); }

    template<class F>
    void                for_each_range(size_t from, size_t to, const F& f)
    {
        for (size_t i = from; i < to; ++i)
            f(i);
    }

    template<class Container, class F>
    void                for_each_range(Container& c, const F& f)
    {
        BOOST_FOREACH(const typename Container::value_type& i, c)
            f(i);
    }

    template<class Container, class F>
    void                for_each_range(const Container& c, const F& f)
    {
        BOOST_FOREACH(const typename Container::value_type& i, c)
            f(i);
    }

    struct mutex
    {
        struct scoped_lock
        {
                        scoped_lock()                   {}
                        scoped_lock(mutex& )            {}
            void        acquire(mutex& ) const          {}
            void        release() const                 {}
        };
    };

    struct task_scheduler_init
    {
                        task_scheduler_init(unsigned)   {}
        void            initialize(unsigned)            {}
        static const unsigned automatic = 0;
        static const unsigned deferred  = 0;
    };

    struct task_group
    {
        template<class Functor>
        void    run(const Functor& f) const             { f(); }
        void    wait() const                            {}
    };

    template<class ID, class NodePointer, class IDTraits, class Allocator>
    struct map_traits
    {
        typedef         std::map<ID, NodePointer,
                                 typename IDTraits::Comparison,
                                 Allocator>                                             type;
        typedef         type                                                            range;
    };

} // dnn
} // ws
} // hera

#endif // TBB

namespace hera
{
namespace ws
{
namespace dnn
{
    template<class Range, class F>
    void                do_foreach(const Range& range, const F& f)                      { do_foreach(boost::begin(range), boost::end(range), f); }
} // dnn
} // ws
} // hera

#endif