如何在java中将对象读/写到mpi文件?

如何在java中将对象读/写到mpi文件?,java,file,serialization,mpi,Java,File,Serialization,Mpi,我试图使用中的java绑定将某些计算的结果直接写入文件。我知道这听起来不是很“高性能”,但在我看来,当我无法完成计算时,这似乎是不必重新开始的最简单(也是最可靠)的方法 我要写入文件的代码如下所示: private Iterator<Object> writeAndCalculate(Iterator<Object> it) { int mode = MPI.MODE_WRONLY | MPI.MODE_CREATE | MPI.MODE_APPEND;

我试图使用中的java绑定将某些计算的结果直接写入文件。我知道这听起来不是很“高性能”,但在我看来,当我无法完成计算时,这似乎是不必重新开始的最简单(也是最可靠)的方法

我要写入文件的代码如下所示:

private Iterator<Object> writeAndCalculate(Iterator<Object> it) {
    int mode = MPI.MODE_WRONLY | MPI.MODE_CREATE | MPI.MODE_APPEND;
    try(mpi.File file = new mpi.File(MPI.COMM_WORLD, getFileName(), mode)) {
        Code code;
        long result;
        int counter = 0;
        while(it.hasNext()) {
            Object o = it.next();
            if(counter++ == myRank) {
                //computations...
                byte[] buf = serialize(new Pair<Code, Long>(code, result);
                file.writeShared(buf, buf.length, MPI.BYTE);
            }
            if(counter == nrOfProcessors) 
                counter = 0;
        }
        it.remove();
    } catch (MPIException mpie) {
        mpie.printStackTrace();
    }

    return it;
}
我还得到了一些反序列化代码:

private Object deserialize(byte[] buf) throws ClassCastException {
    ByteArrayInputStream bis = new ByteArrayInputStream(buf);

    try(ObjectInputStream ois = new ObjectInputStream(bis)) {
        return ois.readObject();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (ClassNotFoundException cnfe) {
        cnfe.printStackTrace();
    } 

    return null;
}
当我想再次读取对象时,问题就出现了,因为我找不到如何反序列化文件中的所有对象。我试图找到类似的东西:

private SortedMap<Code, Long> read() {
    SortedMap<Code, Long> result = new TreeMap<>();
    try(mpi.File file = new mpi.File(MPI.COMM_WORLD, getFileName(), MPI.MODE_RDONLY)) {
        byte[] buf = new byte[(int) file.getSize()];
        file.readAll(buf, buf.length, MPI.BYTE);
        while(/*part of buf not read*/) {
            Pair<Code, Long> pair = deserialize(buf);
            result.merge(pair.getKey(), pair.getValue(), (v1, v2) -> v1 + v2);
        }
    } catch (MPIException e) {
        e.printStackTrace();
    }

    return result;
}
private SortedMap read(){
SortedMap结果=新树映射();
try(mpi.File File=new mpi.File(mpi.COMM\u WORLD,getFileName(),mpi.MODE\RDONLY)){
byte[]buf=新字节[(int)file.getSize()];
readAll(buf,buf.length,MPI.BYTE);
而(/*部分buf未读取*/){
Pair Pair=反序列化(buf);
结果.合并(pair.getKey(),pair.getValue(),(v1,v2)->v1+v2);
}
}捕获(MPIE异常){
e、 printStackTrace();
}
返回结果;
}
但我不知道如何找出我的缓冲区的哪一部分已经被读取,哪一部分仍然包含对象。我想让
反序列化(byte[])
覆盖已读取的值,但我也不知道如何做类似的事情

因为我已经花了好几个小时在这上面了,我变得非常绝望,我将感谢任何能让我看到光明的帮助。如果有人能提出更好的方法,同时从多个处理器将所有内容写入一个文件,请留下建议

我很抱歉问了这么长的问题,但我需要的只是某种方式来存储并发计算结果的
SortedMap
。这应该是这样做的,当计算被取消或中断时,我可以从计算停止的点开始

private SortedMap<Code, Long> read() {
    SortedMap<Code, Long> result = new TreeMap<>();
    try(mpi.File file = new mpi.File(MPI.COMM_WORLD, getFileName(), MPI.MODE_RDONLY)) {
        byte[] buf = new byte[(int) file.getSize()];
        file.readAll(buf, buf.length, MPI.BYTE);
        while(/*part of buf not read*/) {
            Pair<Code, Long> pair = deserialize(buf);
            result.merge(pair.getKey(), pair.getValue(), (v1, v2) -> v1 + v2);
        }
    } catch (MPIException e) {
        e.printStackTrace();
    }

    return result;
}