Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_CrsMatrix_CuSparse.hpp
Go to the documentation of this file.
1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 // Kokkos: Node API and Parallel Node Kernels
6 // Copyright (2008) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 #ifndef KOKKOS_CRSMATRIX_CUSPARSE_HPP_
44 #define KOKKOS_CRSMATRIX_CUSPARSE_HPP_
45 
48 
49 #include <impl/Kokkos_PhysicalLayout.hpp>
50 
51 namespace Kokkos {
58 namespace CuSparse {
59 
69 template<typename T, class RangeVectorType, class CrsMatrixType, class DomainVectorType>
70 bool
71 MV_Multiply_DoCuSparse (typename Kokkos::Impl::enable_if<! Kokkos::Impl::is_same<T, double>::value && ! Kokkos::Impl::is_same<T, float>::value, typename RangeVectorType::value_type>::type s_b, const RangeVectorType& y, typename DomainVectorType::value_type s_a, const CrsMatrixType& A , const DomainVectorType& x) {
72  return false;
73 }
74 
75 template<typename T, class RangeVector,class CrsMatrix,class DomainVector>
76 bool
77 MV_Multiply_DoCuSparse (typename Kokkos::Impl::enable_if<Kokkos::Impl::is_same<T, double>::value, double>::type s_b,
78  const RangeVector& y,
79  double s_a,
80  const CrsMatrix& A,
81  const DomainVector& x)
82 {
83 
84  if (x.dimension_1 () == 1) {
85  cusparseDcsrmv (A.cusparse_handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
86  A.numRows(), A.numCols(), A.nnz(),
87  &s_a,
88  A.cusparse_descr,
89  A.values.ptr_on_device(),
90  (const int*) A.graph.row_map.ptr_on_device(),
91  A.graph.entries.ptr_on_device(),
92  x.ptr_on_device(),
93  &s_b,
94  y.ptr_on_device());
95  } else {
96  Impl::PhysicalLayout layout_x (x);
97  Impl::PhysicalLayout layout_y (y);
98  if ((layout_x.layout_type != layout_x.Left) || layout_y.layout_type != layout_y.Left) {
99  return false;
100  }
101  cusparseDcsrmm (A.cusparse_handle, CUSPARSE_OPERATION_NON_TRANSPOSE,
102  A.numRows(), x.dimension_1(), A.numCols(), A.nnz(),
103  &s_a,
104  A.cusparse_descr,
105  A.values.ptr_on_device(),
106  (const int*) A.graph.row_map.ptr_on_device(),
107  A.graph.entries.ptr_on_device(),
108  x.ptr_on_device(),
109  layout_x.stride[1],
110  &s_b,
111  y.ptr_on_device(),
112  layout_y.stride[1]);
113  }
114  return true;
115 }
116 
117 template<typename T, class RangeVector,class CrsMatrix,class DomainVector>
118 bool MV_Multiply_DoCuSparse(typename Kokkos::Impl::enable_if<Kokkos::Impl::is_same<T,float>::value, float >::type s_b
119  ,const RangeVector & y, float s_a,
120  const CrsMatrix & A , const DomainVector & x) {
121  if(x.dimension_1()==1) {
122  cusparseScsrmv(A.cusparse_handle,CUSPARSE_OPERATION_NON_TRANSPOSE,
123  A.numRows(), A.numCols(), A.nnz(),
124  &s_a,
125  A.cusparse_descr,
126  A.values.ptr_on_device(),
127  (const int*) A.graph.row_map.ptr_on_device(),
128  A.graph.entries.ptr_on_device(),
129  x.ptr_on_device(),
130  &s_b,
131  y.ptr_on_device());
132  } else {
133  Impl::PhysicalLayout layout_x(x);
134  Impl::PhysicalLayout layout_y(y);
135  if((layout_x.layout_type!=layout_x.Left) || layout_y.layout_type!=layout_y.Left) return false;
136  cusparseScsrmm(A.cusparse_handle,CUSPARSE_OPERATION_NON_TRANSPOSE,
137  A.numRows(), x.dimension_1(), A.numCols(), A.nnz(),
138  &s_a,
139  A.cusparse_descr,
140  A.values.ptr_on_device(),
141  (const int*) A.graph.row_map.ptr_on_device(),
142  A.graph.entries.ptr_on_device(),
143  x.ptr_on_device(),
144  layout_x.stride[1],
145  &s_b,
146  y.ptr_on_device(),
147  layout_y.stride[1]);
148  }
149  return true;
150 }
151 
152 //ToDo: strip compatible type attributes (const, volatile); make type of s_b and s_a independent
153 template<class RangeVector,class CrsMatrix,class DomainVector>
154 bool
155 MV_Multiply_Try_CuSparse (typename RangeVector::value_type s_b,
156  const RangeVector& y,
157  typename DomainVector::value_type s_a,
158  const CrsMatrix& A,
159  const DomainVector& x)
160 {
161  if(!Kokkos::Impl::is_same<typename RangeVector::device_type,typename Kokkos::Cuda>::value) return false;
162  if(Kokkos::Impl::is_same<typename RangeVector::non_const_value_type,float>::value&&
163  Kokkos::Impl::is_same<typename DomainVector::non_const_value_type,float>::value&&
164  Kokkos::Impl::is_same<typename CrsMatrix::values_type::non_const_value_type,float>::value) {
165  return MV_Multiply_DoCuSparse<typename RangeVector::value_type,RangeVector,CrsMatrix,DomainVector>(s_b,y,s_a,A,x);
166  } else
167  if(Kokkos::Impl::is_same<typename RangeVector::non_const_value_type,double>::value&&
168  Kokkos::Impl::is_same<typename DomainVector::non_const_value_type,double>::value&&
169  Kokkos::Impl::is_same<typename CrsMatrix::values_type::non_const_value_type,double>::value) {
170  return MV_Multiply_DoCuSparse<typename RangeVector::value_type,RangeVector,CrsMatrix,DomainVector>(s_b,y,s_a,A,x);
171  } else
172  return false;
173 }
174 
175 } // namespace CuSparse
176 } // namespace Kokkos
177 
178 #endif // KOKKOS_CRSMATRIX_CUSPARSE_HPP_