MPI: cleaner map func

This commit is contained in:
violette 2024-04-10 16:41:04 -04:00
parent 03d8401d5f
commit ce4e52a5ac

View file

@ -39,6 +39,12 @@ main(int argc, char **argv)
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
if (argc != 2) {
printf("Please enter key as arg");
exit(-1);
}
char *key = argv[1];
int localmax = 0, globalmax = 0;
// Get the name of the processor
@ -146,6 +152,33 @@ get_num_attr(char *chunk)
return num_attr;
}
int get_max_time(char *lines, size_t num_lines, size_t num_attr, size_t max_attr)
{
struct tm dep_time, arr_time;
time_t max_time = 0;
for (size_t k = 0; k < num_lines; ++k) {
memcpy(&dep_time,
get_word(lines, k, DEPARTURE_TIME, num_attr, max_attr),
sizeof(struct tm));
memcpy(&arr_time,
get_word(lines, k, ARRIVAL_TIME, num_attr, max_attr),
sizeof(struct tm));
// arr_time.tm_sec);
strptime(get_word(lines, k, DEPARTURE_TIME, num_attr,
max_attr),
"%H:%M:%S", &dep_time);
strptime(get_word(lines, k, ARRIVAL_TIME, num_attr,
max_attr),
"%H:%M:%S", &arr_time);
time_t tmp = substr_time(arr_time, dep_time);
if (tmp > max_time)
max_time = tmp;
}
return max_time;
}
int
do_map(char *chunk, size_t num_char)
{
@ -163,30 +196,8 @@ do_map(char *chunk, size_t num_char)
fill_lines(chunk, num_char, num_lines, num_attr, max_attr_size, lines);
struct tm dep_time, arr_time;
time_t max_time = 0;
for (size_t k = 0; k < num_lines; ++k) {
memcpy(&dep_time,
get_word(lines, k, DEPARTURE_TIME, num_attr,
max_attr_size),
sizeof(struct tm));
memcpy(&arr_time,
get_word(lines, k, ARRIVAL_TIME, num_attr,
max_attr_size),
sizeof(struct tm));
// arr_time.tm_sec);
int max_time = get_max_time(lines, num_lines, num_attr, max_attr_size);
strptime(get_word(lines, k, DEPARTURE_TIME, num_attr,
max_attr_size),
"%H:%M:%S", &dep_time);
strptime(get_word(lines, k, ARRIVAL_TIME, num_attr,
max_attr_size),
"%H:%M:%S", &arr_time);
time_t tmp = substr_time(arr_time, dep_time);
if (tmp > max_time)
max_time = tmp;
}
free(lines);
free(chunk);
return max_time;