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