00001 #ifndef VIENNACL_COORDINATE_MATRIX_OPERATIONS_HPP_
00002 #define VIENNACL_COORDINATE_MATRIX_OPERATIONS_HPP_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00024 #include "viennacl/forwards.h"
00025 #include "viennacl/ocl/device.hpp"
00026 #include "viennacl/ocl/handle.hpp"
00027 #include "viennacl/ocl/kernel.hpp"
00028 #include "viennacl/scalar.hpp"
00029 #include "viennacl/vector.hpp"
00030 #include "viennacl/tools/tools.hpp"
00031 #include "viennacl/linalg/kernels/coordinate_matrix_kernels.h"
00032
00033 namespace viennacl
00034 {
00035 namespace linalg
00036 {
00037
00038
00039
00047 template<class SCALARTYPE, unsigned int ALIGNMENT, unsigned int VECTOR_ALIGNMENT>
00048 vector_expression<const coordinate_matrix<SCALARTYPE, ALIGNMENT>,
00049 const vector<SCALARTYPE, VECTOR_ALIGNMENT>,
00050 op_prod > prod_impl(const coordinate_matrix<SCALARTYPE, ALIGNMENT> & mat,
00051 const vector<SCALARTYPE, VECTOR_ALIGNMENT> & vec)
00052 {
00053 return vector_expression<const coordinate_matrix<SCALARTYPE, ALIGNMENT>,
00054 const vector<SCALARTYPE, VECTOR_ALIGNMENT>,
00055 op_prod >(mat, vec);
00056 }
00057
00058
00067 template<class SCALARTYPE, unsigned int ALIGNMENT, unsigned int VECTOR_ALIGNMENT>
00068 viennacl::vector_expression<const viennacl::coordinate_matrix<SCALARTYPE, ALIGNMENT>,
00069 const viennacl::vector<SCALARTYPE, VECTOR_ALIGNMENT>,
00070 viennacl::op_prod > prod_impl(const viennacl::coordinate_matrix<SCALARTYPE, ALIGNMENT> & mat,
00071 const viennacl::vector<SCALARTYPE, VECTOR_ALIGNMENT> & vec,
00072 size_t NUM_THREADS)
00073 {
00074 return viennacl::vector_expression<const viennacl::coordinate_matrix<SCALARTYPE, ALIGNMENT>,
00075 const viennacl::vector<SCALARTYPE, VECTOR_ALIGNMENT>,
00076 viennacl::op_prod >(mat, vec);
00077 }
00078
00079
00088 template<class TYPE, unsigned int ALIGNMENT, unsigned int VECTOR_ALIGNMENT>
00089 void prod_impl(const viennacl::coordinate_matrix<TYPE, ALIGNMENT> & mat,
00090 const viennacl::vector<TYPE, VECTOR_ALIGNMENT> & vec,
00091 viennacl::vector<TYPE, VECTOR_ALIGNMENT> & result)
00092 {
00093 assert(mat.size1() == result.size());
00094 assert(mat.size2() == vec.size());
00095 result.clear();
00096
00097
00098
00099 viennacl::ocl::kernel & k = viennacl::ocl::get_kernel(viennacl::linalg::kernels::coordinate_matrix<TYPE, ALIGNMENT>::program_name(), "vec_mul");
00100 unsigned int thread_num = 256;
00101
00102 k.local_work_size(0, thread_num);
00103
00104 k.global_work_size(0, 64 * thread_num);
00105
00106 viennacl::ocl::enqueue(k(mat.handle12(), mat, mat.handle3(),
00107 vec,
00108 result,
00109 viennacl::ocl::local_mem(sizeof(cl_uint)*thread_num),
00110 viennacl::ocl::local_mem(sizeof(TYPE)*thread_num)) );
00111
00112 }
00113
00114
00115 }
00116
00117
00118
00123 template <typename SCALARTYPE, unsigned int ALIGNMENT>
00124 template <unsigned int MAT_ALIGNMENT>
00125 viennacl::vector<SCALARTYPE, ALIGNMENT> &
00126 viennacl::vector<SCALARTYPE, ALIGNMENT>::operator=(const viennacl::vector_expression< const coordinate_matrix<SCALARTYPE, MAT_ALIGNMENT>,
00127 const viennacl::vector<SCALARTYPE, ALIGNMENT>,
00128 viennacl::op_prod> & proxy)
00129 {
00130
00131 if (proxy.rhs().handle() == this->handle())
00132 {
00133 viennacl::vector<SCALARTYPE, ALIGNMENT> result(proxy.rhs().size());
00134 viennacl::linalg::prod_impl(proxy.lhs(), proxy.rhs(), result);
00135 *this = result;
00136 return *this;
00137 }
00138 else
00139 {
00140 viennacl::linalg::prod_impl(proxy.lhs(), proxy.rhs(), *this);
00141 return *this;
00142 }
00143 return *this;
00144 }
00145
00146
00151 template <typename SCALARTYPE, unsigned int ALIGNMENT>
00152 template <unsigned int MAT_ALIGNMENT>
00153 viennacl::vector<SCALARTYPE, ALIGNMENT> &
00154 viennacl::vector<SCALARTYPE, ALIGNMENT>::operator+=(const vector_expression< const coordinate_matrix<SCALARTYPE, MAT_ALIGNMENT>,
00155 const vector<SCALARTYPE, ALIGNMENT>,
00156 op_prod> & proxy)
00157 {
00158 vector<SCALARTYPE, ALIGNMENT> result(proxy.lhs().size1());
00159 viennacl::linalg::prod_impl(proxy.lhs(), proxy.rhs(), result);
00160 *this += result;
00161 return *this;
00162 }
00163
00168 template <typename SCALARTYPE, unsigned int ALIGNMENT>
00169 template <unsigned int MAT_ALIGNMENT>
00170 viennacl::vector<SCALARTYPE, ALIGNMENT> &
00171 viennacl::vector<SCALARTYPE, ALIGNMENT>::operator-=(const vector_expression< const coordinate_matrix<SCALARTYPE, MAT_ALIGNMENT>,
00172 const vector<SCALARTYPE, ALIGNMENT>,
00173 op_prod> & proxy)
00174 {
00175 vector<SCALARTYPE, ALIGNMENT> result(proxy.get_lhs().size1());
00176 viennacl::linalg::prod_impl(proxy.lhs(), proxy.rhs(), result);
00177 *this -= result;
00178 return *this;
00179 }
00180
00181
00182
00187 template <typename SCALARTYPE, unsigned int ALIGNMENT>
00188 template <unsigned int MAT_ALIGNMENT>
00189 viennacl::vector<SCALARTYPE, ALIGNMENT>
00190 viennacl::vector<SCALARTYPE, ALIGNMENT>::operator+(const vector_expression< const coordinate_matrix<SCALARTYPE, MAT_ALIGNMENT>,
00191 const vector<SCALARTYPE, ALIGNMENT>,
00192 op_prod> & proxy)
00193 {
00194 assert(proxy.get_lhs().size1() == size());
00195 vector<SCALARTYPE, ALIGNMENT> result(size());
00196 viennacl::linalg::prod_impl(proxy.lhs(), proxy.rhs(), result);
00197 result += *this;
00198 return result;
00199 }
00200
00205 template <typename SCALARTYPE, unsigned int ALIGNMENT>
00206 template <unsigned int MAT_ALIGNMENT>
00207 viennacl::vector<SCALARTYPE, ALIGNMENT>
00208 viennacl::vector<SCALARTYPE, ALIGNMENT>::operator-(const vector_expression< const coordinate_matrix<SCALARTYPE, MAT_ALIGNMENT>,
00209 const vector<SCALARTYPE, ALIGNMENT>,
00210 op_prod> & proxy)
00211 {
00212 assert(proxy.get_lhs().size1() == size());
00213 vector<SCALARTYPE, ALIGNMENT> result(size());
00214 viennacl::linalg::prod_impl(proxy.lhs(), proxy.rhs(), result);
00215 result = *this - result;
00216 return result;
00217 }
00218
00219 }
00220
00221
00222 #endif