-
Notifications
You must be signed in to change notification settings - Fork 0
/
Que2.c
179 lines (162 loc) · 4.61 KB
/
Que2.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define BILLION 1000000000.0
long int len = 0;
int *data[2] = {NULL,NULL};
long int get_no_of_lines(char* filename){
FILE* fp = fopen(filename,"r");
char ch;
long int lines = 0;
while(!feof(fp)){
ch = fgetc(fp);
if(ch == '\n'){
lines++;
}
}
fclose(fp);
//printf("lines=%ld\n",lines);
return lines;
}
void create_csv(char* filename){
FILE* fp = fopen(filename,"w");
for(long int i=0;i<10000;i++){
int x = ((double)rand() / (double)(RAND_MAX+1.0) )*1000;
int y = ((double)rand() / (double)(RAND_MAX+1.0) )*1000;
fprintf(fp,"%d,%d\n",x,y);
}
fclose(fp);
}
void read_file(char* filename){
FILE* fp = fopen(filename,"r");
for(long int i=0;i<len;i++){
fscanf(fp,"%d,%d\n",&data[0][i],&data[1][i]);
}
fclose(fp);
}
double compute_t(long int len,int* values[2]){
double sum1 = 0, sum2 = 0;
for(long int i=0;i<len;i++){
sum1 += values[0][i];
sum2 += values[1][i];
}
double mean1 = sum1 / len;
double mean2 = sum2 / len;
double SD1 = 0,SD2 = 0;
for(long int i=0;i<len;i++){
SD1 += (values[0][i]-mean1)*(values[0][i]-mean1);
SD2 += (values[1][i]-mean2)*(values[1][i]-mean2);
}
SD1 = SD1/len;
SD2 = SD1/len;
double t = (mean2-mean1)/sqrt((SD1+SD2)/len);
return t;
}
void get_sample(int size, int* sample[2], int times){
int i=0;
while(i<size){
int t = (int)time(NULL) ^ i ^ times;
double rand = (double)rand_r(&t) / (double)(RAND_MAX+1.0) ;
int index = len*rand;
sample[0][i] = data[0][index];
sample[1][i] = data[1][index];
i++;
}
}
struct args
{
long int times;
int size;
double original_t;
};
void* get_higher_t_count(void* params){
struct args *param = (struct args*) params;
long int times = param->times;
const int size = param->size;
double original_t = param->original_t;
//declare sample
int sample[2][size];
//no of times we get a higher t_stats
long int* count = (long int*)malloc(sizeof(long int));
*count = 0;
double t;
int* sample_data[2] = {sample[0],sample[1]};
while(times--){
get_sample(size, sample_data, times);//times for random generator
t = compute_t(size, sample_data);
if(t >= original_t){
(*count)++;
}
}
//printf("count = %ld\n",*count);
pthread_exit(count);
}
int main(int argc, char **argv){
int thread_count = atoi(argv[1]);
long int nTimes = atoi(argv[2]);
int size = 20;
long int n_per_thread = nTimes / thread_count;
pthread_t thread[thread_count];
int i=0;
long int *result = NULL,sum = 0;
//create file
char filename[] = "data.csv";
create_csv(filename);
//read data from file
const long int length = get_no_of_lines(filename);
int values[2][length];
len = length;
data[0] = values[0];
data[1] = values[1];
read_file(filename);
//time computation
struct timespec start, end;
clock_gettime(CLOCK_REALTIME, &start);
//get original t
double original_t = compute_t(len, data);
printf("original_t = %lf\n",original_t);
while(i<thread_count){
struct args param = { n_per_thread, size, original_t };
if(pthread_create(thread+i, NULL, get_higher_t_count, (void*)¶m)) {
fprintf(stderr, "Error creating thread\n");
return 1;
}
i++;
}
i=0;
while(i<thread_count){
if(pthread_join(thread[i], (void**)&result)) {
fprintf(stderr, "Error joining thread\n");
return 2;
}
sum += *result;
free(result);
result = NULL;
i++;
}
clock_gettime(CLOCK_REALTIME, &end);
// time_spent = end - start
double time_spent = (end.tv_sec - start.tv_sec) +
(end.tv_nsec - start.tv_nsec) / BILLION;
printf("no of times we got higher value = %ld\n",sum);
printf("Time elpased is %lf seconds\n", time_spent);
FILE *fp1 = fopen("report.txt", "a");
fprintf(fp1,"Que2: #Threads = %d, #Higher_than_t = %ld, Time elpased is %lf seconds\n",thread_count,sum,time_spent);
fclose(fp1);
//writing data into file
FILE *fp = NULL;
if(nTimes == 10000000){
fp = fopen("que2_plot_data_10M.txt", "a");
}
else
fp = fopen("que2_plot_data_1M.txt", "a");
if (fp == NULL) {
fprintf(stderr,"Couldn't open file...");
return 3;
}
fprintf(fp,"%d %lf \n",thread_count,time_spent);
fclose(fp);
return 0;
}