avx_matrix.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef AVX_MATRIX
  2. #define AVX_MATRIX
  3. #include "config.hpp"
  4. #include <immintrin.h>
  5. //*************
  6. //* AvxMatrix *
  7. //*************
  8. //! A Matrix class that take advantage of avx instructions
  9. class AvxMatrix{
  10. public:
  11. //! Maximum size of the matrix
  12. static const size_t rank=3*max_len+1;
  13. //! Corresponding length for m256 array
  14. static const size_t avx_rank=(rank-1)/4+1;
  15. //! Real number of colums; must be a multiple of 4
  16. static const size_t columns=4*avx_rank;
  17. //! Total length of the m256 array
  18. static const size_t avx_size=avx_rank*columns;
  19. //! Total length of the double array
  20. static const size_t capacity=16*avx_size;
  21. //! Array of the matrix coefficients
  22. union{
  23. __m256d avx[avx_size];
  24. Reel data[capacity];
  25. };
  26. //! The empty constructor
  27. AvxMatrix();
  28. //! Coefficient accessor
  29. //! \param i line position of the coefficient
  30. //! \param j column position of the coefficient
  31. Reel get(size_t i,size_t j) const;
  32. Reel& get(size_t i,size_t j);
  33. //! Fill matrix with 0
  34. void clear();
  35. //! Display command
  36. //! \param nl number of lines to display
  37. //! \param nc number of columns to display
  38. void display(size_t nl,size_t nc) const;
  39. //! Assume the current matrix M is symmetric and
  40. //! return the ith diagonal term of M square
  41. Reel get_diag_square_sym(size_t i,size_t n) const;
  42. //! Set the current Matrix to be I+1/4.C.B where B
  43. //! is supposed to be symmetric
  44. //! \param C a matrix of size nxn
  45. //! \param B a symmetric matrix of size nxn
  46. //! \param n size of matrices B and C
  47. void from_C_B(const AvxMatrix& C,const AvxMatrix& B,size_t n);
  48. //! Swap lines i and j of the matrix
  49. //! \param i line number
  50. //! \param j line number (differenet from i)
  51. //! \param navx number of m256 blocs to consier per lines
  52. void swap_lines(size_t i,size_t j,size_t navx);
  53. //! Multiply line i by a
  54. //! \param i line number
  55. //! \param a non zero scalar
  56. //! \param navx number of m256 blocs to consider per lines
  57. void mul_line(size_t i,Reel a,size_t navx);
  58. //! Add to line i a multiple by a of line j
  59. //! \param i line number
  60. //! \param j line number
  61. //! \param a non zero scalar
  62. //! \param navx number of m256 blocs to consider per lines
  63. void add_mul_line(size_t i,size_t j,Reel a,size_t navx);
  64. //! Perform Gauss reduction on a top-left submatrix
  65. //! Return the corresponding minor
  66. //! \param nl number of lines of the submatrix
  67. //! \param nc number of columns of the submatrix
  68. Reel Gauss(size_t nl,size_t nc);
  69. };
  70. //*****************
  71. //* AVX constants *
  72. //*****************
  73. //! Array of 4 zeros
  74. static const __m256d zeros=_mm256_set1_pd(0);
  75. //*************
  76. //* Avx block *
  77. //*************
  78. //! Block structure mixing representing m256 as array of 4 doubles
  79. union Block{
  80. __m256d avx;
  81. Reel data[4];
  82. };
  83. //******************
  84. //* Inline methods *
  85. //******************
  86. inline
  87. AvxMatrix::AvxMatrix(){
  88. }
  89. inline Reel&
  90. AvxMatrix::get(size_t i,size_t j){
  91. return data[i*columns+j];
  92. }
  93. inline Reel
  94. AvxMatrix::get(size_t i,size_t j) const{
  95. return data[i*columns+j];
  96. }
  97. #endif