Access Keys:
Skip to content (Access Key - 0)

Computing Pi using Exclusive Tasks

Introduction

To introduce exclusive tasks we will create an application to compute Pi. We will use the Monte Carlo algorithm to generate input values. The values are generated by creating a square then placing a circle within the square. Point selection in the square is calculated using a random number generator. The total number points within the circle is multiplied by 4 and then divided by the total number of points in the square. The result is an approximation of Pi.

You may create your own fabric application and client application, or you may use the files and projects provided in tutorial_samples.zip and tutorial_samples.tgz archives. These files and projects are not included in the Fabric Software Development Kit (SDK) installation, but will be available for later versions. The fabric APIs are not dependent on a particular build tool or development environment (IDE), so you may use your preferred build tools. For your convenience, we have included project files for several platforms. These can be seen in the table to the right.

In this tutorial we will

  • describe an exclusive task
  • compare exclusive tasks to other types of fabric tasks
  • explain an exclusive task's life cycle
  • create an exclusive task
  • configure an exclusive task
  • create a simple client
  • submit requests to the fabric
 Projects Provided For Tutorial Samples
Language Platform
Project Type
Java 1.5
All
Eclipse
Java 1.5
All
Ant scripts
.NET 2.0
Windows
Visual Studio 2005
'C'
Windows Visual Studio 2005
'C' Linux
Make scripts
Unknown macro: {switcher}


What our computing Pi application will do

  • client submits a request to the fabric to execute application's process flow
  • the process flow calls the Monte Carlo Pi task
  • the Monte Carlo Pi task calculates data points and returns the results to the process flow
  • the process flow returns the request to the client
  • the client receives the calculated data points in the completed request
  • client computes Pi using calculated data points and prints the computed Pi to the console
    To learn all the steps for building, packaging, and deploying your own fabric application and client,
    you should reference the Hello World with Process Flows and Tasks tutorial

Monte Carlo Pi Fabric Application

Exclusive task

An exclusive task is any task that you want to restrict the number of instances running in the fabric per worker or per CPU. This allows you to control the number of instances of a specific task running in the fabric. For example, you may have a task that very resource hungry (CPU, memory, or disk intensive for example) and want to restrict how many instances of this code can run at a given time on individual fabric workers. Another example would be if your task code or some underlying library is not thread-safe and you want to ensure that only single copies of the task are not run concurrently.  Classifying the task as exclusive ensures only N instances of that task runs per worker or per CPU.

How do I decide which type of task to choose for my task code?

The following are some general guidelines: 

  • if your task code and its underlying libraries is thread safe, short running, and not resource hungry (CPU, memory, or disk intensive for example) then configure your code as an unlimited task
  • if your task code needs to use a restricted resource, for example you have a limited number of licensed database connections, use a limited task
  • if your task code or its underlying libraries are not thread safe, or your task code will be long running (more than a couple of seconds or more), or is resource hungry (CPU, memory, or disk intensive for example), then configure your code as an exclusive task
  • if you need to run code in the fabric that does not need to service fabric requests from a client, but perhaps needs to monitor a socket, receive work from a fabric accessible memory (FAM) queue, or poll some outside service or database, then configure and model your code to run as a background task.

The fabric is capable of running multiple, concurrent instances of a task on a worker or CPU. However, this is not always desirable. Appistry EAF allows you to configure a task by classifying it as one of four types and adding parameters based on the task type. The table below provides a basic overview of the four task types. For in depth explanations, refer to the appropriate task's tutorial.

Task Type Characteristics Configuration Tutorial
Unlimited
  • No restrictions
  • Multiple instances may run concurrently on multiple workers in the fabric
  • Multiple requests for the task are serviced concurrently depending on available memory and CPU
Task XML file Hello World with Process Flows and tasks
Limited
  • Only a specified number of instances may run concurrently in the fabric (for example, a limited task may be configured to run on 2 out of 10 workers)
  • A worker may run multiple instances of the task
  • Multiple requests for the task are serviced concurrently depending on configuration, available memory, and CPU
Task XML file  
Exclusive
  • Only a specified number of instances may run concurrently per worker or per CPU (for example, an exclusive task will run on every worker but be restricted to only one instance at a time based on worker or CPU)
  • The fabric performs load control to balance requests for exclusive tasks across the fabric
  • When multiple requests for an exclusive task arrive on a single worker, the requests are executed in a gated fashion. For example, if exclusive tasks are configured to allow two concurrent per worker, then two requests are serviced at a time while other requests wait their turn.
  • Good for long running tasks (several seconds or more) or CPU intensive
Task XML file and fabric.cfg
Computing Pi using Exclusive Tasks tutorial
Background
  • Runs constantly waiting for work (like a unix daemon or Windows service)
  • Background tasks are not called from a process flow as part of a client request, but rather get "work" as defined by the task implementer. For example, the background task might monitor a queue in fabric accessible memory (FAM), read from a socket. or poll a service external to the fabric.
  • Since it is not part of a fabric request, background tasks do not have the same inherent reliability as the other task types
Task XML file  

Exclusive task configuration

A specific task is classified as exclusive in its task XML definition file.

However, exclusive task settings are defined fabric wide. Exclusive task configuration consist of two settings in the fabric.cfg file: exclusive-task-type and exclusive-task-count. The exclusive-task-type specifies whether the number instances running is based on worker or CPU. This parameter has two possible values: per-machine and per-cpu. The per-machine value indicates a worker can run the configured number of exclusive tasks. The per-cpu value indicates a worker can run the configured number of exclusive tasks on each CPU. The exclusive-task-count parameter specifies the maximum number of exclusive tasks that may run concurrently on a worker or CPU.

The exclusive-task-type and exclusive-task-count are fabric-wide settings. All exclusive tasks running in the fabric use the same exclusive-task-type and exclusive-task-count settings. The fabric does not support individually configured exclusive tasks.

For example, if you have a fabric of 50 single CPU workers and 50 dual CPU workers and the exclusive task type is per-machine and the exclusive task count is 2, the maximum number of exclusive tasks that may run at the same time is 200, 2 per worker. If the exclusive task type is per-cpu and the exclusive task count is 2, the maximum number of exclusive tasks that may run at the same time is 300, 2 per each single CPU worker and 4 per each dual CPU worker.

Modify the fabric.cfg file

To ensure you are modifying a current version of the fabric.cfg file, obtain a copy directly from the fabric using Fabric Control.

  1. To retrieve a copy of the fabric.cfg file currently in the fabric, run the following command with the correct fabric administrative user, password, and fabric address. Fabric Control stores a copy of the fine in the current working directory.
    fabric_ctl -d 239.255.0.1:30000 -u fabric-admin/fabric-admin get fabric.cfg
    
  2. Open the fabric.cfg file in a text editor. Below is an example this file.
    version=1.0
    exclusive-task-type=per-machine
    exclusive-task-count=1
    concurrent-shutdown-threshold=2
    auto-start-worker=true
    split-region-threshold=20
    max-request-handler-count=1000
    min-request-handler-count=100
    recovery-store-purge-interval=24
    recovery-store-request-ttl=24
    mcast-ttl=1
    request-handler-percentage=100
    request-service-ssl-enabled=false
    request-service-port=16000
    request-service-ssl-port=16001
    admin-server-port=0
    #message-logger-group=239.255.0.1
    #message-logger-port=4000
    action-on-reboot=just-reboot
    exclusive-task-count=1
    exclusive-task-type=per-machine
    guaranteed-non-fam-memory=0
    
  1. If the exclusive-task-type parameter is not in the file, add it to the bottom of the list with a value of per-machine or per-cpu. For example, exclusive-task-type=per-machine.
  2. If the exclusive-task-count parameter is not in the file, add it to the bottom of the list with a numerical value. For example, exclusive-task-count=1.
  3. Increase the version number.
    Version numbers
    All version numbers are right justified and zero filled. When comparing version 1.2 with 1.19, the application fabric actually compares 1.02 with 1.19. Version 1.19 is considered newer than version 1.2. Also, when comparing version 1.0.0 with 1.0 and 1, 1.0.0 is considered newer than version 1.0 and 1.0 is considered newer than version 1.
  4. Save the file and close the text editor.
  5. To deploy the modified fabric.cfg file, run the following command with the correct fabric administrative user, password, and fabric address.
    fabric_ctl -d 239.255.0.1:30000 -u fabric-admin/fabric-admin deploy fabric.cfg
    

