-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
207 lines (153 loc) · 5.41 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <stdio.h>
#include <stdint.h>
#include "clineest.h"
#include "mPrimes.h"
#include <string.h>
#include "mpi.h"
#include <sys/timeb.h>
#include <malloc.h>
struct timeb tstarts, tends;//timers
void MPIPrimesdefault(int argc,char* argv[], Number Limit, int CACHESIZE,int id, int p);
void MPIPrimesMAX(int argc,char* argv[], Number Limit, int CACHESIZE, char vb,int id, int p);
void MPIPrimesFILESSPEED(int argc,char* argv[], Number Limit, int CACHESIZE, char vb,int id, int p);
void MPIPrimesExperimental(int argc,char* argv[], Number Limit, int CACHESIZE, char vb,int id, int p);
int main(int argc,char* argv[]){
unsigned int cachesize = 256*1024;//default
int choice = -1;//other options 4 available
char verbose = 0;
Number pLimit = MAX_INT_SIZE; //assume as default
//MPI
int id;
int p;
MPI_Status status;
//Initialise
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&id);
MPI_Comm_size(MPI_COMM_WORLD,&p);
int i;
for(i = 1;i<argc;++i)
if(strstr(argv[i], "-C")!=NULL){
printf("Attempting to find cacheline for Node %i...\n",id);
cachesize = get_cache_line(1024*1024*32); //cachelines (L2) are getting big these days
if(cachesize<32768) cachesize = 256*1024;
printf("Node %i Cacheline:%i\n\n",id,cachesize);
}
if (argc<3){//asume default, output prime file at decent speed
MPIPrimesdefault(argc,argv, pLimit, cachesize,id,p);//assume default cacheline of 64kb
} else {
int i;
int multiplier;
for (i=1; i< argc; i++) {
switch(argv[i][1]){
case 'S':
case 's':
choice = atoi(argv[i+1]);
break;
case 'V':
case 'v':
verbose = 1;
break;
case 'N':
case 'n':
multiplier = atoi(argv[i+1]);
pLimit = pLimit*multiplier;
if((pLimit<1)||(pLimit>MAX_LONGLONG_SIZE)){
printf("Invalid input size\n");
return -2;
}
else{
if (id == 0)printf("Prime calc for:%llu\n",pLimit);
}
default:
break;
}//end of switch
}//end of for
MPI_Barrier(MPI_COMM_WORLD);//stop before finish
if(choice == 1){
MPIPrimesdefault(argc,argv, pLimit, cachesize,id,p);
}else if(choice ==2){
MPIPrimesMAX(argc,argv, pLimit, cachesize,verbose,id,p);
}else if(choice ==3){
MPIPrimesExperimental(argc,argv, pLimit, cachesize,verbose,id,p);
}else{
printf("usage: -C -S [1-3] -V -N [FACTOR]\n Invalid choice:%i",choice);
return -1;
}
}
return 0;
}
void MPIPrimesdefault(int argc,char* argv[], Number Limit, int CACHESIZE,int id, int p){
if(id==0) printf("Prime default calc for: %llu numbers to be considered\n\n",Limit);
ftime(&tstarts);
//seperate into chunks to calculate
Number a = (Limit)/(p);
Number from = a*(id);
Number to = from + a;
primesMPIFile(CACHESIZE,from,to,id);
ftime(&tends);
MPI_Barrier(MPI_COMM_WORLD);//stop before finish
float diff=((float) (1000.0 * (tends.time - tstarts.time) + (tends.millitm - tstarts.millitm)))/1000.0;
printf("Node %i) Prime Computation complete, time taken:%f\n\n",id,diff);
if(id==0){
printf("Saving to file...\n");
system("sh fileConcat.sh &");
}
MPI_Finalize();
}
void MPIPrimesExperimental(int argc,char* argv[], Number Limit, int CACHESIZE, char vb,int id, int p){
if(id==0) printf("Prime Experimental calc for:%llu numbers to be considered\n\n",Limit);
if(vb)
if(id==0)
printf("\nExperimental with memory alloc changes, usually crashes, file forking\n");
//timing
ftime(&tstarts);
mallopt(M_MXFAST, 1024);//mem size addressing
//seperate into chunks to calculate
Number a = (Limit)/(p);
Number from = a*(id);
Number to = from + a;
primesMPIFile(CACHESIZE,from,to,id);
ftime(&tends);
if(vb) printf("\nNode %i)Barrier reached\n",id);
MPI_Barrier(MPI_COMM_WORLD);//stop before finish
if(vb) printf("\nNode %i)Barrier left\n\n",id);
float diff=((float) (1000.0 * (tends.time - tstarts.time) + (tends.millitm - tstarts.millitm)))/1000.0;;
if(vb){
printf("Node %i) Prime Computation complete, time taken:%f\n\n",id,diff);
printf("Node %i) Calc from:%llu to:%llu\n",id, from, to);
} else {
printf("Node %i) Computation time taken:%f\n\n",id,diff);
}
if(id==0){
printf("Saving to file...\n");
system("sh fileConcat.sh &");
}
MPI_Finalize();
}
void MPIPrimesMAX(int argc,char* argv[], Number Limit, int CACHESIZE, char vb,int id, int p){
if(id==0) printf("Prime Max speed calc for:%llu numbers to be considered\n\n !No file saving!\n",Limit);
if(vb)
if(id==0){
printf("\nMultithreaded compute - Aiming to achieve lowest prime calculation time\n");
printf("\nNumber of threads to use:%i\n", p);
}
//timing
ftime(&tstarts);
//seperate into chunks to calculate
Number a = (Limit)/(p);
Number from = a*(id);
Number to = from + a;
Number found = primesMPI(CACHESIZE,from,to);
ftime(&tends);
if(vb) printf("\nNode %i)Barrier reached\n",id);
MPI_Barrier(MPI_COMM_WORLD);//stop before finish
if(vb) printf("\nNode %i)Barrier left\n\n",id);
float diff=((float) (1000.0 * (tends.time - tstarts.time) + (tends.millitm - tstarts.millitm)))/1000.0;
if(vb){
printf("Node %i) Prime Computation complete, time taken:%f\n",id,diff);
printf("Node %i) Calc from:%llu to:%llu, Primes found: %llu\n",id, from, to, found);
} else {
printf("Node %i) Computation time taken:%f, Primes found: %llu\n\n",id,diff,found);
}
MPI_Finalize();
}