0

i am getting this runtime error :

C:\Users\Richard\Dropbox\Year 3\DISPARP\Coursework>mpjrun -np 2 -dev niodev Ping
PongVariousLengths
MPJ Express (0.35) is started in the cluster configuration
Starting process <0> on <Tornado>
Starting process <1> on <Tornado>
Time for the ping to be sent and recived of 1 kb is 7 miliseconds
transferRate: 142.85714285714286KB per second
Speed of network in Mbps: 0
Time for the ping to be sent and recived of 2 kb is 0 miliseconds
transferRate: InfinityKB per second
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at runtime.daemon.Wrapper.execute(Wrapper.java:165)
        at runtime.daemon.Wrapper.main(Wrapper.java:180)
Caused by: java.lang.ArithmeticException: / by zero
        at PingPongVariousLengths.main(PingPongVariousLengths.java:32)
        ... 6 more

i think what is causing it is, the code is trying to divide by 0

Time for the ping to be sent and recived of 2 kb is 0 miliseconds
transferRate: InfinityKB per second

which causes the error, how can i solve this error, where it still divides by 0 but returns 0 instead

if you need the code just say :)

thanks for the help

EDIT

CODE :

if (myrank == 0) {
            for (int len = minlen; len <= maxlen; len *= 2) { //len=*2 doubles the ping size each time
                long startTime = System.currentTimeMillis();                    
                 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.currentTimeMillis();
                long duration = endTime - startTime;
                System.out.println("Time for the ping to be sent and recived of " + len + " kb is " + duration + " miliseconds");
                double transferRate = (len * 1000.0)/ duration ; //amount of data in kilobytes transferred in 1 second
                System.out.println("transferRate: " + transferRate + "KB per second");
                speedKbps = (len / duration);
                speedMbps = (speedKbps / 1024);
                System.out.println("Speed of network in Mbps: " + speedMbps);
                }
        } 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);
            }
        }
user2065929
  • 1,065
  • 4
  • 21
  • 35
  • I'd say you have two options: 1) given that duration can be zero, check it before you use it to divide. 2) Use 'system.nanoTime()' instead of 'currentTimeMillis()' and adjust the code accordingly. – Ray Stojonic Apr 13 '13 at 21:02

1 Answers1

1

Try using

long startTime = System.nanoTime();
... 
long endTime = System.nanoTime();

and adjust all calculation for nanos.

If it will not work, just check for zero here

speedKbps = duration == 0 ? <big number here> : (len / duration);
Vitaly
  • 2,760
  • 2
  • 19
  • 26
  • Thanks, what do i need to mulitple the nanoTime to get display the result in seconds for the users ? – user2065929 Apr 13 '13 at 21:09
  • millis = 10^-3, nanos = 10^-9 http://stackoverflow.com/questions/924208/java-how-do-you-convert-nanoseconds-to-seconds-using-the-timeunit-class http://stackoverflow.com/questions/3654848/how-to-get-a-meaningful-result-from-subtracting-2-nanotime-objects – Vitaly Apr 13 '13 at 21:13