Vous êtes sur la page 1sur 9

#include <stdio.

h>
#include <mpi.h>
int fact(int a)
{
if(a==1 || a==0)
return a;
else
return a*fact(a-1);
}
int main(int argc, char* argv[])
{
int rank, size, i, a;
int arr[100];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank==0)
{
for(i=0; i<size; i++)
scanf("%d", &arr[i]);
}
MPI_Scatter(arr, 1, MPI_INT, &a, 1, MPI_INT, 0, MPI_COMM_WORLD);
printf("factorial of %d is %d\n", a, fact(a));
MPI_Finalize();
return 0;
}
#include <stdio.h>
#include <mpi.h>
int fact(int a)
{
if(a==1 || a==0)
return a;
else
return a*fact(a-1);
}
int main(int argc, char* argv[])
{
int rank, size, i, a, sum;
int arr[100];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank==0)
{
for(i=0; i<size; i++)
scanf("%d", &arr[i]);
}
MPI_Scatter(arr, 1, MPI_INT, &a, 1, MPI_INT, 0, MPI_COMM_WORLD);
a=fact(a);
MPI_Gather(&a, 1, MPI_INT, arr, 1, MPI_INT, 0, MPI_COMM_WORLD);
if(rank==0)
{
sum=0;
for(i=0; i<size; i++)
{
printf("%d ", arr[i]);
sum+=arr[i];
}
printf("\nSum: %d\n", sum);
}
MPI_Finalize();
return 0;
}
#include <stdio.h>
#include <mpi.h>
float avg(int a[], int n)
{
int i;
float sum=0;
for(i=0; i<n; i++)
sum+=a[i];
return (sum/n);
}
int main(int argc, char* argv[])
{
int rank, size, i, m;
float b, avrg;
int arr[100], a[100];
float fin[100];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank==0)
{
scanf("%d", &m);
for(i=0; i<size*m; i++)
scanf("%d", &arr[i]);
}
MPI_Bcast(&m, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Scatter(arr, m, MPI_INT, a, m, MPI_INT, 0, MPI_COMM_WORLD);
b=avg(a, m);
MPI_Gather(&b, 1, MPI_FLOAT, fin, 1, MPI_FLOAT, 0, MPI_COMM_WORLD);
if(rank==0)
{
avrg=0;
for(i=0; i<size; i++)
{
avrg+=fin[i];
}
avrg=avrg/(float)size;
printf("Average: %f\n", avrg);
}
MPI_Finalize();
return 0;
}
#include <stdio.h>
#include <mpi.h>
#include <string.h>
int non_vowel(char a[], int n)
{
int i, sum=0;
for(i=0; i<n; i++)
if(a[i]!='a' && a[i]!='e' && a[i]!='i' && a[i]!='o' &&
a[i]!='u')
sum++;
return sum;
}
int main(int argc, char* argv[])
{
int rank, size, i, m, b, sum;
char arr[100], a[100];
int fin[100];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank==0)
{
scanf("%s", arr);
m=strlen(arr)/size;
}
MPI_Bcast(&m, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Scatter(arr, m, MPI_CHAR, a, m, MPI_CHAR, 0, MPI_COMM_WORLD);
b=non_vowel(a, m);
MPI_Gather(&b, 1, MPI_INT, fin, 1, MPI_INT, 0, MPI_COMM_WORLD);
if(rank==0)
{
sum=0;
for(i=0; i<size; i++)
{
printf("Non Vowels in process %d: %d\n", i, fin[i]);
sum+=fin[i];
}
printf("Total non vowels: %d\n", sum);
}
MPI_Finalize();
return 0;
}
#include <stdio.h>
#include <mpi.h>
#include <string.h>
int main(int argc, char* argv[])
{
int rank, size, i, m, k;
char s1[100], s2[100], aux1[100], aux2[100], aux3[100], fin[100];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank==0)
{
scanf("%s", s1);
scanf("%s", s2);
m=strlen(s1)/size;
}
MPI_Bcast(&m, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Scatter(s1, m, MPI_CHAR, aux1, m, MPI_CHAR, 0, MPI_COMM_WORLD);
MPI_Scatter(s2, m, MPI_CHAR, aux2, m, MPI_CHAR, 0, MPI_COMM_WORLD);
k=0;
for(i=0; i<m; i++)
{
aux3[k++]=aux1[i];
aux3[k++]=aux2[i];
}
MPI_Gather(aux3, 2*m, MPI_CHAR, fin, 2*m, MPI_CHAR, 0,
MPI_COMM_WORLD);
if(rank==0)
{
for(i=0; i<strlen(s1)*2; i++)
printf("%c", fin[i]);
printf("\n");
}
MPI_Finalize();
return 0;
}
Week 3 codes for PCAP lab
#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 );
if ( r == 0 )
{
scanf( "%s", c );
l = strlen ( c ) / s;
}
MPI_Bcast( &l, 1, MPI_INT, 0, MPI_COMM_WORLD );
int t[l], res_a[l * s];
char res[l * s], v[l];
MPI_Scatter( c, l, MPI_CHAR, v, l, MPI_CHAR, 0, MPI_COMM_WORLD );
for ( i = 0; i < l; i++ )
{
if ( v[i] <= 'Z' )
{
v[i]+= 32;
t[i] = ( int )v[i];
}
else
{
v[i]-= 32;
t[i] = ( int )v[i] ;
}
}
MPI_Gather( v, l, MPI_CHAR, res, l, MPI_CHAR, 0, MPI_COMM_WORLD );
MPI_Gather( t, l, MPI_INT, res_a, l, MPI_INT, 0, MPI_COMM_WORLD );
if ( r == 0 )
{
for ( i = 0; i < l * s ; i++ )
{
printf( " %c\t", res[i] );
printf( "%d\n", res_a[i] );
}
}
MPI_Finalize();
}
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <string.h>

