/**
* A component of a library for
* MoSeS
* Copyright (C) 2006
* Andy Turner,
* University of Leeds.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
package uk.ac.leeds.ccg.andyt.tests.mpi;
import mpi.MPIException;
import mpi.*;
//import uk.ac.leeds.ccg.andyt.projects.moses.utilities.Utilities;
import java.io.File;
class MPITest {
private long time0;
private int size;
private int rank;
protected long getTime0() {
return this.time0;
}
protected void setTime0( long time0 ) {
this.time0 = time0;
}
protected int getSize() {
return this.size;
}
protected void setSize( int size ) {
this.size = size;
}
protected int getRank() {
return this.rank;
}
protected void setRank( int rank ) {
this.rank = rank;
}
public static void main(String[] args) throws MPIException {
MPITest test = new MPITest();
test.run( args );
}
public void run( String[] args ) {
try {
MPI.Init( args );
} catch ( MPIException e ) {
System.out.println( "Exception with MPI.Init( " + args + " )" );
e.printStackTrace();
}
setTime0( System.currentTimeMillis() );
try {
setSize( MPI.COMM_WORLD.Size() );
setRank( MPI.COMM_WORLD.Rank() );
} catch ( MPIException e ) {
System.out.println( "Exception with MPI.COMM_WORLD.Size() or MPI.COMM_WORLD.Rank" );
e.printStackTrace();
}
//runTimes0();
//runTimes1();
//runRankInfo0();
runRankInfo1();
//runTavi();
//System.out.println( "Process complete in " + Utilities.time( System.currentTimeMillis() - test.getTime0() ) );
try {
MPI.Finalize();
} catch ( MPIException e ) {
System.out.println( "Exception with MPI.Finalize()" );
e.printStackTrace();
}
}
/**
* This simple method prints out the time on the masters and slaves by the
* passing of a char[] message.
*/
public void runTimes0() {
int size = getSize();
int rank = getRank();
long[] times = new long[ size ];
char[] message;
int source; // Rank of sender
int tag = 50; // Message tag
if ( rank == 0 ) {
times[ 0 ] = System.currentTimeMillis();
System.out.println( "System.currentTimeMilis() on " + rank + " is " + times[ rank ] );
for ( source = 1 ; source < size; source ++ ) {
try{
MPI.COMM_WORLD.Send( times, 0, times.length, MPI.LONG, source, tag ) ;
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Send to " + source );
e.printStackTrace();
}
}
message = new char[ 1000 ];
for ( source = 1 ; source < size; source ++ ) {
try {
MPI.COMM_WORLD.Recv( message, 0, 1000, MPI.CHAR, MPI.ANY_SOURCE, tag ) ;
//MPI.COMM_WORLD.Recv( message, 0, message.length, MPI.CHAR, MPI.ANY_SOURCE, tag ) ;
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Recv from " + source );
e.printStackTrace();
}
System.out.println( "Message: " + new String( message ) + " recieved in " + Utilities.time( System.currentTimeMillis() - times[ 0 ] ) ) ;
}
} else {
try{
MPI.COMM_WORLD.Recv( times, 0, times.length, MPI.LONG, 0, tag );
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Recv from 0" );
e.printStackTrace();
}
times[ rank ] = System.currentTimeMillis();
message = ( "System.currentTimeMilis() on " + rank + " is " + times[ rank ] ).toCharArray() ;
try {
MPI.COMM_WORLD.Send( message, 0, message.length, MPI.CHAR, 0, tag ) ;
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Send to 0" );
e.printStackTrace();
}
}
}
/**
* This method is better than runTimes0(). It uses a single array for
* storing times. It demonstrates how to send parts of an array via the
* offset arguement on the MPI.COMM_WORLD.Send and MPI.COMM_WORLD.Recv
* method calls.
*/
public void runTimes1() {
int size = getSize();
int rank = getRank();
int source; // Rank of sender
int tag = 50; // Message tag
long[] times = new long[ size ];
Object buf;
int offset;
int count = 1;
Datatype datatype = MPI.LONG;
if ( rank == 0 ) {
times[ 0 ] = System.currentTimeMillis();
System.out.println( "System.currentTimeMilis() on " + rank + " is " + times[ rank ] );
for ( source = 1 ; source < size; source ++ ) {
offset = source;
try{
MPI.COMM_WORLD.Send( times, offset, count, datatype, source, tag ) ;
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Send to " + source );
e.printStackTrace();
}
}
for ( source = 1 ; source < size; source ++ ) {
offset = source;
try {
MPI.COMM_WORLD.Recv( times, offset, count, datatype, MPI.ANY_SOURCE, tag ) ;
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Recv from " + source );
e.printStackTrace();
}
System.out.println( "System.currentTimeMilis() on " + source + " is " + times[ source ] ) ;
}
System.out.println( "Process complete in " + Utilities.time( System.currentTimeMillis() - getTime0() ) );
} else {
offset = rank;
try{
MPI.COMM_WORLD.Recv( times, offset, count, datatype, 0, tag );
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Recv from 0" );
e.printStackTrace();
}
times[ offset ] = System.currentTimeMillis();
try {
MPI.COMM_WORLD.Send( times, offset, count, datatype, 0, tag ) ;
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Send to 0" );
e.printStackTrace();
}
}
}
/**
* @TODO docs
*/
public void runRankInfo0() {
int source; // Rank of sender
int tag = 50; // Message tag
int size = getSize();
int rank = getRank();
long[] times = new long[ size ];
int processorNameLength = 20;
int tmpdirLength = 20;
char[] processorNamesChar = new char[ processorNameLength * size ];
char[] tmpdirsChar = new char[ tmpdirLength * size ];
if ( rank == 0 ) {
String[] processorNames = new String[ size ];
String[] tmpdirs = new String[ size ];
// Set information for master
times[ rank ] = System.currentTimeMillis();
tmpdirs[ rank ] = System.getProperty( "java.io.tmpdir" );
try{
processorNames[ rank ] = MPI.Get_processor_name();
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.Get_processor_name()" );
e.printStackTrace();
}
// Send requests for information from slaves
// time
for ( source = 1 ; source < size; source ++ ) {
try{
MPI.COMM_WORLD.Send( times, source, 1, MPI.LONG, source, tag ) ;
MPI.COMM_WORLD.Send( tmpdirsChar, tmpdirLength * source, tmpdirLength, MPI.CHAR, source, tag ) ;
MPI.COMM_WORLD.Send( processorNamesChar, processorNameLength * source, processorNameLength, MPI.CHAR, source, tag ) ;
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Send to " + source );
e.printStackTrace();
}
}
// Receive information from slaves
for ( source = 1 ; source < size; source ++ ) {
try {
MPI.COMM_WORLD.Recv( times, source, 1, MPI.LONG, MPI.ANY_SOURCE, tag );
MPI.COMM_WORLD.Recv( tmpdirsChar, tmpdirLength * source, tmpdirLength, MPI.CHAR, MPI.ANY_SOURCE, tag );
MPI.COMM_WORLD.Recv( processorNamesChar, processorNameLength * source, processorNameLength, MPI.CHAR, MPI.ANY_SOURCE, tag );
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Recv from " + source );
e.printStackTrace();
}
tmpdirs[ source ] = new String( tmpdirsChar, tmpdirLength * source, tmpdirLength );
processorNames[ source ] = new String( processorNamesChar, processorNameLength * source, processorNameLength );
}
// Print out information
printRankInfo0( processorNames, times, tmpdirs );
System.out.println( "Process complete in " + Utilities.time( System.currentTimeMillis() - getTime0() ) );
} else {
// Recieve and handle requests for information
// Time
try{
MPI.COMM_WORLD.Recv( times, rank, 1, MPI.LONG, 0, tag );
MPI.COMM_WORLD.Recv( tmpdirsChar, tmpdirLength * rank, tmpdirLength, MPI.CHAR, 0, tag );
MPI.COMM_WORLD.Recv( processorNamesChar, processorNameLength * rank, processorNameLength, MPI.CHAR, 0, tag );
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Recv from 0" );
e.printStackTrace();
}
times[ rank ] = System.currentTimeMillis();
tmpdirsChar = System.getProperty( "java.io.tmpdir" ).toCharArray();
try{
processorNamesChar = MPI.Get_processor_name().toCharArray();
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.Get_processor_name()" );
e.printStackTrace();
}
// Send information to master
try {
MPI.COMM_WORLD.Send( times, rank, 1, MPI.LONG, 0, tag );
MPI.COMM_WORLD.Send( tmpdirsChar, tmpdirLength * rank, tmpdirLength, MPI.CHAR, 0, tag ) ;
MPI.COMM_WORLD.Send( processorNamesChar, processorNameLength * rank, processorNameLength, MPI.CHAR, 0, tag ) ;
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Send to 0" );
e.printStackTrace();
}
}
}
private void printRankInfo0( String[] processorNames, long[] times, String[] tmpdirs ) {
for ( int rank = 0; rank < getSize(); rank ++ ) {
System.out.println( "Process rank " + rank + " running on node " + processorNames[ rank ] );
System.out.println( "System.currentTimeMilis() " + times[ rank ] );
System.out.println( "System.getProperty( \"java.io.tmpdir\" ) " + tmpdirs[ rank ] );
}
}
/**
* @TODO docs
*/
public void runRankInfo1() {
int source; // Rank of sender
int tag = 50; // Message tag
int size = getSize();
int rank = getRank();
int infoLevel = 3;
Object[] nodeInfo = new Object[ size ];
for ( int nodeInfoIndex = 0; nodeInfoIndex < size; nodeInfoIndex ++ ) {
nodeInfo[ nodeInfoIndex ] = new Object[ infoLevel ];
}
if ( rank == 0 ) {
String[] processorNames = new String[ size ];
String[] tmpdirs = new String[ size ];
long[] times = new long[ size ];
// Set information for master
try{
processorNames[ rank ] = MPI.Get_processor_name();
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.Get_processor_name()" );
e.printStackTrace();
}
tmpdirs[ rank ] = System.getProperty( "java.io.tmpdir" );
times[ rank ] = System.currentTimeMillis();
// Send requests for information to slaves
for ( source = 1 ; source < size; source ++ ) {
try{
MPI.COMM_WORLD.Send( nodeInfo, source, 1, MPI.OBJECT, source, tag ) ;
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Send to " + source );
e.printStackTrace();
}
}
// Receive information from slaves
for ( source = 1 ; source < size; source ++ ) {
try {
MPI.COMM_WORLD.Recv( nodeInfo, source, 1, MPI.OBJECT, MPI.ANY_SOURCE, tag );
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Recv from " + source );
e.printStackTrace();
}
processorNames[ source ] = ( String ) ( ( Object[] ) nodeInfo[ source ] )[ 0 ];
tmpdirs[ source ] = ( String ) ( ( Object[] ) nodeInfo[ source ] )[ 1 ];
times[ source ] = ( ( Long ) ( ( Object[] ) nodeInfo[ source ] ) [ 2 ] ).longValue();
}
// Print out info
printRankInfo0( processorNames, times, tmpdirs );
System.out.println( "Process complete in " + Utilities.time( System.currentTimeMillis() - getTime0() ) );
} else {
// Recieve and handle requests for information
try{
MPI.COMM_WORLD.Recv( nodeInfo, rank, 1, MPI.OBJECT, 0, tag );
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Recv from 0" );
e.printStackTrace();
}
try {
( ( Object [] ) nodeInfo[ rank ] ) [ 0 ] = new String( MPI.Get_processor_name() );
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.Get_processor_name()" );
e.printStackTrace();
}
( ( Object [] ) nodeInfo[ rank ] ) [ 1 ] = new String( System.getProperty( "java.io.tmpdir" ) );
( ( Object [] ) nodeInfo[ rank ] ) [ 2 ] = new Long( System.currentTimeMillis() );
// Send information to master
try {
MPI.COMM_WORLD.Send( nodeInfo, rank, 1, MPI.OBJECT, 0, tag );
} catch ( MPIException e ) {
System.out.println( "Exception on " + rank + " with MPI.COMM_WORLD.Send to 0" );
e.printStackTrace();
}
}
return;
}
// public void runTavi() {
// int dest; // Rank of destination
// int source; // Rank of sender
// int tag = 50; // Message tag
// try {
// tag = 50;
// rank = MPI.COMM_WORLD.Rank() ;
// size = MPI.COMM_WORLD.Size() ;
// if ( rank != 0 ) {
// runTavi( rank );
// dest = 0;
// char [] message = ( "Processing " + rank + " complete in " + Utilities.time( System.currentTimeMillis() - getTime0() ) ).toCharArray() ;
// MPI.COMM_WORLD.Send( message, 0, message.length, MPI.CHAR, dest, tag) ;
// } else {
// for ( source = 1; source < size; source ++ ) {
// char [] message = new char [40] ;
// MPI.COMM_WORLD.Recv( message, 0, 40, MPI.CHAR, MPI.ANY_SOURCE, tag) ;
// System.out.println( "Message : " + new String( message ) + " : " ) ;
//
// }
// }
// } catch ( MPIException e ) {
// e.printStackTrace();
// }
// }
//
// public void runTavi( int rank ) {
// //Initialisation
// Grid2DSquareCellDoubleJAIFactory jf = new Grid2DSquareCellDoubleJAIFactory();
// Grid2DSquareCellDoubleFactory f = new Grid2DSquareCellDoubleFactory();
// Grid2DSquareCellDoubleFileFactory ff = new Grid2DSquareCellDoubleFileFactory();
// AbstractGrid2DSquareCellDoubleFactory gridFactory;
// String demDataDirectory = "/home/andyt/data/tavi/";
// String maskDataDirectory = demDataDirectory;
// String outDataDirectory = demDataDirectory + "metrics1/";
// ff.setDataDirectory( outDataDirectory );
// //String filename = "dem100";
// String filename = "demclip";
// AbstractGrid2DSquareCellDouble grid = null;
// for ( int nodata = 0; nodata < 2; nodata ++ ) {
// if ( nodata == 0 ) {
// gridFactory = f;
// grid = gridFactory.createGrid2DSquareCellDouble( new File( demDataDirectory, filename + ".asc" ) );
// System.out.println( grid.toString() );
// System.out.println("Masking");
// Grid2DSquareCellDoubleProcessor.mask( grid, -0.0d, 0.0d );
// grid.setName( grid.getName() + "_0Masked" );
// } else {
// gridFactory = jf;
// grid = gridFactory.createGrid2DSquareCellDouble( new File( demDataDirectory, filename + ".asc" ) );
// }
// System.out.println( grid.toString() );
//
// // Kernel parameters
// double cellsize = grid.getCellsize();
// double weightIntersect = 1.0d;
// double weightFactor = 1.0d;
//
// int gridNcols = grid.getNcols();
// int gridNrows = grid.getNrows();
// double noDataValue = grid.getNoDataValue();
// double xllcorner = grid.getXllcorner();
// double yllcorner = grid.getYllcorner();
// gridFactory = ff;
// // Initialise metrics1
// AbstractGrid2DSquareCellDouble[] metrics1 = new AbstractGrid2DSquareCellDouble[65];
// for ( int i = 0; i < metrics1.length; i ++ ) {
// metrics1[ i ] = gridFactory.createGrid2DSquareCellDouble( gridNrows, gridNcols, xllcorner, yllcorner, cellsize, noDataValue, 1 );
// System.out.println("Rank " + rank + " Initialised result[" + i + "]" );
// }
//
// AbstractGrid2DSquareCellDouble fgrid = null;
// int distances = ( int ) Math.ceil( Math.pow( 2.0d, rank ) );
// double distance = cellsize * distances;
// metrics1 = Grid2DSquareCellDoubleProcessorDEM.getMetrics1( metrics1, grid, distance, weightIntersect, weightFactor );
// for ( int i = 0; i < metrics1.length; i ++ ) {
// // mask left
// Grid2DSquareCellDoubleProcessor.mask( metrics1[ i ], 0, 0, gridNrows - 1, distances - 1 );
// // mask right
// Grid2DSquareCellDoubleProcessor.mask( metrics1[ i ], 0, gridNcols - distances, gridNrows - 1, gridNcols - 1 );
// // mask top
// Grid2DSquareCellDoubleProcessor.mask( metrics1[ i ], 0, 0, distances - 1, gridNcols - 1 );
// // mask bottom
// Grid2DSquareCellDoubleProcessor.mask( metrics1[ i ], gridNrows - distances, 0, gridNrows - 1, gridNcols - 1 );
// //metrics1[ i ].setNoDataValue( Math.floor( metrics1[ i ].getGridStatistics().getMin() ) - 1 );
// //Grid2DSquareCellDoubleExchange.toAsciiFile( fgrid, new File( outDataDirectory, filename + "_" + fM[ i ].getName() + "_" + distances + ".asc" ) );
// fgrid = f.createGrid2DSquareCellDouble( metrics1[ i ] );
// Grid2DSquareCellDoubleExchange.toImage( fgrid, new File( outDataDirectory, filename + "_" + metrics1[ i ].getName() + "_" + distances + ".png" ), "PNG" );
// //Grid2DSquareCellDoubleProcessor.mask( fgrid, mask );
// //Grid2DSquareCellDoubleExchange.toAsciiFile( fgrid, new File( outDataDirectory, filename + "_" + fM[ i ].getName() + "_" + distances + "_masked.asc" ) );
// //Grid2DSquareCellDoubleExchange.toImage( fgrid, new File( outDataDirectory, filename + "_" + metrics1[ i ].getName() + "_" + distances + "_masked.png" ), "PNG" );
// //metrics1[ i ].clear();
// }
// }
// }
}