Kokkos Node API and Local Linear Algebra Kernels Version of the Day
TbbTsqr_FillWithZerosTask.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_FillWithZerosTask_hpp
00043 #define __TSQR_TBB_FillWithZerosTask_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 
00058     template<class LocalOrdinal, class Scalar>
00059     class FillWithZerosTask : public tbb::task {
00060     private:
00061       typedef MatView<LocalOrdinal, Scalar> mat_view;
00062       typedef std::pair<mat_view, mat_view> split_type;
00063 
00064     public:
00065       FillWithZerosTask (const size_t P_first, 
00066        const size_t P_last,
00067        MatView<LocalOrdinal, Scalar> C,
00068        const SequentialTsqr<LocalOrdinal, Scalar>& seq,
00069        const bool contiguous_cache_blocks = false)
00070   : P_first_ (P_first), 
00071     P_last_ (P_last), 
00072     C_ (C),
00073     seq_ (seq), 
00074     contiguous_cache_blocks_ (contiguous_cache_blocks)
00075       {}
00076 
00077       tbb::task* execute () 
00078       {
00079   if (P_first_ > P_last_ || C_.empty())
00080     return NULL;
00081   else if (P_first_ == P_last_)
00082     {
00083       execute_base_case ();
00084       return NULL;
00085     }
00086   else
00087     {
00088       // Recurse on two intervals: [P_first, P_mid] and
00089       // [P_mid+1, P_last].
00090       const size_t P_mid = (P_first_ + P_last_) / 2;
00091       split_type C_split = 
00092         partitioner_.split (C_, P_first_, P_mid, P_last_, 
00093           contiguous_cache_blocks_);
00094       // The partitioner may decide that the current block C_
00095       // has too few rows to be worth splitting.  In that case,
00096       // C_split.second (the bottom block) will be empty.  We
00097       // can deal with this by treating it as the base case.
00098       if (C_split.second.empty() || C_split.second.nrows() == 0)
00099         {
00100     execute_base_case ();
00101     return NULL;
00102         }
00103 
00104       // "c": continuation task
00105       tbb::empty_task& c = 
00106         *new( allocate_continuation() ) tbb::empty_task;
00107       // Recurse on the split
00108       FillWithZerosTask& topTask = *new( c.allocate_child() )
00109         FillWithZerosTask (P_first_, P_mid, C_split.first, seq_, 
00110          contiguous_cache_blocks_);
00111       FillWithZerosTask& botTask = *new( c.allocate_child() )
00112         FillWithZerosTask (P_mid+1, P_last_, C_split.second, seq_, 
00113          contiguous_cache_blocks_);
00114       // Set reference count of parent (in this case, the
00115       // continuation task) to 2 (since 2 children -- no
00116       // additional task since no waiting).
00117       c.set_ref_count (2);
00118       c.spawn (botTask);
00119       return &topTask; // scheduler bypass optimization
00120     }
00121       }
00122 
00123     private:
00124       size_t P_first_, P_last_;
00125       mat_view C_;
00126       SequentialTsqr<LocalOrdinal, Scalar> seq_;
00127       Partitioner<LocalOrdinal, Scalar> partitioner_;
00128       bool contiguous_cache_blocks_;
00129 
00130       void 
00131       execute_base_case ()
00132       {
00133   // Fill my partition with zeros.
00134   seq_.fill_with_zeros (C_.nrows(), C_.ncols(), C_.get(), 
00135             C_.lda(), contiguous_cache_blocks_);
00136       }
00137     };
00138   } // namespace TBB
00139 } // namespace TSQR
00140 
00141 
00142 #endif // __TSQR_TBB_FillWithZerosTask_hpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends