Пример #1
0
        public void WriteWorkResult(SubJob subJob, bool overwriteResults)
        {
            KPoint       key = subJob.Position;
            SubJobResult val = subJob.SubJobResult;

            // When writing, include the Area's offset.
            KPoint transKey = key.ToGlobal(_position);

            try
            {
                lock (_repoLock)
                {
                    if (Closed)
                    {
                        return;
                    }

                    if (overwriteResults)
                    {
                        _countsRepo.Change(transKey, val);
                    }
                    else
                    {
                        _countsRepo.Add(transKey, val, saveOnWrite: true);
                    }
                }
            }
            catch
            {
                Debug.WriteLine($"Could not write data for x: {key.X} and y: {key.Y}.");
            }
        }
Пример #2
0
        private void WorkProcessor(BlockingCollection <Job> workQueue, BlockingCollection <SubJob> sendQueue, CancellationToken ct)
        {
            while (true)
            {
                try
                {
                    Job job = _workQueue.Take(ct);
                    job.ResetSubJobsRemainingToBeSent();
                    List <KPoint> repoKeys = job.GetRepoKeysForJob();

                    foreach (KPoint key in repoKeys)
                    {
                        SubJob subJob = new SubJob(job, key);
                        HandleSubJob(subJob);
                    }
                }
                catch (TaskCanceledException tce)
                {
                    Console.WriteLine($"JobReplayProcessor got error: {tce.Message}.");
                    break;
                }
                catch (OperationCanceledException oce)
                {
                    Console.WriteLine($"JobReplayProcessor got error: {oce.Message}.");
                    break;
                }
                catch (Exception e)
                {
                    Console.WriteLine($"JobReplayProcessor got error: {e.Message}.");
                    //break;
                }
            }
        }
Пример #3
0
        private void WorkProcessor(BlockingCollection <SubJob> workQueue, BlockingCollection <SubJob> sendQueue, CancellationToken ct)
        {
            while (!ct.IsCancellationRequested)
            {
                try
                {
                    SubJob subJob = _workQueue.Take(ct);

                    SubJobResult        subJobResult  = _resultCache.GetEmptySubJobResult(readZValues: false);
                    SubJobOperationType operationType = DetermineOperationType(subJob, subJobResult);

                    if (operationType != SubJobOperationType.None && operationType != SubJobOperationType.Fetch)
                    {
                        subJob.OperationType = operationType;
                        subJob.SubJobResult  = subJobResult;
                        if (ProcessSubJob(subJob, ct))
                        {
                            _sendQueue.Add(subJob);
                        }
                        else
                        {
                            // Since we are not using this SubJobResult, mark it as free
                            subJob.SubJobResult.IsFree = true;
                        }
                    }
                    else
                    {
                        // Since we are not using this SubJobResult, mark it as free
                        subJobResult.IsFree = true;
                    }
                }
                catch (TaskCanceledException tce)
                {
                    Console.WriteLine($"Process SubJob, instance:{InstanceNum} got error: {tce.Message}.");
                    break;
                }
                catch (OperationCanceledException oce)
                {
                    Console.WriteLine($"Process SubJob, instance:{InstanceNum} got error: {oce.Message}.");
                    break;
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Process SubJob, instance:{InstanceNum} got error: {e.Message}.");
                    //break;
                }
            }
        }
Пример #4
0
        private void ProcessJobs(BlockingCollection <SubJob> workQueue, CancellationToken ct)
        {
            do
            {
                Job job = GetNextJob(ct);
                if (ct.IsCancellationRequested)
                {
                    break;
                }

                SubJob subJob = job.GetNextSubJob();
                if (subJob != null)
                {
                    //Debug.WriteLine($"Adding subJob for JobId:{subJob.ParentJob.JobId}, the pos is {subJob.MapSectionWorkRequest.MapSection.SectionAnchor.X},{subJob.MapSectionWorkRequest.MapSection.SectionAnchor.Y}.");
                    workQueue.Add(subJob, ct);
                }
            }while (true);
        }
