Shiokaze Framework
A research-oriented fluid solver for computer graphics
RCMatrix_interface.h
Go to the documentation of this file.
1 /*
2 ** RCMatrix_interface.h
3 **
4 ** This is part of Shiokaze, a research-oriented fluid solver for computer graphics.
5 ** Inspired by Robert Bridson's matrix code, re-written by Ryoichi Ando on Feb 7, 2017.
6 **
7 ** Permission is hereby granted, free of charge, to any person obtaining a copy of
8 ** this software and associated documentation files (the "Software"), to deal in
9 ** the Software without restriction, including without limitation the rights to use,
10 ** copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
11 ** Software, and to permit persons to whom the Software is furnished to do so,
12 ** subject to the following conditions:
13 **
14 ** The above copyright notice and this permission notice shall be included in all copies
15 ** or substantial portions of the Software.
16 **
17 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18 ** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19 ** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 ** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21 ** CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22 ** OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24 //
25 #ifndef SHKZ_RCMATRIX_INTERFACE_H
26 #define SHKZ_RCMATRIX_INTERFACE_H
27 //
29 #include <shiokaze/core/console.h>
30 #include <functional>
31 #include <vector>
32 #include <memory>
33 //
35 //
36 template <class N, class T> class RCMatrix_vector_interface;
37 template <class N, class T> class RCFixedMatrix_interface;
38 template <class N, class T> class RCMatrix_interface;
39 //
40 template <class N, class T> using RCMatrix_vector_ptr = std::shared_ptr<RCMatrix_vector_interface<N,T> >;
41 template <class N, class T> using RCFixedMatrix_ptr = std::shared_ptr<RCFixedMatrix_interface<N,T> >;
42 template <class N, class T> using RCMatrix_ptr = std::shared_ptr<RCMatrix_interface<N,T> >;
43 //
45 template <class N, class T> class RCMatrix_allocator_interface {
48 public:
55  virtual RCMatrix_vector_ptr<N,T> allocate_vector( N size=0 ) const = 0;
64  virtual RCMatrix_ptr<N,T> allocate_matrix( N rows=0, N columns=0 ) const = 0;
65 };
66 //
69 template <class N, class T> class RCMatrix_vector_interface : public RCMatrix_allocator_interface<N,T> {
70 public:
77  virtual void copy( const RCMatrix_vector_interface<N,T> *x ) {
78  resize(x->size());
79  x->const_for_each([&]( N row, T value ) {
80  set(row,value);
81  });
82  }
89  virtual void resize( N size ) = 0;
96  virtual N size() const = 0;
103  virtual void clear( T value=T() ) = 0;
110  virtual T at( N index ) const = 0;
119  virtual void set( N index, T value ) = 0;
128  virtual void add( N index, T value ) = 0;
135  virtual void add( const RCMatrix_vector_interface<N,T> *x ) {
136  add_scaled(1.0,x);
137  }
144  virtual void subtract( const RCMatrix_vector_interface<N,T> *x ) {
145  add_scaled(-1.0,x);
146  }
155  virtual void subtract( N index, T value ) = 0;
164  virtual void multiply( N index, T value ) = 0;
173  virtual void divide( N index, T value ) = 0;
180  virtual void parallel_for_each( std::function<void( N row, T& value )> func) = 0;
187  virtual void const_parallel_for_each( std::function<void( N row, T value )> func) const = 0;
194  virtual void interruptible_for_each( std::function<bool( N row, T& value )> func) = 0;
201  virtual void const_interruptible_for_each( std::function<bool( N row, T value )> func) const = 0;
208  virtual T abs_max() const = 0;
217  virtual T dot( const RCMatrix_vector_interface<N,T> *x ) const = 0;
226  virtual void add_scaled( T alpha, const RCMatrix_vector_interface<N,T> *x ) = 0;
233  RCMatrix_vector_ptr<N,T> duplicate() const {
234  auto result = this->allocate_vector();
235  result->copy(this);
236  return result;
237  }
244  void for_each( std::function<void( N row, T& value )> func) {
245  interruptible_for_each([&]( N row, T& value ) {
246  func(row,value);
247  return false;
248  });
249  }
256  void const_for_each( std::function<void( N row, T value )> func) const {
257  const_interruptible_for_each([&]( N row, T value ) {
258  func(row,value);
259  return false;
260  });
261  }
268  template<class Y> void convert_from( const std::vector<Y> &v ) {
269  resize(v.size());
270  for( N i=0; i<v.size(); ++i ) set(i,v[i]);
271  }
278  template<class Y> void convert_to( std::vector<Y> &v ) const {
279  v.resize(size());
280  for( N i=0; i<v.size(); ++i ) v[i] = at(i);
281  }
282 };
283 //
286 template <class N, class T> class RCFixedMatrix_interface : public RCMatrix_allocator_interface<N,T> {
287 public:
296  virtual void multiply( const RCMatrix_vector_interface<N,T> *rhs, RCMatrix_vector_interface<N,T> *result ) const = 0;
304  auto x_save = allocate_vector(x->size());
305  x_save->copy(x);
306  multiply(x_save,x);
307  }
316  template<class Y> void multiply(const std::vector<Y> &rhs, std::vector<T> &result ) const {
317  auto _rhs = allocate_vector(rhs.size()); _rhs->convert_from(rhs);
318  auto _result = allocate_vector(rhs.size());
319  multiply(_rhs,_result);
320  _result->convert_to(result);
321  }
322 };
323 //
326 template <class N, class T> class RCMatrix_interface : public RCMatrix_allocator_interface<N,T> {
327 public:
336  virtual void initialize( N rows, N columns ) = 0;
343  virtual void copy( const RCMatrix_interface<N,T> *m ) = 0;
350  virtual void clear( N row ) = 0;
361  virtual T get( N row, N column ) const = 0;
372  virtual void add_to_element( N row, N column, T increment_value ) = 0;
381  virtual void clear_element( N row, N column ) = 0;
390  virtual void interruptible_for_each( N row, std::function<bool( N column, T& value )> func) = 0;
399  virtual void const_interruptible_for_each( N row, std::function<bool( N column, T value )> func) const = 0;
406  virtual N rows() const = 0;
413  virtual N columns() const = 0;
420  virtual N non_zeros( N row ) const = 0;
427  RCMatrix_ptr<N,T> duplicate() const {
428  auto result = this->allocate_matrix();
429  result->copy(this);
430  return result;
431  }
436  void clear() {
437  initialize(rows(),columns());
438  }
445  N non_zeros() const {
446  N sum (0);
447  for( N row=0; row<rows(); ++row ) sum += non_zeros(row);
448  return sum;
449  }
456  bool empty() const {
457  return non_zeros() == 0;
458  }
467  bool empty( N row ) const {
468  return non_zeros(row) == 0;
469  }
478  void for_each( N row, std::function<void( N column, T& value )> func) {
479  interruptible_for_each(row,[&]( N column, T& value ) {
480  func(column,value);
481  return false;
482  });
483  }
492  void const_for_each( N row, std::function<void( N column, T value )> func) const {
493  const_interruptible_for_each(row,[&]( N column, T value ) {
494  func(column,value);
495  return false;
496  });
497  }
504  virtual void multiply(T value) = 0;
513  virtual void multiply( const RCMatrix_vector_interface<N,T> *rhs, RCMatrix_vector_interface<N,T> *result ) const = 0;
522  virtual void multiply( const RCMatrix_interface<N,T> *m, RCMatrix_interface<N,T> *result ) const = 0;
531  virtual void add( const RCMatrix_interface<N,T> *m, RCMatrix_interface<N,T> *result ) const = 0;
538  virtual void transpose( RCMatrix_interface<N,T> *result ) const = 0;
545  virtual RCFixedMatrix_ptr<N,T> make_fixed() const = 0;
552  RCMatrix_vector_ptr<N,T> multiply( const RCMatrix_vector_interface<N,T> *rhs ) const {
553  assert( rhs->size() == columns());
554  auto result = this->allocate_vector();
555  multiply(rhs,result.get());
556  return result;
557  }
566  RCMatrix_ptr<N,T> multiply( const RCMatrix_interface<N,T> *m ) const {
567  auto result = this->allocate_matrix();
568  multiply(m,result.get());
569  return result;
570  }
579  RCMatrix_ptr<N,T> add( const RCMatrix_interface<N,T> *m ) const {
580  auto result = this->allocate_matrix();
581  add(m,result.get());
582  return result;
583  }
590  RCMatrix_ptr<N,T> transpose() const {
591  auto result = this->allocate_matrix();
592  transpose(result.get());
593  return result;
594  }
603  template<class Y> void multiply( const std::vector<Y> &rhs, std::vector<Y> &result ) const {
604  assert( rhs.size() == columns());
605  auto _rhs = this->allocate_vector(rows()); _rhs->convert_from(rhs);
606  auto _result = this->allocate_vector(rows());
607  multiply(_rhs.get(),_result.get());
608  _result->convert_to(result);
609  }
616  template<class Y> std::vector<Y> multiply( const std::vector<Y> &rhs ) const {
617  assert( rhs.size() == columns());
618  std::vector<Y> result;
619  multiply<Y>(rhs,result);
620  return result;
621  }
622 };
623 //
626 template <class N, class T> class RCMatrix_factory_interface : public recursive_configurable_module, public RCMatrix_allocator_interface<N,T> {
627 public:
628  //
629  DEFINE_MODULE(RCMatrix_factory_interface,"Row Compressed Matrix Factory","RCMatrix","Row compressed matrix module")
630  //
631 };
632 //
634 //
636 //
637 #endif
638 //
RCMatrix_vector_interface
Interface to provide vector calculations.
Definition: RCMatrix_interface.h:36
RCFixedMatrix_interface::multiply
virtual void multiply(const RCMatrix_vector_interface< N, T > *rhs, RCMatrix_vector_interface< N, T > *result) const =0
Apply multiplication to an input vector and substitute to a result vector.
RCMatrix_interface::empty
bool empty() const
Get if the matrix is empty.
Definition: RCMatrix_interface.h:456
RCMatrix_vector_interface::interruptible_for_each
virtual void interruptible_for_each(std::function< bool(N row, T &value)> func)=0
Manipulate values in serial order.
RCFixedMatrix_interface::apply
void apply(RCMatrix_vector_interface< N, T > *x) const
Apply multiplication to an input vector.
Definition: RCMatrix_interface.h:303
RCMatrix_interface::const_interruptible_for_each
virtual void const_interruptible_for_each(N row, std::function< bool(N column, T value)> func) const =0
Read values in serial order.
RCMatrix_interface::columns
virtual N columns() const =0
Get the size of columns.
RCMatrix_interface::duplicate
RCMatrix_ptr< N, T > duplicate() const
Duplicate matrix.
Definition: RCMatrix_interface.h:427
RCMatrix_interface::for_each
void for_each(N row, std::function< void(N column, T &value)> func)
Manipulate elements in serial order.
Definition: RCMatrix_interface.h:478
RCMatrix_vector_interface::add
virtual void add(N index, T value)=0
Add an element value at an input index position.
RCMatrix_interface::const_for_each
void const_for_each(N row, std::function< void(N column, T value)> func) const
Read elements in serial order.
Definition: RCMatrix_interface.h:492
RCMatrix_interface::add_to_element
virtual void add_to_element(N row, N column, T increment_value)=0
Add a value to an element.
recursive_configurable_driver
Class that encapsulates recursive_configurable class.
Definition: recursive_configurable_module.h:76
RCMatrix_interface::multiply
std::vector< Y > multiply(const std::vector< Y > &rhs) const
Apply multiplication to an input vector. Provided to preserve std::vector compatibility.
Definition: RCMatrix_interface.h:616
RCMatrix_interface::multiply
virtual void multiply(T value)=0
Multiply a value to all the elements.
console.h
RCMatrix_vector_interface::size
virtual N size() const =0
Get the size of dimension.
RCMatrix_vector_interface::const_for_each
void const_for_each(std::function< void(N row, T value)> func) const
Read values in serial order.
Definition: RCMatrix_interface.h:256
RCMatrix_interface::rows
virtual N rows() const =0
Get the size of rows.
RCMatrix_interface::make_fixed
virtual RCFixedMatrix_ptr< N, T > make_fixed() const =0
Make a fixed matrix.
RCMatrix_vector_interface::abs_max
virtual T abs_max() const =0
Compute the uniform norm.
RCMatrix_vector_interface::clear
virtual void clear(T value=T())=0
Clear out all the value with the input value. Note that the dimension size of vector remain intact.
RCMatrix_vector_interface::set
virtual void set(N index, T value)=0
Set an element value at an input index position.
RCMatrix_vector_interface::const_parallel_for_each
virtual void const_parallel_for_each(std::function< void(N row, T value)> func) const =0
Read values in parallel.
RCMatrix_interface::get
virtual T get(N row, N column) const =0
Get the element value at the row and the column.
RCMatrix_vector_interface::for_each
void for_each(std::function< void(N row, T &value)> func)
Manipulate values in serial order.
Definition: RCMatrix_interface.h:244
RCMatrix_vector_interface::copy
virtual void copy(const RCMatrix_vector_interface< N, T > *x)
Copy the vector.
Definition: RCMatrix_interface.h:77
RCMatrix_vector_interface::at
virtual T at(N index) const =0
Get an element value at an input index position.
DEFINE_MODULE
#define DEFINE_MODULE(CLASS_T, LNG_NAME, ARG_NAME, DESCRIPTION)
Definition that simplifies the loading module.
Definition: recursive_configurable_module.h:39
SHKZ_BEGIN_NAMESPACE
#define SHKZ_BEGIN_NAMESPACE
Name space beggining definition for shiokaze.
Definition: common.h:39
RCMatrix_interface::multiply
RCMatrix_ptr< N, T > multiply(const RCMatrix_interface< N, T > *m) const
Apply multiplication to an input matrix of the form: [result] = [self][m].
Definition: RCMatrix_interface.h:566
RCMatrix_interface::clear
void clear()
Reset all the elements with zeros.
Definition: RCMatrix_interface.h:436
RCMatrix_factory_interface
Interface for creating Row Compressed Matrix and vector instances. "RCMatrix" is provided as implemen...
Definition: RCMatrix_interface.h:626
RCMatrix_vector_interface::dot
virtual T dot(const RCMatrix_vector_interface< N, T > *x) const =0
Compute the dot product.
RCMatrix_interface::multiply
RCMatrix_vector_ptr< N, T > multiply(const RCMatrix_vector_interface< N, T > *rhs) const
Apply multiplication to an input vector.
Definition: RCMatrix_interface.h:552
RCMatrix_vector_interface::convert_from
void convert_from(const std::vector< Y > &v)
Convert input std::vector to the vector of this type.
Definition: RCMatrix_interface.h:268
RCMatrix_allocator_interface::allocate_matrix
virtual RCMatrix_ptr< N, T > allocate_matrix(N rows=0, N columns=0) const =0
Allocate a matrix.
RCMatrix_vector_interface::add_scaled
virtual void add_scaled(T alpha, const RCMatrix_vector_interface< N, T > *x)=0
Add alpha * x.
RCFixedMatrix_interface::multiply
void multiply(const std::vector< Y > &rhs, std::vector< T > &result) const
Apply multiplication to an input vector and substitute to a result vector. Provided to preserve std::...
Definition: RCMatrix_interface.h:316
RCMatrix_vector_interface::duplicate
RCMatrix_vector_ptr< N, T > duplicate() const
Duplicate this vector.
Definition: RCMatrix_interface.h:233
RCMatrix_vector_interface::subtract
virtual void subtract(const RCMatrix_vector_interface< N, T > *x)
Subtract a vector.
Definition: RCMatrix_interface.h:144
RCMatrix_interface::add
virtual void add(const RCMatrix_interface< N, T > *m, RCMatrix_interface< N, T > *result) const =0
Add a matrix.
RCMatrix_allocator_interface::allocate_vector
virtual RCMatrix_vector_ptr< N, T > allocate_vector(N size=0) const =0
Allocate a vector.
RCMatrix_interface::copy
virtual void copy(const RCMatrix_interface< N, T > *m)=0
Copy the input matrix.
RCMatrix_vector_interface::divide
virtual void divide(N index, T value)=0
Divide an element value at an input index position.
RCMatrix_vector_interface::multiply
virtual void multiply(N index, T value)=0
Multiply an element value at an input index position.
RCMatrix_vector_interface::add
virtual void add(const RCMatrix_vector_interface< N, T > *x)
Add a vector.
Definition: RCMatrix_interface.h:135
RCMatrix_interface::non_zeros
N non_zeros() const
Get the number of all the non-zero entries.
Definition: RCMatrix_interface.h:445
RCMatrix_vector_interface::parallel_for_each
virtual void parallel_for_each(std::function< void(N row, T &value)> func)=0
Manipulate values in parallel.
RCMatrix_interface
Interface for Row Compressed Matrix.
Definition: RCMatrix_interface.h:38
RCMatrix_vector_interface::convert_to
void convert_to(std::vector< Y > &v) const
Convert to std::vector.
Definition: RCMatrix_interface.h:278
RCMatrix_interface::transpose
RCMatrix_ptr< N, T > transpose() const
Transpose this matrix.
Definition: RCMatrix_interface.h:590
SHKZ_END_NAMESPACE
#define SHKZ_END_NAMESPACE
Name space end definition for shiokaze.
Definition: common.h:44
RCMatrix_interface::empty
bool empty(N row) const
Get if a row in the matrix is empty.
Definition: RCMatrix_interface.h:467
RCMatrix_interface::interruptible_for_each
virtual void interruptible_for_each(N row, std::function< bool(N column, T &value)> func)=0
Manipulate values in serial order.
RCMatrix_vector_interface::const_interruptible_for_each
virtual void const_interruptible_for_each(std::function< bool(N row, T value)> func) const =0
Read values in serial order.
RCMatrix_interface::clear_element
virtual void clear_element(N row, N column)=0
Clear out an element with zero.
recursive_configurable_module.h
RCMatrix_vector_interface::resize
virtual void resize(N size)=0
Resize the dimension.
recursive_configurable_module
recursive_configurable class that also inherits module.
Definition: recursive_configurable_module.h:49
RCMatrix_interface::initialize
virtual void initialize(N rows, N columns)=0
Initialize matrix with rows and columns.
RCMatrix_interface::multiply
void multiply(const std::vector< Y > &rhs, std::vector< Y > &result) const
Apply multiplication to an input vector and substitute to a result vector. Provided to preserve std::...
Definition: RCMatrix_interface.h:603
RCFixedMatrix_interface
Specialized Row Compressed Matrix that efficiently performs matrix-vector calculations.
Definition: RCMatrix_interface.h:37
RCMatrix_allocator_interface
Interface to provide allocators for Row Compressed Matrix and vector instances.
Definition: RCMatrix_interface.h:47
RCMatrix_interface::add
RCMatrix_ptr< N, T > add(const RCMatrix_interface< N, T > *m) const
Add to an input matrix.
Definition: RCMatrix_interface.h:579