Assignment 2: A Simple CUDA Renderer

Due: Wednesday Feb 11, 11:59PM EST

100 points total

My Image


In this assignment you will write a parallel renderer in CUDA that draws colored circles. While this renderer is very simple, parallelizing the renderer will require you to design and implement data structures that can be efficiently constructed and manipulated in parallel. This is a challenging assignment so you are advised to start early. Seriously, you are advised to start early. Good luck!

Environment Setup

This assignment requires an NVIDIA GPU with CUDA compute capability 2.0. The following lab machines have adequate GPUs:

  • ghc26/27/31/45/ contain NVIDIA GTX 480 GPU's. The capabilities of this chip were described in lecture. Also, Table F.1 in the CUDA C Programming Guide is a handy reference for the maximum number of CUDA threads per thread block, size of thread block, shared memory, etc. The GTX 480 GPUs support CUDA compute capability 2.0.
  • contains a NVIDIA GTX 650 GPU (compute capability 3.0).
  • ghc28/32-37/39/40/43/ contain NVIDIA GTX 670 GPU's (compute capability 3.0)
  • ghc38/ contain NVIDIA GTX 670 GPUs (compute capability 3.0)
  • contains a NVIDIA GTX 780 GPU (compute capability 3.5). This is the fastest GPU we have in the public labs on campus

Note: the performance capabilities of these GPU's varies widely, meaning that your results will vary across these machines. However, feel free to run your code on any of the specified machines. We have distributed reference solution executables in the starter code directories to grade your code's performance.

To get started:

  1. The NVIDIA CUDA C/C++ Compiler (NVCC) is located at /usr/local/cuda-6.5/bin/, which you need to add to your PATH. To add /usr/local/cuda-6.5/bin to your path, add setenv PATH /usr/local/cuda-6.5/bin:${PATH} to your ~/.cshrc file if using csh or export PATH=/usr/local/cuda-6.5/bin:${PATH} to your ~/.bashrc file if using bash.
  2. The CUDA shared library will be loaded at runtime. It is located at /usr/local/cuda-6.5/lib64/, which you need to add to your LD_LIBRARY_PATH. Add setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:/usr/local/cuda-6.5/lib64/ to your ~/.cshrc file if using csh or export LD_LIBRARY_PATH=/usr/local/cuda-6.5/lib64/:${LD_LIBRARY_PATH} to your ~/.bashrc file if using bash.
  3. Download the Assignment 2 starter code from the course directory: /afs/

The CUDA C programmer's guide is an excellent reference for learning how to program in CUDA.

You can also find a large number of examples in the CUDA SDK /usr/local/cuda-6.5/samples. In addition, there are a wealth of CUDA tutorials and SDK examples on the web (just Google!) and on the NVIDIA developer site. In particular, you may enjoy the free Udacity course Introduction to Parallel Programming in CUDA.

Part 1: CUDA Warm-Up 1: SAXPY (5 pts)

To gain a bit of practice writing CUDA programs your warm-up task is to re-implement the SAXPY function from assignment 1 in CUDA. Starter code for this part of the assignment is located in the /saxpy directory of the assignment tarball.

Please finish off the implementation of SAXPY in the function saxpyCuda in You will need to allocate device global memory arrays and copy the contents of the host input arrays X, Y, and result into CUDA device memory prior to performing the computation. After the CUDA computation is complete, the result must be copied back into host memory. Please see the definition of cudaMemcpy function in Section 3.2.2 of the Programmer's Guide.

As part of your implementation, add timers around the CUDA kernel invocation in saxpyCuda. Your additional timing measurement should not include the time to transfer data to and from device memory (just the time to execute the computation). Note that CUDA kernel's execution on the GPU is asynchronous with the main application thread running on the CPU. Therefore, you will want to place a call to cudaThreadSynchronize following the kernel call to wait for completion of all CUDA work. This call to cudaThreadSynchronize should return before stopping the timer.

