Kokkos Node API and Local Linear Algebra Kernels Version of the Day
TbbTsqr_CacheBlockTask.hpp
00001 //@HEADER
00002 // ************************************************************************
00003 // 
00004 //          Kokkos: Node API and Parallel Node Kernels
00005 //              Copyright (2008) Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 // 
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00038 // 
00039 // ************************************************************************
00040 //@HEADER
00041 
00042 #ifndef __TSQR_TBB_CacheBlockTask_hpp
00043 #define __TSQR_TBB_CacheBlockTask_hpp
00044 
00045 #include <tbb/task.h>
00046 #include <TbbTsqr_Partitioner.hpp>
00047 #include <Tsqr_SequentialTsqr.hpp>
00048 
00051 
00052 namespace TSQR {
00053   namespace TBB {
00054 
00062     template<class LocalOrdinal, class Scalar>
00063     class CacheBlockTask : public tbb::task {
00064     public:
00065       typedef MatView<LocalOrdinal, Scalar> mat_view;
00066       typedef ConstMatView<LocalOrdinal, Scalar> const_mat_view;
00067       typedef std::pair<mat_view, mat_view> split_t;
00068       typedef std::pair<const_mat_view, const_mat_view> const_split_t;
00069 
00070       CacheBlockTask (const size_t P_first__, 
00071           const size_t P_last__,
00072           mat_view& A_out,
00073           const_mat_view& A_in,
00074           const SequentialTsqr<LocalOrdinal, Scalar>& seq) :
00075   P_first_ (P_first__), 
00076   P_last_ (P_last__), 
00077   A_out_ (A_out), 
00078   A_in_ (A_in), 
00079   seq_ (seq)
00080       {}
00081 
00082       tbb::task* execute () 
00083       {
00084   using tbb::task;
00085 
00086   if (P_first_ > P_last_ || A_out_.empty() || A_in_.empty())
00087     return NULL;
00088   else if (P_first_ == P_last_)
00089     {
00090       execute_base_case ();
00091       return NULL;
00092     }
00093   else
00094     {
00095       // Recurse on two intervals: [P_first, P_mid] and
00096       // [P_mid+1, P_last].
00097       const size_t P_mid = (P_first_ + P_last_) / 2;
00098       split_t out_split = 
00099         partitioner_.split (A_out_, P_first_, P_mid, P_last_, true);
00100       const_split_t in_split = 
00101         partitioner_.split (A_in_, P_first_, P_mid, P_last_, false);
00102 
00103       // The partitioner may decide that the current blocks
00104       // A_out_ and A_in_ have too few rows to be worth
00105       // splitting.  (It should split both A_out_ and A_in_ in
00106       // the same way.)  In that case, out_split.second and
00107       // in_split.second (the bottom block) will be empty.  We
00108       // can deal with this by treating it as the base case.
00109       if (out_split.second.empty() || out_split.second.nrows() == 0)
00110         {
00111     execute_base_case ();
00112     return NULL;
00113         }
00114 
00115       // "c": continuation task
00116       tbb::empty_task& c = 
00117         *new( allocate_continuation() ) tbb::empty_task;
00118       // Recurse on the split
00119       CacheBlockTask& topTask = *new( c.allocate_child() )
00120         CacheBlockTask (P_first_, P_mid, out_split.first, 
00121             in_split.first, seq_);
00122       CacheBlockTask& botTask = *new( c.allocate_child() )
00123         CacheBlockTask (P_mid+1, P_last_, out_split.second, 
00124             in_split.second, seq_);
00125       // Set reference count of parent (in this case, the
00126       // continuation task) to 2 (since 2 children -- no
00127       // additional task since no waiting).
00128       c.set_ref_count (2);
00129       c.spawn (botTask);
00130       return &topTask; // scheduler bypass optimization
00131     }
00132       }
00133 
00134     private:
00135       size_t P_first_, P_last_;
00136       mat_view A_out_;
00137       const_mat_view A_in_;
00138       SequentialTsqr<LocalOrdinal, Scalar> seq_;
00139       Partitioner<LocalOrdinal, Scalar> partitioner_;
00140 
00141       void 
00142       execute_base_case ()
00143       {
00144   seq_.cache_block (A_out_.nrows(), A_out_.ncols(), 
00145         A_out_.get(), A_in_.get(), A_in_.lda());
00146       }
00147     };
00148 
00149   } // namespace TBB
00150 } // namespace TSQR
00151 
00152 
00153 #endif // __TSQR_TBB_CacheBlockTask_hpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends