matrix.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * \file matrix.hpp
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 2-11-2011
  5. * \brief This file contains definition of the class Matrix.
  6. */
  7. #include <stdint.h>
  8. #include <cstdlib>
  9. #include <pvfmm_common.hpp>
  10. #include <vector.hpp>
  11. #ifndef _PVFMM_MATRIX_HPP_
  12. #define _PVFMM_MATRIX_HPP_
  13. #ifdef __INTEL_OFFLOAD
  14. #pragma offload_attribute(push,target(mic))
  15. #endif
  16. namespace pvfmm{
  17. template <class T>
  18. class Permutation;
  19. template <class T>
  20. class Matrix{
  21. template <class Y>
  22. friend std::ostream& operator<<(std::ostream& output, const Matrix<Y>& M);
  23. public:
  24. struct
  25. Device{
  26. Device& operator=(Matrix& M){
  27. dim[0]=M.Dim(0);
  28. dim[1]=M.Dim(1);
  29. dev_ptr=(uintptr_t)M[0];
  30. return *this;
  31. }
  32. inline T* operator[](size_t j) const{
  33. return &((T*)dev_ptr)[j*dim[1]];
  34. }
  35. size_t dim[2];
  36. uintptr_t dev_ptr;
  37. int lock_idx;
  38. };
  39. Matrix();
  40. Matrix(size_t dim1, size_t dim2, T* data_=NULL, bool own_data_=true);
  41. Matrix(const Matrix<T>& M);
  42. ~Matrix();
  43. void Swap(Matrix<T>& M);
  44. void ReInit(size_t dim1, size_t dim2, T* data_=NULL, bool own_data_=true);
  45. Device& AllocDevice(bool copy);
  46. void Device2Host(T* host_ptr=NULL);
  47. void Device2HostWait();
  48. void FreeDevice(bool copy);
  49. void Write(const char* fname);
  50. size_t Dim(size_t i) const;
  51. void Resize(size_t i, size_t j);
  52. void SetZero();
  53. Matrix<T>& operator=(const Matrix<T>& M);
  54. Matrix<T>& operator+=(const Matrix<T>& M);
  55. Matrix<T>& operator-=(const Matrix<T>& M);
  56. Matrix<T> operator+(const Matrix<T>& M2);
  57. Matrix<T> operator-(const Matrix<T>& M2);
  58. T& operator()(size_t i,size_t j) const;
  59. T* operator[](size_t i) const;
  60. Matrix<T> operator*(const Matrix<T>& M);
  61. static void DGEMM(Matrix<T>& M_r, const Matrix<T>& A, const Matrix<T>& B, T beta=0.0);
  62. void RowPerm(const Permutation<T>& P);
  63. void ColPerm(const Permutation<T>& P);
  64. Matrix<T> Transpose();
  65. static void Transpose(Matrix<T>& M_r, const Matrix<T>& M);
  66. // Original matrix is destroyed.
  67. Matrix<T> pinv(T eps=-1);
  68. private:
  69. size_t dim[2];
  70. T* data_ptr;
  71. bool own_data;
  72. Device dev;
  73. Vector<char> dev_sig;
  74. };
  75. /**
  76. * /brief P=[e(p1)*s1 e(p2)*s2 ... e(pn)*sn],
  77. * where e(k) is the kth unit vector,
  78. * perm := [p1 p2 ... pn] is the permutation vector,
  79. * scal := [s1 s2 ... sn] is the scaling vector.
  80. */
  81. #define PERM_INT_T size_t
  82. template <class T>
  83. class Permutation{
  84. template <class Y>
  85. friend std::ostream& operator<<(std::ostream& output, const Permutation<Y>& P);
  86. public:
  87. Permutation(){}
  88. Permutation(size_t size);
  89. static Permutation<T> RandPerm(size_t size);
  90. Matrix<T> GetMatrix() const;
  91. size_t Dim() const;
  92. Permutation<T> Transpose();
  93. Permutation<T> operator*(const Permutation<T>& P);
  94. Matrix<T> operator*(const Matrix<T>& M);
  95. template <class Y>
  96. friend Matrix<Y> operator*(const Matrix<Y>& M, const Permutation<Y>& P);
  97. Vector<PERM_INT_T> perm;
  98. Vector<T> scal;
  99. };
  100. }//end namespace
  101. #ifdef __INTEL_OFFLOAD
  102. #pragma offload_attribute(pop)
  103. #endif
  104. #include <matrix.txx>
  105. #endif //_PVFMM_MATRIX_HPP_