Page 1
package gridsim.example01;
/*
* 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 resource with three
* machines.
*
* NOTE: The values used from this example are taken from the GridSim paper.
* http://www.gridbus.org/gridsim/
* $Id: Example1.java,v 1.6 2004/05/29 05:24:00 anthony Exp $
*/
import java.util.Calendar;
import java.util.LinkedList;
import gridsim.*;
/**
* This class creates one Grid resource with three machines. Before creating
* any of GridSim entities, you should remember to call
* <tt>GridSim.Init()</tt>.
*/
class Example1
{
/**
Page 2
* Main function to run this example
*/
public static void main(String[] args)
{
System.out.println("Starting example of how to create one Grid " +
"resource");
try
{
// First step: Initialize the GridSim package. It should be called
// before creating any entities. We can't run GridResource
// entity without initializing GridSim first. We will get run-time
// exception error.
// number of users need to be created. In this example, we put
// zero since we don't create any user entities.
int num_user = 0;
Calendar calendar = Calendar.getInstance();
boolean trace_flag = true; // mean trace GridSim events/activities
// list 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
Page 3
// ReportWriter class
String report_name = null;
// Initialize the GridSim package
System.out.println("Initializing GridSim package");
GridSim.init(num_user, calendar, trace_flag, exclude_from_file,
exclude_from_processing, report_name);
// Since GridSim 3.0, there is another way to initialise GridSim
// without any statistical functionalities.
// The code is commented below:
// GridSim.init(num_user, calendar, trace_flag);
// Second step: Create one Grid resource
GridResource gridResource = createGridResource();
System.out.println("Finish the 1st example");
// NOTE: we do not need to call GridSim.startGridSimulation()
// as there are no user entities to send their jobs to this
// resource.
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Unwanted error happens");
}
}
Page 4
/**
* 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 one Grid resource with three
* Machines that contains one or more PEs.
* @return a GridResource object
*/
private static GridResource createGridResource()
{
System.out.println("Starting to create one Grid resource with " +
"3 Machines ...");
// 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();
System.out.println("Creates a Machine list");
// 2. Create one Machine with its id, number of PEs and MIPS rating per PE
// In this example, we are using a resource from
// hpc420.hpcc.jp, AIST, Tokyo, Japan
// Note: these data are taken the from GridSim paper, page 25.
// In this example, all PEs has the same MIPS (Millions
Page 5
// Instruction Per Second) Rating for a Machine.
int mipsRating = 377;
mList.add( new Machine(0, 4, mipsRating)); // First Machine
System.out.println("Creates the 1st Machine that has 4 PEs and " +
"stores it into the Machine list");
// 3. Repeat the process from 2 if we want to create more Machines
// In this example, the AIST in Japan has 3 Machines with same
// MIPS Rating but different PEs.
// NOTE: if you only want to create one Machine for one Grid resource,
// then you could skip this step.
mList.add( new Machine(1, 4, mipsRating)); // Second Machine
System.out.println("Creates the 2nd Machine that has 4 PEs and " +
"stores it into the Machine list");
mList.add( new Machine(2, 2, mipsRating)); // Third Machine
System.out.println("Creates the 3rd Machine that has 2 PEs and " +
"stores it into the Machine list");
// 4. 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"; // system architecture
String os = "Solaris"; // operating system
double time_zone = 9.0; // time zone this resource located
double cost = 3.0; // the cost of using this resource
Page 6
ResourceCharacteristics resConfig = new ResourceCharacteristics(
arch, os, mList, ResourceCharacteristics.TIME_SHARED,
time_zone, cost);
System.out.println();
System.out.println("Creates the properties of a Grid resource and " +
"stores the Machine list");
// 5. Finally, we need to create a GridResource object.
String name = "Resource_0"; // resource name
double baud_rate = 100.0; // communication speed
long seed = 11L*13*17*19*23+1;
double peakLoad = 0.0; // the resource load during peak hour
double offPeakLoad = 0.0; // the resource load during off-peak hr
double holidayLoad = 0.0; // the resource load during holiday
// incorporates weekends so the grid resource is on 7 days a week
LinkedList<Integer> Weekends = new LinkedList<Integer>();
Weekends.add(new Integer(Calendar.SATURDAY));
Weekends.add(new Integer(Calendar.SUNDAY));
// incorporates holidays. However, no holidays are set in this example
LinkedList<Integer> Holidays = new LinkedList<Integer>();
GridResource gridRes = null;
try
Page 7
{
gridRes = new GridResource(name, baud_rate, seed,
resConfig, peakLoad, offPeakLoad, holidayLoad, Weekends,
Holidays);
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("Finally, creates one Grid resource and stores " +
"the properties of a Grid resource");
return gridRes;
}
}
Example2
package gridsim.example02;
/*
* 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 or more Grid users.
* A Grid user contains one or more Gridlets.
* Therefore, this example also shows how to create Gridlets with
* and without using GridSimRandom class.
*
Page 8
* NOTE: The values used from this example are taken from the GridSim paper.
* http://www.gridbus.org/gridsim/
* $Id: Example2.java,v 1.4 2003/05/19 13:17:49 anthony Exp $
*/
import java.util.*;
import gridsim.*;
/**
* This class shows how to create one or more grid users. In addition, the
* creation of Gridlets also discussed.
*/
class Example2
{
/**
* Main function to run this example
*/
public static void main(String[] args)
{
System.out.println("Starting example of how to create Grid users");
System.out.println();
try
{
// Creates a list of Gridlets
GridletList list = createGridlet();
System.out.println("Creating " + list.size() + " Gridlets");
Page 9
ResourceUserList userList = createGridUser(list);
System.out.println("Creating " + userList.size() + " Grid users");
// print the Gridlets
printGridletList(list);
System.out.println("Finish the example");
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Unwanted error happens");
}
}
/**
* A Grid user has many Gridlets or jobs to be processed.
* This method will show you how to create Gridlets with and without
* GridSimRandom class.
* @return a GridletList object
*/
private static GridletList createGridlet()
{
// Creates a container to store Gridlets
GridletList list = new GridletList();
Page 10
// 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
Random random = new Random();
// sets the PE MIPS Rating
GridSimStandardPE.setRating(100);
// creates 5 Gridlets
int count = 5;
Page 11
double min_range = 0.10;
double max_range = 0.50;
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()*output_size);
// determines the Gridlet file size that varies within the range
// 100 + (10% to 50%)
file_size = (long) GridSimRandom.real(100, min_range, max_range,
random.nextDouble());
// determines the Gridlet output size that varies within the range
// 250 + (10% to 50%)
output_size = (long) GridSimRandom.real(250, min_range, max_range,
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;
Page 12
}
/**
* Creates Grid users. In this example, we create 3 users. Then assign
* these users to Gridlets.
* @return a list of Grid users
*/
private static ResourceUserList createGridUser(GridletList list)
{
ResourceUserList userList = new ResourceUserList();
userList.add(0); // user ID starts from 0
userList.add(1);
userList.add(2);
int userSize = userList.size();
int gridletSize = list.size();
int id = 0;
// assign user ID to particular Gridlets
for (int i = 0; i < gridletSize; i++)
{
if (i != 0 && i % userSize == 0)
id++;
( (Gridlet) list.get(i) ).setUserID(id);
Page 13
}
return userList;
}
private static void printGridletList(GridletList list)
{
int size = list.size();
Gridlet gridlet;
String indent = " ";
System.out.println();
System.out.println("Gridlet ID" + indent + "User ID" + indent +
"length" + indent + " file size" + indent +
"output size");
for (int i = 0; i < size; i++)
{
gridlet = (Gridlet) list.get(i);
System.out.println(indent + gridlet.getGridletID() + indent +
indent + indent + gridlet.getUserID() + indent + indent +
(int) gridlet.getGridletLength() + indent + indent +
(int) gridlet.getGridletFileSize() + indent + indent +
(int) gridlet.getGridletOutputSize() );
}
}
Page 14
} // end class
Example 3
package gridsim.example03;
/*
* Author Anthony Sulistio
* Date: April 2003
* Description: A simple program to demonstrate of how to use GridSim package.
* this example shows how two GridSim entities interact with each
* other.
*
* NOTE: The values used from this example are taken from the GridSim paper.
* http://www.gridbus.org/gridsim/
* $Id: Example3.java,v 1.6 2005/09/16 07:02:15 anthony Exp $
*/
import java.util.*;
import gridsim.*;
/**
* Example3 class creates Gridlets and sends them to the other GridSim
* entities, i.e. Test class.
*/
class Example3 extends GridSim
{
Page 15
private String entityName_;
private GridletList list_;
// Gridlet lists received from Test object
private GridletList receiveList_;
/**
* Allocates a new Example3 object
* @param name the Entity name
* @param baud_rate the communication speed
* @param list a list of Gridlets
* @throws Exception This happens when creating this entity before
* initializing GridSim package or the entity name is
* <tt>null</tt> or empty
* @see gridsim.GridSim#Init(int, Calendar, boolean, String[], String[],
* String)
*/
Example3(String name, double baud_rate, GridletList list) throws Exception
{
super(name);
this.list_ = list;
receiveList_ = new GridletList();
// creates a Test entity, and refer it as "entityName"
entityName_ = "Test";
new Test(entityName_, baud_rate);
Page 16
}
/**
* The core method that handles communications between GridSim entities.
*/
public void body()
{
int size = list_.size();
Gridlet obj, gridlet;
// a loop to get one Gridlet at one time and sends it to other GridSim
// entity
for (int i = 0; i < size; i++)
{
obj = (Gridlet) list_.get(i);
System.out.println("Inside Example3.body() => Sending Gridlet " +
obj.getGridletID());
// Sends one Gridlet at the time with no delay (by using
// GridSimTags.SCHEDULE_NOW constant) to the other GridSim entity
// specified in "entityName"
super.send(entityName_, GridSimTags.SCHEDULE_NOW,
GridSimTags.GRIDLET_SUBMIT, obj);
// Receiving a Gridlet back
gridlet = super.gridletReceive();
Page 17
System.out.println("Inside Example3.body() => Receiving Gridlet "+
gridlet.getGridletID());
// stores the received Gridlet into a new GridletList object
receiveList_.add(gridlet);
}
// Signals the end of simulation to "entityName"
super.send(entityName_, GridSimTags.SCHEDULE_NOW,
GridSimTags.END_OF_SIMULATION);
}
/**
* Gets the list of Gridlets
* @return a list of Gridlets
*/
public GridletList getGridletList() {
return receiveList_;
}
/**
* Creates main() to run this example
*/
public static void main(String[] args)
Page 18
{
System.out.println("Starting Example3");
System.out.println();
try
{
// First step: Initialize the GridSim package. It should be called
// before creating any entities. We can't run this example without
// initializing GridSim first. We will get run-time exception
// error.
int num_user = 0; // number of users need to be created
Calendar calendar = Calendar.getInstance();
boolean trace_flag = true; // mean trace GridSim events
// list 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;
// Initialize the GridSim package
System.out.println("Initializing GridSim package");
GridSim.init(num_user, calendar, trace_flag, exclude_from_file,
Page 19
exclude_from_processing, report_name);
// Second step: Creates a list of Gridlets
GridletList list = createGridlet();
System.out.println("Creating " + list.size() + " Gridlets");
// Third step: Creates the Example3 object
Example3 obj = new Example3("Example3", 560.00, list);
// Fourth step: Starts the simulation
GridSim.startGridSimulation();
// Final step: Prints the Gridlets when simulation is over
GridletList newList = obj.getGridletList();
printGridletList(newList);
System.out.println("Finish Example3");
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Unwanted errors happen");
}
}
Page 20
/**
* This method will show you how to create Gridlets with and without
* GridSimRandom class.
* @return a GridletList object
*/
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);
Page 21
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());
Page 22
// 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("========== OUTPUT ==========");
System.out.println("Gridlet ID" + indent + "STATUS");
for (int i = 0; i < size; i++)
Page 23
{
gridlet = (Gridlet) list.get(i);
System.out.print(indent + gridlet.getGridletID() + indent
+ indent);
if (gridlet.getGridletStatus() == Gridlet.SUCCESS)
System.out.println("SUCCESS");
}
}
}
Example 4
package gridsim.example03;
/*
* Author Anthony Sulistio
* Date: April 2003
* Description: A simple program to demonstrate of how to use GridSim package.
* this example shows how two GridSim entities interact with each
* other.
*
* NOTE: The values used from this example are taken from the GridSim paper.
* http://www.gridbus.org/gridsim/
* $Id: Example3.java,v 1.6 2005/09/16 07:02:15 anthony Exp $
*/
import java.util.*;
import gridsim.*;
Page 24
/**
* Example3 class creates Gridlets and sends them to the other GridSim
* entities, i.e. Test class.
*/
class Example3 extends GridSim
{
private String entityName_;
private GridletList list_;
// Gridlet lists received from Test object
private GridletList receiveList_;
/**
* Allocates a new Example3 object
* @param name the Entity name
* @param baud_rate the communication speed
* @param list a list of Gridlets
* @throws Exception This happens when creating this entity before
* initializing GridSim package or the entity name is
* <tt>null</tt> or empty
* @see gridsim.GridSim#Init(int, Calendar, boolean, String[], String[],
* String)
*/
Example3(String name, double baud_rate, GridletList list) throws Exception
Page 25
{
super(name);
this.list_ = list;
receiveList_ = new GridletList();
// creates a Test entity, and refer it as "entityName"
entityName_ = "Test";
new Test(entityName_, baud_rate);
}
/**
* The core method that handles communications between GridSim entities.
*/
public void body()
{
int size = list_.size();
Gridlet obj, gridlet;
// a loop to get one Gridlet at one time and sends it to other GridSim
// entity
for (int i = 0; i < size; i++)
{
obj = (Gridlet) list_.get(i);
System.out.println("Inside Example3.body() => Sending Gridlet " +
obj.getGridletID());
Page 26
// Sends one Gridlet at the time with no delay (by using
// GridSimTags.SCHEDULE_NOW constant) to the other GridSim entity
// specified in "entityName"
super.send(entityName_, GridSimTags.SCHEDULE_NOW,
GridSimTags.GRIDLET_SUBMIT, obj);
// Receiving a Gridlet back
gridlet = super.gridletReceive();
System.out.println("Inside Example3.body() => Receiving Gridlet "+
gridlet.getGridletID());
// stores the received Gridlet into a new GridletList object
receiveList_.add(gridlet);
}
// Signals the end of simulation to "entityName"
super.send(entityName_, GridSimTags.SCHEDULE_NOW,
GridSimTags.END_OF_SIMULATION);
}
/**
* Gets the list of Gridlets
* @return a list of Gridlets
*/
public GridletList getGridletList() {
Page 27
return receiveList_;
}
/**
* Creates main() to run this example
*/
public static void main(String[] args)
{
System.out.println("Starting Example3");
System.out.println();
try
{
// First step: Initialize the GridSim package. It should be called
// before creating any entities. We can't run this example without
// initializing GridSim first. We will get run-time exception
// error.
int num_user = 0; // number of users need to be created
Calendar calendar = Calendar.getInstance();
boolean trace_flag = true; // mean trace GridSim events
// list of files or processing names to be excluded from any
// statistical measures
String[] exclude_from_file = { "" };
String[] exclude_from_processing = { "" };
Page 28
// 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;
// Initialize the GridSim package
System.out.println("Initializing GridSim package");
GridSim.init(num_user, calendar, trace_flag, exclude_from_file,
exclude_from_processing, report_name);
// Second step: Creates a list of Gridlets
GridletList list = createGridlet();
System.out.println("Creating " + list.size() + " Gridlets");
// Third step: Creates the Example3 object
Example3 obj = new Example3("Example3", 560.00, list);
// Fourth step: Starts the simulation
GridSim.startGridSimulation();
// Final step: Prints the Gridlets when simulation is over
GridletList newList = obj.getGridletList();
printGridletList(newList);
Page 29
System.out.println("Finish Example3");
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Unwanted errors happen");
}
}
/**
* This method will show you how to create Gridlets with and without
* GridSimRandom class.
* @return a GridletList object
*/
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);
Page 30
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
Page 31
// 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();
Page 32
Gridlet gridlet;
String indent = " ";
System.out.println();
System.out.println("========== OUTPUT ==========");
System.out.println("Gridlet ID" + indent + "STATUS");
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");
}
}
}
Example 5
package gridsim.example05;
/*
* Author Anthony Sulistio
* Date: April 2003
* Description: A simple program to demonstrate of how to use GridSim package.
* This example shows how a grid user submits its Gridlets or
* task to many grid resource entities.
Page 33
*
* NOTE: The values used from this example are taken from the GridSim paper.
* http://www.gridbus.org/gridsim/
* $Id: Example5.java,v 1.7 2005/09/19 08:29:10 anthony Exp $
*/
import java.util.*;
import gridsim.*;
/**
* Example5 class creates Gridlets and sends them to many grid resource
* entities
*/
class Example5 extends GridSim
{
private Integer ID_;
private String name_;
private GridletList list_;
private GridletList receiveList_;
private int totalResource_;
/**
* Allocates a new Example5 object
* @param name the Entity name of this object
* @param baud_rate the communication speed
* @param total_resource the number of grid resources available
Page 34
* @throws Exception This happens when creating this entity before
* initializing GridSim package or the entity name is
* <tt>null</tt> or empty
* @see gridsim.GridSim#Init(int, Calendar, boolean, String[], String[],
* String)
*/
Example5(String name, double baud_rate, int total_resource)
throws Exception
{
super(name, baud_rate);
this.name_ = name;
this.totalResource_ = total_resource;
this.receiveList_ = new GridletList();
// Gets an ID for this entity
this.ID_ = new Integer( getEntityId(name) );
System.out.println("Creating a grid user entity with name = " +
name + ", and id = " + this.ID_);
// Creates a list of Gridlets or Tasks for this grid user
this.list_ = createGridlet( this.ID_.intValue() );
System.out.println("Creating " + this.list_.size() + " Gridlets");
}
/**
* The core method that handles communications among GridSim entities
*/
Page 35
public void body()
{
int resourceID[] = new int[this.totalResource_];
double resourceCost[] = new double[this.totalResource_];
String resourceName[] = new String[this.totalResource_];
LinkedList resList;
ResourceCharacteristics resChar;
// waiting to get list of resources. Since GridSim package uses
// multi-threaded environment, your request might arrive earlier
// before one or more grid resource entities manage to register
// themselves to GridInformationService (GIS) entity.
// Therefore, it's better to wait in the first place
while (true)
{
// need to pause for a while to wait GridResources finish
// registering to GIS
super.gridSimHold(1.0); // hold by 1 second
resList = super.getGridResourceList();
if (resList.size() == this.totalResource_)
break;
else
System.out.println("Waiting to get list of resources ...");
}
Page 36
int i = 0;
// a loop to get all the resources available
for (i = 0; i < this.totalResource_; i++)
{
// Resource list contains list of resource IDs not grid resource
// objects.
resourceID[i] = ( (Integer)resList.get(i) ).intValue();
// Requests to resource entity to send its characteristics
super.send(resourceID[i], GridSimTags.SCHEDULE_NOW,
GridSimTags.RESOURCE_CHARACTERISTICS, this.ID_);
// waiting to get a resource characteristics
resChar = (ResourceCharacteristics) super.receiveEventObject();
resourceName[i] = resChar.getResourceName();
resourceCost[i] = resChar.getCostPerSec();
System.out.println("Received ResourceCharacteristics from " +
resourceName[i] + ", with id = " + resourceID[i]);
// record this event into "stat.txt" file
super.recordStatistics("\"Received ResourceCharacteristics " +
"from " + resourceName[i] + "\"", "");
}
Gridlet gridlet;
Page 37
String info;
// a loop to get one Gridlet at one time and sends it to a random grid
// resource entity. Then waits for a reply
Random random = new Random();
int id = 0;
for (i = 0; i < this.list_.size(); i++)
{
gridlet = (Gridlet) this.list_.get(i);
info = "Gridlet_" + gridlet.getGridletID();
id = random.nextInt(this.totalResource_);
System.out.println("Sending " + info + " to " + resourceName[id] +
" with id = " + resourceID[id]);
// Sends one Gridlet to a grid resource specified in "resourceID"
super.gridletSubmit(gridlet, resourceID[id]);
// OR another approach to send a gridlet to a grid resource entity
//super.send(resourceID[id], GridSimTags.SCHEDULE_NOW,
// GridSimTags.GRIDLET_SUBMIT, gridlet);
// Recods this event into "stat.txt" file for statistical purposes
super.recordStatistics("\"Submit " + info + " to " +
resourceName[id] + "\"", "");
// waiting to receive a Gridlet back from resource entity
Page 38
gridlet = super.gridletReceive();
System.out.println("Receiving Gridlet " + gridlet.getGridletID());
// Recods this event into "stat.txt" file for statistical purposes
super.recordStatistics("\"Received " + info + " from " +
resourceName[id] + "\"", gridlet.getProcessingCost());
// stores the received Gridlet into a new GridletList object
this.receiveList_.add(gridlet);
}
// shut down all the entities, including GridStatistics entity since
// we used it to record certain events.
super.shutdownGridStatisticsEntity();
super.shutdownUserEntity();
super.terminateIOEntities();
}
/**
* Gets the list of Gridlets
* @return a list of Gridlets
*/
public GridletList getGridletList() {
return this.receiveList_;
}
/**
Page 39
* This method will show you how to create Gridlets with and without
* GridSimRandom class.
* @param userID the user entity ID that owns these Gridlets
* @return a GridletList object
*/
private GridletList createGridlet(int userID)
{
// 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);
// setting the owner of these Gridlets
gridlet1.setUserID(userID);
gridlet2.setUserID(userID);
gridlet3.setUserID(userID);
Page 40
// 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
Page 41
// 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);
gridlet.setUserID(userID);
// add the Gridlet into a list
list.add(gridlet);
}
return list;
}
////////////////////////// STATIC METHODS ///////////////////////
/**
* Creates main() to run this example
*/
public static void main(String[] args)
{
System.out.println("Starting Example5");
Page 42
try
{
// First step: Initialize the GridSim package. It should be called
// before creating any entities. We can't run this example without
// initializing GridSim first. We will get run-time exception
// error.
int num_user = 1; // number of grid users
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false; // mean don't trace GridSim events
// list 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;
// Initialize the GridSim package
System.out.println("Initializing GridSim package");
GridSim.init(num_user, calendar, trace_flag, exclude_from_file,
exclude_from_processing, report_name);
// Second step: Creates one or more GridResource objects
GridResource resource0 = createGridResource("Resource_0");
Page 43
GridResource resource1 = createGridResource("Resource_1");
GridResource resource2 = createGridResource("Resource_2");
int total_resource = 3;
// Third step: Creates the Example5 object
Example5 obj = new Example5("Example5", 560.00, total_resource);
// Fourth step: Starts the simulation
GridSim.startGridSimulation();
// Final step: Prints the Gridlets when simulation is over
GridletList newList = obj.getGridletList();
printGridletList(newList);
System.out.println("Finish Example5");
}
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).
Page 44
* <p>
* In this simple example, we are simulating one Grid resource with three
* Machines that contains one or more PEs.
* @param name a Grid Resource name
* @return a GridResource object
*/
private static GridResource createGridResource(String name)
{
System.out.println();
System.out.println("Starting to create one Grid resource with " +
"3 Machines");
// 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();
System.out.println("Creates a Machine list");
// 2. Create one Machine with its id, number of PEs and MIPS rating per PE
// In this example, we are using a resource from
// hpc420.hpcc.jp, AIST, Tokyo, Japan
// 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.
int mipsRating = 377;
mList.add( new Machine(0, 4, mipsRating)); // First Machine
System.out.println("Creates the 1st Machine that has 4 PEs and " +
Page 45
"stores it into the Machine list");
// 3. Repeat the process from 2 if we want to create more Machines
// In this example, the AIST in Japan has 3 Machines with same
// MIPS Rating but different PEs.
// NOTE: if you only want to create one Machine for one Grid resource,
// then you could skip this step.
mList.add( new Machine(1, 4, mipsRating)); // Second Machine
System.out.println("Creates the 2nd Machine that has 4 PEs and " +
"stores it into the Machine list");
mList.add( new Machine(2, 2, mipsRating)); // Third Machine
System.out.println("Creates the 3rd Machine that has 2 PEs and " +
"stores it into the Machine list");
// 4. 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"; // system architecture
String os = "Solaris"; // operating system
double time_zone = 9.0; // time zone this resource located
double cost = 3.0; // the cost of using this resource
ResourceCharacteristics resConfig = new ResourceCharacteristics(
arch, os, mList, ResourceCharacteristics.TIME_SHARED,
time_zone, cost);
Page 46
System.out.println("Creates the properties of a Grid resource and " +
"stores the Machine list");
// 5. Finally, we need to create a GridResource object.
double baud_rate = 100.0; // communication speed
long seed = 11L*13*17*19*23+1;
double peakLoad = 0.0; // the resource load during peak hour
double offPeakLoad = 0.0; // the resource load during off-peak hr
double holidayLoad = 0.0; // the resource load during holiday
// incorporates weekends so the grid resource is on 7 days a week
LinkedList Weekends = new LinkedList();
Weekends.add(new Integer(Calendar.SATURDAY));
Weekends.add(new Integer(Calendar.SUNDAY));
// incorporates holidays. However, no holidays are set in this example
LinkedList Holidays = new LinkedList();
GridResource gridRes = null;
try
{
gridRes = new GridResource(name, baud_rate, seed,
resConfig, peakLoad, offPeakLoad, holidayLoad, Weekends,
Holidays);
}
catch (Exception e) {
e.printStackTrace();
Page 47
}
System.out.println("Finally, creates one Grid resource and stores " +
"the properties of a Grid resource");
System.out.println();
return gridRes;
}
/**
* Prints the Gridlet objects
* @param list list of Gridlets
*/
private static void printGridletList(GridletList list)
{
int size = list.size();
Gridlet gridlet;
String indent = " ";
System.out.println();
System.out.println("========== OUTPUT ==========");
System.out.println("Gridlet ID" + indent + "STATUS" + indent +
"Resource ID" + indent + "Cost");
for (int i = 0; i < size; i++)
{
gridlet = (Gridlet) list.get(i);
Page 48
System.out.print(indent + gridlet.getGridletID() + indent
+ indent);
if (gridlet.getGridletStatus() == Gridlet.SUCCESS)
System.out.print("SUCCESS");
System.out.println( indent + indent + gridlet.getResourceID() +
indent + indent + gridlet.getProcessingCost() );
}
}
} // end class
Example 6
package gridsim.example06;
/*
* Author Anthony Sulistio
* Date: April 2003
* Description: A simple program to demonstrate of how to use GridSim package.
* This example shows how a grid user submits its Gridlets or
* task to many grid resource entities.
*
* NOTE: The values used from this example are taken from the GridSim paper.
* http://www.gridbus.org/gridsim/
* $Id: Example6.java,v 1.7 2005/09/20 07:01:24 anthony Exp $
*/
Page 49
import java.util.*;
import gridsim.*;
/**
* Example6 class
*/
class Example6 extends GridSim
{
private Integer ID_;
private String name_;
private GridletList list_;
private GridletList receiveList_;
private int totalResource_;
/**
* Allocates a new Example6 object
* @param name the Entity name of this object
* @param baud_rate the communication speed
* @param total_resource the number of grid resources available
* @throws Exception This happens when creating this entity before
* initializing GridSim package or the entity name is
* <tt>null</tt> or empty
* @see gridsim.GridSim#Init(int, Calendar, boolean, String[], String[],
* String)
*/
Page 50
Example6(String name, double baud_rate, int total_resource)
throws Exception
{
super(name, baud_rate);
this.name_ = name;
this.totalResource_ = total_resource;
this.receiveList_ = new GridletList();
// Gets an ID for this entity
this.ID_ = new Integer( getEntityId(name) );
System.out.println("Creating a grid user entity with name = " +
name + ", and id = " + this.ID_);
// Creates a list of Gridlets or Tasks for this grid user
this.list_ = createGridlet( this.ID_.intValue() );
System.out.println(name + ":Creating "+ this.list_.size() +
" Gridlets");
}
/**
* The core method that handles communications among GridSim entities.
*/
public void body()
{
int resourceID[] = new int[this.totalResource_];
double resourceCost[] = new double[this.totalResource_];
String resourceName[] = new String[this.totalResource_];
Page 51
LinkedList resList;
ResourceCharacteristics resChar;
// waiting to get list of resources. Since GridSim package uses
// multi-threaded environment, your request might arrive earlier
// before one or more grid resource entities manage to register
// themselves to GridInformationService (GIS) entity.
// Therefore, it's better to wait in the first place
while (true)
{
// need to pause for a while to wait GridResources finish
// registering to GIS
super.gridSimHold(1.0); // hold by 1 second
resList = super.getGridResourceList();
if (resList.size() == this.totalResource_)
break;
else
{
System.out.println(this.name_ +
":Waiting to get list of resources ...");
}
}
// a loop to get all the resources available
Page 52
int i = 0;
for (i = 0; i < this.totalResource_; i++)
{
// Resource list contains list of resource IDs not grid resource
// objects.
resourceID[i] = ( (Integer)resList.get(i) ).intValue();
// Requests to resource entity to send its characteristics
super.send(resourceID[i], GridSimTags.SCHEDULE_NOW,
GridSimTags.RESOURCE_CHARACTERISTICS, this.ID_);
// waiting to get a resource characteristics
resChar = (ResourceCharacteristics) super.receiveEventObject();
resourceName[i] = resChar.getResourceName();
resourceCost[i] = resChar.getCostPerSec();
System.out.println(this.name_ +
":Received ResourceCharacteristics from " +
resourceName[i] + ", with id = " + resourceID[i]);
// record this event into "stat.txt" file
super.recordStatistics("\"Received ResourceCharacteristics " +
"from " + resourceName[i] + "\"", "");
}
Gridlet gridlet;
String info;
Page 53
// a loop to get one Gridlet at one time and sends it to a random grid
// resource entity. Then waits for a reply
int id = 0;
for (i = 0; i < this.list_.size(); i++)
{
gridlet = (Gridlet) this.list_.get(i);
info = "Gridlet_" + gridlet.getGridletID();
id = GridSimRandom.intSample(this.totalResource_);
System.out.println(this.name_ + ":Sending " + info + " to " +
resourceName[id] + " with id = " + resourceID[id]);
// Sends one Gridlet to a grid resource specified in "resourceID"
super.gridletSubmit(gridlet, resourceID[id]);
// Recods this event into "stat.txt" file for statistical purposes
super.recordStatistics("\"Submit " + info + " to " +
resourceName[id] + "\"", "");
// waiting to receive a Gridlet back from resource entity
gridlet = super.gridletReceive();
System.out.println(this.name_ + ":Receiving Gridlet " +
gridlet.getGridletID() );
// Recods this event into "stat.txt" file for statistical purposes
super.recordStatistics("\"Received " + info + " from " +
Page 54
resourceName[id] + "\"", gridlet.getProcessingCost());
// stores the received Gridlet into a new GridletList object
this.receiveList_.add(gridlet);
}
// shut down all the entities, including GridStatistics entity since
// we used it to record certain events.
super.shutdownGridStatisticsEntity();
super.shutdownUserEntity();
super.terminateIOEntities();
System.out.println(this.name_ + ":%%%% Exiting body()");
}
/**
* Gets the list of Gridlets
* @return a list of Gridlets
*/
public GridletList getGridletList() {
return this.receiveList_;
}
/**
* This method will show you how to create Gridlets with and without
* GridSimRandom class.
* @param userID the user entity ID that owns these Gridlets
* @return a GridletList object
Page 55
*/
private GridletList createGridlet(int userID)
{
// 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);
// setting the owner of these Gridlets
gridlet1.setUserID(userID);
gridlet2.setUserID(userID);
gridlet3.setUserID(userID);
// Store the Gridlets into a list
list.add(gridlet1);
list.add(gridlet2);
list.add(gridlet3);
Page 56
// We create 5 Gridlets (max.) with the help of GridSimRandom and
// GriSimStandardPE class
// sets the PE MIPS Rating
GridSimStandardPE.setRating(100);
// creates 5 Gridlets
int max = 5;
int count = GridSimRandom.intSample(max);
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(GridSimRandom.doubleSample()*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,
GridSimRandom.doubleSample());
// determines the Gridlet output size that varies within the range
// 250 + (10% to 50%)
output_size = (long) GridSimRandom.real(250, 0.10, 0.50,
GridSimRandom.doubleSample());
// creates a new Gridlet object
Page 57
Gridlet gridlet = new Gridlet(id + i, length, file_size,
output_size);
gridlet.setUserID(userID);
// add the Gridlet into a list
list.add(gridlet);
}
return list;
}
////////////////////// STATIC METHODS //////////////////////////////
/**
* Creates main() to run this example
*/
public static void main(String[] args)
{
System.out.println("Starting Example6");
try
{
// First step: Initialize the GridSim package. It should be called
// before creating any entities. We can't run this example without
// initializing GridSim first. We will get run-time exception
Page 58
// error.
int num_user = 3; // number of grid users
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false; // mean don't trace GridSim events
// list 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;
// Initialize the GridSim package
//System.out.println("Initializing GridSim package");
GridSim.init(num_user, calendar, trace_flag, exclude_from_file,
exclude_from_processing, report_name);
// Second step: Creates one or more GridResource objects
GridResource resource0 = createGridResource("Resource_0");
GridResource resource1 = createGridResource("Resource_1");
GridResource resource2 = createGridResource("Resource_2");
int total_resource = 3;
// Third step: Creates grid users
Page 59
Example6 user0 = new Example6("User_0", 560.00, total_resource);
Example6 user1 = new Example6("User_1", 250.00, total_resource);
Example6 user2 = new Example6("User_2", 150.00, total_resource);
// Fourth step: Starts the simulation
GridSim.startGridSimulation();
// Final step: Prints the Gridlets when simulation is over
GridletList newList = null;
newList = user0.getGridletList();
printGridletList(newList, "User_0");
newList = user1.getGridletList();
printGridletList(newList, "User_1");
newList = user2.getGridletList();
printGridletList(newList, "User_2");
System.out.println("Finish Example6");
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Unwanted errors happen");
}
}
Page 60
/**
* 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 one Grid resource with three
* Machines that contains one or more PEs.
* @param name a Grid Resource name
* @return a GridResource object
*/
private static GridResource createGridResource(String name)
{
// 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. Create one Machine with its id, number of PEs and MIPS rating per PE
// In this example, we are using a resource from
// hpc420.hpcc.jp, AIST, Tokyo, Japan
// 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.
int mipsRating = 377;
mList.add( new Machine(0, 4, mipsRating)); // First Machine
// 3. Repeat the process from 2 if we want to create more Machines
Page 61
// In this example, the AIST in Japan has 3 Machines with same
// MIPS Rating but different PEs.
// NOTE: if you only want to create one Machine for one Grid resource,
// then you could skip this step.
mList.add( new Machine(1, 4, mipsRating)); // Second Machine
mList.add( new Machine(2, 2, mipsRating)); // Third Machine
// 4. 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"; // system architecture
String os = "Solaris"; // operating system
double time_zone = 9.0; // time zone this resource located
double cost = 3.0; // the cost of using this resource
ResourceCharacteristics resConfig = new ResourceCharacteristics(
arch, os, mList, ResourceCharacteristics.TIME_SHARED,
time_zone, cost);
// 5. Finally, we need to create a GridResource object.
double baud_rate = 100.0; // communication speed
long seed = 11L*13*17*19*23+1;
double peakLoad = 0.0; // the resource load during peak hour
double offPeakLoad = 0.0; // the resource load during off-peak hr
double holidayLoad = 0.0; // the resource load during holiday
Page 62
// incorporates weekends so the grid resource is on 7 days a week
LinkedList Weekends = new LinkedList();
Weekends.add(new Integer(Calendar.SATURDAY));
Weekends.add(new Integer(Calendar.SUNDAY));
// incorporates holidays. However, no holidays are set in this example
LinkedList Holidays = new LinkedList();
GridResource gridRes = null;
try {
gridRes = new GridResource(name, baud_rate, seed,
resConfig, peakLoad, offPeakLoad, holidayLoad, Weekends,
Holidays);
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("Creates one Grid resource with name = " + name);
return gridRes;
}
/**
* Prints the Gridlet objects
* @param list list of Gridlets
*/
private static void printGridletList(GridletList list, String name)
{
Page 63
int size = list.size();
Gridlet gridlet;
String indent = " ";
System.out.println();
System.out.println("========== OUTPUT for " + name + " ==========");
System.out.println("Gridlet ID" + indent + "STATUS" + indent +
"Resource ID" + indent + "Cost");
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.print("SUCCESS");
System.out.println( indent + indent + gridlet.getResourceID() +
indent + indent + gridlet.getProcessingCost() );
}
}
} // end class
Example 7
kage gridsim.example08;
Page 64
/*
* Author Anthony Sulistio
* Date: December 2003
* Description: A simple program to demonstrate of how to create your own
* allocation policy.
*
* NOTE: The values used from this example are taken from the GridSim paper.
* http://www.gridbus.org/gridsim/
* $Id: Example8.java,v 1.3 2004/05/29 05:53:28 anthony Exp $
*/
import java.util.*;
import gridsim.*;
/**
* Example8 class
*/
class Example8 extends GridSim
{
private Integer ID_;
private String name_;
private GridletList list_;
private GridletList receiveList_;
private int totalResource_;
Page 65
/**
* Allocates a new Example8 object
* @param name the Entity name of this object
* @param baud_rate the communication speed
* @param total_resource the number of grid resources available
* @throws Exception This happens when creating this entity before
* initializing GridSim package or the entity name is
* <tt>null</tt> or empty
* @see gridsim.GridSim#Init(int, Calendar, boolean, String[], String[],
* String)
*/
Example8(String name, double baud_rate, int total_resource, int numGridlet)
throws Exception
{
super(name, baud_rate);
this.name_ = name;
this.totalResource_ = total_resource;
this.receiveList_ = new GridletList();
// Gets an ID for this entity
this.ID_ = new Integer( getEntityId(name) );
System.out.println("Creating a grid user entity with name = " +
name + ", and id = " + this.ID_);
// Creates a list of Gridlets or Tasks for this grid user
this.list_ = createGridlet(this.ID_.intValue(), numGridlet);
System.out.println(name + ":Creating "+ this.list_.size() +
Page 66
" Gridlets");
}
/**
* The core method that handles communications among GridSim entities.
*/
public void body()
{
int resourceID[] = new int[this.totalResource_];
double resourceCost[] = new double[this.totalResource_];
String resourceName[] = new String[this.totalResource_];
LinkedList resList;
ResourceCharacteristics resChar;
// waiting to get list of resources. Since GridSim package uses
// multi-threaded environment, your request might arrive earlier
// before one or more grid resource entities manage to register
// themselves to GridInformationService (GIS) entity.
// Therefore, it's better to wait in the first place
while (true)
{
// need to pause for a while to wait GridResources finish
// registering to GIS
super.gridSimHold(1.0); // hold by 1 second
resList = getGridResourceList();
Page 67
if (resList.size() == this.totalResource_)
break;
else
{
System.out.println(this.name_ +
":Waiting to get list of resources ...");
}
}
// a loop to get all the resources available
int i = 0;
for (i = 0; i < this.totalResource_; i++)
{
// Resource list contains list of resource IDs not grid resource
// objects.
resourceID[i] = ( (Integer)resList.get(i) ).intValue();
// Requests to resource entity to send its characteristics
send(resourceID[i], GridSimTags.SCHEDULE_NOW,
GridSimTags.RESOURCE_CHARACTERISTICS, this.ID_);
// waiting to get a resource characteristics
resChar = (ResourceCharacteristics) receiveEventObject();
resourceName[i] = resChar.getResourceName();
resourceCost[i] = resChar.getCostPerSec();
System.out.println(this.name_ +
Page 68
":Received ResourceCharacteristics from " +
resourceName[i] + ", with id = " + resourceID[i]);
}
/////////////////////////////////////////////////////
// SUBMITS Gridlets
Gridlet gridlet = null;
String info;
// a loop to get one Gridlet at one time and sends it to a random grid
// resource entity. Then waits for a reply
int id = 0;
boolean success = false;
for (i = 0; i < this.list_.size(); i++)
{
gridlet = (Gridlet) this.list_.get(i);
info = "Gridlet_" + gridlet.getGridletID();
System.out.println(this.name_ + ":Sending " + info + " to " +
resourceName[id] + " with id = " + resourceID[id] +
" at time = " + GridSim.clock() );
// Sends one Gridlet to a grid resource specified in "resourceID"
// Gridlets with even numbers are sent and required an ack
if (i % 2 == 0)
Page 69
{
success = gridletSubmit(gridlet, resourceID[id], 0.0, true);
System.out.println("Ack = " + success);
System.out.println();
}
// Gridlets with odd numbers are sent but not required an ack
else {
success = gridletSubmit(gridlet, resourceID[id], 0.0, false);
}
}
//////////////////////////////////////////////////
// RECEIVES Gridlets
super.gridSimHold(20);
System.out.println("<<<<<< pauses for 20 seconds >>>>>>>>");
// A loop to receive all the Gridlets back
for (i = 0; i < this.list_.size(); i++)
{
// waiting to receive a Gridlet back from resource entity
gridlet = (Gridlet) super.receiveEventObject();
System.out.println(this.name_ + ":Receiving Gridlet " +
gridlet.getGridletID() );
Page 70
// stores the received Gridlet into a new GridletList object
this.receiveList_.add(gridlet);
}
// shut down this simulation
shutdownUserEntity();
terminateIOEntities();
System.out.println(this.name_ + ":%%%% Exiting body()");
}
/**
* Gets the list of Gridlets
* @return a list of Gridlets
*/
public GridletList getGridletList() {
return this.receiveList_;
}
/**
* This method will show you how to create Gridlets with and without
* GridSimRandom class.
* @param userID the user entity ID that owns these Gridlets
* @return a GridletList object
*/
private GridletList createGridlet(int userID, int numGridlet)
{
// Creates a container to store Gridlets
Page 71
GridletList list = new GridletList();
int data[] = { 900, 600, 200, 300, 400, 500, 600 };
int size = 0;
if (numGridlet >= data.length) {
size = 6;
}
else {
size = numGridlet;
}
for (int i = 0; i < size; i++)
{
Gridlet gl = new Gridlet(i, data[i], data[i], data[i]);
gl.setUserID(userID);
list.add(gl);
}
return list;
}
////////////////////// STATIC METHODS //////////////////////////////
/**
* Creates main() to run this example
Page 72
*/
public static void main(String[] args)
{
System.out.println("Starting Example8");
try
{
// First step: Initialize the GridSim package. It should be called
// before creating any entities. We can't run this example without
// initializing GridSim first. We will get run-time exception
// error.
int num_user = 1; // number of grid users
Calendar calendar = Calendar.getInstance();
boolean trace_flag = true; // mean trace GridSim events
// list 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;
// Initialize the GridSim package
GridSim.init(num_user, calendar, trace_flag, exclude_from_file,
Page 73
exclude_from_processing, report_name);
// Second step: Creates one or more GridResource objects
NewPolicy test = new NewPolicy("GridResource_0", "NewPolicy");
GridResource resTest = createGridResource("GridResource_0", test);
// Third step: Creates grid users
int total_resource = 1;
int numGridlet = 4;
double bandwidth = 1000.00;
Example8 user0 = new Example8("User_0", bandwidth, total_resource,
numGridlet);
// Fourth step: Starts the simulation
GridSim.startGridSimulation();
// Final step: Prints the Gridlets when simulation is over
GridletList newList = null;
newList = user0.getGridletList();
printGridletList(newList, "User_0");
System.out.println("Finish Example8");
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Unwanted errors happen");
}
Page 74
}
/**
* 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 one Grid resource with three
* Machines that contains one or more PEs.
* @param name a Grid Resource name
* @return a GridResource object
*/
private static GridResource createGridResource(String name, AllocPolicy obj)
{
// 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. Create one Machine with its id, number of PEs and MIPS rating per PE
// In this example, we are using a resource from
// hpc420.hpcc.jp, AIST, Tokyo, Japan
// 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.
int mipsRating = 377;
mList.add( new Machine(0, 4, mipsRating)); // First Machine
Page 75
// 3. Repeat the process from 2 if we want to create more Machines
// In this example, the AIST in Japan has 3 Machines with same
// MIPS Rating but different PEs.
// NOTE: if you only want to create one Machine for one Grid resource,
// then you could skip this step.
mList.add( new Machine(1, 4, mipsRating)); // Second Machine
mList.add( new Machine(2, 2, mipsRating)); // Third Machine
// 4. 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"; // system architecture
String os = "Solaris"; // operating system
double time_zone = 9.0; // time zone this resource located
double cost = 3.0; // the cost of using this resource
ResourceCharacteristics resConfig = new ResourceCharacteristics(
arch, os, mList,
//ResourceCharacteristics.OTHER_POLICY_SAME_RATING,
ResourceCharacteristics.SPACE_SHARED,
time_zone, cost);
// 5. Finally, we need to create a GridResource object.
double baud_rate = 100.0; // communication speed
long seed = 11L*13*17*19*23+1;
Page 76
double peakLoad = 0.0; // the resource load during peak hour
double offPeakLoad = 0.0; // the resource load during off-peak hr
double holidayLoad = 0.0; // the resource load during holiday
// incorporates weekends so the grid resource is on 7 days a week
LinkedList Weekends = new LinkedList();
Weekends.add(new Integer(Calendar.SATURDAY));
Weekends.add(new Integer(Calendar.SUNDAY));
// incorporates holidays. However, no holidays are set in this example
LinkedList Holidays = new LinkedList();
GridResource gridRes = null;
try
{
ResourceCalendar resCalendar = new ResourceCalendar(time_zone,
peakLoad, offPeakLoad, holidayLoad, Weekends,
Holidays, seed);
gridRes = new GridResource(name, baud_rate, resConfig,
resCalendar, obj);
}
catch (Exception e) {
System.out.println("msg = " + e.getMessage() );
}
System.out.println("Creates one Grid resource with name = " + name);
return gridRes;
Page 77
}
/**
* Prints the Gridlet objects
* @param list list of Gridlets
*/
private static void printGridletList(GridletList list, String name)
{
int size = list.size();
Gridlet gridlet = null;
String indent = " ";
System.out.println();
System.out.println("============= OUTPUT for " + name + " ==========");
System.out.println("Gridlet ID" + indent + "STATUS" + indent +
"Resource ID" + indent + "Cost");
// a loop to print the overall result
int i = 0;
for (i = 0; i < size; i++)
{
gridlet = (Gridlet) list.get(i);
System.out.print(indent + gridlet.getGridletID() + indent
+ indent);
System.out.print( gridlet.getGridletStatusString() );
Page 78
System.out.println( indent + indent + gridlet.getResourceID() +
indent + indent + gridlet.getProcessingCost() );
}
// a loop to print each Gridlet's history
for (i = 0; i < size; i++)
{
gridlet = (Gridlet) list.get(i);
System.out.println( gridlet.getGridletHistory() );
System.out.print("Gridlet #" + gridlet.getGridletID() );
System.out.println(", length = " + gridlet.getGridletLength()
+ ", finished so far = " +
gridlet.getGridletFinishedSoFar() );
System.out.println("===========================================\n");
}
}
} // end class
Example 8
package gridsim.example09;
/*
* Author Anthony Sulistio
* Date: May 2004
* Description:
Page 79
* This example demonstrates how to create and to define your own
* GridResource and GridInformationService entity.
* Please read NewGridResource.java and NewGIS.java for more
* detailed explanations on how to do it.
*
* The scenarios of this example are:
* - initializing GridSim and creating grid user and resource entities
* - starting the simulation
* - a grid resource entity registers new tags to a GridInformationService
* entity
* - a grid user entity sends new tags to a grid resource entity
* - finally exit the simulation
*
* NOTE: The values used from this example are taken from the GridSim paper.
* http://www.gridbus.org/gridsim/
* GridSim version 3.0 or above is needed to run this example.
*
* $Id: Example9.java,v 1.2 2004/05/29 06:53:33 anthony Exp $
*/
import java.util.*;
import gridsim.*;
/**
* Example9 class.
*/
Page 80
class Example9 extends GridSim
{
// constant variables
public static final int HELLO = 900;
public static final int TEST = 901;
private Integer ID_; // entity ID of this object
private String name_; // entity name of this object
private int totalResource_; // number of grid resources avaiable
/**
* Allocates a new grid user entity
* @param name the Entity name of this object
* @param baud_rate the communication speed
* @param total_resource the number of grid resources available
* @throws Exception This happens when creating this entity before
* initializing GridSim package or the entity name is
* <tt>null</tt> or empty
* @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[],
* String)
*/
Example9(String name, double baud_rate, int total_resource)
throws Exception
{
super(name, baud_rate);
this.name_ = name;
Page 81
this.totalResource_ = total_resource;
// Gets an ID for this entity
this.ID_ = new Integer( getEntityId(name) );
System.out.println("Creating a grid user entity with name = " +
name + ", and id = " + this.ID_);
}
/**
* The core method that handles communications among GridSim entities.
*/
public void body()
{
int resourceID[] = new int[this.totalResource_];
String resourceName[] = new String[this.totalResource_];
LinkedList resList;
ResourceCharacteristics resChar;
// waiting to get list of resources. Since GridSim package uses
// multi-threaded environment, your request might arrive earlier
// before one or more grid resource entities manage to register
// themselves to GridInformationService (GIS) entity.
// Therefore, it's better to wait in the first place
while (true)
{
// need to pause for a while to wait GridResources finish
Page 82
// registering to GIS
super.gridSimHold(1.0); // hold by 1 second
resList = getGridResourceList();
if (resList.size() == this.totalResource_)
break;
else
{
System.out.println(this.name_ +
":Waiting to get list of resources ...");
}
}
int SIZE = 12; // size of Integer object is roughly 12 bytes
int i = 0;
// a loop to get all the resources available.
// Once the resources are known, then send HELLO and TEST tag to each
// of them.
for (i = 0; i < this.totalResource_; i++)
{
// Resource list contains list of resource IDs not grid resource
// objects.
resourceID[i] = ( (Integer) resList.get(i) ).intValue();
// Requests to resource entity to send its characteristics
// NOTE: sending directly without using I/O port
Page 83
super.send(resourceID[i], GridSimTags.SCHEDULE_NOW,
GridSimTags.RESOURCE_CHARACTERISTICS, this.ID_);
// waiting to get a resource characteristics
resChar = (ResourceCharacteristics) receiveEventObject();
resourceName[i] = resChar.getResourceName();
// print that this entity receives a particular resource
// characteristics
System.out.println(this.name_ +
":Received ResourceCharacteristics from " +
resourceName[i] + ", with id = " + resourceID[i]);
// send TEST tag to a resource using I/O port.
// It will consider transfer time over a network.
System.out.println(this.name_ + ": Sending TEST tag to " +
resourceName[i] + " at time " + GridSim.clock());
super.send( super.output, GridSimTags.SCHEDULE_NOW, TEST,
new IO_data(this.ID_, SIZE, resourceID[i]) );
// send HELLO tag to a resource using I/O port
System.out.println(this.name_ + ": Sending HELLO tag to " +
resourceName[i] + " at time " + GridSim.clock());
super.send( super.output, GridSimTags.SCHEDULE_NOW, HELLO,
new IO_data(this.ID_, SIZE, resourceID[i]) );
}
Page 84
// need to wait for 10 seconds to allow a resource to process
// receiving events.
super.sim_pause(10);
// shut down all the entities, including GridStatistics entity since
// we used it to record certain events.
shutdownGridStatisticsEntity();
shutdownUserEntity();
terminateIOEntities();
System.out.println(this.name_ + ":%%%% Exiting body()");
}
////////////////////// STATIC METHODS //////////////////////////////
/**
* Creates main() to run this example
*/
public static void main(String[] args)
{
System.out.println("Starting Example9");
try
{
// First step: Initialize the GridSim package. It should be called
// before creating any entities. We can't r#Iun this example without
// initializing GridSim first. We will get run-time exception
// error.
Page 85
int num_user = 1; // number of grid users
Calendar calendar = Calendar.getInstance();
boolean trace_flag = true; // true means trace GridSim events
// Initialize the GridSim package
// Starting from GridSim 3.0, you can specify different type of
// initialisation.
System.out.println("Initializing GridSim package");
// In this example, initialise GridSim without creating
// a default GridInformationService (GIS) entity.
GridSim.init(num_user, calendar, trace_flag, false);
// Create a new GIS entity
NewGIS gis = new NewGIS("NewGIS");
// You need to call this method before the start of simulation
GridSim.setGIS(gis);
// Second step: Creates one or more grid resource entities
NewGridResource resource0 = createGridResource("Resource_0");
int total_resource = 1;
// Third step: Creates one or more grid user entities
Example9 user0 = new Example9("User_0", 560.00, total_resource);
// Fourth step: Starts the simulation
Page 86
GridSim.startGridSimulation();
System.out.println("Finish Example9");
}
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 one Grid resource with three
* Machines that contains one or more PEs.
* @param name a Grid Resource name
* @return a GridResource object
*/
private static NewGridResource createGridResource(String name)
{
// 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();
Page 87
// 2. Create one Machine with its id, number of PEs and MIPS rating per PE
// In this example, we are using a resource from
// hpc420.hpcc.jp, AIST, Tokyo, Japan
// 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.
int mipsRating = 377;
mList.add( new Machine(0, 4, mipsRating)); // First Machine
// 3. 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"; // system architecture
String os = "Solaris"; // operating system
double time_zone = 9.0; // time zone this resource located
double cost = 3.0; // the cost of using this resource
ResourceCharacteristics resConfig = new ResourceCharacteristics(
arch, os, mList, ResourceCharacteristics.SPACE_SHARED,
time_zone, cost);
// 4. Finally, we need to create a GridResource object.
double baud_rate = 100.0; // communication speed
long seed = 11L*13*17*19*23+1;
double peakLoad = 0.0; // the resource load during peak hour
double offPeakLoad = 0.0; // the resource load during off-peak hr
Page 88
double holidayLoad = 0.0; // the resource load during holiday
// incorporates weekends so the grid resource is on 7 days a week
LinkedList Weekends = new LinkedList();
Weekends.add(new Integer(Calendar.SATURDAY));
Weekends.add(new Integer(Calendar.SUNDAY));
// incorporates holidays. However, no holidays are set in this example
LinkedList Holidays = new LinkedList();
ResourceCalendar calendar = new ResourceCalendar(time_zone, peakLoad,
offPeakLoad, holidayLoad, Weekends, Holidays, seed);
NewGridResource gridRes = null;
try
{
// NOTE: The below code creates a NewGridResource object
// instead of its parent class.
gridRes = new NewGridResource(name, baud_rate, resConfig, calendar,
null);
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("Creates one Grid resource with name = " + name);
return gridRes;