예제 #1
0
 private void WorkProcessor(BlockingCollection <SubJob> workQueue, BlockingCollection <SubJob> sendQueue, CancellationToken ct)
 {
     try
     {
         while (!ct.IsCancellationRequested)
         {
             workQueue.TryTake(out SubJob subJob, -1, ct);
             MapSectionResult msr = ProcessSubJob(subJob);
             if (msr != null)
             {
                 subJob.MapSectionResult = msr;
                 _sendQueue.Add(subJob);
             }
         }
     }
     catch (OperationCanceledException)
     {
         Debug.WriteLine("SubJobProcessor canceled.");
         //throw;
     }
     catch (InvalidOperationException ioe)
     {
         Debug.WriteLine($"SubJobProcessor completed. The error is {ioe.Message}.");
         //throw;
     }
 }
예제 #2
0
        public void ReceiveImageData(string connectionId, MapSectionResult mapSectionResult, bool isFinalSection)
        {
            //lock (Lo)
            //{

            //  _hubContext.Clients.Client(connectionId).SendAsync("ImageData", mapSectionResult, isFinalSection).Wait();
            //  System.Threading.Thread.Sleep(200);
            //}

            _hubContext.Clients.Client(connectionId).SendAsync("ImageData", mapSectionResult, isFinalSection).Wait();
        }
예제 #3
0
        private MapSectionResult ProcessSubJob(SubJob subJob)
        {
            if (subJob.ParentJob.CancelRequested)
            {
                Debug.WriteLine("Not Processing Sub Job.");
                return(null);
            }

            if (!(subJob.ParentJob is Job localJob))
            {
                throw new InvalidOperationException("When processing a subjob, the parent job must be implemented by the Job class.");
            }


            MapSectionWorkRequest mswr = subJob.MapSectionWorkRequest;

            KPoint key = new KPoint(mswr.HPtr, mswr.VPtr);
            MapSectionWorkResult workResult = RetrieveWorkResultFromRepo(key, localJob, readZValues: false);

            MapSectionResult result;

            if (workResult == null)
            {
                result = CalculateMapValues(subJob, localJob, ref workResult);
            }
            else
            {
                if (workResult.IterationCount == 0 || workResult.IterationCount == mswr.MaxIterations)
                {
                    // The WorkResult read from file has the correct iteration count. (Or we are not tracking the interation count.)
                    result = new MapSectionResult(localJob.JobId, mswr.MapSection, workResult.Counts);
                }
                else if (workResult.IterationCount < mswr.MaxIterations)
                {
                    // Fetch the entire WorkResult with ZValues
                    workResult = RetrieveWorkResultFromRepo(key, localJob, readZValues: true);

                    // Use the current work results to continue calculations to create
                    // a result with the target iteration count.
                    result = CalculateMapValues(subJob, localJob, ref workResult);
                }
                else
                {
                    throw new InvalidOperationException("Cannot reduce the number of iterations of an existing job.");
                }
            }

            return(result);
        }
예제 #4
0
        private SubJob CreateSubJob(FJobResult jobResult, JobForMq parentJob)
        {
            MapSectionWorkResult workResult = CreateWorkResult(jobResult);

            MapSectionWorkRequest workRequest = CreateMSWR(jobResult, parentJob.SMapWorkRequest.MaxIterations);
            MapSectionResult      msr         = CreateMapSectionResult(parentJob.JobId, workRequest.MapSection, workResult);

            SubJob subJob = new SubJob(parentJob, workRequest)
            {
                MapSectionResult = msr
            };

            // We need to keep track if the last sub job has been sent, not received.
            //if (jobResult.IsFinalResult) parentJob.SetIsLastSubJob(true);

            return(subJob);
        }
예제 #5
0
        private MapSectionResult CalculateMapValues(SubJob subJob, Job localJob, ref MapSectionWorkResult workResult)
        {
            MapSectionWorkRequest mswr = subJob.MapSectionWorkRequest;

            bool overwriteResults;

            if (workResult == null)
            {
                workResult       = BuildInitialWorkingValues(mswr);
                overwriteResults = false;
            }
            else
            {
                overwriteResults = true;
            }

            double[] xValues = localJob.SamplePoints.XValueSections[mswr.HPtr];
            double[] yValues = localJob.SamplePoints.YValueSections[mswr.VPtr];

            //DateTime t0 = DateTime.Now;
            //workResult = _mapCalculator.GetWorkingValues(xValues, yValues, mswr.MaxIterations, workResult);
            //DateTime t1 = DateTime.Now;

            //int[] testCounts = new int[10000];
            //DateTime t2 = DateTime.Now;

            //_mapCalculator.ComputeApprox(xValues, yValues, mswr.MaxIterations, testCounts);
            //List<int> misMatcheIndexes = GetMismatchCount(workResult.Counts, testCounts, out double avgGap);
            //DateTime t3 = DateTime.Now;

            //Debug.WriteLine($"Block: v={mswr.VPtr}, h={mswr.HPtr} has {misMatcheIndexes.Count} mis matches, avgGap={avgGap} and avg {GetAverageCntValue(workResult.Counts)}.");
            //Debug.WriteLine($"Standard: {GetElaspedTime(t0, t1)}, Approx: {GetElaspedTime(t2, t3)}.");

            //_mapCalculator.ComputeApprox(xValues, yValues, mswr.MaxIterations, workResult.Counts);
            workResult = _mapCalculator.GetWorkingValues(xValues, yValues, mswr.MaxIterations, workResult);

            KPoint key = new KPoint(mswr.HPtr, mswr.VPtr);

            localJob.WriteWorkResult(key, workResult, overwriteResults);

            MapSectionResult msr = new MapSectionResult(localJob.JobId, mswr.MapSection, workResult.Counts);

            return(msr);
        }
예제 #6
0
        private MapSectionResult CreateMapSectionResult(int jobId, MapSection mapSection, MapSectionWorkResult workResult)
        {
            MapSectionResult result = new MapSectionResult(jobId, mapSection, workResult.Counts);

            return(result);
        }