28 #ifndef REMORA_KERNELS_DEFAULT_MATRIX_ASSIGN_HPP 29 #define REMORA_KERNELS_DEFAULT_MATRIX_ASSIGN_HPP 31 #include "../vector_assign.hpp" 32 #include "../../detail/traits.hpp" 35 namespace remora{
namespace bindings{
43 template<
class F,
class M>
45 matrix_expression<M, cpu_tag> &m,
46 typename M::value_type t,
49 for(std::size_t i = 0; i != m().size1(); ++i){
51 kernels::assign<F>(rowM,t);
55 template<
class F,
class M>
57 matrix_expression<M, cpu_tag> &m,
58 typename M::value_type t,
61 for(std::size_t j = 0; j != m().size2(); ++j){
62 auto columnM = column(m,j);
63 kernels::assign<F>(columnM,t);
67 template<
class F,
class M,
class Orientation,
class Triangular>
69 matrix_expression<M, cpu_tag> &m,
70 typename M::value_type t,
71 triangular<Orientation,Triangular>
73 matrix_assign<F>(m,t,Orientation());
83 template<
class M,
class E,
class TagE,
class TagM>
85 matrix_expression<M, cpu_tag> &m,
86 matrix_expression<E, cpu_tag>
const& e,
87 row_major, row_major,TagE, TagM
89 for(std::size_t i = 0; i != m().size1(); ++i){
91 kernels::assign(rowM,row(e,i));
98 template<
class M,
class E>
100 matrix_expression<M, cpu_tag> &m,
101 matrix_expression<E, cpu_tag>
const& e,
102 row_major, column_major,dense_tag, dense_tag
105 std::size_t
const blockSize = 8;
106 typename M::value_type blockStorage[blockSize][blockSize];
108 typedef typename M::size_type size_type;
109 size_type size1 = m().size1();
110 size_type size2 = m().size2();
111 for (size_type iblock = 0; iblock < size1; iblock += blockSize){
112 for (size_type jblock = 0; jblock < size2; jblock += blockSize){
113 std::size_t blockSizei = std::min(blockSize,size1-iblock);
114 std::size_t blockSizej = std::min(blockSize,size2-jblock);
117 for (size_type j = 0; j < blockSizej; ++j){
118 for (size_type i = 0; i < blockSizei; ++i){
119 blockStorage[i][j] = e()(iblock+i,jblock+j);
124 for (size_type i = 0; i < blockSizei; ++i){
125 for (size_type j = 0; j < blockSizej; ++j){
126 m()(iblock+i,jblock+j) = blockStorage[i][j];
134 template<
class M,
class E>
136 matrix_expression<M, cpu_tag> &m,
137 matrix_expression<E, cpu_tag>
const& e,
138 row_major, column_major,dense_tag, sparse_tag
140 for(std::size_t j = 0; j != m().size2(); ++j){
141 auto columnM = column(m,j);
142 kernels::assign(columnM,column(e,j));
148 template<
class M,
class E>
150 matrix_expression<M, cpu_tag> &m,
151 matrix_expression<E, cpu_tag>
const& e,
152 row_major, column_major, sparse_tag, dense_tag
154 for(std::size_t i = 0; i != m().size1(); ++i){
155 auto rowM = row(m,i);
156 kernels::assign(rowM,row(e,i));
161 template<
class M,
class E>
163 matrix_expression<M, cpu_tag> &m,
164 matrix_expression<E, cpu_tag>
const& e,
165 row_major, column_major,sparse_tag,sparse_tag
171 typedef typename M::value_type value_type;
172 typedef typename M::size_type size_type;
173 typedef row_major::sparse_element<value_type> Element;
174 std::vector<Element> elements;
176 size_type size2 = m().size2();
177 size_type size1 = m().size1();
178 for(size_type j = 0; j != size2; ++j){
179 typename E::const_column_iterator pos_e = e().column_begin(j);
180 typename E::const_column_iterator end_e = e().column_end(j);
181 for(; pos_e != end_e; ++pos_e){
183 element.i = pos_e.index();
185 element.value = *pos_e;
186 elements.push_back(element);
189 std::sort(elements.begin(),elements.end());
192 m().reserve(elements.size());
193 for(size_type current = 0; current != elements.size();){
195 size_type row = elements[current].i;
196 size_type row_end = current;
197 while(row_end != elements.size() && elements[row_end].i == row)
199 m().reserve_row(row,row_end - current);
202 typename M::row_iterator row_pos = m().row_begin(row);
203 for(; current != row_end; ++current){
204 row_pos = m().set_element(row_pos,elements[current].j,elements[current].value);
211 template<
class M,
class E,
class Triangular>
213 matrix_expression<M, cpu_tag> &m,
214 matrix_expression<E, cpu_tag>
const& e,
215 triangular<row_major,Triangular>, triangular<row_major,Triangular>,
216 packed_tag, packed_tag
218 typedef typename M::row_iterator MIter;
219 typedef typename E::const_row_iterator EIter;
221 for(std::size_t i = 0; i != m().size1(); ++i){
222 MIter mpos = m().row_begin(i);
223 EIter epos = e().row_begin(i);
224 MIter mend = m().row_end(i);
225 REMORA_SIZE_CHECK(mpos.index() == epos.index());
226 for(; mpos!=mend; ++mpos,++epos){
234 template<
class M,
class E,
class Triangular>
236 matrix_expression<M, cpu_tag> &m,
237 matrix_expression<E, cpu_tag>
const& e,
238 triangular<row_major,Triangular>, triangular<column_major,Triangular>,
239 packed_tag, packed_tag
241 typedef typename M::row_iterator MIter;
243 for(std::size_t i = 0; i != m().size1(); ++i){
244 MIter mpos = m().row_begin(i);
245 MIter mend = m().row_end(i);
246 for(; mpos!=mend; ++mpos){
247 *mpos = e()(i,mpos.index());
258 template<
class F,
class M,
class E,
class TagE,
class TagM>
259 void matrix_assign_functor(
260 matrix_expression<M, cpu_tag> &m,
261 matrix_expression<E, cpu_tag>
const& e,
263 row_major, row_major,TagM, TagE
265 for(std::size_t i = 0; i != m().size1(); ++i){
266 auto rowM = row(m,i);
267 kernels::assign(rowM,row(e,i),f);
274 template<
class F,
class M,
class E>
275 void matrix_assign_functor(
276 matrix_expression<M, cpu_tag> &m,
277 matrix_expression<E, cpu_tag>
const& e,
279 row_major, column_major,dense_tag, dense_tag
282 std::size_t
const blockSize = 16;
283 typename M::value_type blockStorage[blockSize][blockSize];
285 typedef typename M::size_type size_type;
286 size_type size1 = m().size1();
287 size_type size2 = m().size2();
288 for (size_type iblock = 0; iblock < size1; iblock += blockSize){
289 for (size_type jblock = 0; jblock < size2; jblock += blockSize){
290 std::size_t blockSizei = std::min(blockSize,size1-iblock);
291 std::size_t blockSizej = std::min(blockSize,size2-jblock);
294 for (size_type j = 0; j < blockSizej; ++j){
295 for (size_type i = 0; i < blockSizei; ++i){
296 blockStorage[i][j] = e()(iblock+i,jblock+j);
301 for (size_type i = 0; i < blockSizei; ++i){
302 for (size_type j = 0; j < blockSizej; ++j){
303 m()(iblock+i,jblock+j) = f(m()(iblock+i,jblock+j), blockStorage[i][j]);
311 template<
class F,
class M,
class E>
312 void matrix_assign_functor(
313 matrix_expression<M, cpu_tag> &m,
314 matrix_expression<E, cpu_tag>
const& e,
316 row_major, column_major,dense_tag, sparse_tag
318 for(std::size_t j = 0; j != m().size2(); ++j){
319 auto columnM = column(m,j);
320 kernels::assign(columnM,column(e,j),f);
325 template<
class F,
class M,
class E>
326 void matrix_assign_functor(
327 matrix_expression<M, cpu_tag> &m,
328 matrix_expression<E, cpu_tag>
const& e,
330 row_major, column_major, sparse_tag, dense_tag
332 for(std::size_t i = 0; i != m().size1(); ++i){
333 auto rowM = row(m,i);
334 kernels::assign(rowM,row(e,i),f);
339 template<
class F,
class M,
class E>
340 void matrix_assign_functor(
341 matrix_expression<M, cpu_tag> &m,
342 matrix_expression<E, cpu_tag>
const& e,
344 row_major, column_major,sparse_tag t,sparse_tag
346 typename matrix_temporary<M>::type eTrans = e;
347 matrix_assign_functor(m,eTrans,f,row_major(),row_major(),t,t);
413 template<
class F,
class M,
class E,
class Triangular>
414 void matrix_assign_functor(
415 matrix_expression<M, cpu_tag> &m,
416 matrix_expression<E, cpu_tag>
const& e,
418 triangular<row_major,Triangular>, triangular<row_major,Triangular>
420 typedef typename M::row_iterator MIter;
421 typedef typename E::const_row_iterator EIter;
424 static_assert(F::left_zero_identity || F::right_zero_identity,
"cannot handle the given packed matrix assignment function");
426 for(std::size_t i = 0; i != m().size1(); ++i){
427 MIter mpos = m().row_begin(i);
428 EIter epos = e().row_begin(i);
429 MIter mend = m().row_end(i);
430 REMORA_SIZE_CHECK(mpos.index() == epos.index());
431 for(; mpos!=mend; ++mpos,++epos){
432 *mpos = f(*mpos,*epos);
438 template<
class F,
class M,
class E,
class Triangular>
439 void matrix_assign_functor(
440 matrix_expression<M, cpu_tag> &m,
441 matrix_expression<E, cpu_tag>
const& e,
443 triangular<row_major,Triangular>, triangular<column_major,Triangular>
445 typedef typename M::row_iterator MIter;
447 static_assert(F::left_zero_identity,
"cannot handle the given packed matrix assignment function");
449 for(std::size_t i = 0; i != m().size1(); ++i){
450 MIter mpos = m().row_begin(i);
451 MIter mend = m().row_end(i);
452 for(; mpos!=mend; ++mpos){
453 *mpos = f(*mpos,e()(i,mpos.index()));