| 6 |
6 |
#include <math.h>
|
| 7 |
7 |
#include <stdio.h>
|
| 8 |
8 |
#include <stdlib.h>
|
|
9 |
#include <limits.h>
|
| 9 |
10 |
#include <mpi.h>
|
| 10 |
11 |
|
| 11 |
12 |
#ifdef TIME
|
| ... | ... | |
| 26 |
27 |
#define ITERATIONS 1000000000
|
| 27 |
28 |
|
| 28 |
29 |
#ifdef LONG
|
| 29 |
|
#define LENGTH unsigned long
|
|
30 |
#define LENGTH long long
|
| 30 |
31 |
#else
|
| 31 |
|
#define LENGTH unsigned int
|
|
32 |
#define LENGTH int
|
| 32 |
33 |
#endif
|
| 33 |
34 |
|
| 34 |
35 |
LENGTH MainLoopGlobal(LENGTH iterations,unsigned int seed_w,unsigned int seed_z)
|
| ... | ... | |
| 78 |
79 |
if (rank==0) {
|
| 79 |
80 |
|
| 80 |
81 |
if (argc > 1) {
|
| 81 |
|
iterations=(LENGTH)atol(argv[1]);
|
|
82 |
iterations=(LENGTH)atoll(argv[1]);
|
| 82 |
83 |
}
|
| 83 |
84 |
else {
|
| 84 |
85 |
printf("\n\tPi : Estimate Pi with Monte Carlo exploration\n\n");
|
| 85 |
86 |
printf("\t\t#1 : number of iterations (default 1 billion)\n\n");
|
| 86 |
87 |
}
|
| 87 |
88 |
|
|
89 |
printf ("Sizeof int = %d bytes.\n", sizeof(int));
|
|
90 |
printf ("Sizeof long = %d bytes.\n", sizeof(long));
|
|
91 |
printf ("Sizeof long long = %d bytes.\n", sizeof(long long));
|
|
92 |
|
|
93 |
printf ("Max int = %u\n", INT_MAX);
|
|
94 |
printf ("Max long = %ld\n", LONG_MAX);
|
|
95 |
printf ("Max long long = %lld\n", LLONG_MAX);
|
|
96 |
|
| 88 |
97 |
part_iterations=iterations/numtasks+1;
|
|
98 |
|
| 89 |
99 |
// Split part of code
|
| 90 |
100 |
for (i=1;i<numtasks;i++) {
|
| 91 |
|
rc = MPI_Send(&part_iterations, 1, MPI_UNSIGNED_LONG, i, tag,
|
|
101 |
|
|
102 |
#ifdef LONG
|
|
103 |
rc = MPI_Send(&part_iterations, 1, MPI_LONG_LONG, i, tag,
|
| 92 |
104 |
MPI_COMM_WORLD);
|
|
105 |
#else
|
|
106 |
rc = MPI_Send(&part_iterations, 1, MPI_INT, i, tag,
|
|
107 |
MPI_COMM_WORLD);
|
|
108 |
#endif
|
| 93 |
109 |
}
|
| 94 |
110 |
|
| 95 |
111 |
#ifdef TIME
|
| ... | ... | |
| 102 |
118 |
gettimeofday(&end,(struct timezone *)0);
|
| 103 |
119 |
useconds=(end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec;
|
| 104 |
120 |
|
| 105 |
|
printf("\tOn %i, find %lu inside in %lu useconds.\n",rank,
|
| 106 |
|
(unsigned long)insides,useconds);
|
|
121 |
printf("\tOn %i, find %lld inside in %lu useconds.\n",rank,
|
|
122 |
(long long)insides,useconds);
|
| 107 |
123 |
#else
|
| 108 |
|
printf("\tOn %i, find %lu inside\n",rank,
|
| 109 |
|
(unsigned long)insides);
|
|
124 |
printf("\tOn %i, find %lld inside\n",rank,
|
|
125 |
(long long)insides);
|
| 110 |
126 |
|
| 111 |
127 |
#endif
|
| 112 |
128 |
|
| 113 |
129 |
// Join part of code
|
| 114 |
130 |
for (i=1;i<numtasks;i++) {
|
| 115 |
|
rc = MPI_Recv(&inside[i], 1, MPI_UNSIGNED_LONG, i, tag,
|
|
131 |
#ifdef LONG
|
|
132 |
rc = MPI_Recv(&inside[i], 1, MPI_LONG_LONG, i, tag,
|
| 116 |
133 |
MPI_COMM_WORLD, &Stat);
|
|
134 |
#else
|
|
135 |
rc = MPI_Recv(&inside[i], 1, MPI_INT, i, tag,
|
|
136 |
MPI_COMM_WORLD, &Stat);
|
|
137 |
#endif
|
| 117 |
138 |
printf("\tReceive %lu inside from %i\n",(unsigned long)inside[i],i);
|
| 118 |
139 |
insides+=inside[i];
|
| 119 |
140 |
}
|
| 120 |
141 |
|
| 121 |
142 |
pi=4.*(float)insides/(float)((iterations/numtasks)*numtasks);
|
| 122 |
143 |
|
| 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);
|
|
144 |
printf("\n\tPi=%.40f\n\twith error %.40f\n\twith %lld iterations\n\n",pi,
|
|
145 |
fabs(pi-4*atan(1.))/pi,(long long)iterations);
|
| 125 |
146 |
}
|
| 126 |
147 |
else
|
| 127 |
148 |
{
|
| 128 |
149 |
// Receive information from master
|
| 129 |
|
rc = MPI_Recv(&part_iterations, 1, MPI_UNSIGNED_LONG, 0, tag,
|
|
150 |
#ifdef LONG
|
|
151 |
rc = MPI_Recv(&part_iterations, 1, MPI_LONG_LONG, 0, tag,
|
| 130 |
152 |
MPI_COMM_WORLD, &Stat);
|
|
153 |
#else
|
|
154 |
rc = MPI_Recv(&part_iterations, 1, MPI_INT, 0, tag,
|
|
155 |
MPI_COMM_WORLD, &Stat);
|
|
156 |
#endif
|
| 131 |
157 |
|
| 132 |
|
printf("\tOn %i, receive from master %lu\n",
|
| 133 |
|
rank,(unsigned long)part_iterations);
|
|
158 |
printf("\tOn %i, receive from master %lld\n",
|
|
159 |
rank,(long long)part_iterations);
|
| 134 |
160 |
|
| 135 |
161 |
#ifdef TIME
|
| 136 |
162 |
gettimeofday(&start,(struct timezone *)0);
|
| ... | ... | |
| 142 |
168 |
gettimeofday(&end,(struct timezone *)0);
|
| 143 |
169 |
useconds=(end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec;
|
| 144 |
170 |
|
| 145 |
|
printf("\tOn %i, find %lu inside in %lu useconds.\n",rank,
|
| 146 |
|
(unsigned long)part_inside,useconds);
|
|
171 |
printf("\tOn %i, find %lld inside in %lu useconds.\n",rank,
|
|
172 |
(long long)part_inside,useconds);
|
| 147 |
173 |
#else
|
| 148 |
|
printf("\tOn %i, find %lu inside\n",rank,
|
| 149 |
|
(unsigned long)part_inside);
|
|
174 |
printf("\tOn %i, find %lld inside\n",rank,
|
|
175 |
(long long)part_inside);
|
| 150 |
176 |
|
| 151 |
177 |
#endif
|
| 152 |
178 |
|