-
Notifications
You must be signed in to change notification settings - Fork 21
/
saxpy.c
130 lines (118 loc) · 3.06 KB
/
saxpy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* @defgroup SAXPY saxpy
*
* @brief This file implements an iterative saxpy operation
*
* @param[in] <-p> {vector size}
* @param[in] <-s> {seed}
* @param[in] <-n> {number of threads to create}
* @param[in] <-i> {maximum itertions}
*
* @author Danny Munera
* @date 2020
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/time.h>
int main(int argc, char* argv[]){
// Variables to obtain command line parameters
unsigned int seed = 1;
int p = 10000000;
int n_threads = 2;
int max_iters = 1000;
// Variables to perform SAXPY operation
double* X;
double a;
double* Y;
double* Y_avgs;
int i, it;
// Variables to get execution time
struct timeval t_start, t_end;
double exec_time;
// Getting input values
int opt;
while((opt = getopt(argc, argv, ":p:s:n:i:")) != -1){
switch(opt){
case 'p':
printf("vector size: %s\n", optarg);
p = strtol(optarg, NULL, 10);
assert(p > 0 && p <= 2147483647);
break;
case 's':
printf("seed: %s\n", optarg);
seed = strtol(optarg, NULL, 10);
break;
case 'n':
printf("threads number: %s\n", optarg);
n_threads = strtol(optarg, NULL, 10);
break;
case 'i':
printf("max. iterations: %s\n", optarg);
max_iters = strtol(optarg, NULL, 10);
break;
case ':':
printf("option -%c needs a value\n", optopt);
break;
case '?':
fprintf(stderr, "Usage: %s [-p <vector size>] [-s <seed>] [-n <threads number>]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
srand(seed);
printf("p = %d, seed = %d, n_threads = %d, max_iters = %d\n", \
p, seed, n_threads, max_iters);
// initializing data
X = (double*) malloc(sizeof(double) * p);
Y = (double*) malloc(sizeof(double) * p);
Y_avgs = (double*) malloc(sizeof(double) * max_iters);
for(i = 0; i < p; i++){
X[i] = (double)rand() / RAND_MAX;
Y[i] = (double)rand() / RAND_MAX;
}
for(i = 0; i < max_iters; i++){
Y_avgs[i] = 0.0;
}
a = (double)rand() / RAND_MAX;
#ifdef DEBUG
printf("vector X= [ ");
for(i = 0; i < p-1; i++){
printf("%f, ",X[i]);
}
printf("%f ]\n",X[p-1]);
printf("vector Y= [ ");
for(i = 0; i < p-1; i++){
printf("%f, ", Y[i]);
}
printf("%f ]\n", Y[p-1]);
printf("a= %f \n", a);
#endif
/*
* Function to parallelize
*/
gettimeofday(&t_start, NULL);
//SAXPY iterative SAXPY mfunction
for(it = 0; it < max_iters; it++){
for(i = 0; i < p; i++){
Y[i] = Y[i] + a * X[i];
Y_avgs[it] += Y[i];
}
Y_avgs[it] = Y_avgs[it] / p;
}
gettimeofday(&t_end, NULL);
#ifdef DEBUG
printf("RES: final vector Y= [ ");
for(i = 0; i < p-1; i++){
printf("%f, ", Y[i]);
}
printf("%f ]\n", Y[p-1]);
#endif
// Computing execution time
exec_time = (t_end.tv_sec - t_start.tv_sec) * 1000.0; // sec to ms
exec_time += (t_end.tv_usec - t_start.tv_usec) / 1000.0; // us to ms
printf("Execution time: %f ms \n", exec_time);
printf("Last 3 values of Y: %f, %f, %f \n", Y[p-3], Y[p-2], Y[p-1]);
printf("Last 3 values of Y_avgs: %f, %f, %f \n", Y_avgs[max_iters-3], Y_avgs[max_iters-2], Y_avgs[max_iters-1]);
return 0;
}