Пример #5
0
        private void HandleSubJob(SubJob subJob)
        {
            SubJobResult        subJobResult  = _resultCache.GetEmptySubJobResult(readZValues: false);
            SubJobOperationType operationType = DetermineOperationType(subJob, subJobResult);

            if (operationType == SubJobOperationType.Fetch)
            {
                Console.WriteLine($"Sub Job Results were retreived from file. {GetDiagInfo(subJob)}");

                subJob.OperationType = operationType;
                subJob.SubJobResult  = subJobResult;
                _sendQueue.Add(subJob);
            }
            else
            {
                // Since we are not using this SubJobResult, mark it as free
                subJobResult.IsFree = true;
            }
        }
Пример #6
0
        private bool FillCountsForBlock2(SubJob subJob, CancellationToken ct, out TimeSpan elasped)
        {
            DateTime t0 = DateTime.Now;

            try
            {
                SubJobResult subJobResult = subJob.SubJobResult;

                uint[]   counts    = subJobResult.Counts;
                bool[]   doneFlags = subJobResult.DoneFlags;
                double[] zValues   = subJobResult.ZValues;

                for (int yPtr = 0; yPtr < FGenerator.BLOCK_WIDTH; yPtr++)
                {
                    if (ct.IsCancellationRequested || subJob.ParentJob.Closed)
                    {
                        break;
                    }
                    subJob.ParentJob.FGenerator.FillXCounts2(subJob.Position.GetPointInt(), ref counts, ref doneFlags, ref zValues, yPtr);
                    //subJob.ParentJob.FGenerator.FillXCountsTest(subJob.Position.GetPointInt(), ref counts, ref doneFlags, ref zValues, yPtr);
                }

                subJobResult.Counts    = counts;
                subJobResult.DoneFlags = doneFlags;
                subJobResult.ZValues   = zValues;
                ReportElaspedTime(t0, $"Block: {subJob.Position}");

                elasped = DateTime.Now - t0;

                return(!(ct.IsCancellationRequested || subJob.ParentJob.Closed));
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Got exception while filling the XCounts. The error is {e.Message}.");
                throw;
            }
        }
Пример #7
0
        private SubJobOperationType DetermineOperationType(SubJob subJob, SubJobResult subJobResult)
        {
            SubJobOperationType result = SubJobOperationType.Unknown;

            if (subJob.ParentJob.Closed)
            {
                Debug.WriteLine($"Not Processing Sub Job. {GetDiagInfo(subJob)}");
                return(SubJobOperationType.None);
            }

            if (TryRetrieveWorkResultFromRepo(subJob, subJobResult))
            {
                uint targetIterations = subJob.ParentJob.FJobRequest.MaxIterations;
                if (subJobResult.IterationCount == 0 || subJobResult.IterationCount == targetIterations)
                {
                    // The WorkResult read from file has the correct iteration count. (Or we are not tracking the iteration count.)
                    //Console.WriteLine("Sub Job Results were retreived from file. {GetDiagInfo(subJob)}");
                    result = SubJobOperationType.Fetch;
                }

                else if (subJobResult.IterationCount < targetIterations)
                {
                    result = SubJobOperationType.IncreaseIterations;
                }
                else
                {
                    result = SubJobOperationType.DecreaseIterations;
                }
            }
            else
            {
                result = SubJobOperationType.Build;
            }

            return(result);
        }
Пример #8
0
        public SubJob GetNextSubJob()
        {
            if (IsCompleted)
            {
                return(null);
            }

            if (_hSectionPtr > _size.W - 1)
            {
                _hSectionPtr = 0;
                _vSectionPtr++;

                if (_vSectionPtr > _size.H - 1)
                {
                    IsCompleted = true;
                    return(null);
                }
            }

            //System.Diagnostics.Debug.WriteLine($"Creating SubJob for hSection: {_hSectionPtr}, vSection: {_vSectionPtr}.");
            SubJob result = new SubJob(_job, new KPoint(_hSectionPtr++, _vSectionPtr));

            return(result);
        }
