-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransport.java
91 lines (77 loc) · 2.73 KB
/
Transport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.mathpar.NAUKMA.examples;
import java.io.*;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import mpi.MPI;
import mpi.MPIException;
import mpi.Status;
public class Transport {
public static void sendArrayOfObjects(Object[] a, int proc, int tag)
throws MPIException, IOException {
for (int i = 0; i < a.length; i++) {
sendObject(a[i], proc, tag + i);
}
}
public static Object[] recvArrayOfObjects(int proc, int tag)
throws MPIException, IOException, ClassNotFoundException {
Object[] o = new Object[4];
for (int i = 0; i < 4; i++) {
o[i] = recvObject(proc, tag + i);
}
return o;
}
public static void sendObject(Object a, int proc, int tag) throws MPIException, IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(a);
byte[] tmp = bos.toByteArray();
MPI.COMM_WORLD.send(tmp, tmp.length, MPI.BYTE, proc, tag);
}
public static Object recvObject(int proc, int tag)
throws MPIException, IOException, ClassNotFoundException {
Status st = MPI.COMM_WORLD.probe(proc, tag);
int size = st.getCount(MPI.BYTE);
byte[] tmp = new byte[size];
MPI.COMM_WORLD.recv(tmp, size, MPI.BYTE, proc, tag);
Object res = null;
ByteArrayInputStream bis = new ByteArrayInputStream(tmp);
ObjectInputStream ois = new ObjectInputStream(bis);
res = ois.readObject();
return res;
}
public static void sendObjects(Object[] a, int proc, int tag) throws MPIException {
ByteArrayOutputStream bos = null;
try {
bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
for (int i = 0; i < a.length; i++) {
oos.writeObject(a[i]);
}
bos.toByteArray();
} catch (Exception ex) {
Logger.getLogger(Transport.class.getName()).log(Level.SEVERE, null, ex);
}
byte[] temp = bos.toByteArray();
ByteBuffer buf = MPI.newByteBuffer(temp.length);
buf.put(temp);
MPI.COMM_WORLD.iSend(buf, temp.length, MPI.BYTE, proc, tag);
}
public static Object[] recvObjects(int m, int proc, int tag) throws MPIException {
Status s = MPI.COMM_WORLD.probe(proc, tag);
int n = s.getCount(MPI.BYTE);
byte[] arr = new byte[n];
MPI.COMM_WORLD.recv(arr, n, MPI.BYTE, proc, tag);
Object[] res = new Object[m];
try {
ByteArrayInputStream bis = new ByteArrayInputStream(arr);
ObjectInputStream ois = new ObjectInputStream(bis);
for (int i = 0; i < m; i++) {
res[i] = ois.readObject();
}
} catch (Exception ex) {
Logger.getLogger(Transport.class.getName()).log(Level.SEVERE, null, ex);
}
return res;
}
}