private static unsafe void AggregateSlabByIntensity(IntensityProjectionMethod method, IntPtr pSlabData, byte[] pixelData, int subsamples, int subsamplePixels, int maxThreads) { fixed(byte *pPixelData = pixelData) { var ptrPixelData = new IntPtr(pPixelData); // only parallelize if requested, and there's enough work for multiple threads to do stuff (otherwise overhead would just kill performance) if (maxThreads > 1 && subsamplePixels > 32768 * maxThreads) { // determine the number of pixels to process per thread job var jobSize = subsamplePixels / maxThreads; // first MAX_THREADS-1 jobs will have exactly JOB_SIZE pixels, and the last will be everything else (thus including any remainder caused by the determination of JOB_SIZE) var lastJob = maxThreads - 1; Enumerable.Range(0, maxThreads).Select(g => new KeyValuePair <int, int>(g * jobSize, g == lastJob ? subsamplePixels - lastJob * jobSize : jobSize)) .AsParallel().WithDegreeOfParallelism(maxThreads) .ForAll(r => method(pSlabData, ptrPixelData, subsamples, subsamplePixels, r.Key, r.Value)); } else { method(pSlabData, ptrPixelData, subsamples, subsamplePixels, 0, subsamplePixels); } } }
private static unsafe void AggregateSlabByIntensity(IntensityProjectionMethod method, IntPtr pSlabData, byte[] pixelData, int subsamples, int subsamplePixels, int maxThreads) { fixed (byte* pPixelData = pixelData) { var ptrPixelData = new IntPtr(pPixelData); // only parallelize if requested, and there's enough work for multiple threads to do stuff (otherwise overhead would just kill performance) if (maxThreads > 1 && subsamplePixels > 32768*maxThreads) { // determine the number of pixels to process per thread job var jobSize = subsamplePixels/maxThreads; // first MAX_THREADS-1 jobs will have exactly JOB_SIZE pixels, and the last will be everything else (thus including any remainder caused by the determination of JOB_SIZE) var lastJob = maxThreads - 1; Enumerable.Range(0, maxThreads).Select(g => new KeyValuePair<int, int>(g*jobSize, g == lastJob ? subsamplePixels - lastJob*jobSize : jobSize)) .AsParallel().WithDegreeOfParallelism(maxThreads) .ForAll(r => method(pSlabData, ptrPixelData, subsamples, subsamplePixels, r.Key, r.Value)); } else { method(pSlabData, ptrPixelData, subsamples, subsamplePixels, 0, subsamplePixels); } } }