Advertisement
Guest User

leo_paulisic

a guest
Nov 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.57 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <stdio.h>
  3.  
  4. int main(int argc, char** argv) {
  5. // Initialize the MPI environment
  6. MPI_Init(NULL, NULL);
  7. //
  8. // // Get the number of processes
  9. int world_size;
  10. MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  11.  
  12. // Get the rank of the process
  13. int world_rank;
  14. MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  15.  
  16. // Get the name of the processor
  17. char processor_name[MPI_MAX_PROCESSOR_NAME];
  18. int name_len;
  19. MPI_Get_processor_name(processor_name, &name_len);
  20.  
  21. // // Print off a hello world message
  22. printf("Hello world from processor %s, rank %d" " out of %d processors\n", processor_name, world_rank, world_size);
  23. // Finalize the MPI environment.
  24. MPI_Finalize();
  25. }
  26.  
  27. #include "mpi.h"
  28. #include <stdio.h>
  29.  
  30. int main(int argc, char **argv) {
  31.  
  32. int my_rank;
  33. int partner;
  34. int size, i,t;
  35. char greeting[100];
  36. MPI_Status stat;
  37.  
  38. MPI_Init(&argc, &argv); /*START MPI */
  39. MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); /*DETERMINE RANK OF THIS PROCESSOR*/
  40. MPI_Comm_size(MPI_COMM_WORLD, &size); /*DETERMINE TOTAL NUMBER OF PROCESSORS*/
  41.  
  42.  
  43. sprintf(greeting, "Hello world: processor %d of %d\n", my_rank, size);
  44.  
  45.  
  46. if (my_rank ==0) {
  47. fputs(greeting, stdout);
  48. for (partner = 1; partner < size; partner++){
  49.  
  50. MPI_Recv(greeting, sizeof(greeting), MPI_BYTE, partner, 1, MPI_COMM_WORLD, &stat);
  51. fputs (greeting, stdout);
  52. }
  53. }
  54. else {
  55. MPI_Send(greeting, strlen(greeting)+1, MPI_BYTE, 0,1,MPI_COMM_WORLD);
  56. }
  57.  
  58. if (my_rank == 0) printf("That is all for now!\n");
  59. MPI_Finalize(); /* EXIT MPI */
  60. }
  61.  
  62. #include "mpi.h"
  63. #include <stdio.h>
  64. int main(int argc, char *argv[])
  65. {
  66. int myid, numprocs, left, right;
  67. int buffer[10], buffer2[10];
  68. MPI_Request request, request2;
  69. MPI_Status status;
  70.  
  71. MPI_Init(&argc,&argv);
  72. MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
  73. MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  74. right = (myid + 1) % numprocs;
  75. left = myid - 1;
  76. if (left < 0)
  77. left = numprocs - 1;
  78. MPI_Irecv(buffer, 10, MPI_INT, left, 123, MPI_COMM_WORLD, &request);
  79. MPI_Isend(buffer2, 10, MPI_INT, right, 123, MPI_COMM_WORLD, &request2);
  80. MPI_Wait(&request, &status);
  81. MPI_Wait(&request2, &status);
  82. MPI_Finalize();
  83. return 0;
  84. }
  85. ~
  86.  
  87.  
  88.  
  89.  
  90.  
  91. MOJE MOJE MOJE MOJE MOJE ____
  92. ______________________________________________________
  93.  
  94.  
  95. distribuirani sustavi
  96.  
  97. korisnik@ODJ-O366-114:~$ ssh student20@172.16.55.121 -p 22
  98. student20@172.16.55.121's password:
  99. Last login: Fri Oct 13 14:36:23 2017 from 172.16.55.1
  100. [student20@bura3 ~]$ uname -a
  101. Linux bura3 2.6.32-642.1.1.el6.x86_64 #1 SMP Fri May 6 14:54:05 EDT 2016 x86_64 x86_64 x86_64 GNU/Linux
  102.  
  103.  
  104.  
  105.  
  106. #!/bin/bash -l
  107. #PBS -l
  108. select=5:ncpus=24:mem=4gb
  109. #PBS -l walltime=00:10:00 naredba se vrti 10min i onda se prekida
  110. #PBS -q workq default nešto
  111. #PBS -N
  112. izracun_br1
  113.  
  114. cd $PBS_O_WORKDIR
  115.  
  116.  
  117. -mpirun -np 48 .......... pokrećemo proces (48 procesa)
  118. -machinefile ............ zadani host file - opcija
  119.  
  120. -u emacsu upišemo
  121. emacs -nw mpi.pbs
  122. -izlazimo iz emacsa crtl-x -c .... za save
  123.  
  124. #PBS -l select=2:ncozs=24:mem=4GB
  125. #PBS -l walltime=00:10:00
  126. #PBS -l place=scatter:excl
  127. #PBS -q workq
  128.  
  129. #PBS -v
  130.  
  131. echo $PBS_NODEFILE
  132. cd $PBS_O_WORKDIR
  133. mpirun -np 48 -machinefile $PBS_NODEFILE hello_world
  134. #mpirun -np 48 -machinefile $PBS_NODEFILE send_rec
  135. mpirun -np 48 -machinefile $PBS_NODEFILE isend_irec
  136. #mpirun -np 48 -machinefile $PBS_NODEFILE barrier
  137.  
  138.  
  139. -u emacsu upišemo
  140. emacs -nw hello_world.c
  141. -izlazimo iz emacsa crtl-x-c za save
  142.  
  143. -za stvaranje direktorija
  144. mkdir vjezbe1
  145.  
  146. -za šetanje
  147. cd ./vjezbe1 (može radit i bez ./)
  148.  
  149. -u emacsu smo upisali
  150.  
  151. #include <mpi.h>
  152. #include <stdio.h>
  153.  
  154. int main(int argc, char** argv){
  155. //Initializr the MPI enviroment
  156. MPI_Init(NULL,NULL);
  157.  
  158. //Get the number of processes
  159. int world_size;
  160. MPI_Comm_size(MPI_COMM_WORLD,&world_size);
  161.  
  162. //Get the rank of processes
  163. int world_rank;
  164. MPI_Comm_rank(MPI_COMM_WORLD,&world_rank);
  165.  
  166. //Get the name of processor
  167. char processor_name[MPI_MAX_PROCESSOR_NAME];
  168. int name_len;
  169. MPI_Get_processor_name(processor_name,&name_len);
  170.  
  171. //print off a hello world message
  172. printf("Hello world from processor %s, rank %d" " out of %d processors\n" , p\
  173. rocessor_name, world_rank, world_size);
  174. //Finalize the MPI enviroment
  175. MPI_Finalize();
  176. }
  177.  
  178.  
  179.  
  180.  
  181. -tada kompaliramo progrma
  182. [student20@bura3 ~]$ mpicc hello_world.c
  183.  
  184.  
  185. -kompalirani file s završnim kodom a.out (automatski se generira kod komp)
  186. [student20@bura3 ~]$ ls
  187. a.out hello_world.c mpi.pbs
  188.  
  189.  
  190. -za provjeru tog file (a.out)
  191.  
  192. [student20@bura3 ~]$ file a.out
  193. a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped
  194. [student20@bura3 ~]$
  195.  
  196. -umjesto a.out možemo promjenit ime u hello_world
  197. [student20@bura3 ~]$ mpicc -o hello_world hello_world.c
  198.  
  199. -pokretanje programa
  200. [student20@bura3 ~]$ ./a.out
  201. Hello world from processor bura3, rank 0 out of 1 processors
  202.  
  203. ILI
  204.  
  205. [student20@bura3 ~]$ ./hello_world
  206. Hello world from processor bura3, rank 0 out of 1 processors
  207.  
  208. -nešto napraviti
  209. vi mpi.pbi
  210.  
  211. -izlazi iz vi sa spremanjem
  212. :wq!
  213.  
  214.  
  215.  
  216. -sprintf (slično kao printf ,samo formatizirano)
  217.  
  218.  
  219.  
  220. 2.ZADATAK
  221.  
  222.  
  223.  
  224. -u novom file pišemo
  225.  
  226. emacs -nw send_rec.c
  227.  
  228. -ovo
  229. #include "mpi.h"
  230. #include <stdio.h>
  231.  
  232. int main(int argc,char **argv){
  233.  
  234. int my_rank;
  235. int partner;
  236. int size,i,t;
  237. char greeting[100];
  238. MPI_Status stat;
  239.  
  240. MPI_Init(&argc,&argv); /*Start MPI*/
  241. MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); /*Determine rank of this processor*/
  242. MPI_Comm_size(MPI_COMM_WORL,&size); /*Determine total number of procesors */
  243.  
  244. sprintf(greeting, "Hello world : processor %d of %d\n", my_rank,size);
  245.  
  246. if(my_rank==0){
  247. fputs(greeting, stdout);
  248. for(partner = 1; partner < size; partner++){
  249.  
  250. MPI_Recv(greeting,sizeof(greeting),MPI_BYTE,partner,1,MPI_COMM_WORLD,&stat);
  251. fputs(greeting,stdout);
  252. }
  253. }
  254. else{
  255. MPI_Send(greeting,strlen(greeting)+1,MPI_BYTE,0,1,MPI_COMM_WORLD);
  256. }
  257. }
  258.  
  259.  
  260.  
  261. -kompaliranje programa
  262.  
  263. [student20@bura3 ~]$ mpicc send_rec.c
  264. send_rec.c: In function ‘main’:
  265. send_rec.c:27: warning: incompatible implicit declaration of built-in function ‘strlen’
  266.  
  267. -i pokretanje programa (sa -np 2 određujemo broj procesa ... kod ovog primjera "2")
  268.  
  269. [student20@bura3 ~]$ mpirun -np 2 ./a.out
  270. Hello world : processor 0 of 2
  271. Hello world : processor 1 of 2
  272. That is all for now
  273.  
  274.  
  275. ILI s 8 procesa
  276.  
  277. [student20@bura3 ~]$ mpirun -np 8 ./a.out
  278. Hello world : processor 0 of 8
  279. Hello world : processor 1 of 8
  280. Hello world : processor 2 of 8
  281. Hello world : processor 3 of 8
  282. Hello world : processor 4 of 8
  283. Hello world : processor 5 of 8
  284. Hello world : processor 6 of 8
  285. Hello world : processor 7 of 8
  286. That is all for now
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294. 3.ZADATAK
  295.  
  296.  
  297. -ponovo pokrećemo emacs -nw isend_rec.c
  298.  
  299. #include "mpi.h"
  300. #include <stdio.h>
  301.  
  302. int main(int argc,char *argv[])
  303. {
  304. int myid, numprocs,left,right;
  305. int buffer[10], buffer2[10];
  306. MPI_Request request,request2;
  307. MPI_Status status;
  308.  
  309. MPI_Init(&argc,&argv);
  310. MPI_Comm_rank(MPI_COMM_WORLD,&myid); /*Determine rank of this processor*/
  311. MPI_Comm_size(MPI_COMM_WORL,&numprocs); /*Determine total number of procesors */
  312. right=(myid+1)%numprocs;
  313. left=myid-1;
  314. if(left<0)
  315. left=numprocs-1;
  316. MPI_Irecv(buffer,10,MPI_INT,left,123,MPI_COMM_WORLD,&request);
  317. MPI_Isend(buffer2,10,MPI_INT,right,123,MPI_COMM_WORLD,&request2);
  318. MPI_Wait(&request,&status);
  319. MPI_Wait(&request2,&status);
  320. MPI_Finalize();
  321. return 0;
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement