Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include "mpi.h"
- #include<math.h>
- int main(int argc,char *argv[]){
- int my_rank;
- int p;
- int source;
- int dest;
- int tag=0;
- int part=0;
- int i;
- char message[100];
- MPI_Status status;
- printf("hello");
- MPI_Init(&argc,&argv);
- MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
- MPI_Comm_size(MPI_COMM_WORLD, &p);
- if(my_rank!=0){
- sprintf(message, "greetings from process %d \n",my_rank);
- dest=0;
- int num1[4], i=0, sum=0;
- source = 0;
- MPI_Recv(&num1, 4, MPI_INT, source, tag, MPI_COMM_WORLD, &status);
- i = 0;
- for(i=0; i<3; i++){
- sum += num1[i];
- }
- MPI_Send(&sum,1, MPI_INT,dest,tag,MPI_COMM_WORLD);
- }
- else{
- i = 0;
- int j = 0;
- int inp[4][3];
- printf("enter numbers: \n");
- scanf("%d", &inp[0][0]);
- int arr_sum = inp[0][0];
- for(i=1; i<4; i++){
- for(j=0; j<3; j++)
- scanf("%d", &inp[i][j]);
- dest = i;
- MPI_Send(&inp[i],4, MPI_INT,dest,tag,MPI_COMM_WORLD);
- }
- for(source=1;source<p;source++){
- MPI_Recv(&part,1,MPI_INT,source,tag,MPI_COMM_WORLD,&status);
- arr_sum += part;
- }
- printf("%d \n",arr_sum);
- }
- MPI_Finalize();
- }
- //arrsum
- #include<mpi.h>
- #include<math.h>
- #include<stdio.h>
- float poly[2][8] = {{0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0},
- {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0}};
- float lagrange(int a, int myid, int size, int rem, float b, int n);
- void main(int argc, char* argv[]){
- int n, p, i, j, rem, num;
- float h, result, b, pi;
- float my_a, my_range;
- int myid, source, dest, tag;
- MPI_Status status;
- float my_result;
- n = 8;
- int a = 0;
- dest = 0;
- tag = 123;
- MPI_Init(&argc,&argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &myid);
- MPI_Comm_size(MPI_COMM_WORLD, &p);
- rem = n%p;
- int size = (n-rem)/p;
- rem = size + rem;
- b = 3.5;
- my_result = lagrange(a, myid, size, rem, b, n);
- printf("Process %d has the partial result of %f\n", myid,my_result);
- if(myid == 0) {
- result = my_result;
- for (i=1;i<p;i++) {
- source = i;
- float my_result1;
- MPI_Recv(&my_result1, 1, MPI_REAL, source, tag,
- MPI_COMM_WORLD, &status);
- result += my_result1;
- }
- printf("The result =%f\n",result);
- }
- else
- MPI_Send(&my_result, 1, MPI_REAL, dest, tag,
- MPI_COMM_WORLD);
- MPI_Finalize();
- }
- float lagrange(int a, int myid, int size, int rem, float b, int n)
- {
- int j, i, aij;
- float value;
- if(myid==0) aij = a + rem;
- else{
- a = a + (myid-1)*size + rem;
- aij = a + size;
- }
- value = 0.0;
- for (i=a;i<aij;i++) {
- float value_i = 1.0;
- for(j=0; j<n; j++){
- if(j != i){
- value_i = value_i*(b-poly[0][j])/(poly[0][i]-poly[0][j]);
- }
- }
- printf("value of %d %d is %f\n", myid, i, poly[1][i]*value_i);
- value += poly[1][i]*value_i;
- }
- printf("\n");
- return (value);
- }
- //lagrange
- #include<mpi.h>
- #include<math.h>
- #include<stdio.h>
- float simpson(float a, int myid, int size, int rem, float b, int n, int p);
- void main(int argc, char* argv[]){
- int n, p, i, j, rem, num;
- float h, result, b, pi;
- float my_a, my_range;
- int myid, source, dest, tag;
- MPI_Status status;
- float my_result;
- n = 12;
- float a = 2.0;
- dest = 0;
- tag = 123;
- MPI_Init(&argc,&argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &myid);
- MPI_Comm_size(MPI_COMM_WORLD, &p);
- int size = n/p;
- rem = size + n%p;
- b = 6.0;
- my_result = simpson(a, myid, size, rem, b, n, p);
- printf("Process %d has the partial result of %f\n", myid,my_result);
- if(myid == 0) {
- result = my_result;
- for (i=1;i<p;i++) {
- source = i;
- float my_result1;
- MPI_Recv(&my_result1, 1, MPI_REAL, source, tag,
- MPI_COMM_WORLD, &status);
- result += my_result1;
- }
- printf("The result =%f\n",result);
- }
- else
- MPI_Send(&my_result, 1, MPI_REAL, dest, tag,
- MPI_COMM_WORLD);
- MPI_Finalize();
- }
- float simpson(float a, int myid, int size, int rem, float b, int n, int p)
- {
- float i, aij;
- float h = (b-a)/n;
- printf("%f\n", h);
- int boom = 0;
- float value;
- if(myid==0){
- aij = a + rem*h;
- }
- else{
- a = a + ((myid-1)*size + rem)*h;
- boom = (myid-1)*size + rem;
- aij = a + size*h;
- if(myid == p-1) aij += h;
- }
- value = 0.0;
- int j = 0;
- for (i=a;i<aij;i+=h) {
- if(boom+j == 0 || boom+j == n)
- value += i*i;
- else if((boom+j)%2 == 0){
- value += 2.0*i*i;
- }
- else{
- value += 4.0*i*i;
- }
- j++;
- // printf("value of %d %d is %f\n", myid, i, value);
- }
- printf("\n");
- return ((1.0/3)*value*h);
- }
- //simpson
- #include <mpi.h>
- #include <math.h>
- #include <stdio.h>
- float fct(float x)
- {
- return x*x*x;
- }
- float integral(float a, int n, float h);
- void main(argc,argv)
- int argc;
- char *argv[];
- {
- int n, p, i, j, ierr,num;
- float h, result, a, b, pi;
- float my_a, my_range;
- int myid, source, dest, tag;
- MPI_Status status;
- float my_result;
- pi = acos(-1.0);
- a = 0.;
- b = 2.;
- n = 100000;
- dest = 0;
- tag = 123;
- MPI_Init(&argc,&argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &myid);
- MPI_Comm_size(MPI_COMM_WORLD, &p);
- h = (b-a)/n;
- num = n/p;
- my_range = (b-a)/p;
- my_a = a + myid*my_range;
- my_result = integral(my_a,num,h);
- printf("Process %d has the partial result of %f\n", myid,my_result);
- if(myid == 0) {
- result = my_result;
- for (i=1;i<p;i++) {
- source = i;
- MPI_Recv(&my_result, 1, MPI_REAL, source, tag,
- MPI_COMM_WORLD, &status);
- result += my_result;
- }
- printf("The result =%f\n",result);
- }
- else
- MPI_Send(&my_result, 1, MPI_REAL, dest, tag,
- MPI_COMM_WORLD);
- MPI_Finalize();
- }
- float integral(float a, int n, float h)
- {
- int j;
- float h2, aij, integ;
- integ = 0.0;
- h2 = h/2.;
- for (j=0;j<n;j++) {
- aij = a + h;
- integ +=0.5*fct(aij)*h+0.5*fct(a)*h;
- a=aij;
- }
- return (integ);
- }
- //integ
- #include<mpi.h>
- #include<math.h>
- #include<stdio.h>
- float mat1[4][4] = {{0.0, 1.0, 2.0, 3.0}, {4.0, 5.0, 6.0, 7.0},
- {0.0, 1.0, 2.0, 3.0}, {4.0, 5.0, 6.0, 7.0}};
- float mat2[4][4] = {{0.0, 1.0, 2.0, 3.0}, {4.0, 5.0, 6.0, 7.0},
- {0.0, 1.0, 2.0, 3.0}, {4.0, 5.0, 6.0, 7.0}};
- float mat3[4][4];
- float matmul(int myid, int size, int rem, int n);
- void main(int argc, char* argv[]){
- int n, p, i, j, rem, num;
- float h, result, b, pi;
- float my_a, my_range;
- int myid, source, dest, tag;
- MPI_Status status;
- float my_result;
- n = 8;
- int a = 0;
- dest = 0;
- tag = 123;
- MPI_Init(&argc,&argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &myid);
- MPI_Comm_size(MPI_COMM_WORLD, &p);
- rem = n%p;
- int size = (n-rem)/p;
- rem = size + rem;
- my_result = matmul(myid, 1, 1, n);
- //printf("Process %d has the partial result of %f\n", myid,my_result);
- if(myid == 0) {
- result = my_result;
- mat3[myid/4][myid%4] = result;
- for (i=1;i<p;i++) {
- source = i;
- float my_result1;
- MPI_Recv(&my_result1, 1, MPI_FLOAT, source, tag,
- MPI_COMM_WORLD, &status);
- mat3[i/4][i%4] = my_result1;
- }
- for (i=0; i<4; i++){
- for (j=0; j<4; j++){
- printf("%f ", mat3[i][j]);
- }
- printf("\n");
- }
- }
- else{
- MPI_Send(&my_result, 1, MPI_FLOAT, dest, tag,
- MPI_COMM_WORLD);
- }
- MPI_Finalize();
- }
- float matmul(int myid, int size, int rem, int n){
- int j, i, aij = 4;
- float value;
- i = myid / 4;
- j = myid % 4;
- int k;
- value = 0.0;
- for (k=0;k<aij;k++) {
- value += mat1[i][k]*mat2[k][j];
- }
- //printf("\n");
- return (value);
- }
- //matmul
- #include<stdio.h>
- #include<string.h>
- #include"mpi.h"
- #include<math.h>
- int routingFn(int j, int i){
- return j+pow(2,i);
- }
- void main(int argc, char* argv[]){
- int my_rank,p,source,dest,tag=0;
- int tag2=1;
- MPI_Status status;
- MPI_Init(&argc, &argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
- MPI_Comm_size(MPI_COMM_WORLD, &p);
- int n;
- int my_value;
- int my_step;
- int total_step = log2(p);
- if(my_rank==0){
- my_step=0;
- int arr[] = {1,2,3,4,5,6,7,8};
- my_value = arr[0];
- int i;
- for(my_step=0;my_step<total_step;my_step++){
- for(i=1;i<p;i++){
- int t = arr[i];
- MPI_Send(&t, 1, MPI_INT,i,tag,MPI_COMM_WORLD);
- MPI_Send(&my_step,1,MPI_INT,i,tag+1,MPI_COMM_WORLD);
- }
- int ns;
- ns = routingFn(my_rank,my_step);
- int k = arr[my_rank];
- MPI_Send(&k, 1, MPI_INT,ns,tag+3,MPI_COMM_WORLD);
- for(i=1;i<p;i++){
- int y;
- MPI_Recv(&y,1,MPI_INT,i,tag+2,MPI_COMM_WORLD,&status);
- arr[i] = y;
- }
- }
- for(i=0;i<p;i++){
- printf("Result for P[%d] : %d\n",i,arr[i]);
- }
- }
- else{
- int j;
- for(j=0;j<total_step;j++){
- source = 0;
- MPI_Recv(&my_value,1,MPI_INT,source,tag,MPI_COMM_WORLD,&status);
- MPI_Recv(&my_step,1,MPI_INT,source,tag+1,MPI_COMM_WORLD,&status);
- int ns;
- if(my_rank%2==0){
- ns = routingFn(my_rank,my_step);
- if(ns<8){
- MPI_Send(&my_value, 1, MPI_INT,ns,tag+3,MPI_COMM_WORLD);
- }
- int incoming;
- int incomingP = my_rank - pow(2,my_step);
- if(my_rank>=pow(2,my_step)){
- MPI_Recv(&incoming, 1, MPI_INT,incomingP,tag+3,MPI_COMM_WORLD,&status);
- }
- my_value += incoming;
- MPI_Send(&my_value, 1, MPI_INT,source,tag+2,MPI_COMM_WORLD);
- }
- else{
- ns = routingFn(my_rank,my_step);
- int incoming;
- int incomingP = my_rank - pow(2,my_step);
- if(my_rank>=pow(2,my_step)){
- MPI_Recv(&incoming, 1, MPI_INT,incomingP,tag+3,MPI_COMM_WORLD,&status);
- }
- if(ns<8){
- MPI_Send(&my_value, 1, MPI_INT,ns,tag+3,MPI_COMM_WORLD);
- }
- my_value += incoming;
- MPI_Send(&my_value, 1, MPI_INT,source,tag+2,MPI_COMM_WORLD);
- }
- }
- }
- MPI_Finalize();
- }
- //prefix
- #include<stdio.h>
- #include<string.h>
- #include<math.h>
- #include "mpi.h"
- int main(int argc,char *argv[]){
- int my_rank;
- int p;
- int source;
- int dest;
- int tag=0;
- float part=0;
- int i=0, n=1, a=0, b=0, size=1, rem=0;
- float h = 0.0;
- MPI_Status status;
- MPI_Init(&argc,&argv);
- MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
- MPI_Comm_size(MPI_COMM_WORLD, &p);
- //printf("hello\n");
- if(my_rank!=0){
- dest=0;
- int num1, i=0;
- float sum=0;
- source = 0;
- MPI_Recv(&num1, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &status);
- i = 0;
- float temp = 0;
- for(i=0; i<size; i++){
- temp = pow((a+((my_rank-1)*size + rem + i)*h), 2) + 1;
- sum += temp;
- }
- if(my_rank==3){
- sum -= 0.5*temp;
- }
- MPI_Send(&sum,1, MPI_FLOAT,dest,tag,MPI_COMM_WORLD);
- }
- else{
- printf("enter n, a, b:\n ");
- scanf("%d %d %d", &n, &a, &b);
- h = (float)(b-a)/(float)n;
- rem = n%3;
- size = (n-rem)/3;
- i = 0;
- int j = 0;
- float inp[4][size];
- float arr_sum = 0.0;
- inp[0][0] = 0.5*(pow(a, 2) + 1);
- arr_sum += inp[0][0];
- for(i=1; i<rem; i++){
- inp[0][i] = pow(a+i*h, 2) + 1;
- arr_sum += inp[0][i];
- }
- for(i=1; i<4; i++){
- dest = i;
- MPI_Send(&i,1, MPI_INT,dest,tag,MPI_COMM_WORLD);
- }
- for(source=1;source<p;source++){
- MPI_Recv(&part,1,MPI_FLOAT,source,tag,MPI_COMM_WORLD,&status);
- arr_sum += part;
- }
- arr_sum *= h;
- printf("%f \n",arr_sum);
- }
- MPI_Finalize();
- }
- //trap
- #include<stdio.h>
- #include<string.h>
- #include "mpi.h"
- int main(int argc, char *arg[]){
- int my_rank, p, source, dest, tag=0;
- int n = 6;
- int a[] = {1, 4, 5, 2, 1, 3};
- MPI_Status status;
- MPI_Init(&argc,&arg);
- MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
- MPI_Comm_size(MPI_COMM_WORLD, &p);
- int curr = a[my_rank];
- for(int it = 0; it <= n/2; it++) {
- for(int ph = 0; ph < 2; ph++) {
- if((ph&1) == (my_rank&1)){
- if(my_rank < n-1) {
- int nxt;
- MPI_Recv(&nxt, 1, MPI_INT, my_rank+1, tag, MPI_COMM_WORLD, &status);
- MPI_Send(&curr, 1, MPI_INT, my_rank+1, tag, MPI_COMM_WORLD);
- curr = nxt < curr ? nxt : curr;
- }
- }
- else {
- if(my_rank > 0) {
- MPI_Send(&curr, 1, MPI_INT, my_rank-1, tag, MPI_COMM_WORLD);
- int prev;
- MPI_Recv(&prev, 1, MPI_INT, my_rank-1, tag, MPI_COMM_WORLD, &status);
- curr = prev > curr ? prev : curr;
- }
- }
- }
- }
- if (my_rank!=0){
- MPI_Send(&curr, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
- }
- else {
- int sarr[n], i = 0;
- sarr[i++] = curr;
- for(int p = 1, num; p < n; p++) {
- MPI_Recv(&num, 1, MPI_INT, p, tag, MPI_COMM_WORLD,&status);
- sarr[i++] = num;
- }
- for(i = 0; i < n; i++){
- printf("%d ", sarr[i]);
- }
- }
- MPI_Finalize();
- return 0;
- }
- //odd-even
- #include<stdio.h>
- #include"mpi.h"
- void main(int argc, char* argv[]){
- int n, p, i, j, rem, num;
- float h, result, b, pi;
- int myid, source=0, dest, tag;
- MPI_Status status;
- float my_result;
- n = 8;
- int a = 0;
- dest = 0;
- tag = 123;
- MPI_Init(&argc,&argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &myid);
- MPI_Comm_size(MPI_COMM_WORLD, &p);
- //rem = n%p;
- //int size = (n-rem)/p;
- //rem = size + rem;
- //my_result = matmul(myid, 1, 1, n);
- //printf("Process %d has the partial result of %f\n", myid,my_result);
- int mat1[8] = {2, 5, 0, 3, 6, 1, 4, 7};
- int my_num = mat1[myid];
- for(j=0; j<n/2 + 1; j++){
- int partner;
- if(myid<p-1 && myid!=0){
- if(myid%2==1){
- partner = myid+1;
- MPI_Send(&my_num, 1, MPI_INT, partner, tag, MPI_COMM_WORLD);
- int comp;
- MPI_Recv(&comp, 1, MPI_INT, partner, tag, MPI_COMM_WORLD, &status);
- if(my_num>comp) my_num = comp;
- }
- if(myid%2==0){
- partner = myid-1;
- int comp;
- MPI_Recv(&comp, 1, MPI_INT, partner, tag, MPI_COMM_WORLD, &status);
- MPI_Send(&my_num, 1, MPI_INT, partner, tag, MPI_COMM_WORLD);
- if(my_num<comp) my_num = comp;
- }
- }
- else if(myid<p-1){
- if(myid%2==0){
- partner = myid+1;
- MPI_Send(&my_num, 1, MPI_INT, partner, tag, MPI_COMM_WORLD);
- int comp;
- MPI_Recv(&comp, 1, MPI_INT, partner, tag, MPI_COMM_WORLD, &status);
- if(my_num>comp) my_num = comp;
- }
- if(myid%2==1){
- partner = myid-1;
- int comp;
- MPI_Recv(&comp, 1, MPI_INT, partner, tag, MPI_COMM_WORLD, &status);
- MPI_Send(&my_num, 1, MPI_INT, partner, tag, MPI_COMM_WORLD);
- if(my_num<comp) my_num = comp;
- }
- }
- }
- if(myid!=0) MPI_Send(&my_num, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
- for(i=1;i<=p-1;i++) {
- source = i;
- int num;
- MPI_Recv(&num, 1, MPI_INT, source, tag,
- MPI_COMM_WORLD, &status);
- printf("%d ", num);
- }
- printf("\n");
- MPI_Finalize();
- }
- //oddeven
- #include <stdio.h>
- #include <math.h>
- #include <mpi.h>
- int shuffle(int rank, int size){
- int n = log2(size);
- return ((rank<<1) | (rank>>(n-1))) & ((1<<n)-1);
- }
- int unshuffle(int rank, int size){
- int n = log2(size);
- return ((rank>>1) | (rank<<(n-1))) & ((1<<n)-1);
- }
- int main(int argc, char* argv[]){
- int rank, size, sour, dest, x, mask, val;
- int arr[100];
- MPI_Status status;
- MPI_Init(&argc,&argv);
- MPI_Comm_rank(MPI_COMM_WORLD,&rank);
- MPI_Comm_size(MPI_COMM_WORLD,&size);
- mask = (rank+1) % 2;
- if(rank == 0){
- scanf("%d",&x);
- for(int i=0; i<size-2; ++i)
- scanf("%d",arr+i);
- arr[size-2] = 1;
- for(int i=0; i<size-1; ++i)
- MPI_Send(&arr[i],1,MPI_INT,i+1,0,MPI_COMM_WORLD);
- for(int i=0; i<size-1; ++i)
- MPI_Send(&x,1,MPI_INT,i+1,1,MPI_COMM_WORLD);
- }
- else{
- MPI_Recv(&val,1,MPI_INT,0,0,MPI_COMM_WORLD,&status);
- MPI_Recv(&x,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
- sour = unshuffle(rank-1,size-1)+1;
- dest = shuffle(rank-1,size-1)+1;
- for(int i=0; i<log2(size-1); ++i){
- if(mask == 1)
- val *= x;
- x = x * x;
- MPI_Send(&mask,1,MPI_INT,dest,0,MPI_COMM_WORLD);
- MPI_Recv(&mask,1,MPI_INT,sour,0,MPI_COMM_WORLD,&status);
- }
- if(rank == size-1)
- val = 0;
- for(int i=0; i<log2(size-1); ++i){
- MPI_Send(&val,1,MPI_INT,dest,0,MPI_COMM_WORLD);
- MPI_Recv(&val,1,MPI_INT,sour,0,MPI_COMM_WORLD,&status);
- int temp = val;
- if((rank-1)%2 == 0){
- MPI_Send(&temp,1,MPI_INT,rank+1,0,MPI_COMM_WORLD);
- MPI_Recv(&val,1,MPI_INT,rank+1,0,MPI_COMM_WORLD,&status);
- }
- else{
- MPI_Recv(&temp,1,MPI_INT,rank-1,0,MPI_COMM_WORLD,&status);
- val += temp;
- MPI_Send(&val,1,MPI_INT,rank-1,0,MPI_COMM_WORLD);
- }
- }
- printf("%d %d\n",rank,val);
- }
- MPI_Finalize();
- return 0;
- }
- // polynomial multiplication
- #include<stdio.h>
- #include<math.h>
- #include<mpi.h>
- int a[10][10],b[10][10],c[10][10];
- int main(int argc,char* argv[]){
- int rank,p,n,tag,source,dest;
- MPI_Init(&argc,&argv);
- MPI_Status status;
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- MPI_Comm_size(MPI_COMM_WORLD, &p);
- if(rank==0){
- scanf("%d",&n);
- int temp=n*n;
- for(int i=0;i<n;i++)
- for(int j=0;j<n;j++)
- scanf("%d",&a[i][j]);
- for(int i=0;i<n;i++)
- for(int j=0;j<n;j++)
- scanf("%d",&b[i][j]);
- for(int i=0;i<n;i++)
- for(int j=0;j<n;j++)
- {
- dest=i*n+j+1;
- MPI_Send(&a[i][j],1,MPI_INT,dest,0,MPI_COMM_WORLD);
- MPI_Send(&b[i][j],1,MPI_INT,dest,1,MPI_COMM_WORLD);
- MPI_Send(&n,1,MPI_INT,dest,2,MPI_COMM_WORLD);
- }
- for(int i=0;i<n;i++)
- for(int j=0;j<n;j++)
- {
- dest=i*n+j+1;
- int x;
- MPI_Recv(&x,1,MPI_INT,dest,0,MPI_COMM_WORLD,&status);
- c[i][j]=x;
- }
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++)
- {
- printf("%d\t",c[i][j]);
- }
- printf("\n");
- }
- }
- else{
- int x,y,z=0;
- MPI_Recv(&x,1,MPI_INT,0,0,MPI_COMM_WORLD,&status);
- MPI_Recv(&y,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
- MPI_Recv(&n,1,MPI_INT,0,2,MPI_COMM_WORLD,&status);
- //left shift in a and upword shift in b
- int i=(rank-1)/n;
- int j=(rank-1)%n;
- for(int k=0;k<n-1;k++){
- if(i>k){
- dest=i*n+(j-1+n)%n+1;
- MPI_Send(&x,1,MPI_INT,dest,0,MPI_COMM_WORLD);
- source=i*n+(j+1)%n+1;
- MPI_Recv(&x,1,MPI_INT,source,0,MPI_COMM_WORLD,&status);
- }
- if(j>k){
- dest=((i-1+n)%n)*n+j+1;
- MPI_Send(&y,1,MPI_INT,dest,0,MPI_COMM_WORLD);
- source=((i+1)%n)*n+j+1;
- MPI_Recv(&y,1,MPI_INT,source,0,MPI_COMM_WORLD,&status);
- }
- }
- for(int k=0;k<n;k++){
- z+=x*y;
- dest=i*n+(j-1+n)%n+1;
- MPI_Send(&x,1,MPI_INT,dest,0,MPI_COMM_WORLD);
- source=i*n+(j+1)%n+1;
- MPI_Recv(&x,1,MPI_INT,source,0,MPI_COMM_WORLD,&status);
- dest=((i-1+n)%n)*n+j+1;
- MPI_Send(&y,1,MPI_INT,dest,0,MPI_COMM_WORLD);
- source=((i+1)%n)*n+j+1;
- MPI_Recv(&y,1,MPI_INT,source,0,MPI_COMM_WORLD,&status);
- }
- MPI_Send(&z,1,MPI_INT,0,0,MPI_COMM_WORLD);
- }
- MPI_Finalize();
- return 0;
- }
- // 2d torus matrix multiplication
Add Comment
Please, Sign In to add comment