#include <iostream>

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

#define NELEMENTS 7

using namespace std;

int main(int argc, char ** argv)
{
    int rank, size;
    int buf[NELEMENTS];
    
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    
    if (rank == 0) {
        for(int i = 0; i < NELEMENTS; i++) buf[i] = 1 + i*i;
    }
    
    MPI_Bcast(buf, NELEMENTS, MPI_INT, 0, MPI_COMM_WORLD);
    
//    for(rank = 1; rank < size; rank++)
//    {
//        for(int i = 0; i < NELEMENTS; i++)
//        {
//            cout<<"buf["<<i<<"] = "<<buf[i]<<endl;
//        }
//        cout<<endl;
//    }
    
    if (rank == 0) {
        cout<<"I am processor "<<rank<<endl;
        cout<<endl;
        for(int i = 0; i < NELEMENTS; i++)
        {
            cout<<"buf["<<i<<"] = "<<buf[i]<<endl;
        }
    }
    
    cout<<endl;
    
    if (rank == 1) {
        cout<<"I am processor "<<rank<<endl;
        cout<<endl;
        for(int i = 0; i < NELEMENTS; i++)
        {
            cout<<"buf["<<i<<"] = "<<buf[i]<<endl;
        }
    }
    
    cout<<endl;
    
    if (rank == 2) {
        cout<<"I am processor "<<rank<<endl;
        cout<<endl;
        for(int i = 0; i < NELEMENTS; i++)
        {
            cout<<"buf["<<i<<"] = "<<buf[i]<<endl;
        }
    }
    
    cout<<endl;
    
    if (rank == 3) {
        cout<<"I am processor "<<rank<<endl;
        cout<<endl;
        for(int i = 0; i < NELEMENTS; i++)
        {
            cout<<"buf["<<i<<"] = "<<buf[i]<<endl;
        }
    }
    
//    if (rank == 0) {
//        cout<<"I am processor "<<rank<<endl;
//        cout<<endl;
//        for(int i = 0; i < NELEMENTS; i++)
//        {
//            cout<<"buf["<<i<<"] = "<<buf[i]<<endl;
//        }
//    }
    
    MPI_Finalize();
    return 0;
}
