-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchars_counter_parallelized_2.c
96 lines (79 loc) · 2.02 KB
/
chars_counter_parallelized_2.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <sys/time.h>
char *buf;
int length;
int ascii[128]={0};
int num_threads=12;
int **arr;
void init(){
arr = malloc(num_threads * sizeof(int *));
for(int i=0;i<num_threads; i++){
arr[i] = malloc(128 * sizeof(int));
}
}
void * worker( void *arg )
{
int tid, portion_size, portion_remainder, start, end;
tid = *(int *)(arg);
portion_size = length / num_threads;
portion_remainder = length % num_threads;
if(tid>=portion_remainder){
start = tid * portion_size + portion_remainder;
end = (tid+1) * portion_size + portion_remainder;
}
else{
start = tid * portion_size + tid;
end = (tid+1) * portion_size + (tid+1);
}
for(int i=start;i<end;i++){
arr[tid][(int)buf[i]]++;
}
}
int main(){
int i,j;
struct timeval tstart, tend;
double exectime;
init();
pthread_t * threads;
threads = (pthread_t *) malloc( 128 * sizeof(pthread_t) );
FILE *f = fopen("./lorem.txt", "r");
if (f)
{
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);
buf = malloc(length);
if (buf)
{
fread(buf, 1, length, f);
}
fclose(f);
}
gettimeofday( &tstart, NULL );
for (i = 0; i < num_threads; ++i ) {
int *tid;
tid = (int *) malloc( sizeof(int) );
*tid = i;
pthread_create( &threads[i], NULL, worker, (void *)tid );
}
for (i = 0; i < num_threads; ++i ) {
pthread_join( threads[i], NULL );
}
for(i=0;i<128;i++){
for(j=0;j<num_threads;j++){
ascii[i]+=arr[j][i];
}
}
gettimeofday( &tend, NULL );
for(int i=0;i<128;i++){
printf("Occurrences of Ascii %d = %d\n",i,ascii[i]);
}
exectime = (tend.tv_sec - tstart.tv_sec) * 1000.0; // sec to ms
exectime += (tend.tv_usec - tstart.tv_usec) / 1000.0; // us to ms
printf("File Length: %d\n",length);
printf( "Execution time:%.3lf sec\n", exectime/1000.0);
return 0;
}