int comp ( int a, int r )


{
int i, res = 1;
for ( i = 1; i <= ( r + 2 ); i++ )
{
res*= a;
}
return res;
}
int main ( int argc, char * argv[] )
{
int r, s, i, j, l, m;
int a[100];
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &r );
MPI_Comm_size( MPI_COMM_WORLD, &s );
if ( r == 0 )
{
scanf( "%d", &m );
printf( "Enter %d elements\n", m * s );
for ( i = 0; i < m * s; i++ )
{
scanf( "%d", &a[i] );
}
}
MPI_Bcast( &m, 1, MPI_INT, 0, MPI_COMM_WORLD );
int t[m], res[m * s];
MPI_Scatter( a, m, MPI_INT, t, m, MPI_INT, 0, MPI_COMM_WORLD );
for ( i = 0; i < m; i++ )
{
t[i] = comp( t[i], r );
}
MPI_Gather( t, m, MPI_INT, res, m, MPI_INT, 0, MPI_COMM_WORLD );
if ( r == 0 )
{
for ( i = 0; i < m * s ; i++ )
{
printf( " %d\t", res[i] );
}
}
MPI_Finalize();
}
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <string.h>

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


{
int r, s, i, j, l, m;
int a[100];
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &r );
MPI_Comm_size( MPI_COMM_WORLD, &s );
if ( r == 0 )
{
scanf( "%d", &m );
printf( "Enter %d elements\n", m * s );
for ( i = 0; i < m * s; i++ )
{
scanf( "%d", &a[i] );
}
}
MPI_Bcast( &m, 1, MPI_INT, 0, MPI_COMM_WORLD );
int t[m], res[m * s];
MPI_Scatter( a, m, MPI_INT, t, m, MPI_INT, 0, MPI_COMM_WORLD );
for ( i = 0; i < m; i++ )
{
t[i] = ( t[i] * ( t[i] + 1 ) / 2 );
}
MPI_Gather( t, m, MPI_INT, res, m, MPI_INT, 0, MPI_COMM_WORLD );
if ( r == 0 )
{
for ( i = 0; i < m * s ; i++ )
{
printf( " %d\t", res[i] );
}
}
MPI_Finalize();
}