Question 1. What performance do you observe compared to the sequential CPU-based implementation of SAXPY (recall program 5 from Assignment 1)? Compare and explain the difference between the results provided by two sets of timers (the timer you added and the timer that was already in the provided starter code). Are the bandwidth values observed roughly consistent with the reported bandwidths available to the different components of the machine? (Hint: You should use the web to track down the memory bandwidth of an NVIDIA GTX 480 GPU, and the maximum transfer speed of the computer's PCIe-x16 bus)

Part 2: CUDA Warm-Up 2: Parallel Prefix-Sum (10 pts)

Now that you're familiar with the basic structure and layout of CUDA programs, as a second exercise you are asked to come up with parallel implementation of the function find_repeats which, given a list of integers A, returns a list of all indices i for which A[i] == A[i+1].

For example, given the array {1,2,2,1,1,1,3,5,3,3}, your program should output the array {1,3,4,8}.

Exclusive Prefix Sum

We want you to implement find_repeats by first implementing parallel exclusive prefix-sum operation (which you may remember as scan from 15-210).

Exlusive prefix sum takes an array A and produces a new array output that has, at each index i, the sum of all elements up to but not including A[i]. For example, given the array A={1,4,6,8,2}, the output of exclusive prefix sum output={0,1,5,11,19}.

A recursive implementation should be familiar to you from 15-210. As a review (or for those that did not take 15-210), the following code is a C implementation of a work-efficient, parallel implementation of scan. In addition, details on prefix-sum (and its more general relative, scan) can be found in in the 15-210 lecture notes, Section 6.2. Note: Some of you may wish to skip the following recursive implementation and jump to the iterative version below.

void exclusive_scan_recursive(int* start, int* end, int* output, int* scratch)
    int N = end - start;

    if (N == 0) return;
    else if (N == 1)
     output[0] = 0;

    // sum pairs in parallel.
    for (int i = 0; i < N/2; i++)
     output[i] = start[2*i] + start[2*i+1];

    // prefix sum on the compacted array.
    exclusive_scan_recursive(output, output + N/2, scratch, scratch + (N/2));

    // finally, update the odd values in parallel.
    for (int i = 0; i < N; i++)
     output[i] = scratch[i/2];
     if (i % 2)
         output[i] += start[i-1];

While the above code expresses our intent well, it is not particularly amenable to a CUDA implementation. The GTX 480 GPUs on the GHC machines do not not even support recursion in CUDA! Therefore, we can unwind the recursion tree and express our algorithm in an iterative manner. The following code should show you how one traverses the recursion tree and unwind it. Luckily for us we do not actually even need a stack to express our code.

The following "C-like" code is an iterative version of scan. We use parallel_for to indicate potentially parallel loops. You might also want to take a look at Kayvon's notes on exclusive scan.

void exclusive_scan_iterative(int* start, int* end, int* output)
    int N = end - start;
    memmove(output, start, N*sizeof(int));
    // upsweep phase.
    for (int twod = 1; twod < N; twod*=2)
     int twod1 = twod*2;
     parallel_for (int i = 0; i < N; i += twod1)
         output[i+twod1-1] += output[i+twod-1];

    output[N-1] = 0;

    // downsweep phase.
    for (int twod = N/2; twod >= 1; twod /= 2)
     int twod1 = twod*2;
     parallel_for (int i = 0; i < N; i += twod1)
         int t = output[i+twod-1];
         output[i+twod-1] = output[i+twod1-1];
         output[i+twod1-1] += t; // change twod1 to twod to reverse prefix sum.

You are welcome to use this general algorithm to implement a version of parallel prefix sum in CUDA.
You must implement exclusive_scan function in scan/ Your implementation will consist of both host and device code. The implementation will require multiple kernel launches.

Note: The reference scan implemenation above assumes that the input array's length (N) is a power of 2. In the cudaScan function, we solve this problem by rounding the input array length to the next power of 2 when allocating the corresponding buffers on the GPU. However, we only copy back N elements from the GPU buffer back to the CPU buffer. This fact should simplify your CUDA implementation.

Implementing "Find Repeats" Using Prefix Sum

Once you have written exclusive_scan, you should implement the function find_repeats in scan/ This will involve writing more device code, in addition to one or more calls to exclusive_scan. Your code should write the list of repeated elements into the provided output pointer (in device memory), and then return the size of the output list.

When using your exclusive_scan implementation, remember that the contents of the start array are copied over to the output array. Also, the arrays passed to exclusive_scan are assumed to be in device memory.

Grading: We will test your code for correctness and performance on random input arrays.

For reference, a scan score table is provided below, showing the performance of a simple CUDA implementation on a Gates machine with a GTX 480. To check the correctness and performance score of your scan and find_repeats implementation, run make check_scan and make check_find_repeats respectively. Doing so will produce a reference table like so; your score is based solely on the performance of your code. In order to get full credit, your code must performa within 20% of the provided reference solution.

Scan Score Table:
| Element Count   | Fast Time       | Your Time       | Score           |
| 10000           | 0.387           | 0.010 (F)       | 0               |
| 100000          | 0.770           | 0.070 (F)       | 0               |
| 1000000         | 1.771           | 0.167 (F)       | 0               |
| 2000000         | 2.799           | 0.150 (F)       | 0               |
|                                   | Total score:    | 0/5             |

Test Harness: By default, the test harness runs on a pseudo-randomly generated array that is the same every time the program is run, in order to aid in debugging. You can pass the argument -i random to run on a random array - we will do this when grading. We encourage you to come up with alternate inputs to your program to help you evaluate it. You can also use the -n <size> option to change the length of the input array.

The argument --thrust will use the Thrust Library's implementation of exclusive scan. Up to two points of extra credit for anyone that can create an implementation is competitive with Thrust.

Part 3: A Simple Circle Renderer (85 pts)

Now for the real show!

The directory /render of the assignment starter code contains an implementation of renderer that draws colored circles. Build the code, and run the render with the following command line: ./render rgb. You will see an image of three circles appear on screen ('q' closes the window). Now run the renderer with the command line ./render snow. You should see an animation of falling snow.

The assignment starter code contains two versions of the renderer: a sequential, single-threaded C++ reference implementation, implemented in refRenderer.cpp, and an incorrect parallel CUDA implementation in

Renderer Overview

We encourage you to familiarize yourself with the structure of the renderer codebase by inspecting the reference implementation in refRenderer.cpp. The method setup is called prior to rendering the first frame. In your CUDA-accelerated renderer, this method will likely contain all your renderer initialization code (allocating buffers, etc). render is called each frame and is responsible for drawing all circles into the output image. The other main function of the renderer, advanceAnimation, is also invoked once per frame. It updates circle positions and velocities. You will not need to modify advanceAnimation in this assignment.

The renderer accepts an array of circles (3D position, velocity, radius, color) as input. The basic sequential algorithm for rendering each frame is:

Clear image
for each circle
    update position and velocity
for each circle
    compute screen bounding box
    for all pixels in bounding box
        compute pixel center point
        if center point is within the circle
            compute color of circle at point
            blend contribution of circle into image for this pixel

Figure 2 illustrates the basic algorithm for computing circle-pixel coverage using point-in-circle tests. Notice that a circle contributes color to an output pixel only if the pixel's center lies within the circle.

Point in circle test

CUDA Renderer

After familiarizing yourself with the circle rendering algorithm as implemented in the reference code, now study the CUDA implementation of the renderer provided in You can run the CUDA implementation of the renderer using the --renderer cuda program option.

The provided CUDA implementation parallelizes computation across all input circles, assigning one circle to each CUDA thread. While this CUDA implementation is a complete implementation of the mathematics of a circle renderer, it contains several major errors that you will fix in this assignment. Specifically: the current implementation does not ensure image update is an atomic operation and it does not preserve the required order of image updates (the ordering requirement will be described below).

Renderer Requirements

Your parallel CUDA renderer implementation must maintain two invariants that are preserved trivially in the sequential implementation.

  1. Atomicity: All image update operations must be atomic. The critical region includes reading the four 32-bit floating-point values (the pixel's rgba color), blending the contribution of the current circle with the current image value, and then writing the pixel's color back to memory.
  2. Order: Your renderer must perform updates to an image pixel in circle input order. That is, if circle 1 and circle 2 both contribute to pixel P, any image updates to P due to circle 1 must be applied to the image before updates to P due to circle 2. As discussed in class, preserving the ordering requirement allows for correct rendering of transparent surfaces. (It has a number of other benefits for graphics systems. If curious, talk to Kayvon.) A key observation is that the definition of order only specifies the order of updates to the same pixel. Thus, as shown in Figure 3, there are no ordering requirements between circles that do not contribute to the same pixel. These circles can be processed independently.


Since the provided CUDA implementation does not satisfy these requirements, the result of not correctly respecting order can be seen by running the CUDA renderer implementation on the rgb and circles scenes. You will see horizontal streaks through the resulting images. These streaks will change with each frame.

What You Need To Do

Your job is to write the fastest, correct CUDA renderer implementation you can. You may take any approach you see fit, but your renderer must adhere to the atomicity and order requirements specified above. A solution that does not meet both requirements will be given no more than 10 points on part 2 of the assignment. We have already given you such a solution!

A good place to start would be to read through and convince yourself that it does not meet the correctness requirement. To visually see the effect of violation of above two requirements, compile the program with make. Then run ./render -r cuda rgb which should display the three circles image. Compare this image with the image generated by sequential code by running ./render rgb.

Following are some of the options to ./render:

 -b  --bench START:END    Benchmark mode, do not create display. Time frames from START to END 
 -c  --check              Runs both sequential and cuda versions and checks correctness of cuda version
 -f  --file  FILENAME     Dump frames in benchmark mode (FILENAME_xxxx.ppm)
 -r  --renderer WHICH     Select renderer: WHICH=ref or cuda
 -s  --size  INT          Make rendered image x pixels
 -?  --help               Prints information about switches mentioned here. 

Checker code: To detect correctness of the program, render has a convenient --check option. This option runs the sequential version of the reference CPU renderer along with your CUDA renderer and then compares the resulting images to ensure correctness. The time taken by your CUDA renderer implementation is also printed.

We provide are total of five circle datasets you will be graded on. However, in order to receive full credit, your code must pass all of our test-benches. To check the correctness and performance score of your code, run make check in the /render directory. If you run it on the starter code, the program will print a table like the following, along with the results of our entire test set:

Score table:
| Scene Name      | Fast Time (Tf)  | Your Time (T)   | Score           |
| rgb             | 0.2615          | 137.9401 (F)    | 0               |
| rand10k         | 10.1625         | 107.9893 (F)    | 0               |
| rand100k        | 101.1337        | 1084.3314 (F)   | 0               |
| pattern         | 0.9361          | 4.2878 (F)      | 0               |
| snowsingle      | 59.4531         | 19.1290 (F)     | 0               |
|                                   | Total score:    | 0/65            |

Note: on some runs, you may receive credit for some of these scenes, since the provided renderer's runtime is non-deterministic. This doesn't change the fact that the current CUDA renderer is incorrect.

"Fast time" is the performance of a good solution on your current machine (in the provided render_ref executable). "Your time" is the performance of your current CUDA renderer solution. Your grade will depend on the performance of your implementation compared to these reference implementations (see Grading Guidelines).

Along with your code, we would like you to hand in a clear, high-level description of how your implementation works as well as a brief description of how you arrived at this solution. Specifically address approaches you tried along the way, and how you went about determining how to optimize your code (For example, what measurements did you perform to guide your optimization efforts?).

Aspects of your work that you should mention in the write-up include:

  1. Include both partners names and andrew id's at the top of your write-up.
  2. Replicate the score table generated for your solution and specify which machine you ran your code on.
  3. Describe how you decomposed the problem and how you assigned work to CUDA thread blocks and threads (and maybe even warps).
  4. Describe where synchronization occurs in your solution.
  5. What, if any, steps did you take to reduce communication requirements (e.g., synchronization or main memory bandwidth requirements)?
  6. Briefly describe how you arrived at your final solution. What other approaches did you try along the way. What was wrong with them?

Grading Guidelines

  • The write-up for the assignment is worth 20 points.
  • Your implementation is worth 65 points. These are equally divided into 13 points per scene as follows:

    • 2 correctness points per scene.
    • 11 performance points per scene (only obtainable if the solution is correct). Your performance will be graded with respect to the performance of a provided benchmark renderer, Tf:
    • No performance points will be given for solutions having time (T) an order of 10 times the magnitude of Tf.
    • Full performance points will be given for solution within 20% of the optimized solution ( T < 1.20 * Tf )
    • For other values of T (for 1.20 Tf <= T < 10 * Tf), your performance score on scale 1 to 11 will be calculated as :

      $Score = \RoundUp \Big(\frac{T_f}{T} * 11)$

  • Up to five points extra credit (instructor discretion) for solutions that achieve significantly greater performance than required. Your write up must clearly explain your approach thoroughly.

  • Up to five points extra credit (instructor discretion) for a high-quality parallel CPU-only renderer implementation that achieves good utilization of all cores and SIMD vector units of the cores. Feel free to use any tools at your disposal (e.g., SIMD intrinsics, ISPC, pthreads). To receive credit you should analyze the performance of your GPU and CPU-based solutions and discuss the reasons for differences in implementation choices made.

Assignment Tips and Hints

Below are a set of tips and hints compiled from previous years. Note that there are various ways to implement your renderer and not all hints may apply to your approach.

  • To facilitate remote development and benchmarking, we have created a --benchmark option to the render program. This mode does not open a display, and instead runs the renderer for the specified number of frames.
  • When in benchmark mode, the --file option sets the base file name for PPM images created at each frame. Created files are basename xxxx.ppm. No PPM files are created if the --file option is not used.
  • There are two potential axes of parallelism in this assignment. One axis is parallelism across pixels another is parallelism across circles (provided the ordering requirement is respected for overlapping circles).
  • The prefix-sum operation provided in exclusiveScan.cu_inl may be valuable to you on this assignment (not all solutions may choose to use it). See the simple description of a prefix-sum here. We have provided an implementation of an exclusive prefix-sum on a power-of-two-sized array in shared memory. The provided code does not work on non-power-of-two inputs. IT ALSO REQUIRES THAT THE NUMBER OF THREADS IN THE THREAD BLOCK BE THE SIZE OF THE ARRAY.
  • You are allowed to use the Thrust library in your implementation if you so choose. Thrust is not necessary to achieve the performance of the optimized CUDA reference implementations.
  • Is there data reuse in the renderer? What can be done to exploit this reuse?
  • The circle-intersects-box tests provided to you in circleBoxTest.cu_inl are your friend.
  • How will you ensure atomicity of image update since there is no CUDA language primitive that performs the logic of the image update operation atomically? Constructing a lock out of global memory atomic operations is one solution, but keep in mind that even if your image update is atomic, the updates must be performed in the required order. We suggest that you think about ensuring order in your parallel solution first, and only then consider the atomicity problem (if it still exists at all) in your solution.
  • If you are having difficulty debugging your CUDA code, you can use printf directly from device code if you use a sufficiently new GPU and CUDA library: see this brief guide on how to print from CUDA.
  • If you find yourself with free time, have fun making your own scenes!

3.4 Hand-in Instructions

Please submit your work in the directory: /afs/cs/academic/class/15418-s15-users/<ANDREW ID>/asst2

Remember: You need to execute aklog to avoid permission issues. 618 students should replace 418 with 618 in the above URL.

  1. Please submit your writeup as the file writeup.pdf.
  2. Please submit your code in the file code.tgz. Just submit your full assignment 2 source tree. To keep submission sizes small, please do a make clean in the program directories prior to creating the archive, and remove any residual output images, etc. Before submitting the source files, make sure that all code is compilable and runnable! We should be able to simply untar code.tgz, make, then execute your programs in /saxpy, /scan, and /render without manual intervention.
  3. If you're working with a partner, submit your partner's Andrew ID in the file PARTNER. Please make just one submission per partnership. One partner should have all 3 of these files in their handin directory for assignment 2, and the other partner should have no files in their handin directory for assignment 2.

Our grading scripts will rerun the checker code allowing us to verify your score matches what you submitted in the writeup.pdf. We might also try to run your code on other datasets to further examine its correctness.