Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. obraz w tablicy (wiersze nastepujace po sobie)
  2.  
  3. !!wejscie: rozne obrazy rozne rozmiary (dane obrazu mozna generowac random)
  4.  
  5. zadanie: rozmycie - uśrednienie wartości swojej i wartości swoich sąsiadów (+) w tablicy
  6.  
  7. ?? wiele obrazow w jednej tablicy?
  8.  
  9.  
  10. in: iloscobr, w1, h1, w2, h2
  11.  
  12. mozna zahardkodowac wielkosc obrazow
  13. moga byc rozne width, height
  14.  
  15. do slave wysylamy info o rozmiarze obrazka i jego indeksie (mozna jako struct, jest jakas metoda)
  16. (mozna przez dynamic arr)
  17.  
  18.  
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <mpi.h>
  23. #include <stdlib.h>
  24. #include <math.h>
  25. #define PRECISION 0.000001
  26. #define RANGESIZE 1
  27. #define DATA 0
  28. #define RESULT 1
  29. #define FINISH 2
  30.  
  31. const int picturesCount = 5;
  32. const char image[] = { 10, 5,
  33. 2, 4,
  34. 3, 9,
  35. 11, 11,
  36. 1, 10 };
  37.  
  38. //#define DEBUG
  39. //
  40. double f (double x)
  41. {
  42. return sin (x) * sin (x) / x;
  43. }
  44.  
  45. double
  46. SimpleIntegration (double a, double b)
  47. {
  48. double i;
  49. double sum = 0;
  50. for (i = a; i < b; i += PRECISION)
  51. sum += f (i) * PRECISION;
  52. return sum;
  53. }
  54.  
  55. int getStartOfPicture(int pictureNumber){
  56. int=0;
  57. for(i=0;i<pictureNumber;i++){
  58. arrSize += image[i*2] * image[i*2+1];
  59. }
  60. }
  61.  
  62. int
  63. main (int argc, char **argv)
  64. {
  65. int myrank, proccount,i;
  66. long pictureSize;
  67. long arrSize = 0;
  68. for(i=0;i<picturesCount;i++){
  69. arrSize += image[i*2] * image[i*2+1];
  70. }
  71. printf("arrsize: %li", arrSize);
  72.  
  73. int *inputArr = malloc (sizeof (int) * arrSize);
  74. for (i=0; i<arrSize; i++)
  75. {
  76. inputArr[i] = 0;
  77. }
  78.  
  79. double a = 1, b = 100;
  80. double range[2];
  81. double result = 0, resulttemp;
  82. int sentcount = 0;
  83. MPI_Status status;
  84.  
  85. // Initialize MPI
  86. MPI_Init (&argc, &argv);
  87.  
  88. // find out my rank
  89. MPI_Comm_rank (MPI_COMM_WORLD, &myrank);
  90.  
  91. // find out the number of processes in MPI_COMM_WORLD
  92. MPI_Comm_size (MPI_COMM_WORLD, &proccount);
  93.  
  94. if (proccount < 2)
  95. {
  96. printf ("Run with at least 2 processes");
  97. MPI_Finalize ();
  98. return -1;
  99. }
  100.  
  101. if (((b - a) / RANGESIZE) < 2 * (proccount - 1))
  102. {
  103. printf ("More subranges needed");
  104. MPI_Finalize ();
  105. return -1;
  106. }
  107.  
  108. // now the master will distribute the data and slave processes will perform computations
  109. if (myrank == 0)
  110. {
  111. range[0] = a;
  112.  
  113. // first distribute some ranges to all slaves
  114. for (i = 1; i < proccount; i++)
  115. {
  116. //range[1] = range[0] + RANGESIZE;
  117. int element = i-1;
  118. int width = image[element];
  119. int height = image[element+1];
  120. int numberOfElements = width*height;
  121.  
  122. int *arr = malloc (sizeof (int) * numberOfElements + 2);
  123. arr[0] = width;
  124. arr[1] = height;
  125.  
  126. int startElement = getStartOfPicture(element);
  127.  
  128. //przypisywanie wartosci
  129. for(int j = startElement; j<numberOfElements;j++){
  130. arr[j+2] = image[j];
  131. arr[j+3] = image[j+1];
  132. }
  133.  
  134. // send it to process i
  135. MPI_Send (arr, numberOfElements+2, MPI_INT, i, DATA, MPI_COMM_WORLD);
  136. sentcount++;
  137. range[0] = range[1];
  138. }
  139. do
  140. {
  141. // distribute remaining subranges to the processes which have completed their parts
  142. MPI_Recv (&resulttemp, 1, MPI_DOUBLE, MPI_ANY_SOURCE, RESULT,
  143. MPI_COMM_WORLD, &status);
  144. result += resulttemp;
  145. #ifdef DEBUG
  146. printf ("\nMaster received result %f from process %d",
  147. resulttemp, status.MPI_SOURCE);
  148. fflush (stdout);
  149. #endif
  150. // check the sender and send some more data
  151. range[1] = range[0] + RANGESIZE;
  152. if (range[1] > b)
  153. range[1] = b;
  154. #ifdef DEBUG
  155. printf ("\nMaster sending range %f,%f to process %d",
  156. range[0], range[1], status.MPI_SOURCE);
  157. fflush (stdout);
  158. #endif
  159. MPI_Send (range, 2, MPI_DOUBLE, status.MPI_SOURCE, DATA,
  160. MPI_COMM_WORLD);
  161. range[0] = range[1];
  162. }
  163.  
  164. while (range[1] < b);
  165. // now receive results from the processes
  166. for (i = 0; i < (proccount - 1); i++)
  167. {
  168. MPI_Recv (&resulttemp, 1, MPI_DOUBLE, MPI_ANY_SOURCE, RESULT,
  169. MPI_COMM_WORLD, &status);
  170. #ifdef DEBUG
  171. printf ("\nMaster received result %f from process %d",
  172. resulttemp, status.MPI_SOURCE);
  173. fflush (stdout);
  174. #endif
  175. result += resulttemp;
  176. }
  177. // shut down the slaves
  178. for (i = 1; i < proccount; i++)
  179. {
  180. MPI_Send (NULL, 0, MPI_DOUBLE, i, FINISH, MPI_COMM_WORLD);
  181. }
  182. // now display the result
  183. printf ("\nHi, I am process 0, the result is %f\n", result);
  184. }
  185. else
  186. { // slave
  187. // this is easy - just receive data and do the work
  188. do
  189. {
  190. MPI_Probe (0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
  191.  
  192. if (status.MPI_TAG == DATA)
  193. {
  194. MPI_Recv (range, 2, MPI_DOUBLE, 0, DATA, MPI_COMM_WORLD,
  195. &status);
  196. // compute my part
  197. resulttemp = SimpleIntegration (range[0], range[1]);
  198. // send the result back
  199. MPI_Send (&resulttemp, 1, MPI_DOUBLE, 0, RESULT,
  200. MPI_COMM_WORLD);
  201. }
  202. }
  203. while (status.MPI_TAG != FINISH);
  204. }
  205.  
  206. // Shut down MPI
  207. MPI_Finalize ();
  208.  
  209. return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement