Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #define ITERATIONS_NUM 10
- #define RANDOM_MAX 100
- #define RANDOM_MIN 1
- #define ARRAY_SIZE 8192
- #define MIN_SIZE 2048
- typedef struct
- {
- float a, b, c, d;
- } vector;
- clock_t start, stop;
- double time_result;
- vector vecs_arr[ARRAY_SIZE];
- //randomize values for vectors
- void randomizer() //N = 2048 || 4096 || 8192
- {
- for(int i = 0; i < ARRAY_SIZE; ++i)
- {
- vecs_arr[i].a = rand()%RANDOM_MAX+RANDOM_MIN;
- vecs_arr[i].b = rand()%RANDOM_MAX+RANDOM_MIN;
- vecs_arr[i].c = rand()%RANDOM_MAX+RANDOM_MIN;
- vecs_arr[i].d = rand()%RANDOM_MAX+RANDOM_MIN;
- }
- }
- //SIMD
- double addSIMD(vector *vec1, vector *vec2)
- {
- vector vec;
- start = clock();
- asm("movups %1, %%xmm0 \n"
- "movups %2, %%xmm1 \n"
- "addps %%xmm1, %%xmm0 \n"
- "movups %%xmm0, %0 \n"
- : "=m" (vec)
- : "m" (vec1),"m" (vec2)
- );
- stop = clock();
- time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
- return time_result;
- }
- double subSIMD(vector *vec1, vector *vec2)
- {
- vector vec;
- start = clock();
- asm("movups %1, %%xmm0 \n"
- "movups %2, %%xmm1 \n"
- "subps %%xmm1, %%xmm0 \n"
- "movups %%xmm0, %0 \n"
- : "=m" (vec)
- : "m" (vec1),"m" (vec2)
- );
- stop = clock();
- time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
- return time_result;
- }
- double mulSIMD(vector *vec1, vector *vec2)
- {
- vector vec;
- start = clock();
- asm("movups %1, %%xmm0 \n"
- "movups %2, %%xmm1 \n"
- "mulps %%xmm1, %%xmm0 \n"
- "movups %%xmm0, %0 \n"
- : "=m" (vec)
- : "m" (vec1),"m" (vec2)
- );
- stop = clock();
- time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
- return time_result;
- }
- double divSIMD(vector *vec1, vector *vec2)
- {
- vector vec;
- start = clock();
- asm("movups %1, %%xmm0 \n"
- "movups %2, %%xmm1 \n"
- "divps %%xmm1, %%xmm0 \n"
- "movups %%xmm0, %0 \n"
- : "=m" (vec)
- : "m" (vec1),"m" (vec2)
- );
- stop = clock();
- time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
- return time_result;
- }
- //SISD
- double addSISD(float *a, float *b)
- {
- float res;
- start = clock();
- asm("fld %1 \n"
- "fadd %2 \n"
- "fstp %0 \n"
- : "=m" (res)
- : "m" (a),"m" (b)
- );
- stop = clock();
- time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
- return time_result;
- }
- double subSISD(float *a, float *b)
- {
- float res;
- start = clock();
- asm("fld %1 \n"
- "fsub %2 \n"
- "fstp %0 \n"
- : "=m" (res)
- : "m" (a),"m" (b)
- );
- stop = clock();
- time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
- return time_result;
- }
- double mulSISD(float *a, float *b)
- {
- float res;
- start = clock();
- asm("fld %1 \n"
- "fmul %2 \n"
- "fstp %0 \n"
- : "=m" (res)
- : "m" (a),"m" (b)
- );
- stop = clock();
- time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
- return time_result;
- }
- double divSISD(float *a, float *b)
- {
- float res;
- start = clock();
- asm("fld %1 \n"
- "fdiv %2 \n"
- "fstp %0 \n"
- : "=m" (res)
- : "m" (a),"m" (b)
- );
- stop = clock();
- time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
- return time_result;
- }
- int main (int argc, char* argv[])
- {
- double add_time_simd, sub_time_simd, mul_time_simd, div_time_simd;
- double add_time_sisd, sub_time_sisd, mul_time_sisd, div_time_sisd;
- FILE *fp;
- if ((fp=fopen("test.txt", "w"))==NULL) {
- printf ("Cannot open file!\n");
- exit(1);
- }
- for(int i = MIN_SIZE; i < ARRAY_SIZE+1; i+=i)
- {
- randomizer();
- add_time_simd = 0; sub_time_simd = 0; mul_time_simd = 0; div_time_simd = 0;
- add_time_sisd = 0; sub_time_sisd = 0; mul_time_sisd = 0; div_time_sisd = 0;
- for(int j = 0; j < ITERATIONS_NUM; ++j)
- {
- //SIMD
- for(int k = 0; k < i; ++k)
- {
- add_time_simd += addSIMD(&vecs_arr[k], &vecs_arr[k]);
- sub_time_simd += subSIMD(&vecs_arr[k], &vecs_arr[k]);
- mul_time_simd += mulSIMD(&vecs_arr[k], &vecs_arr[k]);
- div_time_simd += divSIMD(&vecs_arr[k], &vecs_arr[k]);
- }
- //SISD - ile liczb to 8192? 16tys czy 8tys?
- for(int k = 0; k < i; ++k)
- {
- add_time_sisd += addSISD(&vecs_arr[k].a, &vecs_arr[k].a);
- add_time_sisd += addSISD(&vecs_arr[k].b, &vecs_arr[k].b);
- add_time_sisd += addSISD(&vecs_arr[k].c, &vecs_arr[k].c);
- add_time_sisd += addSISD(&vecs_arr[k].d, &vecs_arr[k].d);
- sub_time_sisd += subSISD(&vecs_arr[k].a, &vecs_arr[k].a);
- sub_time_sisd += subSISD(&vecs_arr[k].b, &vecs_arr[k].b);
- sub_time_sisd += subSISD(&vecs_arr[k].c, &vecs_arr[k].c);
- sub_time_sisd += subSISD(&vecs_arr[k].d, &vecs_arr[k].d);
- mul_time_sisd += mulSISD(&vecs_arr[k].a, &vecs_arr[k].a);
- mul_time_sisd += mulSISD(&vecs_arr[k].b, &vecs_arr[k].b);
- mul_time_sisd += mulSISD(&vecs_arr[k].c, &vecs_arr[k].c);
- mul_time_sisd += mulSISD(&vecs_arr[k].d, &vecs_arr[k].d);
- div_time_sisd += divSISD(&vecs_arr[k].a, &vecs_arr[k].a);
- div_time_sisd += divSISD(&vecs_arr[k].b, &vecs_arr[k].b);
- div_time_sisd += divSISD(&vecs_arr[k].c, &vecs_arr[k].c);
- div_time_sisd += divSISD(&vecs_arr[k].d, &vecs_arr[k].d);
- }
- }
- fprintf (fp, "TYP OBLICZEN: SIMD \n Liczba liczb: %d \n Sredni czas:\n +%f\n -%f\n *%f\n /%f\n",
- i, add_time_simd/10, sub_time_simd/10, mul_time_simd/10, div_time_simd/10);
- fprintf (fp, "TYP OBLICZEN: SISD \n Liczba liczb: %d \n Sredni czas:\n +%f\n -%f\n *%f\n /%f\n",
- i, add_time_sisd/10, sub_time_sisd/10, mul_time_sisd/10, div_time_sisd/10);
- }
- fclose (fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement