2013-04-13 41 views
0

我收到此運行時錯誤:運行時錯誤的Java/MPI(被零除)

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 

我認爲是什麼原因造成的,代碼試圖通過0

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

分這將導致錯誤,我怎麼能解決這個錯誤,它仍然0分,但返回0,而不是

,如果你需要的代碼只是說:)

感謝您的幫助

編輯

CODE:使用

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

,並調整所有計算毫微

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); 
      } 
     } 
+0

我們需要的代碼 –

+0

以上代碼 – user2065929

+0

加入我說你有兩個選擇:1)鑑於持續時間可以是零,檢查在你用它來分割之前。 2)使用'system.nanoTime()'而不是'currentTimeMillis()'並相應地調整代碼。 –

回答

1

嘗試。

如果都不行,只是檢查零這裏

speedKbps = duration == 0 ? <big number here> : (len/duration); 
+0

謝謝,我需要多少次才能讓納米時間在幾秒鐘內顯示結果給用戶? – user2065929

+0

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