コード例 #1
0
ファイル: parallel_for.hpp プロジェクト: CCJY/asynchronous
 void operator()()const
 {
     boost::asynchronous::continuation_result<void> task_res = this_task_result();
     // advance up to cutoff
     Iterator it = boost::asynchronous::detail::find_cutoff(beg_,cutoff_,end_);
     // if not at end, recurse, otherwise execute here
     if (it == end_)
     {
         std::for_each(beg_,it,func_);
         task_res.set_value();
     }
     else
     {
         boost::asynchronous::create_callback_continuation_job<Job>(
                     // called when subtasks are done, set our result
                     [task_res](std::tuple<boost::asynchronous::expected<void>,boost::asynchronous::expected<void> > res)
                     {
                         try
                         {
                             // get to check that no exception
                             std::get<0>(res).get();
                             std::get<1>(res).get();
                             task_res.set_value();
                         }
                         catch(std::exception& e)
                         {
                             task_res.set_exception(boost::copy_exception(e));
                         }
                     },
                     // recursive tasks
                     parallel_for_helper<Iterator,Func,Job>(beg_,it,func_,cutoff_,this->get_name(),prio_),
                     parallel_for_helper<Iterator,Func,Job>(it,end_,func_,cutoff_,this->get_name(),prio_)
            );
     }
 }
コード例 #2
0
 void operator()()
 {
     boost::asynchronous::continuation_result<bool> task_res = this_task_result();
     try
     {
         // advance up to cutoff
         Iterator it = boost::asynchronous::detail::find_cutoff(beg_,cutoff_,end_);
         if (it == end_)
         {
             task_res.set_value(std::is_sorted(beg_,end_,func_));
         }
         else
         {
             // optimize for not sorted range
             auto it2 = it;
             std::advance(it2,-1);
             // TODO safe_advance must handle negative distance
             if (func_(*it,*it2))
             {
                 task_res.set_value(false);
                 return;
             }
             boost::asynchronous::create_callback_continuation_job<Job>(
                         // called when subtasks are done, set our result
                         [task_res](std::tuple<boost::asynchronous::expected<bool>,boost::asynchronous::expected<bool> > res) mutable
                         {
                             try
                             {
                                 // get to check that no exception
                                 bool res1 = std::get<0>(res).get();
                                 bool res2 = std::get<1>(res).get();
                                 task_res.set_value(res1 && res2);
                             }
                             catch(...)
                             {
                                 task_res.set_exception(std::current_exception());
                             }
                         },
                         // recursive tasks
                         parallel_is_sorted_helper<Iterator,Func,Job>
                             (beg_,it,func_,cutoff_,this->get_name(),prio_),
                         parallel_is_sorted_helper<Iterator,Func,Job>
                             (it,end_,func_,cutoff_,this->get_name(),prio_)
             );
         }
     }
     catch(...)
     {
         task_res.set_exception(std::current_exception());
     }
 }
コード例 #3
0
 void operator()()
 {
     boost::asynchronous::continuation_result<void> task_res = this_task_result();
     // advance up to cutoff
     Iterator it = boost::asynchronous::detail::find_cutoff(beg_reverse_,cutoff_,end_reverse_);
     if (it == end_reverse_)
     {
         auto end_swap = end_;
         std::advance(end_swap,-(std::distance(beg_,beg_reverse_)+1));
         for (auto it2 = beg_reverse_; it2 != it; ++it2)
         {
             std::iter_swap(it2,end_swap);
             --end_swap;
         }
         task_res.set_value();
     }
     else
     {
         boost::asynchronous::create_callback_continuation_job<Job>(
                     // called when subtasks are done, set our result
                     [task_res](std::tuple<boost::asynchronous::expected<void>,boost::asynchronous::expected<void> > res)
                     {
                         try
                         {
                             // get to check that no exception
                             std::get<0>(res).get();
                             std::get<1>(res).get();
                             task_res.set_value();
                         }
                         catch(std::exception& e)
                         {
                             task_res.set_exception(boost::copy_exception(e));
                         }
                     },
                     // recursive tasks
                     parallel_reverse_helper<Iterator,Job>
                         (beg_,end_,beg_reverse_,it,cutoff_,this->get_name(),prio_),
                     parallel_reverse_helper<Iterator,Job>
                         (beg_,end_,it,end_reverse_,cutoff_,this->get_name(),prio_)
         );
     }
 }
コード例 #4
0
 void operator()()
 {
     boost::asynchronous::continuation_result<void> task_res = this_task_result();
     
     // Advance iterator up to the cutoff
     Iterator it = boost::asynchronous::detail::find_cutoff(beg_, cutoff_, end_);
     // If the end is reached, use this thread to execute the subtask, otherwise recurse
     if (it == end_)
     {
         std::iota(beg_, it, start_);
         task_res.set_value();
     }
     else
     {
         auto distance = std::distance(beg_, it);
         boost::asynchronous::create_callback_continuation_job<Job>(
                     // Callback
                     [task_res]
                     (std::tuple<boost::asynchronous::expected<void>, boost::asynchronous::expected<void> > res) mutable
                     {
                         try
                         {
                             // Call get() to check for exceptions
                             std::get<0>(res).get();
                             std::get<1>(res).get();
                             task_res.set_value();
                         }
                         catch (std::exception& e)
                         {
                             task_res.set_exception(boost::copy_exception(e));
                         }
                     },
                     // recursive tasks
                     parallel_iota_inplace_helper<Iterator, Job>(beg_, it, start_, cutoff_, this->get_name(), prio_),
                     parallel_iota_inplace_helper<Iterator, Job>(it, end_, start_ + distance, cutoff_, this->get_name(), prio_)
            );
     }
 }
コード例 #5
0
 void operator()()const
 {
     // the result of this task, will be either set directly if < cutoff, otherwise when taks is ready
     boost::asynchronous::continuation_result<long> task_res = this_task_result();
     if (n_<cutoff_)
     {
         // n < cutoff => execute ourselves
         task_res.set_value(serial_fib(n_));
     }
     else
     {
         // n> cutoff, create 2 new tasks and when both are done, set our result (res(task1) + res(task2))
         boost::asynchronous::create_continuation_job<boost::asynchronous::any_serializable>(
                     // called when subtasks are done, set our result
                     [task_res](std::tuple<boost::future<long>,boost::future<long> > res)
                     {
                         long r = std::get<0>(res).get() + std::get<1>(res).get();
                         task_res.set_value(r);
                     },
                     // recursive tasks
                     fib_task(n_-1,cutoff_),
                     fib_task(n_-2,cutoff_));
     }
 }