📄 example1.java
字号:
/* * Author: Anthony Sulistio * Date: April 2003 * Description: A simple program to demonstrate of how to use GridSim package * This example shows how to create one Grid user and one Grid * resource. * * NOTE: The values used from this example are taken from the GridSim paper. * http://www.gridbus.org/gridsim/ * $Id: Example1.java,v 1.5 2004/05/07 06:08:11 anthony Exp $ */import java.util.*;import gridsim.*;import gridbroker.*;/** * This class shows how to creae one Grid user and one Grid resource and run * the simulation using Grid broker. */class Example1{ /** * Main function to run the example */ public static void main(String[] args) { try { // First step: Initialize the GridSim package. It should be called // before creating any entities. String[] name = {"User0"}; // In this example, only do 1 user int num_user = name.length; // number of users need to be created Calendar calendar = Calendar.getInstance(); boolean trace_flag = true; // mean trace GridSim events/activities // lists of files or processing names to be excluded from any // statistical measures String[] exclude_from_file = { "" }; String[] exclude_from_processing = { "" }; // the name of a report file to be written. We don't want to write // anything here. See other examples of using the ReportWriter class String report_name = null; // Initializes the GridSim entities, such as GridSimRandom, // GridStatistics, GridSimShutdown and GridInformationService. // In addition, it initializes the SimJava package. GridSim.init(num_user, calendar, trace_flag, exclude_from_file, exclude_from_processing, report_name); // Second step: Create one or more Grid resource int i = 0; String[] resource = {"Resource0"}; // In this example, only do 1 for (i = 0; i < resource.length; i++) { GridResource gridResource = createGridResource(resource[i]); } // Third step: Create one or more Grid user for (i = 0; i < num_user; i++) { UserEntity user = createGridUser(name[i], resource); } // Fourth step: Start the simulation GridSim.startGridSimulation(); } catch (Exception e) { e.printStackTrace(); System.out.println("Unwanted errors happen"); } } /** * Creates one Grid resource. A Grid resource contains one or more Machines * Similarly, a Machine contains one or more PEs (Processing Elements or * CPUs). * <p> * In this simple example, we are simulating a Grid resource with one * Machine that contains one or more PEs. * @return a GridResource object * @throws Exception if unable to create GridResource object */ private static GridResource createGridResource(String name) throws Exception { // Here are the steps needed to create a Grid resource: // 1. We need to create an object of MachineList to store one or more // Machines MachineList mList = new MachineList(); // 2. A Machine contains one or more PEs or CPUs. Therefore, should // create an object of PEList to store these PEs before creating // a Machine. PEList peList = new PEList(); // 3. Create PEs and add these into an object of PEList. // In this example, we are using a resource from // pitcairn.mcs.anl.gov, ANL, Chicago, USA that has 4 PEs. // Note: these data are taken the from GridSim paper, page 25. // In this example, all PEs has the same MIPS (Millions // Instruction Per Second) Rating for a Machine. peList.add( new PE(0, 377) ); // need to store PE id and MIPS Rating peList.add( new PE(1, 377) ); peList.add( new PE(2, 377) ); peList.add( new PE(3, 377) ); // 4. Create one Machine with its id and list of PEs or CPUs mList.add( new Machine(0, peList) ); // 5. Create a ResourceCharacteristics object that stores the // properties of a Grid resource: architecture, OS, list of // Machines, allocation policy: time- or space-shared, time zone and // its price (G$/PE time unit). String arch = "Sun Ultra"; String os = "Solaris"; double time_zone = 0.0; double cost = 3.0; ResourceCharacteristics resConfig = new ResourceCharacteristics( arch, os, mList, ResourceCharacteristics.TIME_SHARED, time_zone, cost); // 6. Finally, we need to create a GridResource object. double baud_rate = 100.0; long seed = 11L*13*17*19*23+1; double peakLoad = 0.0; double offPeakLoad = 0.0; double holidayLoad = 0.0; LinkedList Weekends = new LinkedList(); Weekends.add(new Integer(Calendar.SATURDAY)); Weekends.add(new Integer(Calendar.SUNDAY)); LinkedList Holidays = new LinkedList(); GridResource gridRes = new GridResource(name, baud_rate, seed, resConfig, peakLoad, offPeakLoad, holidayLoad, Weekends, Holidays); return gridRes; } /** * Creates one Grid user. A Grid user has many Gridlets or jobs to be * processed. * @return an UserEntity object * @throws Exception if unable to create UserEntity object */ private static UserEntity createGridUser(String name, String[] resource) throws Exception { // Here are the steps needed: // 1. We need to create one or more Gridlets or jobs/tasks. In this // example, we need to create three Gridlets. 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); Gridlet gridlet2 = new Gridlet(1, 5000, 500, 500); Gridlet gridlet3 = new Gridlet(2, 9000, 900, 900); // 2. Store the Gridlets into a list GridletList list = new GridletList(); list.add(gridlet1); list.add(gridlet2); list.add(gridlet3); // 3. We need to create an experiment int strategy = Experiment.OPTIMIZE_TIME; // true means using deadline and budget factors [0.0 - 1.0] // false means using deadline and budget values boolean flag = false; double d_factor = 10000.0; // deadline value double b_factor = 10000.0; // budget value String report_file = "example1.txt"; Experiment expt = new Experiment(id, list, strategy, flag, d_factor, b_factor, report_file, resource); // 4. Finally, we need to create an UserEntity object double baud_rate = 56.0; long seed = 11L*13*17*19*23+1; double delay = 5.0; boolean result_display = true; // we want to display Gridlet processing // report UserEntity userEntity = new UserEntity(name, expt, baud_rate, seed, delay, result_display); return userEntity; } } // end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -