anutka

Untitled

Nov 11th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include<math.h>
  5. #include <smmintrin.h>
  6.  
  7. float dot_product(const float *A, const float *B, int startA, int endA, int startB, int endB){
  8. union {
  9. __m128 xmm;
  10. float f[4];
  11. } result;
  12. result.f[0] = 0.0;
  13. result.f[1] = 0.0;
  14. result.f[2] = 0.0;
  15. result.f[3] = 0.0;
  16.  
  17. int i = startA;
  18. int t = startB;
  19.  
  20. for ( i = startA; i + 3 < endA; i += 4) {
  21.  
  22. union {
  23. float a[4];
  24. __m128 xmm;
  25. } value1;
  26.  
  27. for(int j = 0; j < 4; j++) {
  28. value1.a[j] = A[i+j];
  29. }
  30.  
  31. union {
  32. float a[4];
  33. __m128 xmm;
  34. } value2;
  35.  
  36. for(int j = 0; j < 4; j++) {
  37. value2.a[j] = B[t+j];
  38. }
  39. __m128 val = _mm_dp_ps(value1.xmm, value2.xmm, 0xF1);
  40. result.xmm = _mm_add_ss(result.xmm, val);
  41. t +=4;
  42. }
  43. double res = result.f[0];
  44. for (; i < endA; i++) {
  45. res += A[i]*B[t];
  46. t++;
  47. }
  48. return res;
  49. }
  50.  
  51.  
  52. void multiply(float*A, float*B, float*res, int N, int M) {
  53. for (int i = 0; i < N; i++) {
  54. for (int j = 0; j < N; j++) {
  55. res[i*N+j] = dot_product(A, B, i*M, (i+1)*M, j*M, (j+1)*M);
  56. }
  57. }
  58. }
  59. void Print(float* res, int N, int M) {
  60. for (int i = 0; i < N; i++) {
  61. for (int j = 0; j < M; j++) {
  62. printf("%f ", res[i*M + j]);
  63. }
  64. printf("\n");
  65. }
  66. }
  67.  
  68. int main() {
  69. int N, M;
  70. scanf("%d %d", &N, &M); //N -количество строк, M - количество столбцов
  71. float *A = calloc(N*M, 4);
  72. float *B = calloc(N*M, 4);
  73. float *res = calloc(N*M, 4);
  74. for (int i = 0; i < N; i++) {
  75. for (int j = 0; j < M; j++) {
  76. scanf("%f", &A[i*M + j]);
  77. }
  78. }
  79. for (int i = 0; i < M; i++) {
  80. for (int j = 0; j < N; j++) {
  81. scanf("%f", &B[j*M + i]);
  82. }
  83. }
  84. multiply(A, B,res, N, M);
  85. Print(res, N, N);
  86. return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment