Sunteți pe pagina 1din 9

#include <stdio.

h>
#include <mpi.h>
#include <string.h>

int fac(int x){


if(x==1 || x==0)
return 1;
else
return x*fac(x-1);
}

void main(int argc, char *argv[]){

int rank,size,i;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);

int num=fac(rank+1);

int sum;
MPI_Scan(&num,&sum,1,MPI_INT,MPI_SUM,MPI_COMM_WORLD);

if(rank==size-1)
printf("Sum: %d\n",sum);
MPI_Finalize();
}
#include <stdio.h>
#include <mpi.h>

void main(int argc, char *argv[]){

int rank,size,i;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);

int done=0,n;
double h,sum,x,mypi,pi;
double PI=3.14159265;

while(!done){
if(rank==0){
printf("Intervals: (0 quits)");
scanf("%d",&n);
}

MPI_Bcast(&n,1,MPI_INT,0,MPI_COMM_WORLD);
if(n==0)break;

h=1.0/(double)n;
sum=0.0;

for(i=rank+1;i<=n;i+=size){
x=h*((double)i-0.5);
sum+=4.0/(1.0+x*x);
}

mypi=h*sum;
MPI_Reduce(&mypi,&pi,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);

if(rank==0)
printf("Pi is approx %f, Error: %f",pi,fabs(pi-PI));
}
MPI_Finalize();
}
#include <stdio.h>
#include <mpi.h>

void main(int argc, char *argv[]){

int rank,size,i,j,n;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);

int a[3][3];
int b[3],count=0;
if(rank==0){
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("Enter n: ");
scanf("%d",&n);
}
MPI_Bcast(&n,1,MPI_INT,0,MPI_COMM_WORLD);
MPI_Scatter(a,3,MPI_INT,b,3,MPI_INT,0,MPI_COMM_WORLD);
int c[3];
for(i=0;i<3;i++)
if(b[i]==n)
count++;
MPI_Gather(&count,1,MPI_INT,c,1,MPI_INT,0,MPI_COMM_WORLD);
if(rank==0){
int final=0;
for(i=0;i<3;i++)
final+=c[i];
printf("Count of %d: %d",n,final);
}
MPI_Finalize();
}
#include <mpi.h>
#include <stdio.h>

void ErrorHandler (int error){


if(error!=MPI_SUCCESS){
char error_str[2048];
int len,error_class;
MPI_Error_class(error,&error_class);
MPI_Error_string(error_class,error_str,&len);
printf("Class:%d Error:%s\n",error_class,error_str);
}
else
printf("No error\n");
}

void main(int argc, char * argv[]){


int c=3;
int error,rank,size;
error=MPI_Init(&argc,&argv);
ErrorHandler(error);
MPI_Errhandler_set(MPI_COMM_WORLD,MPI_ERRORS_RETURN);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
error=MPI_Comm_size(c,&size);
ErrorHandler(error);
MPI_Finalize();
}
#include <mpi.h>
#include <stdio.h>

