anutka

Untitled

Nov 10th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 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. for(int j = 0; j < 4; j++) {
  27. value1.a[j] = A[i+j];
  28. }
  29.  
  30. union {
  31. float a[4];
  32. __m128 xmm;
  33. } value2;
  34. for(int j = 0; j < 4; j++) {
  35. value2.a[j] = B[t+j];
  36. }
  37. __m128 val = _mm_dp_ps(value1.xmm, value2.xmm, 0xF1);
  38. result.xmm = _mm_add_ss(result.xmm, val);
  39. t +=4;
  40. }
  41. double res = result.f[3];
  42. for (i; i < endA; i++) {
  43. res += A[i]*B[t];
  44. t++;
  45. }
  46. return res;
  47. }
  48.  
  49.  
  50. void multiply(float*A, float*B, float*res, int N, int M) {
  51. for (int i = 0; i < N; i++) {
  52. for (int j = 0; j < N; j++) {
  53. res[i*N+j] = dot_product(A, B, i*M, (i+1)*M, j*M, (j+1)*M);
  54. }
  55. }
  56. }
  57. void Print(float* res, int N, int M) {
  58. for (int i = 0; i < N; i++) {
  59. for (int j = 0; j < M; j++) {
  60. printf("%f ", res[i*M + j]);
  61. }
  62. printf("\n");
  63. }
  64. }
  65.  
  66. int main() {
  67. int N, M;
  68. scanf("%d %d", &N, &M); //N -количество строк, M - количество столбцов
  69. float *A = calloc(N*M, 4);
  70. float *B = calloc(N*M, 4);
  71. float *res = calloc(N*M, 4);
  72. for (int i = 0; i < N; i++) {
  73. for (int j = 0; j < M; j++) {
  74. scanf("%f", &A[i*M + j]);
  75. }
  76. }
  77. for (int i = 0; i < M; i++) {
  78. for (int j = 0; j < N; j++) {
  79. scanf("%f", &B[j*M + i]);
  80. }
  81. }
  82. multiply(A, B,res, N, M);
  83. Print(res, N, N);
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment