Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.56 KB | None | 0 0
  1. /* Sieve of eratosthenes */
  2. #include <mpi.h>
  3. #include <math.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. //#include "MyMPI.h"
  8.  
  9. #define MIN(a,b) ((a)<(b)?(a):(b))
  10. #define BLOCK_LOW(id,p,n) ((id)*(n)/(p))
  11. #define BLOCK_HIGH(id,p,n) (BLOCK_LOW(((id)+1),p,n)-1)
  12. #define BLOCK_SIZE(id,p,n) ((BLOCK_LOW(((id)+1),p,n))-(BLOCK_LOW(id,p,n)))
  13. #define BLOCK_OWNER(index,p,n) (((p)*(index)+1)-1)/(n))
  14.  
  15. int main (int argc, char *argv[])
  16. {
  17. int count; /* local prime count */
  18. double elapsed_time; /* execution time */
  19. int first; /* index of the first sieve */
  20. int global_count; /* global count of prime numbers */
  21. int high_value; /* highest value assigned to this process */
  22. int i; /* loop counter */
  23. int id; /* this process id */
  24. int index; /* index of the current sieve */
  25. int low_value; /* lowest value assigned to this process */
  26. int *marked; /* array elements to be marked */
  27. int n; /* value of the largest number */
  28. int p; /* number of processes */
  29. int proc0_size; /* number of elements assigned to process zero */
  30. /* this is to find if process zero has all primes */
  31. int prime; /* current prime or sieve */
  32. int size; /* elements in marked array */
  33.  
  34. MPI_Init (&argc, &argv);
  35. /* start timer */
  36. MPI_Barrier(MPI_COMM_WORLD);
  37. elapsed_time = -MPI_Wtime();
  38. MPI_Comm_rank (MPI_COMM_WORLD, &id);
  39. MPI_Comm_size (MPI_COMM_WORLD, &p);
  40.  
  41. if (argc != 2)
  42. {
  43. if (!id)
  44. printf ("Command line: %s <m>\n", argv[0]);
  45.  
  46. MPI_Finalize();
  47. exit (1);
  48. }
  49.  
  50. n = atoi(argv[1]);
  51.  
  52. /* find how many elements are assigned to this process */
  53. low_value = 2 + BLOCK_LOW(id,p,n-1);
  54. high_value = 2 + BLOCK_HIGH(id,p,n-1);
  55. size = BLOCK_SIZE(id,p,n-1);
  56. proc0_size = (n-1)/p;
  57. if ((2 + proc0_size) < (int) sqrt((double) n))
  58. {
  59. if (!id)
  60. printf ("Too many processes\n");
  61.  
  62. MPI_Finalize();
  63. exit (1);
  64. }
  65.  
  66. marked = (int *) malloc (size * sizeof(int));
  67.  
  68. if (marked == NULL)
  69. {
  70. printf ("Cannot allocate enough memory\n");
  71. MPI_Finalize();
  72. exit (1);
  73. }
  74.  
  75. for (i = 0; i < size; i++) marked[i] = 0;
  76.  
  77. if (!id) index = 0;
  78.  
  79. prime = 2;
  80. do {
  81. if (prime * prime > low_value)
  82. first = prime * prime - low_value;
  83. else {
  84. if (!(low_value % prime)) first = 0;
  85. else first = prime - (low_value % prime);
  86. }
  87. for (i = first; i < size; i += prime) marked[i] = 1;
  88. if (!id) {
  89. while (marked[++index]);
  90. prime = index + 2;
  91. }
  92. MPI_Bcast (&prime, 1, MPI_INT, 0, MPI_COMM_WORLD);
  93. } while (prime * prime <= n);
  94.  
  95. count = 0;
  96.  
  97. for (i = 0; i < size; i++)
  98. if (!marked[i]) count++;
  99.  
  100. MPI_Reduce (&count, &global_count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  101.  
  102. elapsed_time += MPI_Wtime();
  103.  
  104. if (!id) {
  105. printf ("%d primes are less than or equal to %d\n", global_count, n);
  106. printf ("Total elapsed time: %10.6f\n", elapsed_time);
  107. }
  108.  
  109. MPI_Finalize ();
  110.  
  111. return 0;
  112. }
  113.  
  114. /* Sieve of eratosthenes */
  115. #include <mpi.h>
  116. #include <math.h>
  117. #include <stdio.h>
  118. #include <stdlib.h>
  119.  
  120. //#include "MyMPI.h"
  121.  
  122. #define MIN(a,b) ((a)<(b)?(a):(b))
  123. #define BLOCK_LOW(id,p,n) ((id)*(n)/(p))
  124. #define BLOCK_HIGH(id,p,n) (BLOCK_LOW(((id)+1),p,n)-1)
  125. #define BLOCK_SIZE(id,p,n) ((BLOCK_LOW(((id)+1),p,n))-(BLOCK_LOW(id,p,n)))
  126. #define BLOCK_OWNER(index,p,n) (((p)*(index)+1)-1)/(n))
  127.  
  128. int main (int argc, char *argv[])
  129. {
  130. int count; /* local prime count */
  131. double elapsed_time; /* execution time */
  132. int first; /* index of the first sieve */
  133. int global_count; /* global count of prime numbers */
  134. int high_value; /* highest value assigned to this process */
  135. int i; /* loop counter */
  136. int id; /* this process id */
  137. int index; /* index of the current sieve */
  138. int low_value; /* lowest value assigned to this process */
  139. int *marked; /* array elements to be marked */
  140. int n; /* value of the largest number */
  141. int p; /* number of processes */
  142. int proc0_size; /* number of elements assigned to process zero */
  143. /* this is to find if process zero has all primes */
  144. int prime; /* current prime or sieve */
  145. int size; /* elements in marked array */
  146.  
  147. MPI_Init (&argc, &argv);
  148. /* start timer */
  149. MPI_Barrier(MPI_COMM_WORLD);
  150. elapsed_time = -MPI_Wtime();
  151. MPI_Comm_rank (MPI_COMM_WORLD, &id);
  152. MPI_Comm_size (MPI_COMM_WORLD, &p);
  153.  
  154. if (argc != 2)
  155. {
  156. if (!id)
  157. printf ("Command line: %s <m>\n", argv[0]);
  158.  
  159. MPI_Finalize();
  160. exit (1);
  161. }
  162.  
  163. n = atoi(argv[1]);
  164.  
  165. /* find how many elements are assigned to this process */
  166. low_value = 2 + BLOCK_LOW(id,p,n-1);
  167. high_value = 2 + BLOCK_HIGH(id,p,n-1);
  168. size = BLOCK_SIZE(id,p,n-1);
  169. proc0_size = (n-1)/p;
  170. if ((2 + proc0_size) < (int) sqrt((double) n))
  171. {
  172. if (!id)
  173. printf ("Too many processes\n");
  174.  
  175. MPI_Finalize();
  176. exit (1);
  177. }
  178.  
  179. marked = (int *) malloc (size * sizeof(int));
  180.  
  181. if (marked == NULL)
  182. {
  183. printf ("Cannot allocate enough memory\n");
  184. MPI_Finalize();
  185. exit (1);
  186. }
  187.  
  188. for (i = 0; i < size; i++)
  189. if (!(low_value % 2)) //if value is divisible by 2
  190. marked[i] = i%2==0 ? 1:0; //mark value prime if divisible by two
  191. else
  192. marked[i] = i%2==0 ? 0:1; //mark value composite if not divisible by 2
  193.  
  194. if (!id)
  195. {
  196. index = 1;
  197. marked[0] = 0;
  198. }
  199.  
  200. prime=3;
  201. do {
  202. if (prime * prime > low_value)
  203. first = prime * prime - low_value;
  204. else {
  205. if (!(low_value % prime)) first = 0;
  206. else first = prime - (low_value % prime);
  207. }
  208. for (i = first; i < size; i += prime) marked[i] = 1;
  209.  
  210. if (!id) {
  211. while (marked[index]) index += 2;
  212. prime = index + 2;
  213. index += 2;
  214. }
  215. MPI_Bcast (&prime, 1, MPI_INT, 0, MPI_COMM_WORLD);
  216. } while (prime * prime <= n);
  217.  
  218. count = 0;
  219.  
  220. for (i = 0; i < size; i++)
  221. if (!marked[i]) count++;
  222.  
  223. MPI_Reduce (&count, &global_count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  224.  
  225. elapsed_time += MPI_Wtime();
  226.  
  227. if (!id) {
  228. printf ("%d primes are less than or equal to %d\n", global_count, n);
  229. printf ("Total elapsed time: %10.6f\n", elapsed_time);
  230. }
  231.  
  232. MPI_Finalize ();
  233.  
  234. return 0;
  235. }
  236.  
  237. /* Sieve of eratosthenes */
  238. #include <mpi.h>
  239. #include <math.h>
  240. #include <stdio.h>
  241. #include <stdlib.h>
  242.  
  243. //#include "MyMPI.h"
  244.  
  245. #define MIN(a,b) ((a)<(b)?(a):(b))
  246. #define BLOCK_LOW(id,p,n) ((id)*(n)/(p))
  247. #define BLOCK_HIGH(id,p,n) (BLOCK_LOW(((id)+1),p,n)-1)
  248. #define BLOCK_SIZE(id,p,n) ((BLOCK_LOW(((id)+1),p,n))-(BLOCK_LOW(id,p,n)))
  249. #define BLOCK_OWNER(index,p,n) (((p)*(index)+1)-1)/(n))
  250.  
  251. int main (int argc, char *argv[])
  252. {
  253. int count; /* local prime count */
  254. double elapsed_time; /* execution time */
  255. int first; /* index of the first sieve */
  256. int global_count; /* global count of prime numbers */
  257. int high_value; /* highest value assigned to this process */
  258. int i; /* loop counter */
  259. int id; /* this process id */
  260. int index; /* index of the current sieve */
  261. int low_value; /* lowest value assigned to this process */
  262. int *marked; /* array elements to be marked */
  263. int *sieves; /* array of sieving primes from k -> sqrt(n) */
  264. int n; /* value of the largest number */
  265. int sqrtN; /* square root of n */
  266. int p; /* number of processes */
  267. int proc0_size; /* number of elements assigned to process zero */
  268. /* this is to find if process zero has all primes */
  269. int prime; /* current prime or sieve */
  270. int size; /* elements in marked array */
  271.  
  272. MPI_Init (&argc, &argv);
  273. /* start timer */
  274. MPI_Barrier(MPI_COMM_WORLD);
  275. elapsed_time = -MPI_Wtime();
  276. MPI_Comm_rank (MPI_COMM_WORLD, &id);
  277. MPI_Comm_size (MPI_COMM_WORLD, &p);
  278.  
  279. if (argc != 2)
  280. {
  281. if (!id)
  282. printf ("Command line: %s <m>\n", argv[0]);
  283.  
  284. MPI_Finalize();
  285. exit (1);
  286. }
  287.  
  288. n = atoi(argv[1]);
  289.  
  290. /* find how many elements are assigned to this process */
  291. low_value = 2 + BLOCK_LOW(id,p,n-1);
  292. high_value = 2 + BLOCK_HIGH(id,p,n-1);
  293. size = BLOCK_SIZE(id,p,n-1);
  294. proc0_size = (n-1)/p;
  295. sqrtN = (int) sqrt((double) n);
  296. if ((2 + proc0_size) < sqrtN)
  297. {
  298. if (!id)
  299. printf ("Too many processes\n");
  300.  
  301. MPI_Finalize();
  302. exit (1);
  303. }
  304.  
  305. marked = (int *) malloc (size * sizeof(int));
  306. sieves = (int *) malloc ((sqrtN) * sizeof(int));
  307.  
  308. if ((marked == NULL) || (sieves == NULL))
  309. {
  310. printf ("Cannot allocate enough memory\n");
  311. MPI_Finalize();
  312. exit (1);
  313. }
  314.  
  315. for (i = 0; i < size; i++)
  316. if (!(low_value % 2))
  317. marked[i] = i%2==0 ? 1:0; //mark all that are divisible by 2 as composite
  318. else
  319. marked[i] = i%2==0 ? 0:1; //mark all that are not divisible by 2 as prime
  320.  
  321. for (i=0; i < (sqrtN-2); i++) sieves[i]= i%2==0 ? 1:0; //mark composites up to the square root of N
  322. if (!id)
  323. marked[0] = 0;
  324.  
  325. //Create a list of sieving primes on each process from 2 -> SQRT(n)
  326. sieves[0] = 0;
  327. index = 1;
  328. prime = 3;
  329. do
  330. {
  331. for(i=prime-2+prime; i<(sqrtN-2); i+=prime) sieves[i] = 1;
  332. while (sieves[index]) index += 2;
  333. prime = index + 2;
  334. index += 2;
  335. } while (prime * prime <= sqrtN);
  336.  
  337. //Mark all of the sieving primes from 2 -> n
  338. index = 1;
  339. prime = 3;
  340. do {
  341. if (prime * prime > low_value)
  342. first = prime * prime - low_value;
  343. else {
  344. if (!(low_value % prime)) first = 0;
  345. else first = prime - (low_value % prime);
  346. }
  347. for (i = first; i < size; i += prime) marked[i] = 1;
  348.  
  349. while (sieves[index]) index += 2;
  350. prime = index + 2;
  351. index += 2;
  352.  
  353. } while (prime * prime <= n);
  354.  
  355. //Count the primes in each process
  356. count = 0;
  357. for (i = 0; i < size; i++)
  358. if (!marked[i]) count++;
  359.  
  360. //SUM each count into process 0.
  361. MPI_Reduce (&count, &global_count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  362.  
  363. elapsed_time += MPI_Wtime();
  364.  
  365. if (!id) {
  366. printf ("%d primes are less than or equal to %d\n", global_count, n);
  367. printf ("Total elapsed time: %10.6f\n", elapsed_time);
  368. }
  369.  
  370. MPI_Finalize ();
  371.  
  372. return 0;
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement