matrix.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #ifndef _PVFMM_MATRIX_HPP_
  8. #define _PVFMM_MATRIX_HPP_
  9. #include <cstdlib>
  10. #include <vector>
  11. #include <iostream>
  12. #include <vector.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. #ifdef __INTEL_OFFLOAD
  26. __attribute__ ((target(mic)))
  27. #endif
  28. Device{
  29. Device& operator=(Matrix& M){
  30. dim[0]=M.Dim(0);
  31. dim[1]=M.Dim(1);
  32. dev_ptr=(uintptr_t)M[0];
  33. return *this;
  34. }
  35. inline T* operator[](size_t j) const{
  36. return &((T*)dev_ptr)[j*dim[1]];
  37. }
  38. size_t dim[2];
  39. uintptr_t dev_ptr;
  40. int lock_idx;
  41. };
  42. Matrix();
  43. Matrix(size_t dim1, size_t dim2, T* data_=NULL, bool own_data_=true);
  44. Matrix(const Matrix<T>& M);
  45. ~Matrix();
  46. void Swap(Matrix<T>& M);
  47. void ReInit(size_t dim1, size_t dim2, T* data_=NULL, bool own_data_=true);
  48. Device& AllocDevice(bool copy);
  49. void Device2Host(T* host_ptr=NULL);
  50. void Device2HostWait();
  51. void FreeDevice(bool copy);
  52. void Write(const char* fname);
  53. size_t Dim(size_t i) const;
  54. void Resize(size_t i, size_t j);
  55. void SetZero();
  56. Matrix<T>& operator=(const Matrix<T>& M);
  57. Matrix<T>& operator+=(const Matrix<T>& M);
  58. Matrix<T>& operator-=(const Matrix<T>& M);
  59. Matrix<T> operator+(const Matrix<T>& M2);
  60. Matrix<T> operator-(const Matrix<T>& M2);
  61. T& operator()(size_t i,size_t j) const;
  62. T* operator[](size_t i) const;
  63. Matrix<T> operator*(const Matrix<T>& M);
  64. static void DGEMM(Matrix<T>& M_r, const Matrix<T>& A, const Matrix<T>& B, T beta=0.0);
  65. // cublasXgemm wrapper
  66. static void CUBLASXGEMM(Matrix<T>& M_r, const Matrix<T>& A, const Matrix<T>& B, T beta=0.0);
  67. void RowPerm(const Permutation<T>& P);
  68. void ColPerm(const Permutation<T>& P);
  69. Matrix<T> Transpose();
  70. static void Transpose(Matrix<T>& M_r, const Matrix<T>& M);
  71. Matrix<T> pinv();
  72. Matrix<T> pinv(T eps);
  73. private:
  74. size_t dim[2];
  75. T* data_ptr;
  76. bool own_data;
  77. Device dev;
  78. Vector<char> dev_sig;
  79. };
  80. /**
  81. * /brief P=[e(p1)*s1 e(p2)*s2 ... e(pn)*sn],
  82. * where e(k) is the kth unit vector,
  83. * perm := [p1 p2 ... pn] is the permutation vector,
  84. * scal := [s1 s2 ... sn] is the scaling vector.
  85. */
  86. #define PERM_INT_T size_t
  87. template <class T>
  88. class Permutation{
  89. template <class Y>
  90. friend std::ostream& operator<<(std::ostream& output, const Permutation<Y>& P);
  91. public:
  92. Permutation(){}
  93. Permutation(size_t size);
  94. static Permutation<T> RandPerm(size_t size);
  95. Matrix<T> GetMatrix() const;
  96. size_t Dim() const;
  97. Permutation<T> Transpose();
  98. Permutation<T> operator*(const Permutation<T>& P);
  99. Matrix<T> operator*(const Matrix<T>& M);
  100. template <class Y>
  101. friend Matrix<Y> operator*(const Matrix<Y>& M, const Permutation<Y>& P);
  102. Vector<PERM_INT_T> perm;
  103. Vector<T> scal;
  104. };
  105. }//end namespace
  106. #include <matrix.txx>
  107. #ifdef __INTEL_OFFLOAD
  108. #pragma offload_attribute(pop)
  109. #endif
  110. #endif //_PVFMM_MATRIX_HPP_