Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <mpi.h>
- #include <math.h>
- #define ROOT 0
- #define MSG_TAG 100
- int S=234;
- int K=3;
- int N=7;
- int i,w,x,j;
- int main(int argc,char **argv)
- {
- int size,tid;
- int R = 1;
- MPI_Init(&argc, &argv);
- int matrix [N][N] ;
- MPI_Comm_size( MPI_COMM_WORLD, &size );
- MPI_Comm_rank( MPI_COMM_WORLD, &tid );
- int res;
- int reallycount=0;
- if ( tid == 0 ) {
- MPI_Status status;
- int p,r;
- int i,j;
- int liczba;
- // wypelnianie macierzy glownej
- int matrix [N][N] ;
- for (i=0;i<N;i++){
- for(j=0;j<N;j++){
- matrix[i][j]=1+i*5;
- liczba = matrix[i][j];
- }
- }
- //zliczanie ile bedzie podmacierzy
- int podmacierze=0;
- for (i=0;i<N;i++){
- for(j=0;j<N;j++){
- if (i+K<=N & j+K<=N){
- podmacierze++;
- }
- }
- }
- printf("PODMACIERZE: %d\n", podmacierze);
- //PODZIAL PODMACIERZY NA PROCESY, WYSLANIE DO KAZDEGO PROCESU INFO, ILE DOSTANIE PODMACIERZY DO SPRAWDZENIA
- int x;
- for(x=1;x<size;x++){
- int forsend = (podmacierze/(size-x));
- podmacierze=podmacierze-forsend;
- printf("podmacierze obecnie %d\n", podmacierze);
- MPI_Send( &forsend, 1, MPI_INT, size-x, MSG_TAG, MPI_COMM_WORLD );
- }
- // WYSYLANIE PODMACIERZY
- int count=1;
- for (i=0;i<N;i++){
- for(j=0;j<N;j++){
- int matrix2[K][K];
- if (i+K<=N & j+K<=N){
- int proces = count%size;
- if(proces==0){
- proces++;
- count++;
- }
- for (w=0;w<K;w++){
- for (x=0;x<K;x++){
- matrix2[w][x]= matrix[i+w][i+x];
- int send = matrix2[w][x];
- MPI_Send( &send, 1, MPI_INT, proces, MSG_TAG, MPI_COMM_WORLD );
- }
- }
- count++;
- reallycount++;
- }
- }
- }
- printf("wyslalem %d podmacierzy\n", reallycount);
- }
- else {
- int forme=0;
- MPI_Status status3;
- MPI_Recv( &forme, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status3);
- printf("jestem proces numer %d, dla mnie jest %d podmacierzy\n", tid, forme);
- int cos;
- MPI_Status status2;
- int l,k;
- int z;
- int numberIteration=0;
- int countRec=0;
- int g;
- for(g=0;g<forme;g++){
- int sum=0;
- for (l=0;l<K;l++){
- for(k=0;k<K;k++){
- int wait;
- MPI_Recv( &wait, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status2);
- sum+=wait;
- }
- } printf("proces: %d, suma : %d\n",tid,sum);
- if(sum==S){printf("ZNALEZIONO : %d\n", sum);
- }
- numberIteration++;
- printf("proces: %d, numer iteracji : %d\n",tid,numberIteration);
- }
- }
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement