- legionvs2008silverlight2rtm.zip
- Documentation
- Libraries
- Clog
- Orpius.Logging.dll
- Orpius.Logging.Log4NetLogStrategy.dll
- Orpius.Logging.Log4NetLogStrategy.pdb
- Orpius.Logging.pdb
- Silverlight
- Orpius.Logging.Silverlight.dll
- Orpius.Logging.Silverlight.pdb
- Orpius.Logging.Silverlight.UI.dll
- Orpius.Logging.Silverlight.UI.pdb
- LineGraph
- Decav.Windows.Controls.LineGraph.dll
- Decav.Windows.Controls.LineGraph.pdb
- Log4Net
- Open Legion Solution.bat
- Source
- GridComputing
- GridComputing.sln
- GridComputing.suo
- GridComputing
- GridManager
- GridManagerWpf
- SilverlightAgent
- Website
- Libraries
- Decav.Windows.Controls.LineGraph
- LineGraph
- Tasks
- MutexExample
- Client
- Server
- PrimeFinder
- Client
- Server
- legionvs2008beta2.zip
|
/*
<File>
<Copyright>Copyright © 2007, Daniel Vaughan. All rights reserved.</Copyright>
<License see="prj:///Documentation/License.txt"/>
<Owner Name="Daniel Vaughan" Email="dbvaughan@gmail.com"/>
<CreationDate>2007-12-13 21:52:15Z</CreationDate>
<LastSubmissionDate>$Date: $</LastSubmissionDate>
<Version>$Revision: $</Version>
</File>
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Web;
using Orpius.Logging;
namespace Orpius.GridComputing.Tasks
{
/// <summary>
/// The server-side implementation of the Prime Finder task.
/// This task dispatches ranges of numbers to be searched
/// by slave agents for prime numbers.
/// </summary>
class PrimeFinderMaster : MasterTask
{
/// <summary>
/// Once we reach this, we are done.
/// </summary>
const long searchCeiling = 100000 * 500;//long.MaxValue;
/// <summary>
/// This is the range size sent to agents.
/// </summary>
const int rangeSize = 100000;
long taskCounter;
/// <summary>
/// The results the agents send back are stored here.
/// </summary>
readonly List<long> primeList = new List<long>();
readonly object primeListLock = new object();
/// <summary>
/// The highest part of the search range
/// that we have dispatched so far.
/// </summary>
long searchUpper;
/// <summary>
/// A list of client ids that have been tasked.
/// </summary>
readonly List<long> runningTasks = new List<long>();
readonly object runningLock = new object();
bool allTasksDispatched;
public PrimeFinderMaster()
{
StepsGoal = searchCeiling;
Starting += PrimeFinderTask_Starting;
Stopping += PrimeFinderTask_Stopping;
}
void PrimeFinderTask_Starting(object sender, EventArgs e)
{
/* Perform tasks, before we begin, here. */
Log.Info("PrimeFinderTask Starting.");
}
void PrimeFinderTask_Stopping(object sender, EventArgs e)
{
/* Perform any cleanup activities here. */
Log.Info("PrimeFinderTask Stopping.");
}
/// <summary>
/// Gets the run data for the <see cref="Agent"/> slave task.
/// This data should encapsulate the task segment
/// that will be worked on by the slave. <seealso cref="Job"/>
/// </summary>
/// <param name="agent">The agent requesting the run data.</param>
/// <returns>The job for the agent to work on.</returns>
public override Job GetJob(IAgent agent)
{
lock (runningLock)
{
if (Completed)
{
throw new InvalidOperationException("Invalid state. Task is completed.");
}
Job data = new Job(taskCounter);
if (!allTasksDispatched)
{ /* Determine a range to be searched. */
data.Start = taskCounter * rangeSize;
if (data.Start < searchCeiling)
{
long proposedUpper = data.Start + rangeSize;
long actualUpper = proposedUpper < searchCeiling ? proposedUpper : searchCeiling;
data.End = actualUpper;
searchUpper = data.End;
if (!runningTasks.Contains(taskCounter))
{
runningTasks.Add(taskCounter);
}
taskCounter++;
return data;
}
else if (runningTasks.Count > 0)
{
taskCounter = 0;
allTasksDispatched = true;
}
}
if (allTasksDispatched)
{ /* Reuse previously dispatched search ranges. */
if (runningTasks.Count > 0)
{
Log.Info("Running Tasks count = " + runningTasks.Count);
/* We should be using an elevator algorithm to find the next task id.
* We will leave this for later. */
taskCounter = runningTasks[0];
data.Start = taskCounter * rangeSize;
Debug.Assert(data.Start <= searchCeiling);
long proposedUpper = data.Start + rangeSize;
long actualUpper = proposedUpper < searchCeiling ? proposedUpper : searchCeiling;
data.End = actualUpper;
return data;
}
}
Completed = true;
throw new InvalidOperationException("Invalid state. Task is completed.");
}
}
/// <summary>
/// Joins the specified task result. This is called
/// when a slave task completes its <see cref="Job"/>,
/// after having called <see cref="GetJob"/>;
/// returning the results to be integrated
/// by the associated <see cref="MasterTask"/>.
/// </summary>
/// <param name="taskResult">The task result.</param>
public override void Join(TaskResult taskResult)
{
lock (runningLock)
{
if (Completed)
{
return;
}
var array = (IEnumerable<long>)taskResult.Result;
foreach (int prime in array)
{
/* We should verify that it's really prime! */
lock (primeListLock)
{
if (!primeList.Contains(prime))
{
primeList.Add(prime);
}
}
}
runningTasks.Remove(taskResult.JobId);
StepsCompleted += rangeSize;
if (runningTasks.Count == 0
&& searchUpper >= searchCeiling)
{ /* Save the results to file. */
string fileName = HttpContext.Current.Server.MapPath("PrimeFinderTaskOutput.txt");
StringBuilder sb = new StringBuilder();
lock (primeListLock)
{
foreach (long prime in primeList)
{
sb.Append(prime);
sb.Append(" ");
}
}
string outputText = sb.ToString();
File.WriteAllText(fileName, outputText);
Completed = true; /* We're done. */
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.
Previously Daniel was a nine-time Microsoft MVP and co-founder of
Outcoder, a Swiss software and consulting company.
Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.
Daniel is the developer behind several acclaimed mobile apps including
Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably
Codon.
Would you like Daniel to bring value to your organisation?
Please contact
Blog |
Twitter
Xamarin Experts
Windows 10 Experts