Example #1
0
// Run Game of Life
void Simulate(int n, int G, int** world) 
{
    int** g[2];
    int last = p - 1;

    g[0] = world;

    // Run for G generations
    for(gen = 0; gen < G; gen++)
    {
        // Barrier: synch cells to same generation
        MPI_Barrier();

        // Send data accross border
        if(rank == NODE)
            MPI_Send();

        // Send data accross border
        if(rank == last)
            MPI_Send();

        // Each rank i determines states for cells it owns
        DetermineState(world, row, col);

        if(n <= 16)
            DisplayGoL();
    }
}
Example #2
0
int main(int argc,char *argv[])
{
	int rank, p, N, G,X;

    struct timeval t1, t2, t3, t4;

    // Change this "N" from 2 up to 16

    N = 16;

    p = 2;

    G = atoi(argv[1]);

//    X = 3;

    X = 1;

    MPI_Init(&argc, &argv);

    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    MPI_Comm_size(MPI_COMM_WORLD, &p);



    //Timing for total runtime

    struct timeval totalRuntimeStart, totalRuntimeEnd;

    int totalRuntime;



    // Timing for average time per generation(excluding display)

    struct timeval avgPerGenerationStart, avgPerGenerationEnd;

    int averageTimePerGeneration;

    int tempTime=0;


    // Average time per the display function

    struct timeval displayRuntimeStart, displayRuntimeEnd;

    int averageDisplayTimePerGeneration;

    int displayTempTime;

    int displayMethodCounter = 0;





    // Time for different communication steps.

    struct timeval mpiCallRuntimeStart, mpiCallRuntimeEnd;





    int MAX_SIZE = 30;

    int MAX_TRIAL = 100;

    int userInput;


    int randomseed[p];

    srand(time(NULL));

    gettimeofday(&totalRuntimeStart, NULL);
	int i;
    if(rank == 0)
    {
    	for (i = 0; i < p; i++)
    	            randomseed[i] = rand();
    }
    int received_random;
    int scatterTime;
    gettimeofday(&t3, NULL);
    MPI_Scatter(randomseed, 1, MPI_INT, &received_random, 1,MPI_INT,0, MPI_COMM_WORLD);
    gettimeofday(&t4, NULL);
   
    scatterTime =  (t4.tv_sec-t3.tv_sec)*1000 + (t4.tv_usec-t3.tv_usec)/1000; 
    //printf("%d\t%d", rank, scatterTime);


    int start_index, end_index;

	start_index = rank * (N / p);

	end_index = (rank + 1) * (N / p) - 1;

	int cols_size = (end_index - start_index) +1;

	int effective_cols_size = cols_size + 2;

	int matrix[N][effective_cols_size];


    struct timeval barrier1s,barrier1e;
    int tBarrier = 0;
    int tBarrierAvg = 0;
     //initialize matrix part

    int currentGatherTime = 0;

     GenerateInitialGoL(rank, p, start_index, N, effective_cols_size,matrix,received_random);
     for(i=0;i<G;i++)

            {
             	gettimeofday(&avgPerGenerationStart, NULL);

                gettimeofday(&barrier1s, NULL);
                MPI_Barrier(MPI_COMM_WORLD);
                gettimeofday(&barrier1e, NULL);
                tBarrier += (barrier1e.tv_sec-barrier1s.tv_sec)*1000 + (barrier1e.tv_usec-barrier1s.tv_usec)/1000;
                Simulate(i,rank,N,effective_cols_size,matrix,p);

                if(i%X==0)

                {
                    gettimeofday(&displayRuntimeStart, NULL);

                    currentGatherTime += DisplayGoL(N, effective_cols_size, matrix,rank);

                    gettimeofday(&displayRuntimeEnd, NULL);

                    displayTempTime += ((displayRuntimeEnd.tv_sec-displayRuntimeStart.tv_sec)*1000 + ((displayRuntimeEnd.tv_usec - displayRuntimeStart.tv_usec)/1000));

                    displayMethodCounter++;
                }

                gettimeofday(&avgPerGenerationEnd, NULL);

                //printf("START: %d\n ", avgPerGenerationStart.tv_sec);
                //printf("END: %d\n ", avgPerGenerationEnd.tv_sec);
                tempTime+= ((avgPerGenerationEnd.tv_sec-avgPerGenerationStart.tv_sec)*1000 + ((avgPerGenerationEnd.tv_usec - avgPerGenerationStart.tv_usec)/1000));





            }

                tBarrierAvg = tBarrier / G; 
                averageTimePerGeneration = tempTime/G;

            // Average the number of times the display method was called.

            averageDisplayTimePerGeneration = displayTempTime / displayMethodCounter;
            currentGatherTime = currentGatherTime / displayMethodCounter;




            // Get the total runtime

            gettimeofday(&totalRuntimeEnd, NULL);

            totalRuntime = (totalRuntimeEnd.tv_sec - totalRuntimeStart.tv_sec)*1000 + (totalRuntimeEnd.tv_usec - totalRuntimeStart.tv_usec)/1000;

    //printf("%d\t%d\n",rank, totalRuntime);
    //printf("%d\t%d\n",rank, tBarrierAvg);
    //printf("RANK: %d     AVERAGE RUNTIME PER GENERATION: %d\n", rank, averageTimePerGeneration);

   // printf("RANK: %d     AVERAGE TIME PER DISPLAY METHOD: %d\n\n\n", rank, averageDisplayTimePerGeneration);
    printf("%d\t%d\n", rank, currentGatherTime);


    MPI_Finalize();

    //This will output the array.



}