java - 运行时错误 java/mpi(除以零)

标签 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

如果您需要代码,只需说:)

感谢您的帮助

编辑

代码:

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

最佳答案

尝试使用

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

并调整所有纳秒计算。

如果不起作用,只需在此处检查零

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

关于java - 运行时错误 java/mpi(除以零),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15992830/

相关文章:

java - 从Consumer端创建主题

java - Android 应用程序未在 KitKat 中运行

c++ - 通过 MPI 发送和接收二维数组

c - 程序不会从用户那里获取输入来再次循环。

python - Scikit-learn:并行化随机梯度下降

java - SimGrid 中的 send 和 dsend 有什么区别吗?

java - 我们如何为 TextView 中的所有单词设置可点击?

java - Java中的HashMap是如何使用equals()和hashCode()来查找对象的?

algorithm - 并行矩阵乘积

c - 使用 MPI_Pack 和 Unpack 以及 MPI_send 和 Recieve 时收到的垃圾值