?? processor.cs
字號:
{
Array.Copy(array2, j, result, k, array2.Length - j);
}
return result;
}
/// <summary>
/// Partition the stored datas with main cells.
/// </summary>
public void Partition()
{
Logger.getInstance().debug(this.Name + " wait for the selection of main cells!");
mainCellReady.WaitOne();
Logger.getInstance().debug(this.Name + " begins to partition!");
this.partitionFlags = new int[this.ProcessorNumbers];
if (this.localData[0] > mainCell[0])
this.partitionFlags[0] = -1;
else
this.partitionFlags[0] = 0;
int i = 1;
int j = 0;
for (; i < this.partitionFlags.Length; i++)
{
for (; j < this.localData.Length; j++)
{
if (this.localData[j] > mainCell[i - 1])
{
this.partitionFlags[i] = j;
break;
}
}
if (j == this.localData.Length)
this.partitionFlags[i] = this.localData.Length;
}
Logger.getInstance().debug(this.Name + " has finished the partition!");
myGlobalChange.Release(this.ProcessorNumbers);
}
/// <summary>
/// The processor thread will run this method.
/// </summary>
public void Run()
{
this.initlize();
//local sort...
Logger.getInstance().debug(this.Name + " : " + "begin to sort...");
this.LocalSort();
Logger.getInstance().debug(this.Name + " : " + "first sort is completed!");
bool needToSortSamples = false;
lock (syncObj)
{
if (SamplesNotHandled)
{
needToSortSamples = true;
SamplesNotHandled = false;
}
}
if (needToSortSamples)
{
Logger.getInstance().debug(this.Name + " is going to selected samples!");
//get samples
int[] samples = new int[this.ProcessorNumbers * this.ProcessorNumbers];
for (int i = 0; i < this.ProcessorNumbers; i++)
{
Processor p = (Processor)processors[i];
Array.Copy(p.GetSamples(), 0, samples, i * this.ProcessorNumbers, this.ProcessorNumbers);
}
Logger.getInstance().debug(this.Name + " begins to sort samples!");
samples = this.Sort(samples);//sort samples
//select main numbers
Logger.getInstance().debug(this.Name + " begins to select main cells!");
for (int i = 0; i < mainCell.Length; i++)
{
mainCell[i] = samples[this.ProcessorNumbers * (i + 1)];
}
Logger.getInstance().debug("Main cells have be selected by " + this.Name + "!");
mainCellReady.Release(this.ProcessorNumbers);
}
//partition the array
this.Partition();
//global change
this.GlobalExchangeAndSort();
//
}
public int[] Sort(int[] array)
{
int[] result = this.Sort(array, 0, array.Length);
return result;
}
public int[] Sort(int[] array, int start, int length)
{
if (length < 0)
{
throw new ArgumentOutOfRangeException("Argument \"length\" must be greater than 0!");
}
if (start < 0 || start >= array.Length)
{
throw new ArgumentOutOfRangeException("Argument \"start\" is out of range!");
}
if (start + length > array.Length)
length = array.Length - start;
if (length > 4)
{
int[] a1 = Sort(array, start, length / 2);
int[] a2 = Sort(array, start + length / 2, length - length / 2);
a1 = this.MergeSort(a1, a2);
return a1;
}
else
{
int[] result = new int[length];
Array.Copy(array, start, result, 0, length);
for (int i = 0; i < length; i++)
{
int min = i;
for(int j = (i + 1); j < length; j++)
{
if (result[j] < result[min])
{
min = j;
}
}
if (min != i)
{
int temp = result[i];
result[i] = result[min];
result[min] = temp;
}
}
return result;
}
}
#region Properties
public string Name
{
get
{
return "Processor " + this.SegmentNumber.ToString();
}
}
public static bool SamplesNotHandled
{
get
{
return mySamplesNotHandled;
}
set
{
mySamplesNotHandled = value;
}
}
public int SegmentNumber
{
get
{
return mySegmentNumber;
}
set
{
this.mySegmentNumber = value;
}
}
public int Start
{
get
{
return countPerProcessor * mySegmentNumber;
}
}
public int Length
{
get
{
if (this.mySegmentNumber == (this.ProcessorNumbers - 1))
{
return (this.myOriginalData.Length - this.Start);
}
else
{
return countPerProcessor;
}
}
}
public int End
{
get
{
return (this.Start + this.Length);
}
}
public int ProcessorNumbers
{
get
{
return processors.Length;
}
}
public static int[] SortedDatas
{
get
{
return myGlobalSortedData.GetArray();
}
}
public AutoResetEvent GlobalSortedDataOk;
#endregion
//
private int mySegmentNumber;//This feilds represents the segment number of this processor.
private AutoResetEvent mySamplesReady;//This is used to indicated wether samples are ready to be selected.
private Semaphore myGlobalChange;//This is used to enable golobal exchanges.
private int[] myOriginalData;//This is used to store the datas to be sored.
private static MyArrayList<int> myGlobalSortedData; //This is used to store the datas sored finally.
private int[] localData;//This is used to store the datas after the first sore.
private int[] partitionFlags;//This is used to store the indexes of each partition that is divided by main cells.
public static Processor[] processors;//All processors will be stored here.
private static bool mySamplesNotHandled = true;//This feild will indicate whether samples has been sorted : true if sorted, false if not.
private static object syncObj = new object(); //This is used to control exclusive access to some feilds of this class.(eg. SamplesNotHandled)
private static int[] mainCell;//Main cells.
private static Semaphore mainCellReady;//This indicated whether main cell are ready.
private static int countPerProcessor = -1;//This stores how much numbers shoud be processed by this processor.
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -