public SubJob GetNextSubJob() { if (IsCompleted) { return(null); } if (_hSectionPtr > SamplePoints.NumberOfHSections - 1) { _hSectionPtr = 0; _vSectionPtr++; if (_vSectionPtr > SamplePoints.NumberOfVSections - 1) { IsCompleted = true; return(null); } } //System.Diagnostics.Debug.WriteLine($"Creating SubJob for hSection: {_hSectionPtr}, vSection: {_vSectionPtr}."); int left = _hSectionPtr * SECTION_WIDTH; int top = _vSectionPtr * SECTION_HEIGHT; MapSection mapSection = new MapSection(new Point(left, top), new CanvasSize(SECTION_WIDTH, SECTION_HEIGHT)); MapSectionWorkRequest mswr = new MapSectionWorkRequest(mapSection, MaxIterations, _hSectionPtr++, _vSectionPtr); SubJob result = new SubJob(this, mswr); return(result); }
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); }
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); }
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); }
private void QueueWork(BlockingCollection <SubJob> workQueue, CancellationToken ct) { do { IJob job = GetNextJob(ct); if (ct.IsCancellationRequested) { return; } if (job.RequiresQuadPrecision()) { JobForMq jobForMq = GetJobForMqFromJob(job); jobForMq.MqRequestCorrelationId = SendJobToMq(jobForMq); Debug.WriteLine($"Starting a new ImageResultListener for {jobForMq.JobId}."); MqImageResultListener resultListener = new MqImageResultListener(jobForMq, INPUT_Q_PATH, _sendQueue, WaitDuration); resultListener.Start(); jobForMq.MqImageResultListener = resultListener; jobForMq.MarkAsCompleted(); } else if (job is Job localJob) { SubJob subJob = localJob.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); } } else if (job is JobForMq) { throw new InvalidOperationException("Job does not require quad precision and it is not a local job."); } else { throw new ArgumentException("The IJob is neither a Job or a JobForMq."); } } while (true); }
private async Task ReceiveImageResultsAsync() { using (MessageQueue inQ = GetJobResponseQueue()) { while (!_cts.IsCancellationRequested /*&& !jobForMq.IsLastSubJob*/) { Message m = await MqHelper.ReceiveMessageByCorrelationIdAsync(inQ, _jobForMq.MqRequestCorrelationId, _waitDuration); if (m == null) { Debug.WriteLine($"No FGenResult message present for correlationId: {FMsgId(_jobForMq.MqRequestCorrelationId)}."); continue; } FJobResult jobResult = (FJobResult)m.Body; PointInt pos = jobResult.Area.Point; Debug.WriteLine($"Received FJobResult for correlationId: {FMsgId(_jobForMq.MqRequestCorrelationId)}, X:{pos.X}, Y:{pos.Y}."); SubJob subJob = CreateSubJob(jobResult, _jobForMq); _sendQueue.Add(subJob); } if (_jobForMq.IsLastSubJob) { Debug.WriteLine($"The result listener for {_jobForMq.JobId} is stopping. We have received the last result."); } else if (_cts.IsCancellationRequested) { Debug.WriteLine($"The result listener for {_jobForMq.JobId} has been cancelled."); } else { Debug.WriteLine($"The result listener for {_jobForMq.JobId} is stopping for unknown reason."); } } }