Пример #9
0
        private string GetDiagInfo(SubJob subJob)
        {
            string result = $"inst:{InstanceNum}, job:{subJob.ParentJob.JobId}, position:{subJob.Position}.";

            return(result);
        }
Пример #10
0
        private bool TryRetrieveWorkResultFromRepo(SubJob subJob, SubJobResult subJobResult)
        {
            bool result = subJob.ParentJob.RetrieveWorkResultFromRepo(subJob.Position, subJobResult);

            return(result);
        }
Пример #11
0
        private bool ProcessSubJob(SubJob subJob, CancellationToken ct)
        {
            if (subJob.ParentJob.Closed)
            {
                Debug.WriteLine($"Not Processing Sub Job. {GetDiagInfo(subJob)}");
                return(false);
            }

            uint targetIterations = subJob.ParentJob.FJobRequest.MaxIterations;

            switch (subJob.OperationType)
            {
            case SubJobOperationType.Unknown:
                throw new InvalidOperationException("Trying to process a subJob with op type = unknown.");

            case SubJobOperationType.Fetch:
            {
                // The WorkResult read from file has the correct iteration count. (Or we are not tracking the iteration count.)
                Console.WriteLine($"Sub Job Results were retreived from file. {GetDiagInfo(subJob)}");
                break;
            }

            case SubJobOperationType.Build:
            {
                Console.WriteLine($"Sub Job Results are being calculated from zero. {GetDiagInfo(subJob)}");

                //SubJobResult subJobResult = _resultCache.GetEmptySubJobResult(readZValues: false);

                //if (FillCountsForBlock(subJob, ct, out TimeSpan elasped))
                //{
                //	subJob.SubJobResult.IsFree = true;
                //	subJob.SubJobResult = subJobResult;

                //	if (FillCountsForBlock2(subJob, ct, out TimeSpan elasped2))
                //	{
                //		TimeSpan diff = elasped - elasped2;
                //		string et = diff.ToString(@"mm\:ss\.ffff");

                //		Console.WriteLine($"Using new ver took {et} less time.");

                //		subJob.SubJobResult.IterationCount = targetIterations;
                //		subJob.ParentJob.WriteWorkResult(subJob, overwriteResults: false);
                //	}
                //}
                //break;

                if (FillCountsForBlock2(subJob, ct, out TimeSpan elasped2))
                {
                    subJob.SubJobResult.IterationCount = targetIterations;
                    subJob.ParentJob.WriteWorkResult(subJob, overwriteResults: false);
                }
                break;
            }

            case SubJobOperationType.IncreaseIterations:
            {
                Console.WriteLine($"Increasing the iteration count. {GetDiagInfo(subJob)}");

                // Mark the current subJobResult as free.
                subJob.SubJobResult.IsFree = true;

                // Fetch a new one with ZValues included.
                SubJobResult subJobResult = _resultCache.GetEmptySubJobResult(readZValues: true);
                TryRetrieveWorkResultFromRepo(subJob, subJobResult);
                subJob.SubJobResult = subJobResult;

                // Use the current work results to continue calculations to create
                // a result with the target iteration count.
                if (FillCountsForBlock2(subJob, ct, out TimeSpan elasped2))
                {
                    subJobResult.IterationCount = targetIterations;
                    subJob.ParentJob.WriteWorkResult(subJob, overwriteResults: true);
                }
                break;
            }

            case SubJobOperationType.DecreaseIterations:
                throw new InvalidOperationException("Cannot reduce the number of iterations of an existing job.");

            case SubJobOperationType.None:
                throw new InvalidOperationException("Trying to process a subJob with op type = none.");

            default:
                throw new InvalidOperationException("Trying to process a subJob with an unrecognized op type.");
            }

            return(true);
        }