Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include<math.h>
- #include <smmintrin.h>
- float dot_product(const float *A, const float *B, int startA, int endA, int startB, int endB){
- union {
- __m128 xmm;
- float f[4];
- } result;
- result.f[0] = 0.0;
- result.f[1] = 0.0;
- result.f[2] = 0.0;
- result.f[3] = 0.0;
- int i = startA;
- int t = startB;
- for ( i = startA; i + 3 < endA; i += 4) {
- union {
- float a[4];
- __m128 xmm;
- } value1;
- for(int j = 0; j < 4; j++) {
- value1.a[j] = A[i+j];
- }
- union {
- float a[4];
- __m128 xmm;
- } value2;
- for(int j = 0; j < 4; j++) {
- value2.a[j] = B[t+j];
- }
- __m128 val = _mm_dp_ps(value1.xmm, value2.xmm, 0xF1);
- result.xmm = _mm_add_ss(result.xmm, val);
- t +=4;
- }
- double res = result.f[0];
- for (; i < endA; i++) {
- res += A[i]*B[t];
- t++;
- }
- return res;
- }
- void multiply(float*A, float*B, float*res, int N, int M) {
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < N; j++) {
- res[i*N+j] = dot_product(A, B, i*M, (i+1)*M, j*M, (j+1)*M);
- }
- }
- }
- void Print(float* res, int N, int M) {
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < M; j++) {
- printf("%f ", res[i*M + j]);
- }
- printf("\n");
- }
- }
- int main() {
- int N, M;
- scanf("%d %d", &N, &M); //N -количество строк, M - количество столбцов
- float *A = calloc(N*M, 4);
- float *B = calloc(N*M, 4);
- float *res = calloc(N*M, 4);
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < M; j++) {
- scanf("%f", &A[i*M + j]);
- }
- }
- for (int i = 0; i < M; i++) {
- for (int j = 0; j < N; j++) {
- scanf("%f", &B[j*M + i]);
- }
- }
- multiply(A, B,res, N, M);
- Print(res, N, N);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment