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