-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.c
107 lines (87 loc) · 2.65 KB
/
main.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
/**
* Parallel Map Lab
* CS 241 - Fall 2016
*/
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "map.h"
#include "mappers.h"
#include "par_map.h"
#define SEED 241
void validate_args(int argc, char *argv[]) {
// Verify correct number of arguments:
if (argc != 4) {
fprintf(stderr, "usage: %s <mapper_name> <list_len> <num_threads>\n",
argv[0]);
exit(1);
}
// Verifying that list_len and num_threads are actually integers greater than
// 1:
char *endptr;
for (int i = 2; i <= 3; i++) {
char *str_value = argv[i];
long value = strtol(str_value, &endptr, 10);
if (*endptr != '\0') {
fprintf(stderr, "Failed to convert an [%s] to a long!\n", str_value);
exit(3);
}
if (value < 1) {
fprintf(stderr, "[%s] needs to be greater than or equal to 1!\n",
str_value);
exit(4);
}
}
}
double *gen_random_list(size_t num_elems) {
double *list = (double *)malloc(sizeof(double) * num_elems);
for (size_t i = 0; i < num_elems; ++i) {
list[i] = ((double)rand() / (double)RAND_MAX);
}
return list;
}
bool verify(double *output_list, double *input_list, mapper map_func,
size_t list_len) {
// call on 'map_func' to modify 'list' inplace
// then verify element wise with 'output_list'.
double *soln_list = map(input_list, list_len, map_func);
for (size_t i = 0; i < list_len; ++i) {
if (soln_list[i] != output_list[i]) {
return false;
}
}
free(output_list);
free(soln_list);
return true;
}
int main(int argc, char *argv[]) {
validate_args(argc, argv);
// Seeding random number generator
srand(SEED);
char *mapper_name = argv[1];
size_t list_len = strtol(argv[2], NULL, 10);
size_t num_threads = strtol(argv[3], NULL, 10);
double *list = gen_random_list(list_len);
mapper map_func = get_mapper(mapper_name);
double *list_copy = (double *)malloc(sizeof(double) * list_len);
memcpy(list_copy, list, sizeof(double) * list_len);
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
double *ret_list = par_map(list_copy, list_len, map_func, num_threads);
clock_gettime(CLOCK_MONOTONIC, &end);
double diff =
(end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1E9;
printf("par_map ran in %f seconds\n", diff);
bool passed = verify(ret_list, list_copy, map_func, list_len);
if (passed) {
printf("Congratulations you have succesfully ran par_map with %s on a list "
"with %zu elements, and %zu threads\n",
mapper_name, list_len, num_threads);
}
free(list);
free(list_copy);
return passed;
}