matrix.hpp 3.6 KB

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