-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
172 lines (156 loc) · 4.3 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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mpi.h"
int isSubstring(char s1[],char s2[] )
{
int M = strlen(s1);
int N = strlen(s2);
/* A loop to slide pat[] one by one */
int i ;
for (i = 0; i <= N - M; i++)
{
int j;
/* For current index i, check for pattern match */
for (j = 0; j < M; j++)
if (s2[i + j] != s1[j])
break;
if (j == M)
return i;
}
return -1;
}
void clear_file(const char *filename)
{
FILE *output = fopen(filename, "w");
fclose(output);
}
int main(int argc, char * argv[])
{
double starttime ;
int my_rank; /* rank of process */
int p ; /* number of process */
int source = 0; /* rank of sender */
int dest = 0 ; /* rank of receiver */
int tag = 0; /* tag for messages */
char message[100]; /* storage for message */
MPI_Status status; /* return status for */
int count = 0 ;
int irecv[100] ;
int isend[100] ;
int start = 1 ;
int end = 50;
/* Start up MPI */
MPI_Init( &argc, &argv );
/* Find out process rank */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
/* Find out number of process */
MPI_Comm_size(MPI_COMM_WORLD, &p);
int i, j ;
char query[100] ;
int sum = 0 ;
int average=50/p;
int remainder=50%p ;
int tempp ;
int input ;
FILE * fp;
fp = fopen ("output.txt","w");
if(my_rank== 0)
{
starttime = MPI_Wtime();
sum = 0 ;
printf("Enter Your Query: ");
scanf("%[^\n]%*c",query);
tempp=0;
for(i=0; i<p; i++)
{
{
if (remainder<=0)
{
input = average ;
printf("%d\n", input);
}
else if (remainder>0)
{
input = average+1 ;
printf("%d\n", input);
}
isend[i]=tempp;
tempp+=input;
irecv[i]=tempp;
}
}
}
MPI_Bcast(&sum,1, MPI_CHAR, 0, MPI_COMM_WORLD);
// sum += my_rank ;
MPI_Bcast(&query,strlen(query), MPI_CHAR, 0, MPI_COMM_WORLD);
MPI_Bcast(&count,1, MPI_CHAR, 0, MPI_COMM_WORLD);
MPI_Scatter(&isend, 1, MPI_INT, &irecv, 1, MPI_INT, 0,MPI_COMM_WORLD);
count = 0 ;
const char s[2] = " ";
int tokCounter = 0, wordsCounter = 0;
int Result ;
FILE *filePointer;
char intt [10];
char filename[100] = "Aristo-Mini-Corpus P-";
char temp[100];
strcpy(temp,filename);
for (i = start ; i <= end ; i++ )
{
sprintf(intt,"%d",i);
strcat(temp,intt);
char FileName[100] = "Aristo-Mini-Corpus\\";
strcat(FileName,temp);
strcat(FileName,".txt");
filePointer = fopen(FileName, "r");
memset(temp,' ',100);
strcpy(temp,filename);
char dataToBeRead[1000];
if(filePointer == NULL)
{
perror("Unable to open file!");
return (-1) ;
}
else
{
count=0;
while (fgets(dataToBeRead, sizeof(dataToBeRead), filePointer) != NULL)
{
char tempquery[100];
strcpy(tempquery, query);
char * p = strtok(tempquery, s);
tokCounter =0;
wordsCounter =0;
while( p != NULL)
{
tokCounter++;
if (isSubstring(p, dataToBeRead) != -1)
{
wordsCounter++;
}
p = strtok(NULL, s);
}
if (wordsCounter == tokCounter)
{
count++;
sum++ ;
fprintf (fp,"%s\n",dataToBeRead);
}
free(p);
}
}
fclose(filePointer) ;
}
MPI_Reduce(&sum, &Result, 1, MPI_INT, MPI_SUM, 0,MPI_COMM_WORLD);
if (my_rank==0)
{
printf("Search Results Found: %d\n",Result);
double finish = MPI_Wtime();
double exec = finish - starttime ;
printf("Execution time of master = %f\n",exec);
}
fprintf (fp,"Search Results Found: %d\n",sum);
fclose (fp);
MPI_Finalize();
return 0;
}