void main(int argc, char * argv[]){


int rank,size,i,j,n;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
int a[4][4],b[4],c[4],d[4][4];

if(rank==0){
printf("Enter matrix\n");
for(i=0;i<4;i++)
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
}

MPI_Scatter(a,4,MPI_INT,b,4,MPI_INT,0,MPI_COMM_WORLD);
MPI_Scan(b,c,4,MPI_INT,MPI_SUM,MPI_COMM_WORLD);
MPI_Gather(c,4,MPI_INT,d,4,MPI_INT,0,MPI_COMM_WORLD);
if(rank==0)
{
printf("\nOutput:\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf("%d ",d[i][j]);
printf("\n");
}
}

// for(j=0;j<4;j++)
// printf("%d ",c[j]);
// printf("\n");

MPI_Finalize();
}
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
//#define MPI_COMM_WORLD mcw
int main ( int argc, char * argv[] )
{
int r, s;
int a[3][3], b[3][3], i, j, d[3][3];
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &r );
MPI_Comm_size( MPI_COMM_WORLD, &s );
if ( r == 0 )
{
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
scanf( "%d", &a[i][j] );
}
}
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
scanf( "%d", &b[i][j] );
}
}
}
int m[3], n[3], q[3];
MPI_Scatter( a, 3, MPI_INT, m, 3, MPI_INT, 0, MPI_COMM_WORLD );
MPI_Scatter( b, 3, MPI_INT, n, 3, MPI_INT, 0, MPI_COMM_WORLD );
for ( i = 0; i < 3; i++ )
{
q[i] = m[i] + n[i];
}
MPI_Gather( q, 3, MPI_INT, d, 3, MPI_INT, 0, MPI_COMM_WORLD );
if ( r == 0 )
{
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
printf( "%d\t", d[i][j] );
}
printf( "\n" );
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
//#define MPI_COMM_WORLD mcw
int prime( int a[3] )
{
int i, j, c = 0, res = 0;
for ( i = 0; i < 3; i++ )
{
for ( j = 2; j <= ( a[i] / 2 ); j++ )
{
if ( a[i] % j == 0 )
{
c++;
break;
}
}
if ( c == 0 && a[i] != 1 )
{
res+= a[i];
}
c = 0;
}
return res;
}
int main ( int argc, char * argv[] )
{
int r, s;
int a[3][3], b[3][3], i, j, d[3][3];
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &r );
MPI_Comm_size( MPI_COMM_WORLD, &s );
if ( r == 0 )
{
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
scanf( "%d", &a[i][j] );
}
}
}
int m[3], n[3], q[3], res, final;
MPI_Scatter( a, 3, MPI_INT, m, 3, MPI_INT, 0, MPI_COMM_WORLD );
res = prime( m );
MPI_Reduce( &res, &final, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD );
if ( r == 0 )
{
printf( "%d", final );
}
}
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

int main ( int argc, char * argv[] )


{
int r, s, i, j;
int a[3][3], a_t[3][3];
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &r );
MPI_Comm_size( MPI_COMM_WORLD, &s );
if ( r == 0 )
{
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
scanf( "%d", &a[i][j] );
a_t[j][i] = a[i][j];
}
}
}
int m[3], n[3], max[3], min[3];
MPI_Scatter( a_t, 3, MPI_INT, m, 3, MPI_INT, 0, MPI_COMM_WORLD );
MPI_Scatter( a, 3, MPI_INT, n, 3, MPI_INT, 0, MPI_COMM_WORLD );
MPI_Reduce( m, max, 3, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD );
MPI_Reduce( n, min, 3, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD );
if ( r == 0 )
{
for ( i = 0; i < 3; i++ )
{
printf( "%d %d\n", max[i],min[i]);
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <string.h>

int main ( int argc, char * argv[] )


{
int r, s, i, j, l;
char c[100];
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &r );
MPI_Comm_size( MPI_COMM_WORLD, &s );
char res[100];
if ( r == 0 )
{
scanf( "%s", c );
l = strlen ( c );
}
int m[3], n[3], max[3], min[3];
char a[r], v;
MPI_Scatter( c, 1, MPI_CHAR, &v, 1, MPI_CHAR, 0, MPI_COMM_WORLD );
//a = { v };
for ( i = 0; i < r + 1; i++ )
{
a[i] = v;
}
for ( i = r + 1; i < s; i++ )
{
a[i] = '\0';
}
//memset( a, v, sizeof( a ) );
//MPI_Scatter( a_t, 3, MPI_INT, n, 3, MPI_INT, 0, MPI_COMM_WORLD );
MPI_Gather( a, s, MPI_CHAR, res, s, MPI_CHAR, 0, MPI_COMM_WORLD );
if ( r == 0 )
{
//res[( s * ( s + 1 ) / 2 )] = '\0';
for ( i = 0; i < s * s ; i++ )
{
printf( "%c", res[i] );
}
//printf( "%s", res );
}
MPI_Finalize();
}

S-ar putea să vă placă și