Пример #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
        public static void ClearSubJobResult(SubJobResult subJobResult)
        {
            if (!subJobResult.IsFree)
            {
                Debug.WriteLine("Clearing a SubJobResult that is still in use.");
            }

            uint[] counts = subJobResult.Counts;
            for (int i = 0; i < counts.Length; i++)
            {
                counts[i] = 0;
            }

            subJobResult.IterationCount = 0;

            bool[] doneFlags = subJobResult.DoneFlags;
            for (int i = 0; i < doneFlags.Length; i++)
            {
                doneFlags[i] = false;
            }

            double[] zValues = subJobResult.ZValues;
            for (int i = 0; i < zValues.Length; i++)
            {
                zValues[i] = 0;
            }
        }
Пример #3
0
        public SubJobResult GetEmptySubJobResult()
        {
            if (_emptySubJobResult == null)
            {
                int  size           = FGenerator.BLOCK_WIDTH * FGenerator.BLOCK_HEIGHT;
                uint iterationCount = 0;
                _emptySubJobResult = SubJobResult.GetEmptySubJobResult(size, iterationCount, InstanceNum, true);
            }

            SubJobResult.ClearSubJobResult(_emptySubJobResult);
            return(_emptySubJobResult);
        }
Пример #4
0
        //public uint[] GetCountsForLine(int linePtr)
        //{
        //	uint[] result = _fGenerator.GetXCounts(linePtr);
        //	return result;
        //}

        public void FillCountsForBlock(PointInt position, SubJobResult subJobResult)
        {
            uint[]   counts    = subJobResult.Counts;
            bool[]   doneFlags = subJobResult.DoneFlags;
            double[] zValues   = subJobResult.ZValues;

            _fGenerator.FillXCounts(position, ref counts, ref doneFlags, ref zValues);

            subJobResult.Counts    = counts;
            subJobResult.DoneFlags = doneFlags;
            subJobResult.ZValues   = zValues;
        }
Пример #5
0
        private SubJobResult GetEmptySubJobResult(KPoint dummy)
        {
            if (_emptySubJobResult == null)
            {
                int    size         = FGenerator.BLOCK_WIDTH * FGenerator.BLOCK_HEIGHT;
                string instanceName = "MainJob";
                _emptySubJobResult = SubJobResult.GetEmptySubJobResult(size, instanceName, includeZValuesOnRead: false);
            }

            SubJobResult.ClearSubJobResult(_emptySubJobResult);
            return(_emptySubJobResult);
        }
Пример #6
0
        private FJobResult GetResultFromSubJob(int jobId, PointInt position, SubJobResult subJobResult)
        {
            MqMessages.PointInt resultPos = new MqMessages.PointInt(
                position.X() * FGenerator.BLOCK_WIDTH,
                position.Y() * FGenerator.BLOCK_HEIGHT
                );

            MqMessages.SizeInt      resultSize = new MqMessages.SizeInt(FGenerator.BLOCK_WIDTH, FGenerator.BLOCK_HEIGHT);
            MqMessages.RectangleInt area       = new MqMessages.RectangleInt(resultPos, resultSize);

            FJobResult fJobResult = new FJobResult(jobId, area, subJobResult.Counts);

            return(fJobResult);
        }
Пример #7
0
        public bool RetrieveWorkResultFromRepo(KPoint riKey, SubJobResult workResult)
        {
            // When writing include the Area's offset.
            KPoint transKey = riKey.ToGlobal(_position);

            lock (_repoLock)
            {
                if (Closed)
                {
                    return(false);
                }
                bool result = _countsRepo.ReadParts(transKey, workResult);
                return(result);
            }
        }
Пример #8
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;
                }
            }
        }
Пример #9
0
        public IDictionary <int, int> GetHistogram()
        {
            IEnumerable <KPoint> keys = GetAllRepoKeys();

            if (keys == null)
            {
                return(null);
            }

            IDictionary <int, int> result = new Dictionary <int, int>();

            foreach (KPoint key in keys)
            {
                SubJobResult subJobResult = GetEmptySubJobResult(key);

                if (RetrieveWorkResultFromRepo(key, subJobResult))
                {
                    foreach (int cntAndEsc in subJobResult.Counts)
                    {
                        int cnt = cntAndEsc / 10000;
                        if (result.TryGetValue(cnt, out int occurances))
                        {
                            result[cnt] = occurances + 1;
                        }
                        else
                        {
                            result[cnt] = 1;
                        }
                    }

                    subJobResult.IsFree = true;
                }
            }

            if (Closed)             // Check to see if we have been closed since this method was called.
            {
                return(null);
            }
            else
            {
                return(result);
            }
        }
Пример #10
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;
            }
        }
Пример #11
0
        private void ProcessSubJob(JobProcessor2 jobProcessor, PointInt position, string requestMsgId, MessageQueue outQ)
        {
            int jobId = jobProcessor.FGenJob.JobId;

            //int w = jobProcessor.FGenJob.Area.W();

            //uint[] counts = jobProcessor.GetCountsForLine(linePtr);

            SubJobResult subJobResult = jobProcessor.GetEmptySubJobResult();

            jobProcessor.FillCountsForBlock(position, subJobResult);
            //Debug.WriteLine(ReportSampleOfCounts(subJobResult.Counts));

            FJobResult fJobResult = GetResultFromSubJob(jobId, position, subJobResult);

            Message r = new Message(fJobResult)
            {
                CorrelationId = requestMsgId
            };

            Console.WriteLine($"Sending a response message with corId = {FMsgId(requestMsgId)}, instance:{jobProcessor.InstanceNum}.");
            outQ.Send(r);
        }
Пример #12
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;
            }
        }
Пример #13
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);
        }
Пример #14
0
        private bool TryRetrieveWorkResultFromRepo(SubJob subJob, SubJobResult subJobResult)
        {
            bool result = subJob.ParentJob.RetrieveWorkResultFromRepo(subJob.Position, subJobResult);

            return(result);
        }
Пример #15
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);
        }
Пример #16
0
        public static SubJobResult GetEmptySubJobResult(int size, string instanceName, bool includeZValuesOnRead)
        {
            SubJobResult result = new SubJobResult(new uint[size], EMPTY_SUB_JOB_RESULT_MARKER, new double[size * 4], new bool[size], instanceName, size, true, includeZValuesOnRead);

            return(result);
        }