






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Scott Bai, Ali Hussain, Nicolas Zea, Gene Wu Final Project Report Due: May 9th, 2007
The Two Point Angular Correlation Function (TPACF) is a mathematical equation that has applications in many areas. In our project, the Two Point Angular Correlation Function is used as a way to measure the probability of finding an astronomical body at a given angular distance from another astronomical body. We were given working sequential code as a starting point. The sequential code took in two kinds of data to process. The first consisted of a set of valid data points measured from a region of space. These data points represented the spherical coordinates of actual astronomical objects in space. The number of data points that this set contained could range up to 97,178 data points. The second kind of data was randomly generated data. The randomly generated data sets contained coordinates similar to the actual data set, but with random distribution. The number of random sets used varied and could be set with an input parameter, but each set must contain at least the same amount of points as data points being used. It is not uncommon to run the TPACF program with as many as 100 random data sets.
w
Figure 1: TPACF Equation
The TPACF (listed above as w) is calculated according to the equation found in Figure 1. The three most computationally intensive parts of the equation are the computations of the correlation functions: DD, DR, and RR. DD is the auto-correlation of the actual data set. DR is the cross-correlation of the actual data set to a given random set. RR is the auto-correlation of the random sets. Note that there will be only one DD to calculate, but there will be multiple DRs and RRs to calculate. There will be as many DRs and RRs as there are random sets used. Each of the correlation functions consists of a series of dot products between coordinate pairs. Each of these calculations is an independent floating point operation that can be done in parallel. The results of these calculations are organized into histograms.
The sequential code first takes in the data set and random sets. Then it converts all the spherical coordinates from the input files to Cartesian coordinates. Next, the code finds
the bin boundaries that will be used by DD, DR and RR to generate the histograms. After that, the correlations functions (DD, DR, and RR) are calculated. Once the correlation functions have been computed, the results of the DR and RR calculations are summed to form a single DR and a single RR histogram. The final step is to calculate the TPACF for each bin boundary.
1.1 Parallelism The high degree of parallelism in the TPACF makes it an interesting application to parallelize and run on the G80. The computations that could potentially be done in parallel are the conversion of data points from spherical to Cartesian coordinates, the dot product calculations involved in the correlation functions(DD, DR and RR), and the summations of DR and RR for use in the final calculation of the TPACF. The calculations to find DD, DR and RR are the most computationally intense part of TPACF. The calculations involved in finding these functions are independent both within the functions themselves and across functions. To exploit the parallelism within correlation functions, all the dot products done in a given correlation function can be spread across different threads. Given N elements in a set, there would be NN calculations to do for the cross correlation functions (DRs) and approximately NN/2 calculations to do for the autocorrelation functions (DD and RRs). The second kind of parallelism uses the fact that the correlations functions are independent from each other. So, the DD, DRs and RRs can be calculated in parallel. Given M random sets there would be M + 1 auto- correlations to calculate and M cross-correlations to calculate.
The greatest difficulty in parallelizing TPACF arises when the histograms are generated. Generating histograms is easy when there is one thread and things can be done sequentially. However, with multiple threads storing their results into the same histogram, there is a danger of losing the results of some threads due to scheduling race conditions. Each thread attempting to write into a shared histogram needs to load a value, increment that value, and store the value back. To prevent the loss of data, each thread uses its ID to tag its writes into the shared histograms. The threads attempt to read the data back after writing into the histogram. If the thread cannot find its own tag in the value it reads back, then it assumes that another thread overwrote its results and that it needs to reload the data, increment and then repeat the write/tag process. If there are too many conflicts, then there is a performance penalty due to re-execution of the write/tag and check process.
1.2 Precision Issues The CPU gold code provided by the sponsor calculates w for 30 different values of theta, organized in a logarithmic binning scheme ranging between 0.01 and 10000, with 5 bins per decade. It uses double-precision floating point to calculate which bin each distance value belongs in, i.e. which range of theta it fits in. Since CUDA does not yet support double-precision, we used single-precision floating point for these calculations. In calculating the distances, the smallest distances -- those which would belong to the 10 lowest bins -- cannot be differentiated in single-precision, and are treated as 0. As a result, these values are placed in the lowest bin instead of the proper locations. To correctly calculate the values for these lower bins requires 41 bits of fixed point precision, which exceeds what we have available. To resolve this issue, we decided to use only the top 20 bins out of the 30 that were in the gold code. The sponsor agreed to this.
0 to BLOCKSIZE - 1 (^) 2BLOCSIZE - 1BLOCKSIZE to ……. (N-1)BLOCSIZE toNBLOCKSIZE -
Block1 Block
Block1 Block
Block1 Block
Block N/ BLOCKSIZE
Block N/ BLOCKSIZE
Block N/ BLOCKSIZE
Iteration 1
Iteration N/ BLOCKSIZE
Iteration 2
t0 t1 t2 t3 tn
BLOCKSIZE elements of Set 1
Iteration 1
Iteration 2
Iteration 3
Iteration BLOCKSIZE Figure 2: A single kernel call for version 1
0 to BLOCKSIZE - 1 (^) 2BLOCSIZE - 1BLOCKSIZE to ……. (N-1)BLOCSIZE toNBLOCKSIZE -
Set 2 with N elements
0 to BLOCKSIZE - 1
BLOCKSIZE to2*BLOCSIZE - 1
…….
(N-1)BLOCSIZE toN*BLOCKSIZE -
Set 1 with N elements
Outer iteration 0 Inner iteration 0
Outer iteration 0 Inner iteration 1
Outer iteration: 1 Inner iteration: 0
Outer iteration: 1 Inner iteration: 1
Outer iteration: N/BLOCK_SIZE Inner iteration: 0
Outer iteration: N/BLOCK_SIZE Inner iteration: 1
Outer iteration: 0 Inner iteration: N/BLOCK_SIZE
Outer iteration: 1 Inner iteration: N/BLOCK_SIZE
Outer iteration: N/BLOCK_SIZE Inner iteration: N/BLOCK_SIZE
…..
…..
…..
….. ….. …..
DD Block 0
RR 0 Block 1 Block 2^ RR 1
RR NUM_SETS Block NUM_SETS
DR 0 Block NUM_SETS+
DR 1 Block NUM_SETS+
DR NUM_SETS Block 2xNUM_SETS +1 2xNUM_SETS blocks in a single
kernel call
t0 t1 t2 t3 tn
BLOCKSIZE elements of Set 2
BLOCKSIZE elements of Set 1
……………..……………..……………..…………….. ……………..
Iteration 1 Iteration 2 Iteration 3
Iteration BLOCKSIZE
Figure 3: Kernel call for version 2 calculating all DD, DR and RR values
data to self). The remaining N blocks calculate the cross correlation of the data with the random sets. This is shown in Figure 3.
Each of the blocks must do calculations equal to the square of the number of elements for cross correlation and around half that for auto correlation. To perform these calculations each thread brings in a element from the first set into the shared memory. The distance calculations are performed for all the elements in the second set with the elements from the first set resident in shared memory.
This is implemented in the program using three for loops. The outermost loop iterates over all the elements in the first set and brings BLOCK_SIZE elements into shared memory. The middle loop iterates over all the elements in the second set and brings in one element into the register file for each thread, at a time. The innermost loop calculates the correlation of all the elements from the first set resident in shared memory with that threads element from the second set (resident in the register file).
Shared memory is used for holding the working set of the first set and the histograms. Initially it also held the second set elements. This was changed because the second set values are not shared across threads, so the second set elements can be held in a register. This brings down the usage of shared memory for the data down to 3kb, allowing us to put 16 histograms per warp.
After the kernel finishes execution we compute the omega values on the CPU. This requires summing up 200 histograms. There is still some parallelism in this but it scales only linearly with the size of the problem. Since the distance calculations increase quadratically with the number of elements we decided this would be a negligible cost.
Software Pipelining To hide the latency of global memory accesses we tried using software pipelining. Instead of fetching the data to be used in the current iteration, we loaded the first set of data before entering the loop, and within the loop fetched the data for the next iteration. This was meant to make global memory accesses completely transparent at the cost of using more shared memory. However, when actually run, the overhead of implementing software pipelining and the cost of an extra 6k of shared memory was more than the benefit gained and the performance decreased slightly even when the histograms were disabled. Instead we opted to use that shared memory for increasing the number of histograms per warp, thereby decreasing our binning conflicts.
Version 2 Bank Conflicts There are no bank conflicts when using the first set. When writing into shared memory each thread accesses a contiguous memory location. When reading from shared memory, all threads access the same memory location so the required word is broadcast.
The bank conflicts in histograms showed interesting behavior. Initially we declared the histograms as:
unsigned long warp_hists[NUM_BINS][NUM_HISTOGRAMS+1]
The +1 factor removes all bank conflicts as long as the number of histograms is a multiple of 16 or there are 2 or more histograms per warp. For 2 histograms per warp it boosts performance by 10%. Another alternative was to use the number of bins as the second array index. In our case of 20 bins this significantly reduces the bank conflicts and makes the performance the same as with the +1 case and actually better when there is only one histogram per warp. But the number of bins is a parameter to our program and we don’t want to force it to be a non power of 2, so we decided against this. Reducing the bank conflicts had a smaller effect as we increased the number of histograms per warp. Trying to reduce bank conflicts using 8 or 16 histograms per warp decreases he performance for a small amount. The other place where the histograms are accessed is when coalescing the histograms after finishing the calculations. Since this code is such a small part of our code, any optimizations we do for this would part would have a negligible effect.
Handling of Auto Correlation When calculating the auto correlation for a dataset half the calculations are done twice (if all points are used). The gold code avoids all of these extra calculations. In the CUDA version we avoid most of the extra calculations by having the points loaded from the second set be based off the location of the points being used from the first set. This allows us to operate on chunks of data the same size as the square of the number of threads. By doing this we can eliminate most of our redundant calculations but we will still have some redundant calculations present.
Histogram Implementation The histogram implementation followed the algorithm discussed in the lecture slides and recommended by NVIDIA (with the addition that the histograms were needed to be declared with the volatile keyword in addition to the shared keyword). Our
overall performance, though some of them moved the bottleneck from writing to the warp histograms, to coalescing the histograms in a block (or vice versa). The ideas tried included multiple histograms per warp, using shared memory more efficiently to reuse data points, and removing bank conflicts by making array strides non-power-of-two.
In implementation 2, we surpassed our original performance goals (of at least 10x). Although we have not been able to run the full dataset due to the time limitations of kernel invocations, our results for shorter calls have been accurate (according to the previous verification standards), so we believe the algorithm to be accurate.
Figure 4 shows our speedup compared to the most optimized gold code provided by the sponsor when we keep the dataset size at 2048 and vary the number of blocks (which is itself dependant on the number of random sets used). Our best speedup is around 44x is in this case.
Speedup vs # total blocks in grid (with 2048 elements per set)
40
41
42
43
44
45
0 50 100 150 200 Number of total blocks
Figure 4: Speedup vs Grid size
Only those grid sizes that use the largest percentage of the SM’s available on the chip are shown. For the G80, these are those that are closest to multiples of 32. Although the G has 16 SM’s, and those grid sizes closest to multiples of 16 should be the “sweet spots”, the speedup was consistently better when the grid size was close to a multiple of 32. Although using other grid sizes doesn’t hurt performance, it does not take as full advantage of the SM’s available, and therefore the speedup is not as large.
Figure x2 shows the speedup when the number of random sets is kept at 7 blocks, and the dataset size is varied. In this case, the size of the dataset results in an exponential expansion of the processing time. Therefore, although the maximum dataset size is 97178, we were only able to process up to 24k elements, due to the kernel crashing on larger dataset sizes. 7 random sets means that there are only 15 total blocks available. Although this results in a smaller speedup than if 31 blocks were being used, it allowed us to test larger dataset sizes. Again, the more elements available, the more speedup shown, although the difference is not as pronounced as varying the grid size.
Speedup vs # of Elements (with 7 random sets)
30
31
32
33
34
35
0 5000 10000 15000 20000 25000 30000 Number of elements (dataset size)
Figure 5: Speedup vs Element size
In all, our best speedup was for a random set size of 95 (giving us 191 available blocks) and a data set size of 8192, for a total speedup of 46.535181x.
These numbers are significantly better than those presented in the presentation. This is due to the modifications mentioned in the implementation of version 2: putting the random sets in registers instead of shared memory, thereby letting us use more shared memory for the warp histograms which let us have 16 histograms per warp; removing some unnecessary if statements in our innermost loop; making all integer calculations unsigned; and reducing the number of redundant calculations for the autocorrelation portions of the algorithm.
VI. Conclusion
Overall, the TPACF algorithm has turned out to be well suited to CUDA, with max speedup greater than 45x compared to a sequential implementation. Because not only are the results of the distance calculations within a set independent of each other (which version 1 exploits), the sets themselves are independent (which version 2 exploits). As mentioned before, the largest factor preventing further speedup is the lack of an atomic increment instruction, which leaves room for race conditions when updating the histogram. We were forced to take this into account in our design, but if that were not an issue a better solution may have been possible.
Another issue, which we were forced to deal with, was that due to the large number of input values needed, in order to perform efficient calculations we were forced to push the shared memory usage and register usage to the limits. Future hardware revisions with more resources may remedy this problem as well as that of the loss of accuracy due to single precision values.