matrix.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 GEMM(Matrix<T>& M_r, const Matrix<T>& A, const Matrix<T>& B, T beta=0.0);
  62. // cublasgemm wrapper
  63. static void CUBLASGEMM(Matrix<T>& M_r, const Matrix<T>& A, const Matrix<T>& B, T beta=0.0);
  64. void RowPerm(const Permutation<T>& P);
  65. void ColPerm(const Permutation<T>& P);
  66. Matrix<T> Transpose();
  67. static void Transpose(Matrix<T>& M_r, const Matrix<T>& M);
  68. // Original matrix is destroyed.
  69. void SVD(Matrix<T>& tU, Matrix<T>& tS, Matrix<T>& tVT);
  70. // Original matrix is destroyed.
  71. Matrix<T> pinv(T eps=-1);
  72. private:
  73. size_t dim[2];
  74. T* data_ptr;
  75. bool own_data;
  76. Device dev;
  77. Vector<char> dev_sig;
  78. #if defined(PVFMM_HAVE_CUDA)
  79. cudaEvent_t lock;
  80. #endif
  81. };
  82. /**
  83. * /brief P=[e(p1)*s1 e(p2)*s2 ... e(pn)*sn],
  84. * where e(k) is the kth unit vector,
  85. * perm := [p1 p2 ... pn] is the permutation vector,
  86. * scal := [s1 s2 ... sn] is the scaling vector.
  87. */
  88. #define PERM_INT_T size_t
  89. template <class T>
  90. class Permutation{
  91. template <class Y>
  92. friend std::ostream& operator<<(std::ostream& output, const Permutation<Y>& P);
  93. public:
  94. Permutation(){}
  95. Permutation(size_t size);
  96. static Permutation<T> RandPerm(size_t size);
  97. Matrix<T> GetMatrix() const;
  98. size_t Dim() const;
  99. Permutation<T> Transpose();
  100. Permutation<T> operator*(const Permutation<T>& P);
  101. Matrix<T> operator*(const Matrix<T>& M);
  102. template <class Y>
  103. friend Matrix<Y> operator*(const Matrix<Y>& M, const Permutation<Y>& P);
  104. Vector<PERM_INT_T> perm;
  105. Vector<T> scal;
  106. };
  107. }//end namespace
  108. #ifdef __INTEL_OFFLOAD
  109. #pragma offload_attribute(pop)
  110. #endif
  111. #include <matrix.txx>
  112. #endif //_PVFMM_MATRIX_HPP_