static uint[][] HistogramByteComponentsParInner(uint[] inArray, Int32 l, Int32 r, int parallelThreshold = 16 * 1024) { const int numberOfBins = 256; const int numberOfDigits = sizeof(uint); uint[][] countLeft = null; uint[][] countRight = null; if (l > r) // zero elements to compare { countLeft = new uint[numberOfDigits][]; for (int i = 0; i < numberOfDigits; i++) { countLeft[i] = new uint[numberOfBins]; } return(countLeft); } if ((r - l + 1) <= parallelThreshold) { countLeft = new uint[numberOfDigits][]; for (int i = 0; i < numberOfDigits; i++) { countLeft[i] = new uint[numberOfBins]; } uint[] countLeft0 = countLeft[0]; uint[] countLeft1 = countLeft[1]; uint[] countLeft2 = countLeft[2]; uint[] countLeft3 = countLeft[3]; var union = new Algorithm.UInt32ByteUnion(); for (int current = l; current <= r; current++) // Scan the array and count the number of times each digit value appears - i.e. size of each bin { union.integer = inArray[current]; countLeft0[union.byte0]++; countLeft1[union.byte1]++; countLeft2[union.byte2]++; countLeft3[union.byte3]++; } return(countLeft); } int m = (r + l) / 2; Parallel.Invoke( () => { countLeft = HistogramByteComponentsParInner(inArray, l, m, parallelThreshold); }, () => { countRight = HistogramByteComponentsParInner(inArray, m + 1, r, parallelThreshold); } ); // Combine left and right results (reduce step) for (int i = 0; i < numberOfDigits; i++) { for (int j = 0; j < numberOfBins; j++) { countLeft[i][j] += countRight[i][j]; } } return(countLeft); }
public static uint[][] HistogramByteComponentsPar(uint[] inArray, Int32 l, Int32 r) { const int numberOfBins = 256; const int numberOfDigits = sizeof(uint); uint[][] countLeft = new uint[numberOfDigits][]; for (int i = 0; i < numberOfDigits; i++) { countLeft[i] = new uint[numberOfBins]; } if (l > r) // zero elements to compare { return(countLeft); } if ((r - l + 1) <= ThresholdByteCount) { var union = new Algorithm.UInt32ByteUnion(); for (int current = l; current <= r; current++) // Scan the array and count the number of times each digit value appears - i.e. size of each bin { union.integer = inArray[current]; countLeft[0][union.byte0]++; countLeft[1][union.byte1]++; countLeft[2][union.byte2]++; countLeft[3][union.byte3]++; } return(countLeft); } int m = (r + l) / 2; uint[][] countRight = new uint[numberOfDigits][]; for (int i = 0; i < numberOfDigits; i++) { countRight[i] = new uint[numberOfBins]; } Parallel.Invoke( () => { countLeft = HistogramByteComponentsPar(inArray, l, m); }, () => { countRight = HistogramByteComponentsPar(inArray, m + 1, r); } ); // Combine left and right results for (int i = 0; i < numberOfDigits; i++) { for (int j = 0; j < numberOfBins; j++) { countLeft[i][j] += countRight[i][j]; } } return(countLeft); }