Navigation


Documentation/HowToWriteYourOwnDAC-Agent

How to write your own DAC agent

Using DAC as a framework is very simple. If you want to gain profits from parallel computing the only thing which you have to invent is a way to divide your complicated task on sub tasks. Below you will find very simple example which shows you how easy and efficient using DAC can be.

Lets say that we want to write example agent which computes power of two for given n-number passed as a parameter. To divide this task for sub tasks we can compute that by recursively dividing it with 2 and computing power of (n-1). Dividing stops when the power n is equal to 0 as 20 = 1.

We made the most tricky part so the rest is piece of cake :)

At the beginning of the implementation we have to remember about two things:

  • our agent has to extend abstract class org.dacframe.Agent
  • in our agent we have to implement execute() method which defines computation done by our agent

Below you will find initial (without code responsible for computation in execute() method) version of our agent:

public class AgentPowerOfTwo extends Agent{

        private final int n; //power of two (2^n)
        private int sum=0;
        
        private final Agent recipient;

    private enum Action { DIVIDE_OR_COMPUTE, CONSUME_RESULTS }
        private Action nextAction = Action.DIVIDE_OR_COMPUTE;
        
        public AgentPowerOfTwo(Agent parent, int n) {
                super(null);
                this.recipient = parent;
                this.n=n;
        }
        
        public AgentPowerOfTwo(int n) {
            this(null, n);
        }

        private static final long serialVersionUID = 1L;

        @SuppressWarnings("unchecked")
        public void execute() throws DACException {
          getLogger().info("AgentPowerOfTwo is starting execute()\n\t[n = "+n+
                "]\n\t[agent="+this+"]\n\t[action="+nextAction+"]");
                
        }

}

As you see the code is not complicated.

Just now we have to fill execute() method which is responsible for computation. Below you will find example content:

public void execute() throws DACException {
     getLogger().info("AgentPowerOfTwo is starting execute()\n\t[n = "+n+
                "]\n\t[agent="+this+"]\n\t[action="+nextAction+"]");
     if(nextAction==Action.DIVIDE_OR_COMPUTE){
        if(n==0) //leaf - return 1
                getAgentManager().putResult(recipient.getIdentString(), 1);
        else{  //not leaf - divide with 2 and send n-1
                getAgentManager().sendAgent(new AgentPowerOfTwo(this, n-1));
                getAgentManager().sendAgent(new AgentPowerOfTwo(this, n-1));
                nextAction=Action.CONSUME_RESULTS;
                getAgentManager().sendAgent(new ResultsAggregatorAgent(this, 2));
        }
      }
      else{  //CONSUME_RESULTS
        //sum up
        List<Object> subAgentsResults =    
              getAgentManager().receiveAgentResults(getIdentString(), 1);
        assert subAgentsResults.size() == 1;
        //System.out.println(this + " consumed children results");
        List ls =(List<Integer>)subAgentsResults.get(0);
        //int res = 0;
        for (Object x : ls)
                sum += (Integer)x;
        if(recipient==null){ //first agent
                //print result
                getLogger().info("\n 2^"+n+" = "+sum);
        }
        else{
                //send result
                getAgentManager().putResult(recipient.getIdentString(), sum);
        }
     }
}

As you see that is also not so complicated.

That is all - your agent is ready to use!

Further details (eg. how to start your agent) you can find here  Tutorials:

Example provided here you can also find in samples directory of the DAC distribution.