分享

GridSim例子三:示范怎么样使用一个GridSim包

 smog 2009-05-13
 
目的 :示范怎么样使用一个GridSim包,这个例子还演示两个GridSim实体(Example31和Test)如何进行交互
/*
 * 步骤
 * 第一步:初始化GirdSim包。应该在创建任何实体之前申明它
 * 第二步:创建一个网格作业列表
 * 第三步:创建Example31对象,里面包括核心方法去处理GridSim实体间的通信
 * 第四步:开始模拟GridSim.startGridSimulation();
 * 第五步: 模拟结束之后打印网格作业列表
 */
import java.util.*;
import gridsim.*;

/**
 * Example31类创建网格作业并且把他们发送到其他GridSim实体i.e. Test class.
 */
class Example31 extends GridSim
{
 private String entityName_;
 private GridletList list_;
 
 //从Test对象收到的网格作业的列表
 private GridletList receiveList_;
 
  /**
     * 分配一个新的Example31对象
     * @参数 name  实体名字
     * @参数 baud_rate    传输速度
     * @参数 list  网格作业的一个列表
     * @throws Exception 当在初始化一个GridSim包之前创建了这个实体或者实体名为空时<tt>null</tt> or empty会抛出例外                 
     * @see gridsim.GridSim#Init(int, Calendar, boolean, String[], String[],
             String)
     */
     Example31(String name, double baud_rate, GridletList list)throws Exception
     {
      super(name);
      this.list_ = list;
      receiveList_ = new GridletList();
      
      //创建一个Test实体,称为一个entityName
      entityName_ = "Test";
      new Test(entityName_, baud_rate);
     }
      /**
     * 核心方法去处理GridSim实体间的通信
     */
     public void body()
     {
      int size = list_.size();
      Gridlet obj, gridlet;
      //一个循环:一次得到一个网格作业,并把它发送到其他GridSim实体
        for(int i = 0; i < size; i++)
        {
         obj = (Gridlet) list_.get(i);
         
         System.out.println("在Example3.body()方法内 => 发送网格作业 " +
                    obj.getGridletID());
                   
            //在没有延迟的时刻下发送一个网格作业(使用常量GridSimTags.SCHEDULE_NOW)
            //到其他的GridSim实体
            super.send(entityName_, GridSimTags.SCHEDULE_NOW, GridSimTags.GRIDLET_SUBMIT, obj);
            //收到一个网格作业回复
            gridlet = super.gridletReceive();
            System.out.println("在Example3.body()方法内 => 收到网格作业回复 "+
                    gridlet.getGridletID());
            //将收到的网格作业存放在一个新的GridletList对象中
            receiveList_.add(gridlet);
        }
        //结束一个"entityName"模拟信号
        super.send(entityName_, GridSimTags.SCHEDULE_NOW,
                GridSimTags.END_OF_SIMULATION); 
    }
   
    /**
     * 得到一个网格作业列表
     * @return a list of Gridlets
     */
     public GridletList getGridletList() {
        return receiveList_;
    }
   
    /**
     * 创建一个main()函数
     */
    public static void main(String[] args)
    {
        System.out.println("开始例子Example31");
        System.out.println();
        try
        {
            // 第一步:初始化GirdSim包。应该在创建任何实体之前申明它
            //如果我们先不初始化GirdSim,我们就不能运行这个例子,会在运行的时候出错
            int num_user = 0;   // 需要创建的用户数
            Calendar calendar = Calendar.getInstance();
            boolean trace_flag = true;  // 追踪 GridSim events
           
            //用任何统计学方法排出的文件或者处理器的名字列表
            String[] exclude_from_file = { "" };
            String[] exclude_from_processing = { "" };
           
            //记下报告文件的名字。在这个例子里面我们不需要写什么东西
            // See other examples of using the ReportWriter class
            String report_name = null;
            // 初始化 GridSim 包
            System.out.println("初始化 GridSim 包");
            GridSim.init(num_user, calendar, trace_flag, exclude_from_file,
                    exclude_from_processing, report_name);

            // 第二步:创建一个网格作业列表
            GridletList list = createGridlet();
            System.out.println("创建" + list.size() + "网格作业");

            // 第三步:创建Example31对象
            Example31 obj = new Example31("Example31", 560.00, list);

            // 第四步:开始模拟
            GridSim.startGridSimulation();
            // 最后一步: 模拟结束之后打印网格作业列表
            GridletList newList = obj.getGridletList();
            printGridletList(newList);
            System.out.println("完成 Example31");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("发生的错误");
        }
    }

    /**
     * 一个网格用户有许多要处理的作业
     * 这个方法告诉你用或者不用GridSimRandom类去创建网格作业
     * @return a GridletList 对象
     * 参考例子2里面有具体说明,在此不再解释了
     */
    private static GridletList createGridlet()
    {
        // Creates a container to store Gridlets
        GridletList list = new GridletList();
        // We create three Gridlets or jobs/tasks manually without the help
        // of GridSimRandom
        int id = 0;
        double length = 3500.0;
        long file_size = 300;
        long output_size = 300;
        Gridlet gridlet1 = new Gridlet(id, length, file_size, output_size);
        id++;
        Gridlet gridlet2 = new Gridlet(id, 5000, 500, 500);
        id++;
        Gridlet gridlet3 = new Gridlet(id, 9000, 900, 900);
        // Store the Gridlets into a list
        list.add(gridlet1);
        list.add(gridlet2);
        list.add(gridlet3);
        // We create 5 Gridlets with the help of GridSimRandom and
        // GriSimStandardPE class
        long seed = 11L*13*17*19*23+1;
        Random random = new Random(seed);
        // sets the PE MIPS Rating
        GridSimStandardPE.setRating(100);
        // creates 5 Gridlets
        int count = 5;
        for (int i = 1; i < count+1; i++)
        {
            // the Gridlet length determines from random values and the
            // current MIPS Rating for a PE
            length = GridSimStandardPE.toMIs(random.nextDouble()*50);
            // determines the Gridlet file size that varies within the range
            // 100 + (10% to 40%)
            file_size = (long) GridSimRandom.real(100, 0.10, 0.40,
                                    random.nextDouble());
            // determines the Gridlet output size that varies within the range
            // 250 + (10% to 50%)
            output_size = (long) GridSimRandom.real(250, 0.10, 0.50,
                                    random.nextDouble());
            // creates a new Gridlet object
            Gridlet gridlet = new Gridlet(id + i, length, file_size,
                                    output_size);
            // add the Gridlet into a list
            list.add(gridlet);
        }
        return list;
    }
    /**
     * Prints the Gridlet objects
     * @param list  a list of Gridlets
     */
    private static void printGridletList(GridletList list)
    {
        int size = list.size();
        Gridlet gridlet;
        String indent = "    ";
        System.out.println();
        System.out.println("========== 输出 ==========");
        System.out.println("网格 ID" + indent + indent + "状态");
        for (int i = 0; i < size; i++)
        {
            gridlet = (Gridlet) list.get(i);
            System.out.print(indent + gridlet.getGridletID() + indent
                    + indent);
            if (gridlet.getGridletStatus() == Gridlet.SUCCESS)
                System.out.println("SUCCESS");
        }
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多