forked from IpovsOperatingSystems/Lab4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel_sum.c
160 lines (138 loc) · 3.81 KB
/
parallel_sum.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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <getopt.h>
#include "utils.h"
struct SumArgs {
int *array;
int begin;
int end;
};
int Sum(const struct SumArgs *args) {
int sum = 0;
for(int i = args->begin; i < args->end; ++i) {
sum += (args->array)[i];
}
return sum;
}
void *ThreadSum(void *args) {
struct SumArgs *sum_args = (struct SumArgs *)args;
return (void *)(size_t)Sum(sum_args);
}
int main(int argc, char **argv) {
uint32_t threads_num = 0;
uint32_t array_size = 0;
uint32_t seed = 0;
while (true) {
int current_optind = optind ? optind : 1;
static struct option options[] = {{"seed", required_argument, 0, 0},
{"threads_num", required_argument, 0, 0},
{"array_size", required_argument, 0, 0},
{0, 0, 0, 0}};
int option_index = 0;
int c = getopt_long(argc, argv, "", options, &option_index);
if (c == -1) break;
switch (c) {
case 0:
switch (option_index) {
case 0:
{
seed = atoi(optarg);
break;
}
case 1:
{
int tn;
tn = atoi(optarg);
if(tn <= 0) {
return 1;
}
threads_num = tn;
break;
}
case 2:
{
int as = atoi(optarg);
if(as <= 0)
{
return 1;
}
array_size = as;
break;
}
defalut:
printf("Index %d is out of options\n", option_index);
}
break;
case '?':
break;
default:
printf("getopt returned character code 0%o?\n", c);
}
}
if (optind < argc) {
printf("Has at least one no option argument\n");
return 1;
}
if (seed == -1 || array_size == -1 || threads_num == -1 || optind == 1) {
printf("Usage: %s --seed \"num\" --threads_num \"num\" --array_size \"num\" \n",
argv[0]);
return 1;
}
pthread_t *threads = malloc(sizeof(pthread_t) * threads_num);
int *array = malloc(sizeof(int) * array_size);
GenerateArray(array, array_size, seed);
printf("Generated array \n");
for(int i = 0; i < array_size; ++i) {
printf("%d, ", array[i]);
if (i != 0 && (i+1) % (array_size/threads_num) == 0) { printf("\n"); }
}
struct SumArgs *args = malloc(sizeof(struct SumArgs) * threads_num);
int tc = array_size/threads_num;
for(int i = 0; i < threads_num; ++i)
{
args[i].array = array;
args[i].begin = tc*i;
if(i == threads_num - 1) {
args[i].end = tc*(i+1) + array_size%threads_num;
} else {
args[i].end = tc*(i+1);
}
}
struct timeval start_time;
gettimeofday(&start_time, NULL);
for (uint32_t i = 0; i < threads_num; ++i) {
if (pthread_create(&threads[i], NULL, ThreadSum, (void *)&(args[i]))) {
printf("Error: pthread_create failed!\n");
return 1;
}
}
int total_sum = 0;
int tnn = threads_num;
for (uint32_t i = 0; i < tnn; ++i) {
int sum = 0;
pthread_join(threads[i], (void **)&sum);
printf("Sum: %d\n", sum);
total_sum += sum;
}
struct timeval finish_time;
gettimeofday(&finish_time, NULL);
double elapsed_time = (finish_time.tv_sec - start_time.tv_sec) * 1000.0;
elapsed_time += (finish_time.tv_usec - start_time.tv_usec) / 1000.0;
free(array);
free(args);
free(threads);
printf("Total: %d\n", total_sum);
printf("Elapsed time: %fms\n", elapsed_time);
return 0;
}