Monte Carlo Pi task

Creating the code for an exclusive task is the same as creating the code in the Hello World tutorial.

Monte Carlo Pi Task XML Definition

The Monte Carlo Pi Task XML Definition file is almost identical to the Hello World Task XML Definition file. The main difference is that the Task element has two additional attributes: type and busy-wait-timeout.

Attribute Description
type The type attribute identifies the task's classification. The possible values are unlimited, limited, exclusive, and background. 
busy-wait-timeout The busy-wait-timeout attribute indicates the amount of time, in milliseconds, the fabric waits for an available worker when workers running exclusive tasks are busy.

For this XML file, add the type attribute with a value of exclusive to the Task element as illustrated below.

Read more about task XML definitions under Task Module Definition XML.

Monte Carlo Pi Process Flow and Application XML Definitions

The Monte Carlo Pi Process Flow XML Definition and Monte Carlo Pi Application XML Definition files are similar to Hello World Process Flow XML Definition and Hello World Application XML Definition files. They are illustrated below.

Java and .NET process flow definition file

Read more about process flows under Process Flow Definition XML.

  monte_carlo_pi_java_flow (in tutorial_samples/<java|dotnet|cpp>/monte_carlo_pi/>/monte_carlo_pi/)

1. <?xml version="1.0"?>
2. <!DOCTYPE process SYSTEM "ProcessDefinition.dtd">
3. <process name="monte_carlo_pi_flow" default_retries="0" default_timeout="30">
4.     <state name="start">
5.         <task name="computeMonteCarloPiPoints"/>
6.         <arc task-status="complete" next-state="finish"/>
7.     </state>
8. </process>

Java and .NET application definition file

Read more about application definitions under Fabric Application Definition XML.

Building, packaging and deploying the Monte Carlo Pi Fabric Application

Now we must build, package, and deploy our fabric application. Follow the instructions in the information box below, and then return to this tutorial.

The Building, Packaging, and Deploying a Fabric Application tutorial gives directions for building, packaging and deploying fabric applications.

Simple Monte Carlo Pi Fabric Client

We have provided a simple, blocking client for testing your Monte Carlo Pi fabric application. This client is very similar to the Hello World Fabric Client.

"Okay, where's the real Monte Carlo Pi fabric client?"
To really make use of our new exclusive task, we need a client that can push enough requests into the fabric to drive up utilization on our workers. Our simple Monte Carlo Pi client listed here won't do that. To really put the Monte Carlo Pi fabric application through it's paces, we have provided the [Multi-threaded, Asynchronous Client Tutorial] (coming soon!). Test your deployed Monte Carlo Pi fabric application using the simple client here, and then go over and put your fabric through its paces in the [Multi-threaded, Asynchronous Client Tutorial] (coming soon!).

The expected output from our client's call to the fabric application should be similar to that below (the values will vary).

Pi computed using Monte Carlo method
Total points: 6000000
Valid points: 4712959
PI: 3.1419726666666667


Running the Monte Carlo Pi Fabric Client and Application

Before running the Monte Carlo Pi client, verify the following.

  • The fabric is running. If the fabric is not running, refer to Installing the Fabric for instructions.
  • The Log Monitor is running in a separate window or terminal. To start the Log Monitor run the following command. The MCAST address must match the fabric-address set in the addr.cfg file.
    log_monitor 239.255.0.1:4000
    _
    

Run the client

Run the Monte Carlo Pi client. The following output appears:
 
 

Mixing it up
It is entirely possible to call the one language version of the Monte Carlo Pi fabric application using a different language version of the client. For example, you can use the Java client to call the 'C' version of the fabric application. Just change the Java client to use the "monte_carlo_pi_cpp_app" fabric application instead of the "monte_carlo_pi_java_app" version.

This is made possible by the fact that our application only uses primitive data types. If you have a fabric client and application that passes objects (for example Java client passing Java objects to/from a Java-based fabric application), then this would, of course, not work.


When using Java and Ant
You can issue the command "ant run" to execute the client.