I chose Python because of its simplicity and the size of coverage area
there are a lot more docs about Python&MPI friendship then about Java&MPI (there are no tutorials/manuals about mpijava at all)
MPIJava seems to me complicated and confusing. And PyMPI is great, simple and fast-to-implement.
So, the result is: 70 lines of code on Python (create 5 matrixes, split 2 of them on equal parts, spread them through all workers, gather computation results) and one evening of fun coding
because I’m a newbie in Python and it is very impressive and it admires me
example:
if mpi.rank == 0 :
MB = MT = MC = MD = ME = create_matrix(n)
mpi.bcast((MT, MD, ME))
print mpi.rank, "MT, MD, ME broadcasted"
else :
print mpi.rank, "Waiting for messages"
MB = MC = []
MT, MD, ME = mpi.bcast()
localMB = mpi.scatter(MB)
localMC = mpi.scatter(MC)
print mpi.rank,"MB, MC scattered"
MAr = sub_matrix(mult_matrix(localMB, MT), mult_matrix(mult_matrix(localMC, MD), ME))
MA = mpi.gather(MAr)
if mpi.rank == 0 :
#print "String received", masterString
print mpi.rank,"Results gathered"
print print_matrix(MA)
else:
print mpi.rank,"Worker finished his work"
How to create a simple client-server application using RMI interface and run it on linux?
- We need to create an interface that extends java.rmi.Remote interface:
public interface IFacade extends Remote {
String sayHello() throws RemoteException; // its important exception - its required for object exporting
}
- Then we are implementing this interface. In
main method we need to get|create a registry object, export our object marked as Remote, and bind it to some name (and actually start the server). Here we can specify the port number for binding.
public class Facade implements IFacade {
public String sayHello() {
return "hello";
}
public static void main(String[] args) {
int port = 7001;
String name = "Facade";
try {
IFacade facade = new Facade();
Registry registry = LocateRegistry.createRegistry(port);
IFacade stub = (IFacade) UnicastRemoteObject.exportObject(facade, port);
registry.bind(name, stub);
System.out.println("Server is up and running");
BufferedReader rdr = new BufferedReader(
new InputStreamReader(System.in));
while (true) {
System.out.println("Type EXIT to shutdown the server.");
if ("EXIT".equals(rdr.readLine())) {
break;
}
}
registry.unbind(name);
UnicastRemoteObject.unexportObject(facade, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
- We need to start rmiregistry service:
traut@traut-laptop:~$ rmiregistry
Bad port number - using default
default port number is OK
- About client:
public class Client extends Thread {
@Override
public void run() {
super.run();
System.out.println("Client started");
try {
Registry registry = LocateRegistry.getRegistry(host, port);
IFacade facade = (IFacade) registry.lookup("Facade");
System.out.println(facade.sayHello());
} catch (Exception e) {
e.printStackTrace();
return;
}
}
public static void main(String[] args) {
int port = 7001;
String host = "traut-laptop";
new Client(host, port).start();
new Client(host, port).start();
}
}
- Fin
A few Interesting things:
This release adds support for the dynamic generation of stub classes at runtime, obviating the need to use the Java(tm) Remote Method Invocation (Java RMI) stub compiler, rmic, to pregenerate stub classes for remote objects. Note that rmic must still be used to pregenerate stub classes for remote objects that need to support clients running on earlier versions.
Java RMI Release Notes
So, we don’t need to create stub files with rmic program. It must be processed silently undercover from 1.5
I’m not created and used here SecurityManager, policy files, and other boring stuff that documentation advice to use
RMI is still easy-to-understand interface but horrible to implement and to use