anutka

Untitled

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