sidlglove

Untitled

Nov 13th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 24.26 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include "mpi.h"
  4. #include<math.h>
  5.  
  6.  
  7. int main(int argc,char *argv[]){
  8. int my_rank;
  9. int p;
  10. int source;
  11. int dest;
  12. int tag=0;
  13. int part=0;
  14. int i;
  15. char message[100];
  16. MPI_Status status;
  17. printf("hello");
  18. MPI_Init(&argc,&argv);
  19.  
  20. MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
  21. MPI_Comm_size(MPI_COMM_WORLD, &p);
  22.  
  23. if(my_rank!=0){
  24. sprintf(message, "greetings from process %d \n",my_rank);
  25. dest=0;
  26. int num1[4], i=0, sum=0;
  27. source = 0;
  28. MPI_Recv(&num1, 4, MPI_INT, source, tag, MPI_COMM_WORLD, &status);
  29. i = 0;
  30. for(i=0; i<3; i++){
  31.         sum += num1[i];
  32. }
  33.  
  34. MPI_Send(&sum,1, MPI_INT,dest,tag,MPI_COMM_WORLD);
  35. }
  36.  
  37. else{
  38.  
  39. i = 0;
  40. int j = 0;
  41. int inp[4][3];
  42. printf("enter numbers: \n");
  43. scanf("%d", &inp[0][0]);
  44. int arr_sum = inp[0][0];
  45. for(i=1; i<4; i++){
  46.         for(j=0; j<3; j++)
  47.                 scanf("%d", &inp[i][j]);
  48.         dest = i;
  49.         MPI_Send(&inp[i],4, MPI_INT,dest,tag,MPI_COMM_WORLD);
  50. }
  51.  
  52.  
  53. for(source=1;source<p;source++){
  54.         MPI_Recv(&part,1,MPI_INT,source,tag,MPI_COMM_WORLD,&status);
  55.         arr_sum += part;
  56. }
  57. printf("%d \n",arr_sum);
  58. }
  59. MPI_Finalize();
  60. }
  61.  
  62. //arrsum
  63.  
  64.  
  65. #include<mpi.h>
  66. #include<math.h>
  67. #include<stdio.h>
  68.  
  69. float poly[2][8] = {{0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0},
  70.                                                 {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0}};
  71.  
  72. float lagrange(int a, int myid, int size, int rem, float b, int n);
  73.  
  74.  
  75. void main(int argc, char* argv[]){
  76.  
  77.         int n, p, i, j, rem, num;
  78.         float h, result, b, pi;
  79.     float my_a, my_range;
  80.  
  81.         int myid, source, dest, tag;
  82.         MPI_Status status;
  83.         float my_result;
  84.         n = 8;    
  85.         int a = 0;  
  86.         dest = 0;        
  87.         tag = 123;        
  88.  
  89.         MPI_Init(&argc,&argv);            
  90.         MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  91.         MPI_Comm_size(MPI_COMM_WORLD, &p);    
  92.         rem = n%p;
  93.         int size = (n-rem)/p;
  94.         rem = size + rem;
  95.         b = 3.5;
  96.  
  97.     my_result = lagrange(a, myid, size, rem, b, n);
  98.  
  99.       printf("Process %d has the partial result of %f\n", myid,my_result);
  100.  
  101.       if(myid == 0) {
  102.                 result = my_result;
  103.         for (i=1;i<p;i++) {
  104.           source = i;
  105.           float my_result1;          
  106.           MPI_Recv(&my_result1, 1, MPI_REAL, source, tag,
  107.                         MPI_COMM_WORLD, &status);
  108.           result += my_result1;
  109.         }
  110.         printf("The result =%f\n",result);
  111.       }
  112.       else
  113.         MPI_Send(&my_result, 1, MPI_REAL, dest, tag,
  114.                       MPI_COMM_WORLD);      
  115.  
  116.       MPI_Finalize();
  117. }
  118. float lagrange(int a, int myid, int size, int rem, float b, int n)
  119. {
  120.       int j, i, aij;
  121.                 float value;
  122.  
  123.                 if(myid==0) aij = a + rem;
  124.                 else{
  125.                         a = a + (myid-1)*size + rem;
  126.                         aij = a + size;
  127.                 }
  128.       value = 0.0;            
  129.       for (i=a;i<aij;i++) {
  130.         float value_i = 1.0;    
  131.         for(j=0; j<n; j++){
  132.                 if(j != i){
  133.                         value_i = value_i*(b-poly[0][j])/(poly[0][i]-poly[0][j]);
  134.                 }
  135.         }
  136.         printf("value of %d %d is %f\n", myid, i, poly[1][i]*value_i);
  137.         value += poly[1][i]*value_i;
  138.         }
  139.         printf("\n");
  140.                 return (value);
  141. }
  142.  
  143. //lagrange
  144.  
  145.  
  146. #include<mpi.h>
  147. #include<math.h>
  148. #include<stdio.h>
  149.  
  150. float simpson(float a, int myid, int size, int rem, float b, int n, int p);
  151.  
  152.  
  153. void main(int argc, char* argv[]){
  154.  
  155.         int n, p, i, j, rem, num;
  156.         float h, result, b, pi;
  157.         float my_a, my_range;
  158.  
  159.         int myid, source, dest, tag;
  160.         MPI_Status status;
  161.         float my_result;
  162.         n = 12;    
  163.         float a = 2.0;  
  164.         dest = 0;        
  165.         tag = 123;        
  166.  
  167.         MPI_Init(&argc,&argv);            
  168.         MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  169.         MPI_Comm_size(MPI_COMM_WORLD, &p);    
  170.         int size = n/p;
  171.         rem = size + n%p;
  172.         b = 6.0;
  173.  
  174.       my_result = simpson(a, myid, size, rem, b, n, p);
  175.  
  176.       printf("Process %d has the partial result of %f\n", myid,my_result);
  177.  
  178.       if(myid == 0) {
  179.         result = my_result;
  180.         for (i=1;i<p;i++) {
  181.           source = i;
  182.           float my_result1;        
  183.           MPI_Recv(&my_result1, 1, MPI_REAL, source, tag,
  184.                         MPI_COMM_WORLD, &status);
  185.           result += my_result1;
  186.         }
  187.         printf("The result =%f\n",result);
  188.       }
  189.       else
  190.         MPI_Send(&my_result, 1, MPI_REAL, dest, tag,
  191.                       MPI_COMM_WORLD);      
  192.  
  193.       MPI_Finalize();
  194. }
  195.  
  196. float simpson(float a, int myid, int size, int rem, float b, int n, int p)
  197. {
  198.       float i, aij;
  199.         float h = (b-a)/n;
  200.         printf("%f\n", h);
  201.         int boom = 0;
  202.       float value;
  203.         if(myid==0){
  204.         aij = a + rem*h;
  205.         }
  206.         else{
  207.                 a = a + ((myid-1)*size + rem)*h;
  208.                         boom = (myid-1)*size + rem;
  209.             aij = a + size*h;
  210.             if(myid == p-1) aij += h;
  211.  
  212.         }
  213.       value = 0.0;
  214.       int j = 0;            
  215.       for (i=a;i<aij;i+=h) {
  216.                 if(boom+j == 0 || boom+j == n)
  217.                         value += i*i;
  218.  
  219.                 else if((boom+j)%2 == 0){
  220.                         value += 2.0*i*i;
  221.                 }    
  222.  
  223.                 else{
  224.                         value += 4.0*i*i;
  225.                 }
  226.  
  227.         j++;
  228.        // printf("value of %d %d is %f\n", myid, i, value);
  229.         }
  230.         printf("\n");
  231.       return ((1.0/3)*value*h);
  232. }
  233.  
  234. //simpson
  235.  
  236.  
  237. #include <mpi.h>
  238. #include <math.h>
  239. #include <stdio.h>
  240. float fct(float x)
  241. {
  242.       return x*x*x;
  243. }
  244.  
  245. float integral(float a, int n, float h);
  246. void main(argc,argv)
  247. int argc;
  248. char *argv[];
  249. {
  250.  
  251.       int n, p, i, j, ierr,num;
  252.       float h, result, a, b, pi;
  253.       float my_a, my_range;
  254.  
  255.       int myid, source, dest, tag;
  256.       MPI_Status status;
  257.       float my_result;
  258.  
  259.       pi = acos(-1.0);
  260.       a = 0.;          
  261.       b = 2.;    
  262.       n = 100000;      
  263.       dest = 0;        
  264.       tag = 123;        
  265.  
  266.       MPI_Init(&argc,&argv);            
  267.       MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  268.       MPI_Comm_size(MPI_COMM_WORLD, &p);    
  269.       h = (b-a)/n;  
  270.       num = n/p;        
  271.       my_range = (b-a)/p;
  272.       my_a = a + myid*my_range;
  273.       my_result = integral(my_a,num,h);
  274.  
  275.       printf("Process %d has the partial result of %f\n", myid,my_result);
  276.  
  277.       if(myid == 0) {
  278.         result = my_result;
  279.         for (i=1;i<p;i++) {
  280.           source = i;          
  281.           MPI_Recv(&my_result, 1, MPI_REAL, source, tag,
  282.                         MPI_COMM_WORLD, &status);
  283.           result += my_result;
  284.         }
  285.         printf("The result =%f\n",result);
  286.       }
  287.       else
  288.         MPI_Send(&my_result, 1, MPI_REAL, dest, tag,
  289.                       MPI_COMM_WORLD);      
  290.  
  291.       MPI_Finalize();
  292. }
  293. float integral(float a, int n, float h)
  294. {
  295.       int j;
  296.       float h2, aij, integ;
  297.  
  298.       integ = 0.0;            
  299.       h2 = h/2.;
  300.       for (j=0;j<n;j++) {    
  301.       aij = a + h;        
  302.       integ +=0.5*fct(aij)*h+0.5*fct(a)*h;
  303.      a=aij;
  304.  }
  305.       return (integ);
  306. }
  307.  
  308. //integ
  309.  
  310.  
  311. #include<mpi.h>
  312. #include<math.h>
  313. #include<stdio.h>
  314.  
  315. float mat1[4][4] = {{0.0, 1.0, 2.0, 3.0}, {4.0, 5.0, 6.0, 7.0},
  316.                                                 {0.0, 1.0, 2.0, 3.0}, {4.0, 5.0, 6.0, 7.0}};
  317.  
  318. float mat2[4][4] = {{0.0, 1.0, 2.0, 3.0}, {4.0, 5.0, 6.0, 7.0},
  319.                                                 {0.0, 1.0, 2.0, 3.0}, {4.0, 5.0, 6.0, 7.0}};
  320.  
  321. float mat3[4][4];
  322.  
  323.  
  324. float matmul(int myid, int size, int rem, int n);
  325.  
  326.  
  327. void main(int argc, char* argv[]){
  328.  
  329.         int n, p, i, j, rem, num;
  330.         float h, result, b, pi;
  331.         float my_a, my_range;
  332.  
  333.         int myid, source, dest, tag;
  334.         MPI_Status status;
  335.         float my_result;
  336.         n = 8;    
  337.         int a = 0;  
  338.         dest = 0;        
  339.         tag = 123;        
  340.  
  341.         MPI_Init(&argc,&argv);            
  342.         MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  343.         MPI_Comm_size(MPI_COMM_WORLD, &p);    
  344.         rem = n%p;
  345.         int size = (n-rem)/p;
  346.         rem = size + rem;
  347.  
  348.         my_result = matmul(myid, 1, 1, n);
  349.  
  350.         //printf("Process %d has the partial result of %f\n", myid,my_result);
  351.  
  352.                 if(myid == 0) {
  353.                         result = my_result;
  354.                         mat3[myid/4][myid%4] = result;
  355.                 for (i=1;i<p;i++) {
  356.                                 source = i;
  357.                                 float my_result1;          
  358.                                 MPI_Recv(&my_result1, 1, MPI_FLOAT, source, tag,
  359.                                                 MPI_COMM_WORLD, &status);
  360.                             mat3[i/4][i%4] = my_result1;
  361.  
  362.                 }
  363.  
  364.                 for (i=0; i<4; i++){
  365.                         for (j=0; j<4; j++){
  366.                                 printf("%f ", mat3[i][j]);
  367.                         }
  368.                         printf("\n");
  369.                 }
  370.                 }
  371.  
  372.                 else{
  373.                 MPI_Send(&my_result, 1, MPI_FLOAT, dest, tag,
  374.                           MPI_COMM_WORLD);      
  375.                 }
  376.                 MPI_Finalize();
  377. }
  378.  
  379. float matmul(int myid, int size, int rem, int n){
  380.         int j, i, aij = 4;
  381.                 float value;
  382.  
  383.                 i = myid / 4;
  384.                 j = myid % 4;
  385.                 int k;
  386.  
  387.         value = 0.0;            
  388.                 for (k=0;k<aij;k++) {    
  389.                         value += mat1[i][k]*mat2[k][j];
  390.                 }
  391.  
  392.                 //printf("\n");
  393.                 return (value);
  394. }
  395.  
  396. //matmul
  397.  
  398.  
  399.  
  400. #include<stdio.h>
  401. #include<string.h>
  402. #include"mpi.h"
  403. #include<math.h>
  404.  
  405. int routingFn(int j, int i){
  406.         return j+pow(2,i);
  407. }
  408.  
  409.  
  410. void main(int argc, char* argv[]){
  411.         int my_rank,p,source,dest,tag=0;
  412.         int tag2=1;
  413.         MPI_Status status;
  414.         MPI_Init(&argc, &argv);
  415.         MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  416.         MPI_Comm_size(MPI_COMM_WORLD, &p);
  417.         int n;
  418.         int my_value;
  419.         int my_step;
  420.         int total_step = log2(p);
  421.         if(my_rank==0){
  422.                 my_step=0;
  423.                 int arr[] = {1,2,3,4,5,6,7,8};
  424.                 my_value = arr[0];
  425.                 int i;
  426.                 for(my_step=0;my_step<total_step;my_step++){
  427.                         for(i=1;i<p;i++){
  428.                                 int t = arr[i];
  429.                                 MPI_Send(&t, 1, MPI_INT,i,tag,MPI_COMM_WORLD);
  430.                                 MPI_Send(&my_step,1,MPI_INT,i,tag+1,MPI_COMM_WORLD);
  431.                         }
  432.  
  433.                         int ns;
  434.                         ns = routingFn(my_rank,my_step);
  435.                         int k = arr[my_rank];
  436.                         MPI_Send(&k, 1, MPI_INT,ns,tag+3,MPI_COMM_WORLD);
  437.                         for(i=1;i<p;i++){
  438.                                 int y;
  439.                                 MPI_Recv(&y,1,MPI_INT,i,tag+2,MPI_COMM_WORLD,&status);
  440.  
  441.                                 arr[i] = y;
  442.                         }
  443.                 }
  444.                 for(i=0;i<p;i++){
  445.                         printf("Result for P[%d] : %d\n",i,arr[i]);
  446.                 }
  447.         }
  448.         else{
  449.                 int j;
  450.                 for(j=0;j<total_step;j++){
  451.                         source = 0;
  452.                         MPI_Recv(&my_value,1,MPI_INT,source,tag,MPI_COMM_WORLD,&status);
  453.                         MPI_Recv(&my_step,1,MPI_INT,source,tag+1,MPI_COMM_WORLD,&status);
  454.                         int ns;
  455.                         if(my_rank%2==0){
  456.                                 ns = routingFn(my_rank,my_step);
  457.                                 if(ns<8){
  458.                                         MPI_Send(&my_value, 1, MPI_INT,ns,tag+3,MPI_COMM_WORLD);
  459.                                 }
  460.                                 int incoming;
  461.                                 int incomingP = my_rank - pow(2,my_step);
  462.                                 if(my_rank>=pow(2,my_step)){
  463.                                         MPI_Recv(&incoming, 1, MPI_INT,incomingP,tag+3,MPI_COMM_WORLD,&status);
  464.                                 }
  465.  
  466.                                 my_value += incoming;
  467.  
  468.                                 MPI_Send(&my_value, 1, MPI_INT,source,tag+2,MPI_COMM_WORLD);
  469.                         }
  470.                         else{
  471.                                 ns = routingFn(my_rank,my_step);
  472.                                 int incoming;
  473.                                 int incomingP = my_rank - pow(2,my_step);
  474.                                 if(my_rank>=pow(2,my_step)){
  475.                                         MPI_Recv(&incoming, 1, MPI_INT,incomingP,tag+3,MPI_COMM_WORLD,&status);
  476.                                 }
  477.                                 if(ns<8){
  478.                                         MPI_Send(&my_value, 1, MPI_INT,ns,tag+3,MPI_COMM_WORLD);
  479.                                 }      
  480.                                 my_value += incoming;
  481.  
  482.                                 MPI_Send(&my_value, 1, MPI_INT,source,tag+2,MPI_COMM_WORLD);
  483.                         }
  484.                 }
  485.  
  486.         }
  487.  
  488.         MPI_Finalize();
  489.  
  490. }
  491.  
  492. //prefix
  493.  
  494.  
  495.  
  496. #include<stdio.h>
  497. #include<string.h>
  498. #include<math.h>
  499. #include "mpi.h"
  500.  
  501. int main(int argc,char *argv[]){
  502. int my_rank;
  503. int p;
  504. int source;
  505. int dest;
  506. int tag=0;
  507. float part=0;
  508. int i=0, n=1, a=0, b=0, size=1, rem=0;
  509. float h = 0.0;
  510.  
  511. MPI_Status status;
  512.  
  513. MPI_Init(&argc,&argv);
  514.  
  515. MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
  516. MPI_Comm_size(MPI_COMM_WORLD, &p);
  517. //printf("hello\n");
  518.  
  519. if(my_rank!=0){
  520.         dest=0;
  521.         int num1, i=0;
  522.         float sum=0;
  523.         source = 0;
  524.         MPI_Recv(&num1, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &status);
  525.         i = 0;
  526.         float temp = 0;
  527.         for(i=0; i<size; i++){
  528.                 temp = pow((a+((my_rank-1)*size + rem + i)*h), 2) + 1;
  529.                 sum += temp;
  530.  
  531.         }
  532.  
  533.         if(my_rank==3){
  534.                 sum -= 0.5*temp;
  535.         }
  536.  
  537. MPI_Send(&sum,1, MPI_FLOAT,dest,tag,MPI_COMM_WORLD);
  538. }
  539.  
  540. else{
  541.  
  542. printf("enter n, a, b:\n ");
  543. scanf("%d %d %d", &n, &a, &b);
  544. h = (float)(b-a)/(float)n;
  545.  
  546. rem = n%3;
  547. size = (n-rem)/3;
  548.  
  549. i = 0;
  550. int j = 0;
  551. float inp[4][size];
  552. float arr_sum = 0.0;
  553. inp[0][0] = 0.5*(pow(a, 2) + 1);
  554. arr_sum += inp[0][0];
  555. for(i=1; i<rem; i++){
  556.         inp[0][i] = pow(a+i*h, 2) + 1;
  557.         arr_sum += inp[0][i];
  558. }
  559.  
  560. for(i=1; i<4; i++){
  561.         dest = i;
  562.         MPI_Send(&i,1, MPI_INT,dest,tag,MPI_COMM_WORLD);
  563. }
  564.  
  565.  
  566. for(source=1;source<p;source++){
  567.         MPI_Recv(&part,1,MPI_FLOAT,source,tag,MPI_COMM_WORLD,&status);
  568.         arr_sum += part;
  569. }
  570. arr_sum *= h;
  571. printf("%f \n",arr_sum);
  572. }
  573. MPI_Finalize();
  574. }
  575.  
  576. //trap
  577.  
  578.  
  579.  
  580. #include<stdio.h>
  581. #include<string.h>
  582. #include "mpi.h"
  583.  
  584. int main(int argc, char *arg[]){
  585.         int my_rank, p, source, dest, tag=0;
  586.         int n = 6;
  587.         int a[] = {1, 4, 5, 2, 1, 3};
  588.  
  589.         MPI_Status status;
  590.         MPI_Init(&argc,&arg);
  591.         MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  592.         MPI_Comm_size(MPI_COMM_WORLD, &p);
  593.         int curr = a[my_rank];
  594.         for(int it = 0; it <= n/2; it++) {
  595.                 for(int ph = 0; ph < 2; ph++) {
  596.                         if((ph&1) == (my_rank&1)){
  597.                                 if(my_rank < n-1) {
  598.                                         int nxt;
  599.                                         MPI_Recv(&nxt, 1, MPI_INT, my_rank+1, tag, MPI_COMM_WORLD, &status);
  600.                                         MPI_Send(&curr, 1, MPI_INT, my_rank+1, tag, MPI_COMM_WORLD);
  601.                                         curr = nxt < curr ? nxt : curr;
  602.                                 }
  603.                         }
  604.                         else {
  605.                                 if(my_rank > 0) {
  606.                                         MPI_Send(&curr, 1, MPI_INT, my_rank-1, tag, MPI_COMM_WORLD);
  607.                                         int prev;
  608.                                         MPI_Recv(&prev, 1, MPI_INT, my_rank-1, tag, MPI_COMM_WORLD, &status);
  609.                                         curr = prev > curr ? prev : curr;
  610.                                 }
  611.                         }
  612.  
  613.                 }
  614.         }
  615.  
  616.         if (my_rank!=0){
  617.  
  618.  
  619.                 MPI_Send(&curr, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
  620.  
  621.         }
  622.         else {
  623.                 int sarr[n], i = 0;
  624.                 sarr[i++] = curr;
  625.                 for(int p = 1, num; p < n; p++) {
  626.  
  627.                         MPI_Recv(&num, 1, MPI_INT, p, tag, MPI_COMM_WORLD,&status);
  628.                         sarr[i++] = num;
  629.  
  630.                 }
  631.                 for(i = 0; i < n; i++){
  632.                         printf("%d ", sarr[i]);
  633.                 }
  634.         }
  635.         MPI_Finalize();
  636.         return 0;
  637. }
  638.  
  639. //odd-even
  640.  
  641.  
  642.  
  643. #include<stdio.h>
  644. #include"mpi.h"
  645.  
  646. void main(int argc, char* argv[]){
  647.  
  648.         int n, p, i, j, rem, num;
  649.         float h, result, b, pi;
  650.         int myid, source=0, dest, tag;  
  651.         MPI_Status status;
  652.         float my_result;
  653.         n = 8;    
  654.         int a = 0;  
  655.         dest = 0;        
  656.         tag = 123;        
  657.  
  658.         MPI_Init(&argc,&argv);            
  659.         MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  660.         MPI_Comm_size(MPI_COMM_WORLD, &p);  
  661.         //rem = n%p;
  662.         //int size = (n-rem)/p;
  663.         //rem = size + rem;
  664.         //my_result = matmul(myid, 1, 1, n);
  665.  
  666.         //printf("Process %d has the partial result of %f\n", myid,my_result);
  667.  
  668.                 int mat1[8] = {2, 5, 0, 3, 6, 1, 4, 7};
  669.                 int my_num = mat1[myid];
  670.  
  671.                 for(j=0; j<n/2 + 1; j++){
  672.                         int partner;
  673.                         if(myid<p-1 && myid!=0){
  674.                                 if(myid%2==1){
  675.                                         partner = myid+1;
  676.                                         MPI_Send(&my_num, 1, MPI_INT, partner, tag, MPI_COMM_WORLD);
  677.                                         int comp;
  678.                                         MPI_Recv(&comp, 1, MPI_INT, partner, tag, MPI_COMM_WORLD, &status);
  679.                                         if(my_num>comp) my_num = comp;
  680.                                 }
  681.  
  682.                                 if(myid%2==0){
  683.                                         partner = myid-1;
  684.                                         int comp;
  685.                                         MPI_Recv(&comp, 1, MPI_INT, partner, tag, MPI_COMM_WORLD, &status);
  686.                                         MPI_Send(&my_num, 1, MPI_INT, partner, tag, MPI_COMM_WORLD);
  687.                                         if(my_num<comp) my_num = comp;
  688.                                 }
  689.  
  690.                         }
  691.  
  692.                         else if(myid<p-1){
  693.                                 if(myid%2==0){
  694.                                         partner = myid+1;
  695.                                         MPI_Send(&my_num, 1, MPI_INT, partner, tag, MPI_COMM_WORLD);
  696.                                         int comp;
  697.                                         MPI_Recv(&comp, 1, MPI_INT, partner, tag, MPI_COMM_WORLD, &status);
  698.                                         if(my_num>comp) my_num = comp;
  699.                                 }
  700.  
  701.                                 if(myid%2==1){
  702.                                         partner = myid-1;
  703.                                         int comp;
  704.                                         MPI_Recv(&comp, 1, MPI_INT, partner, tag, MPI_COMM_WORLD, &status);
  705.                                         MPI_Send(&my_num, 1, MPI_INT, partner, tag, MPI_COMM_WORLD);
  706.                                         if(my_num<comp) my_num = comp;
  707.                                 }
  708.                         }
  709.                 }
  710.  
  711.                 if(myid!=0) MPI_Send(&my_num, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);  
  712.  
  713.                 for(i=1;i<=p-1;i++) {
  714.                         source = i;
  715.                         int num;
  716.                         MPI_Recv(&num, 1, MPI_INT, source, tag,
  717.                                         MPI_COMM_WORLD, &status);
  718.                         printf("%d ", num);
  719.  
  720.                 }      
  721.                 printf("\n");
  722.  
  723.         MPI_Finalize();
  724. }
  725.  
  726. //oddeven
  727.  
  728. #include <stdio.h>
  729. #include <math.h>
  730. #include <mpi.h>
  731.  
  732. int shuffle(int rank, int size){
  733.     int n = log2(size);
  734.     return ((rank<<1) | (rank>>(n-1))) & ((1<<n)-1);
  735. }
  736.  
  737. int unshuffle(int rank, int size){
  738.     int n = log2(size);
  739.     return ((rank>>1) | (rank<<(n-1))) & ((1<<n)-1);
  740. }
  741.  
  742. int main(int argc, char* argv[]){
  743.     int rank, size, sour, dest, x, mask, val;
  744.     int arr[100];
  745.     MPI_Status status;
  746.     MPI_Init(&argc,&argv);
  747.     MPI_Comm_rank(MPI_COMM_WORLD,&rank);
  748.     MPI_Comm_size(MPI_COMM_WORLD,&size);
  749.     mask = (rank+1) % 2;
  750.     if(rank == 0){
  751.         scanf("%d",&x);
  752.         for(int i=0; i<size-2; ++i)
  753.             scanf("%d",arr+i);
  754.         arr[size-2] = 1;
  755.         for(int i=0; i<size-1; ++i)
  756.             MPI_Send(&arr[i],1,MPI_INT,i+1,0,MPI_COMM_WORLD);
  757.         for(int i=0; i<size-1; ++i)
  758.             MPI_Send(&x,1,MPI_INT,i+1,1,MPI_COMM_WORLD);
  759.     }
  760.     else{
  761.         MPI_Recv(&val,1,MPI_INT,0,0,MPI_COMM_WORLD,&status);
  762.         MPI_Recv(&x,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
  763.         sour = unshuffle(rank-1,size-1)+1;
  764.         dest = shuffle(rank-1,size-1)+1;
  765.         for(int i=0; i<log2(size-1); ++i){
  766.             if(mask == 1)
  767.                 val *= x;
  768.             x = x * x;
  769.             MPI_Send(&mask,1,MPI_INT,dest,0,MPI_COMM_WORLD);
  770.             MPI_Recv(&mask,1,MPI_INT,sour,0,MPI_COMM_WORLD,&status);
  771.         }
  772.         if(rank == size-1)
  773.             val = 0;
  774.         for(int i=0; i<log2(size-1); ++i){
  775.             MPI_Send(&val,1,MPI_INT,dest,0,MPI_COMM_WORLD);
  776.             MPI_Recv(&val,1,MPI_INT,sour,0,MPI_COMM_WORLD,&status);
  777.             int temp = val;
  778.             if((rank-1)%2 == 0){
  779.                 MPI_Send(&temp,1,MPI_INT,rank+1,0,MPI_COMM_WORLD);
  780.                 MPI_Recv(&val,1,MPI_INT,rank+1,0,MPI_COMM_WORLD,&status);
  781.             }
  782.             else{
  783.                 MPI_Recv(&temp,1,MPI_INT,rank-1,0,MPI_COMM_WORLD,&status);
  784.                 val += temp;
  785.                 MPI_Send(&val,1,MPI_INT,rank-1,0,MPI_COMM_WORLD);
  786.             }
  787.         }
  788.         printf("%d %d\n",rank,val);
  789.     }
  790.     MPI_Finalize();
  791.     return 0;
  792. }
  793.  
  794. // polynomial multiplication
  795.  
  796. #include<stdio.h>
  797. #include<math.h>
  798. #include<mpi.h>
  799. int a[10][10],b[10][10],c[10][10];
  800.  
  801. int main(int argc,char* argv[]){
  802.  
  803. int rank,p,n,tag,source,dest;
  804.  
  805.         MPI_Init(&argc,&argv);            
  806.         MPI_Status status;
  807.         MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  808.         MPI_Comm_size(MPI_COMM_WORLD, &p);
  809.        
  810.  
  811. if(rank==0){
  812.  
  813.     scanf("%d",&n);
  814.  
  815.     int temp=n*n;
  816.  
  817.     for(int i=0;i<n;i++)
  818.         for(int j=0;j<n;j++)
  819.         scanf("%d",&a[i][j]);
  820.    
  821.    for(int i=0;i<n;i++)
  822.         for(int j=0;j<n;j++)
  823.         scanf("%d",&b[i][j]);
  824.    
  825.    for(int i=0;i<n;i++)
  826.         for(int j=0;j<n;j++)
  827.         {
  828.         dest=i*n+j+1;
  829.        
  830.         MPI_Send(&a[i][j],1,MPI_INT,dest,0,MPI_COMM_WORLD);
  831.        
  832.         MPI_Send(&b[i][j],1,MPI_INT,dest,1,MPI_COMM_WORLD);
  833.        
  834.         MPI_Send(&n,1,MPI_INT,dest,2,MPI_COMM_WORLD);
  835.        
  836.         }
  837.        
  838.        
  839.           for(int i=0;i<n;i++)
  840.         for(int j=0;j<n;j++)
  841.         {
  842.         dest=i*n+j+1;
  843.         int x;
  844.         MPI_Recv(&x,1,MPI_INT,dest,0,MPI_COMM_WORLD,&status);
  845.         c[i][j]=x;
  846.        
  847.         }
  848.        
  849.           for(int i=0;i<n;i++){
  850.                 for(int j=0;j<n;j++)
  851.                     {
  852.                     printf("%d\t",c[i][j]);
  853.        
  854.                     }
  855.                     printf("\n");
  856.                     }
  857.    
  858.  
  859.  
  860. }
  861.  
  862. else{
  863.     int x,y,z=0;
  864.  
  865.         MPI_Recv(&x,1,MPI_INT,0,0,MPI_COMM_WORLD,&status);
  866.  
  867.         MPI_Recv(&y,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
  868.  
  869.         MPI_Recv(&n,1,MPI_INT,0,2,MPI_COMM_WORLD,&status);
  870.  
  871. //left shift in a and upword shift in b
  872.        
  873.        
  874.         int i=(rank-1)/n;
  875.         int j=(rank-1)%n;
  876.        
  877.         for(int k=0;k<n-1;k++){
  878.             if(i>k){
  879.        
  880.             dest=i*n+(j-1+n)%n+1;
  881.             MPI_Send(&x,1,MPI_INT,dest,0,MPI_COMM_WORLD);
  882.        
  883.             source=i*n+(j+1)%n+1;
  884.             MPI_Recv(&x,1,MPI_INT,source,0,MPI_COMM_WORLD,&status);
  885.        
  886.             }
  887.        
  888.             if(j>k){
  889.        
  890.             dest=((i-1+n)%n)*n+j+1;
  891.             MPI_Send(&y,1,MPI_INT,dest,0,MPI_COMM_WORLD);
  892.        
  893.             source=((i+1)%n)*n+j+1;
  894.             MPI_Recv(&y,1,MPI_INT,source,0,MPI_COMM_WORLD,&status);
  895.        
  896.        
  897.             }
  898.        
  899.  
  900.         }
  901.        
  902.        
  903.         for(int k=0;k<n;k++){
  904.        
  905.         z+=x*y;
  906.        
  907.            dest=i*n+(j-1+n)%n+1;
  908.             MPI_Send(&x,1,MPI_INT,dest,0,MPI_COMM_WORLD);
  909.        
  910.             source=i*n+(j+1)%n+1;
  911.             MPI_Recv(&x,1,MPI_INT,source,0,MPI_COMM_WORLD,&status);
  912.        
  913.            
  914.        
  915.             dest=((i-1+n)%n)*n+j+1;
  916.             MPI_Send(&y,1,MPI_INT,dest,0,MPI_COMM_WORLD);
  917.        
  918.             source=((i+1)%n)*n+j+1;
  919.             MPI_Recv(&y,1,MPI_INT,source,0,MPI_COMM_WORLD,&status);
  920.        
  921.  
  922.        
  923.        
  924.         }
  925.        
  926.        
  927.         MPI_Send(&z,1,MPI_INT,0,0,MPI_COMM_WORLD);
  928.  
  929.  
  930.  
  931.  
  932. }
  933.  
  934.  
  935.  
  936. MPI_Finalize();
  937.  
  938. return 0;
  939.  
  940. }
  941.  
  942. // 2d torus matrix multiplication
Add Comment
Please, Sign In to add comment