-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
143 lines (107 loc) · 3.63 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
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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#define COLUMNS 25
#define BUFFER_SIZE 100
// #define FILE_NAME "pubg-data.csv"
int **matrix; // 2-d array - dynamic
int data_per_thread;
void *thread_routine(void *args)
{
int start = *((int *)args);
int end = start + data_per_thread;
// printf("Thread is called - %u\n", pthread_self());
int max = 0;
for (size_t i = start; i < end; i++)
{
// printf("%d\n", matrix[i][22]); // walkDistance
if (matrix[i][22] > max)
max = matrix[i][22];
}
printf("%d to %d\n", start, end);
printf("Max from Thread ID: %d\n", max);
int *temp = (int *)malloc(sizeof(int));
*temp = max;
return (void *)temp;
}
int main(int argc, char const *argv[])
{
char *file_name = "pubg-data.csv";
FILE *fp;
fp = fopen(file_name, "r");
char column_names[1000];
fscanf(fp, "%s\n", column_names); // read columns header
// printf("%s\n", column_names);
int fd[2];
pipe(fd);
if (fork() == 0) // child
{
close(fd[0]); // close read end
dup2(fd[1], 1); // to stdout[1] - fd[1] = write
close(fd[1]);
execlp("wc", "wc", "-l", file_name, NULL);
}
char buffer[BUFFER_SIZE];
close(fd[1]); // close write end
read(fd[0], buffer, BUFFER_SIZE);
int n_lines = atoi(buffer); // char[] to int
close(fd[0]);
// printf("%d\n", n_lines);
data_per_thread = n_lines / 10;
// if (n_lines > 1000)
// {
// data_per_thread = 100;
// }
// else if (n_lines > 10000)
// {
// data_per_thread = 2000;
// }
int n_threads = n_lines / data_per_thread;
matrix = (int **)malloc(sizeof(int *) * n_lines);
pthread_t *ptr_tid;
ptr_tid = (pthread_t *)malloc(sizeof(pthread_t) * n_threads);
int *temp = (int *)malloc(sizeof(int) * n_threads);
for (size_t i = 0; i < n_lines; i++)
{
matrix[i] = (int *)malloc(sizeof(int) * COLUMNS);
}
// data_per_thread = n_lines / n_threads;
int i = 0;
int thread_count = 0;
while (!feof(fp))
{
// printf("Kushal - %d\n",i);
// fscanf(fp, "%s\n", column_names); // read columns header
fscanf(fp, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", &matrix[i][0], &matrix[i][1], &matrix[i][2], &matrix[i][3], &matrix[i][4], &matrix[i][5], &matrix[i][6], &matrix[i][7], &matrix[i][8], &matrix[i][9], &matrix[i][10], &matrix[i][11], &matrix[i][12], &matrix[i][13], &matrix[i][14], &matrix[i][15], &matrix[i][16], &matrix[i][17], &matrix[i][18], &matrix[i][19], &matrix[i][20], &matrix[i][21], &matrix[i][22], &matrix[i][23], &matrix[i][24]);
// fscanf(fp, "%d,%d,%d", &matrix[i][0], &matrix[i][1], &matrix[i][2]);
// printf("%d,%d,%d,%d\n", matrix[i][0], matrix[i][1], matrix[i][2], matrix[i][24]);
i += 1;
// printf("%d\n",i);
if ((i % data_per_thread) == 0 || (i + 1) == n_lines)
{
if (i == (n_lines - 1))
i++;
temp[thread_count] = i;
pthread_create(&ptr_tid[thread_count++], NULL, thread_routine, (void *)&temp[thread_count - 1]);
}
}
// printf("Thread Count - %d\n", thread_count);
int *ret, max = 0;
for (size_t i = 0; i < thread_count; i++)
{
pthread_join(ptr_tid[i], (void *)&ret);
if (max < *ret)
max = *ret;
free(ret);
}
printf("Max - %d\n", max);
//free up memory
free(temp);
free(ptr_tid);
for (size_t i = 0; i < n_lines; i++)
free(matrix[i]);
free(matrix);
return 0;
}