user2065929
user2065929

Reputation: 1095

How to loop a piece of code x times and take the average

i am looking to loop the main section of the code below, and then calculates the average of the results collected from the loop

here is the code :

class PingPongVariousLengths {

    static public void main(String[] args) {

        MPI.Init(args);
        int myrank = MPI.COMM_WORLD.Rank();
        int tag = 99;
        int maxlen = 104857600; //200 megabytes     104857600 characters * 2 bytes per character = 209715200 bytes total, or 200 megabytes
        int minlen = 1; // 2 bytes
        char [] sendbuff = new char [maxlen];
        char [] recvbuff = new char [maxlen];
        long speedKbps;
        long speedMbps;


        if (myrank == 0) {
            for (int len = minlen; len <= maxlen; len *= 2) { //len=*2 doubles the ping size each time
                long startTime = System.nanoTime();                  
                 MPI.COMM_WORLD.Send(sendbuff, 0, len, MPI.CHAR, 1, tag);
                 MPI.COMM_WORLD.Recv(recvbuff, 0, len, MPI.CHAR, 1, tag);
                long endTime = System.nanoTime();
                long duration = endTime - startTime;
                long durationseconds = (duration * 10-9); // Converts nanoseconds to seconds
                System.out.println("Time for the ping to be sent and recived of " + (len*2) + " bytes is " + durationseconds + " seconds"); // multiples by 2 becuase 1 character is 2 bytes
                //double transferRate = ((len*2.0) / durationseconds ) ; //amount of data in bytes transferred in 1 second. Currently returning 0 for every result
                //System.out.println("transferRate: " + transferRate + " bytes per second");
                double transferRateMb = ((len*524288.0) / durationseconds ) ; //amount of data in megabytes transferred in 1 second. 
                System.out.println("transferRate (megabytes) : " + transferRateMb + " megabytes per second");
                }
        } else if (myrank == 1) {
            for (int len = minlen; len <= maxlen; len *= 2) {
                 MPI.COMM_WORLD.Recv(recvbuff, 0, len, MPI.CHAR, 0, tag);
                 MPI.COMM_WORLD.Send(recvbuff, 0, len, MPI.CHAR, 0, tag);
            }
        }


        MPI.Finalize();
    }
}

i am trying to loop it say 10 or 20 times,the code from

            for (int len = minlen; len <= maxlen; len *= 2) { //len=*2 doubles the ping size each time

to

                long durationseconds = (duration * 10-9); // Converts nanoseconds to seconds

i want to run 20 times and then the results for duration to be average over the 10 or 20

how can i best and most efficiently go about this

Upvotes: 0

Views: 111

Answers (2)

Stephen Donecker
Stephen Donecker

Reputation: 2391

If I understood you correctly you wanted an average of duration seconds for each length increment. In order to get the average of your duration seconds you need to move the length looping outside of the average looping. See the code snippet below.

long durationseconds;
int MAX_LOOPS = 20;

for (int len = minlen; len <= maxlen; len *= 2) {
        if (myrank == 0) {
                durationseconds = 0;
                for (int i = 0; i < MAX_LOOPS; i++) {
                        long startTime = System.nanoTime();           
                        MPI.COMM_WORLD.Send(sendbuff, 0, len, MPI.CHAR, 1, tag);
                        MPI.COMM_WORLD.Recv(recvbuff, 0, len, MPI.CHAR, 1, tag);
                        long endTime = System.nanoTime();
                        long duration = endTime - startTime;
                        durationseconds = durationseconds + (duration * 10-9);
                }
                durationseconds = durationseconds / MAX_LOOPS;
                System.out.println("Average time for the ping to be sent and recived of " + (len*2) + " bytes is " + durationseconds + " seconds");
                double transferRateMb = ((len*524288.0) / durationseconds );
                System.out.println("average transferRate (megabytes) : " + transferRateMb + " megabytes per second");
        } else if (myrank == 1) {
                MPI.COMM_WORLD.Recv(recvbuff, 0, len, MPI.CHAR, 0, tag);
                MPI.COMM_WORLD.Send(recvbuff, 0, len, MPI.CHAR, 0, tag);
        }
}

Upvotes: 1

0x6C38
0x6C38

Reputation: 7076

long totalDuration = 0;
for (int i = 0; i<nTimes;i++){
... //Code N stuff here
totalDuration += duration;
}
long avgDuration = totalTime /nTimes;

Upvotes: 0

Related Questions