8 |
8 |
#include <stdlib.h>
|
9 |
9 |
#include <mpi.h>
|
10 |
10 |
|
|
11 |
#ifdef TIME
|
|
12 |
#include <sys/time.h>
|
|
13 |
#endif
|
|
14 |
|
11 |
15 |
// Marsaglia RNG very simple implementation
|
12 |
16 |
#define znew ((z=36969*(z&65535)+(z>>16))<<16)
|
13 |
17 |
#define wnew ((w=18000*(w&65535)+(w>>16))&65535)
|
... | ... | |
55 |
59 |
int numtasks,rank,rc,tag=1,i;
|
56 |
60 |
float pi;
|
57 |
61 |
|
|
62 |
#ifdef TIME
|
|
63 |
struct timeval start,end;
|
|
64 |
long int useconds;
|
|
65 |
#endif
|
|
66 |
|
58 |
67 |
MPI_Status Stat;
|
59 |
68 |
|
60 |
69 |
rc = MPI_Init(&argc,&argv);
|
... | ... | |
76 |
85 |
printf("\t\t#1 : number of iterations (default 1 billion)\n\n");
|
77 |
86 |
}
|
78 |
87 |
|
79 |
|
part_iterations=iterations/numtasks;
|
|
88 |
part_iterations=iterations/numtasks+1;
|
80 |
89 |
// Split part of code
|
81 |
90 |
for (i=1;i<numtasks;i++) {
|
82 |
91 |
rc = MPI_Send(&part_iterations, 1, MPI_UNSIGNED_LONG, i, tag,
|
83 |
92 |
MPI_COMM_WORLD);
|
84 |
93 |
}
|
85 |
94 |
|
|
95 |
#ifdef TIME
|
|
96 |
gettimeofday(&start,(struct timezone *)0);
|
|
97 |
#endif
|
|
98 |
|
86 |
99 |
insides=MainLoopGlobal(part_iterations,seed_w,seed_z);
|
87 |
|
printf("\tOn %i, find %lu inside\n",rank,(unsigned long)insides);
|
88 |
|
|
|
100 |
|
|
101 |
#ifdef TIME
|
|
102 |
gettimeofday(&end,(struct timezone *)0);
|
|
103 |
useconds=(end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec;
|
|
104 |
|
|
105 |
printf("\tOn %i, find %lu inside in %lu useconds.\n",rank,
|
|
106 |
(unsigned long)insides,useconds);
|
|
107 |
#else
|
|
108 |
printf("\tOn %i, find %lu inside\n",rank,
|
|
109 |
(unsigned long)insides);
|
|
110 |
|
|
111 |
#endif
|
|
112 |
|
89 |
113 |
// Join part of code
|
90 |
114 |
for (i=1;i<numtasks;i++) {
|
91 |
115 |
rc = MPI_Recv(&inside[i], 1, MPI_UNSIGNED_LONG, i, tag,
|
... | ... | |
96 |
120 |
|
97 |
121 |
pi=4.*(float)insides/(float)((iterations/numtasks)*numtasks);
|
98 |
122 |
|
99 |
|
printf("\n\tPi=%f with error %f and %lu iterations\n\n",pi,
|
100 |
|
fabs(pi-4*atan(1))/pi,(unsigned long)iterations);
|
|
123 |
printf("\n\tPi=%.40f\n\twith error %.40f\n\twith %lu iterations\n\n",pi,
|
|
124 |
fabs(pi-4*atan(1.))/pi,(unsigned long)iterations);
|
101 |
125 |
}
|
102 |
126 |
else
|
103 |
127 |
{
|
... | ... | |
108 |
132 |
printf("\tOn %i, receive from master %lu\n",
|
109 |
133 |
rank,(unsigned long)part_iterations);
|
110 |
134 |
|
|
135 |
#ifdef TIME
|
|
136 |
gettimeofday(&start,(struct timezone *)0);
|
|
137 |
#endif
|
|
138 |
|
111 |
139 |
part_inside=MainLoopGlobal(part_iterations,seed_w,seed_z);
|
112 |
140 |
|
113 |
|
printf("\tOn %i, find %lu inside\n",rank,(unsigned long)part_inside);
|
|
141 |
#ifdef TIME
|
|
142 |
gettimeofday(&end,(struct timezone *)0);
|
|
143 |
useconds=(end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec;
|
114 |
144 |
|
|
145 |
printf("\tOn %i, find %lu inside in %lu useconds.\n",rank,
|
|
146 |
(unsigned long)part_inside,useconds);
|
|
147 |
#else
|
|
148 |
printf("\tOn %i, find %lu inside\n",rank,
|
|
149 |
(unsigned long)part_inside);
|
|
150 |
|
|
151 |
#endif
|
|
152 |
|
115 |
153 |
rc = MPI_Send(&part_inside, 1, MPI_UNSIGNED_LONG, 0, tag, MPI_COMM_WORLD);
|
116 |
154 |
}
|
117 |
155 |
|