大家好,我创建了以下代码:

import mpi.* ;

class Hello {

  static public void main(String[] args) {
    MPI.Init(args);



int myrank = MPI.COMM_WORLD.Rank();
long startTime = System.currentTimeMillis();
if (myrank == 0) {
        String s = "111";
        byte[] bytes = s.getBytes();
        StringBuilder binary = new StringBuilder();
        for (byte b : bytes) {
            int val = b;
            for (int i = 0; i < 8; i++) {
                binary.append((val & 128) == 0 ? 0 : 1);
                val <<= 1;
            }
            binary.append(' ');
        }
        System.out.println("'" + s + "' to binary: " + binary);

//int i = 54564;
//String text = String.format("%15d", i);
//System.out.println(message);  //testing

    char[] mess = binary.toCharArray();
    MPI.COMM_WORLD.Send(mess, 0, mess.length, MPI.CHAR, 1, 99);
} else {
    char[] mess = new char[20];
    MPI.COMM_WORLD.Recv(mess, 0, 200, MPI.CHAR, 0, 99);
    System.out.println("received:" + new String(mess));
}

long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
System.out.println(duration);
    MPI.Finalize();
  }
}


它应该做的是从111输入中以二进制形式创建3个字节,然后将这3个二进制字节发送到网络上的另一台计算机(基本的乒乓程序)

但是我无法编译

我得到这些错误:

Hello.java:28: error: cannot find symbol
    char[] mess = binary.toCharArray();
                        ^
  symbol:   method toCharArray()
  location: variable binary of type StringBuilder
1 error

C:\Users\Richard\Dropbox\Year 3\DISPARP\Coursework>javac Hello.java
Hello.java:30: error: cannot find symbol
    char[] mess = binary.toCharArray();
                        ^
  symbol:   method toCharArray()
  location: variable binary of type StringBuilder
1 error


还有一个问题是

111' to binary: 00110001 00110001 00110001


3个字节?

编辑:

已更改为char[] mess = binary.toString().toCharArray();

但现在得到更多错误:(



MPJ Express (0.35) is started in the cluster configuration
Starting process <0> on <Raptor>
Starting process <1> on <Predator>
'111' to binary: 00110001 00110001 00110001
340
mpi.MPIException: java.lang.ArrayIndexOutOfBoundsException: Out of bounds in des
t array: 26
        at mpi.SimplePackerChar.unpack(SimplePackerChar.java:112)
        at mpi.Comm.recv(Comm.java:1305)
        at mpi.Comm.Recv(Comm.java:1255)
        at Hello.main(Hello.java:34)
        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.ArrayIndexOutOfBoundsException: Out of bounds in dest array
: 26
        at mpjbuf.Buffer.readCheckArgs(Buffer.java:3650)
        at mpjbuf.Buffer.read(Buffer.java:3333)
        at mpi.SimplePackerChar.unpack(SimplePackerChar.java:110)
        ... 9 more
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: mpi.MPIException: mpi.MPIException: mpi.MPIException: java.lang.Array
IndexOutOfBoundsException: Out of bounds in dest array: 26
        at mpi.Comm.Recv(Comm.java:1259)
        at Hello.main(Hello.java:34)
        ... 6 more
Caused by: mpi.MPIException: mpi.MPIException: java.lang.ArrayIndexOutOfBoundsEx
ception: Out of bounds in dest array: 26
        at mpi.Comm.recv(Comm.java:1317)
        at mpi.Comm.Recv(Comm.java:1255)
        ... 7 more
Caused by: mpi.MPIException: java.lang.ArrayIndexOutOfBoundsException: Out of bo
unds in dest array: 26
        at mpi.SimplePackerChar.unpack(SimplePackerChar.java:112)
        at mpi.Comm.recv(Comm.java:1305)
        ... 8 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: Out of bounds in dest array
: 26
        at mpjbuf.Buffer.readCheckArgs(Buffer.java:3650)
        at mpjbuf.Buffer.read(Buffer.java:3333)
        at mpi.SimplePackerChar.unpack(SimplePackerChar.java:110)
        ... 9 more

最佳答案

未为toCharArray定义方法StringBuilder。但是,您可以使用String返回的toString中的方法:

char[] mess = binary.toString().toCharArray();


编辑:我不熟悉MPI,但尝试使char数组缓冲区更大以在Recv期间接受更多数据:

char[] mess = new char[2000];
MPI.COMM_WORLD.Recv(...);

关于java - 无法编译,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15592380/

10-09 08:19