This repository has been archived by the owner on Dec 18, 2020. It is now read-only.
forked from presi11/SO-Lab3-20201
-
Notifications
You must be signed in to change notification settings - Fork 0
/
saxpyT(deprecated).c
215 lines (195 loc) · 4.86 KB
/
saxpyT(deprecated).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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* @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>
#include <pthread.h>
/* Define globally accessible variables to perform SAXPY operation and a mutex */
double *X;
double a;
double *Y;
double *Y_avgs;
pthread_mutex_t mutex;
typedef struct
{
int start;
int end;
int max_iters;
int p;
} thread_args;
void *thread_SAXPY(void *args)
{
thread_args *input = (thread_args *) args;
int start = input->start;
int end = input->end;
int max_iters = input->max_iters;
int p = input->p;
int i,it;
double acc;
// printf("Thread values start = %d, end = %d, max_iters = %d, p = %d \n", start, end, max_iters, p);
//SAXPY iterative SAXPY mfunction
for (it = 0; it < max_iters; it++)
{
acc = 0;
for ( i = start; i < end; i++)
{
Y[i] = Y[i] + a * X[i];
acc += Y[i];
}
pthread_mutex_lock(&mutex);
Y_avgs[it] += (acc/p); //Critical section?
pthread_mutex_unlock(&mutex);
}
return 0;
}
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
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);
pthread_t threads[n_threads];
thread_args thread_input_array[n_threads];
//pthread_mutex_init(&mutex, NULL);
//int rc;
int t;
void *status;
//Create threads
for (t = 0; t < n_threads; t++)
{
thread_input_array[t].max_iters = max_iters;
thread_input_array[t].p = p;
thread_input_array[t].start = (p / n_threads) * t;
thread_input_array[t].end = (p / n_threads) * (t + 1);
//printf("In main: creating thread %d\n", t);
// rc = pthread_create(&threads[t], NULL, thread_SAXPY, (void *)&thread_input_array[t]);
pthread_create(&threads[t], NULL, &thread_SAXPY, &thread_input_array[t]);
// if (rc)
// {
// printf("ERROR; return code from pthread_create() is %d\n", rc);
// exit(-1);
// }
}
//Wait for threads
for (t = 0; t < n_threads; t++)
{
//rc = pthread_join(threads[t], &status);
pthread_join(threads[t], &status);
// if (rc)
// {
// printf("ERROR; return code from pthread_join() is %d\n", rc);
// exit(-1);
// }
#ifdef DEBUG
printf("Main: completed join with thread %d having a status of %ld\n", t, (long)status);
#endif
}
pthread_mutex_destroy(&mutex);
// Calculate avgs
// for (it = 0; it < max_iters; it++)
// {
// 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;
}