001/*
002 * $Id$
003 */
004
005package edu.jas.kern;
006
007
008import java.io.IOException;
009
010import junit.framework.Test;
011import junit.framework.TestCase;
012import junit.framework.TestSuite;
013
014import mpi.MPI;
015import mpi.MPIException;
016import mpi.Status;
017
018
019/**
020 * MPIEngine tests with JUnit.
021 * @author Heinz Kredel
022 */
023
024public class MPIEngineTest extends TestCase {
025
026
027    /**
028     * main
029     */
030    public static void main(String[] args) {
031        cmdline = args;
032        junit.textui.TestRunner.run(suite());
033        MPIEngine.terminate();
034    }
035
036
037    static String[] cmdline;
038
039
040    static mpi.Comm engine;
041
042
043    /**
044     * Constructs a <CODE>MPIEngineTest</CODE> object.
045     * @param name String.
046     */
047    public MPIEngineTest(String name) {
048        super(name);
049    }
050
051
052    /**
053     * suite.
054     */
055    public static Test suite() {
056        TestSuite suite = new TestSuite(MPIEngineTest.class);
057        return suite;
058    }
059
060
061    @Override
062    protected void setUp() {
063        if (engine == null) {
064            try {
065                engine = MPIEngine.getCommunicator(cmdline);
066            } catch (IOException e) {
067                e.printStackTrace();
068            } catch (MPIException e) {
069                e.printStackTrace();
070            }
071        }
072    }
073
074
075    @Override
076    protected void tearDown() {
077        if (engine == null) {
078            return;
079        }
080        engine = null;
081    }
082
083
084    /**
085     * Test MPIEngine.
086     */
087    public void testMPIEngine() throws MPIException {
088        int me = engine.Rank();
089        int size = engine.Size();
090        assertTrue("size > 0", size > 0);
091        assertTrue("0 <= me < size", 0 <= me && me < size);
092        //System.out.println("testMPIEngine(): Hello World from " + me + " of " + size);
093    }
094
095
096    /**
097     * Test communication.
098     */
099    public void testCommunication() throws MPIException {
100        int me = engine.Rank();
101        int size = engine.Size();
102        int tag = 13;
103        int[] data = new int[5];
104        if (me == 0) {
105            //System.out.println("testCommunication(): from " + me + " of " + size);
106            for (int i = 1; i < size; i++) {
107                data[0] = i;
108                engine.Send(data, 0, data.length, MPI.INT, i, tag);
109            }
110        } else {
111            Status stat = engine.Recv(data, 0, data.length, MPI.INT, 0, tag);
112            int cnt = stat.Get_count(MPI.INT);
113            int elem = stat.Get_elements(MPI.INT);
114            //System.out.println("testCommunication(): status " + me + ", " + cnt + ", " + elem);
115            //System.out.println("testCommunication(): received " + Arrays.toString(data));
116            assertTrue("length == count", data.length == cnt);
117            assertTrue("recv == me", data[0] == me);
118            assertTrue("elem >= 0: " + elem, elem >= 0);
119        }
120        //System.out.println("testCommunication(): done");
121    }
122
123}