Parameter Passing and Parallelization - notes on the program

In program RuntimeDemo we consider the variant ways, parameters can be passed to method layers. Furthermore, we test the looping implementation of the considered method layers against their parallel implementation.


Unit Class DemoUnit - Using JLayer Annotations

The DemoUnit class provides layer methods with three different signatures. One method has no parameter, the second has an unannotated parameter, and the third has a parameter with annotation @LayerParam. The methods with parameter return a value.

In order to test the looping implementation against the parallel implementation, each method type occurs twice, once marked with annotation @LayerMethod and the other time with annotation @LayerMethod(runtimeMode=RuntimeMode.FORKJOIN).


  @LayerUnit
  public class DemoUnit {
  
    long duration = 100;
	
    @LayerMethod
    public void loopMethod1(){
      long startTime = System.currentTimeMillis();
      while ( (System.currentTimeMillis() - startTime) < duration) { }
    }
	
    @LayerMethod
    public int loopMethod2(int par){
      long startTime = System.currentTimeMillis();
      while ( (System.currentTimeMillis() - startTime) < duration) { }
      return par;
    }
	
    @LayerMethod
    public int int loopMethod3(@LayerParam int par){
      long startTime = System.currentTimeMillis();
      while ( (System.currentTimeMillis() - startTime) < duration) { }
      return par;
    }
	
    @LayerMethod(runtimeMode=RuntimeMode.FORKJOIN)
    public void forkjoinMethod1(){
      long startTime = System.currentTimeMillis();
      while ( (System.currentTimeMillis() - startTime) < duration) { }
    }
	
    @LayerMethod(runtimeMode=RuntimeMode.FORKJOIN)
    public int forkjoinMethod2(int par){
      long startTime = System.currentTimeMillis();
      while ( (System.currentTimeMillis() - startTime) < duration) { }
      return par;
    }
	
    @LayerMethod(runtimeMode=RuntimeMode.FORKJOIN)
    public int int forkjoinMethod3(@LayerParam int par){
      long startTime = System.currentTimeMillis();
      while ( (System.currentTimeMillis() - startTime) < duration) { }
      return par;
    }

  }
  

All methods consist only of a loop in which they stay for a certain time. They only differ in the signature and, if necessary, the return of a value.


Establishing Global Structure and Dynamics - Using the Generated Wrapper Class

The DemoLayers class essentially shows how to control the method layers with the various parameter profiles. The parts of the code that depend on the JLayer framework are color coded.

At the beginning you find the declaration of some arrays and wrapper objects. The wrapper demoLayer is used to wrap the underlying array of DemoUnits, similarly the paramLayer is used to wrap an array of Integers to be used as parameters for the method layers with annotated parameters, and the resultLayer object is used to pick up the results from the return-value method layers.


  public class DemoLayers {
  
    // the underlying demo layer
    DemoUnit[]        demoArray;
    Layer_DemoUnit_ demoLayer; 
    
    // parameter layer
    Integer[]    paramArray;
    BasedLayer paramLayer; 

    // result layer
    BasedLayer resultLayer; 

    // the size of all layers (and arrays)
    int size = 50;
	
    public void createLayers() {
      // demoLayer wraps demoArray
      demoArray = new DemoUnit[size];
      for(int i = 0; i < size; i++) {
        demoArray[i] = new DemoUnit();
      }
      demoLayer = new Layer_DemoUnit_(demoArray); 

      // paramLayer wraps paramArray
      paramArray = new Integer[size];
      for(int i = 0; i < size; i++) {
        paramArray[i] = i;
      }
      paramLayer = new LayerBase(paramArray); 
    }
	
    public void runLoopMethods() {
      // simple layer method
      demoLayer.loopMethod1(); 

      // layer method with parameter
      resultLayer = demoLayer.loopMethod2(0); 	

      // layer method with layer parameter
      resultLayer = demoLayer.loopMethod3(paramLayer); 	
    }
	
    public void runForkJoinMethods() {
      // simple layer method
      demoLayer.forkjoinMethod1(); 

      // layer method with parameter
      resultLayer = demoLayer.forkjoinMethod2(0); 	

      // layer method with layer parameter
      resultLayer = demoLayer.forkjoinMethod3(paramLayer); 	
    }

  }
  

The createLayers() method creates and initializes the array/wrapper pairs demoArray/demoLayer and paramArray/paramLayer. Finally, method runLoopMethods() call those method layers which are executed in a loop and runForkJoinMethods() calls those which ar executed in parallel.


More Information