Equation Solution
High Performance by Design

This example demonstrates how to manipulate tasks, how to apply parallel lock for critical sections, and how to synchronize tasks by parallel event. Two classes, "mul" and "example", are used for illustartion.

The class "mul" has two parallel methods. The method "byConstant" calculates c[M], and the method "xyz" implements [Y][Z] and finds the maximal coefficient. The class "mul" is as follow:

public class mul
{
/*
; constructor
*/
public mul( ) {}

/*
; method 1 -- [M] = coefficient x [M]
*/
public void byConstant(float[][] matrix,
Integer dimInput,
Float constantInput,
int[] index,
parallelLock lockA)
{
/*
; local variables
*/
int row;
int i4temp;
int dim = dimInput.intValue();
float constant = constantInput.floatValue();

/*
; dynamically assigne a row to a task
*/
while (true)
{
synchronized(lockA)
{
if(index[0] == dim) return;
row = index[0]; // the row to calcualte
index[0] ++;
}

/*
; multiply by "constant"
; this block is executed in parallel
*/
for (i4temp = 0; i4temp < dim; i4temp++)
{
matrix[row][i4temp] = matrix[row][i4temp] *
constant;
}
}
}

/*
; method 2 -- [X] <-- [Y][Z], and then find
; the max. coefficient of [X]
*/
public void xyz(float[][] x,
float[][] y,
float[][] z,
Integer dimInput,
int[] index,
float[] result,
parallelLock lockA,
parallelEvent eventA)
{
/*
; local variables
*/
int dim = dimInput.intValue();
int column;
int i4temp;
int j4temp;

/*
; dynamically assign a column of [X] to a task
*/
loopA: while (true)
{
lockA.lockon( );
if(index[0] == dim)
{
lockA.unlock( );
eventA.post();
break loopA;
}
column = index[0];
index[0] ++;
lockA.unlock( );

/*
; compute a column of [X] in parallel
*/
for (i4temp = 0; i4temp < dim; i4temp ++)
{
x[i4temp][column] = 0.0f;
for(j4temp = 0; j4temp < dim; j4temp++)
{
x[i4temp][column] = x[i4temp][column]+
y[i4temp][j4temp]*
z[j4temp][column];
}
}
}

/*
; sequential segment to find the max.
; coefficient of [X]
; // only one task does the if{}
*/
if(!eventA.ifComplete())
{
result[0] = 0.0F;
for (i4temp = 0; i4temp < dim; i4temp ++)
{
for(j4temp = 0; j4temp < dim; j4temp++)
{
result[0] = Math.max(result[0],
x[i4temp][j4temp]);
}
}
eventA.complete(); // free all other tasks
}
}

}

MAIN PROGRAM

The main program "example" dispatchs methods "byConstant" and "xyz" to run in parallel. The main program is as follow.

public class example
{
/*
; class members
*/
final int cpu = 10;
int[] pid = new int[cpu];

/*
; entry point
*/
public static void main(String args[])
{
new example();
}

/*
; main program
*/
public example()
{
/*
*/
parallelLock lockA = new parallelLock();
parallelEvent eventA = new parallelEvent();

/*
; get method 1
*/
mul mul = new mul();

/*
; method 1 -- multiply by a constant
*/
// generate input data
final int n = 1000; // dimension assumed

// generate test matrix by random number
float[][] matrix = new float[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
matrix[i][j] = (float)Math.random();
}
}
int[] index = {0};
float constant = 10.0F;
Integer N = new Integer(n);
Float Constant = new Float(constant);

System.out.print("Method 1 .... ");

// dispatch for parallel processing
for (int i = 0; i < cpu-1; i++)
{
"byConstant",
matrix,
N,
Constant,
index,
lockA);
}
mul.byConstant(matrix,
N,
Constant,
index,
lockA);

// wait for completion

// free resource
System.out.println("done"
matrix = null;
index = null;
N = null;
Constant = null;

/*
; method 2
*/
System.out.print("Method 2 .... ");

final int dim = 100;
Integer Dim = new Integer(dim);
int[] column = {0};
float[] result = new float[1];

// generate test matrices by random number
float[][] x = new float[dim][dim];
float[][] y = new float[dim][dim];
float[][] z = new float[dim][dim];
for (int i = 0; i < dim; i++
{
for (int j = 0; j < dim; j++)
{
y[i][j] = (float)Math.random();
}
}
for (int i = 0; i < dim; i++)
{
for (int j = 0; j < dim; j++)
{
z[i][j] = (float)Math.random();
}
}

eventA.initialize(cpu);

// dispatch for parallel processing
for (int i = 0; i < cpu-1; i++)
{
"xyz",
x,
y,
z,
Dim,
column,
result,
lockA,
eventA);
}
mul.xyz(x,y,z,Dim,column,result,lockA,eventA);

// wait for completion
for (int i = 0 ; i < cpu-1; i++)
{