#ifndef HERA_WS_PARALLEL_H #define HERA_WS_PARALLEL_H #include #include #include #include #ifdef TBB #include #include #include #include #include #include namespace hera { namespace ws { namespace dnn { using tbb::mutex; using tbb::task_scheduler_init; using tbb::task_group; using tbb::task; template struct vector { typedef tbb::concurrent_vector type; }; template struct atomic { typedef tbb::atomic type; static T compare_and_swap(type& v, T n, T o) { return v.compare_and_swap(n,o); } }; template void do_foreach(Iterator begin, Iterator end, const F& f) { tbb::parallel_do(begin, end, f); } template void for_each_range_(const Range& r, const F& f) { for (typename Range::iterator cur = r.begin(); cur != r.end(); ++cur) f(*cur); } template 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_, _1, f), ap); tbb::parallel_for(from, to, f); } template 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_, _1, f), ap); tbb::parallel_for(c.range(), boost::bind(&for_each_range_, _1, f)); } template void for_each_range(Container& c, const F& f) { //static tbb::affinity_partitioner ap; //tbb::parallel_for(c.range(), boost::bind(&for_each_range_, _1, f), ap); tbb::parallel_for(c.range(), boost::bind(&for_each_range_, _1, f)); } template struct map_traits { typedef tbb::concurrent_hash_map 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 void save(Archive& ar, const tbb::concurrent_vector& v, const unsigned int file_version) { stl::save_collection(ar, v); } template void load(Archive& ar, tbb::concurrent_vector& v, const unsigned int file_version) { stl::load_collection, stl::archive_input_seq< Archive, tbb::concurrent_vector >, stl::reserve_imp< tbb::concurrent_vector > >(ar, v); } template void serialize(Archive& ar, tbb::concurrent_vector& v, const unsigned int file_version) { split_free(ar, v, file_version); } template void save(Archive& ar, const tbb::atomic& v, const unsigned int file_version) { T v_ = v; ar << v_; } template void load(Archive& ar, tbb::atomic& v, const unsigned int file_version) { T v_; ar >> v_; v = v_; } template void serialize(Archive& ar, tbb::atomic& v, const unsigned int file_version) { split_free(ar, v, file_version); } } } #else #include #include #include namespace hera { namespace ws { namespace dnn { template struct vector { typedef ::std::vector type; }; template 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 void do_foreach(Iterator begin, Iterator end, const F& f) { std::for_each(begin, end, f); } template void for_each_range(size_t from, size_t to, const F& f) { for (size_t i = from; i < to; ++i) f(i); } template void for_each_range(Container& c, const F& f) { BOOST_FOREACH(const typename Container::value_type& i, c) f(i); } template 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 void run(const Functor& f) const { f(); } void wait() const {} }; template struct map_traits { typedef std::map type; typedef type range; }; using boost::progress_timer; } // dnn } // ws } // hera #endif // TBB namespace hera { namespace ws { namespace dnn { template void do_foreach(const Range& range, const F& f) { do_foreach(boost::begin(range), boost::end(range), f); } } // dnn } // ws } // hera #endif