Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 96 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,102 @@ CUDA Stream Compaction

**University of Pennsylvania, CIS 565: GPU Programming and Architecture, Project 2**

* (TODO) YOUR NAME HERE
* Tested on: (TODO) Windows 22, i7-2222 @ 2.22GHz 22GB, GTX 222 222MB (Moore 2222 Lab)
* Carolina Zheng
* Tested on: Windows 7, i7-6700 @ 3.40GHz 16GB, Quadro K620 (Moore 100 Lab)

### (TODO: Your README)
### Performance Analysis

Include analysis, etc. (Remember, this is public, so don't put
anything here that you don't want to share with the world.)
I determined the runtimes by running each implementation on arrays seeded with fixed values and averaging the times over 10 runs.

#### Block Size vs. Performance

![](img/block-size.png)


The optimal block size for the naive implementation was 64 threads vs. 32 threads for the work-efficient implementation. I was surprised by the fact that a small block size was optimal: I would have expected smaller block sizes to decrease performance because it would max out the block slots on each SM before maxing out the thread slots. The runtime difference between block sizes was very small (<1 ms), however, which makes me think that block size didn't factor in too much overall.

In terms of occupancy, the limiting factor was likely thread slots. My kernel functions didn't use shared memory and used few registers.

#### Scan Implementation vs. Performance

![](img/scan-runtime.png)


| Array size (2^x) | CPU | Naive | Efficient | Thrust |
|:----------------:| ---:|------:|----------:|-------:|
| 4 | 0.00033 | 0.0453 | 0.0694 | 1.183 |
| 8 | 0.00228 | 0.0712 | 0.127 | 1.106 |
| 12 | 0.00982 | 0.175 | 0.2007 | 2.002 |
| 16 | 0.166 | 2.091 | 0.595 | 12.76 |
| 18 | 0.853 | 8.757 | 1.528 | 25.78 |
| 20 | 2.56 | 37.38 | 5.82 | 78.57 |
| 21 | 4.96 | 77.58 | 11.43 | 148.22 |
| 22 | 10.28 | - | - | 289.54 |


Overall, the CPU implementation had the best performance, following by my work-efficient GPU implementation, then the naive GPU implementation, and finally the thrust library call.

The results for work-efficient vs. naive were as expected. On small arrays, the naive implementation was faster. However, once the array size increased from 2^12 to 2^16, the naive scan jumped in runtime by a factor of 20, while the efficient runtime only increased by a factor of 3. As the array size kept doubling, the two implementations' performance deteriorated by about the same multiplier, but because of this initial jump, the efficient scan outperformed the naive scan by a factor of about 7 for array sizes of 2^18 to 2^21. My GPU implementations failed to run on array size 2^22.

The CPU implementation's runtime increased rather smoothly as array size increased, and did not exhibit any jumps.

The thrust library call was much slower than the other implementations: on large array sizes, it was twice as slow as the naive scan and slower by a factor of over 10 compared to the work-efficient scan. It scaled well to increasing array size.

I believe that the performance bottlenecks were different for CPU vs. GPU implementations vs. thrust. For the CPU, computation time was probably the bottleneck due to the fact that all operations had to be performed serially. For the naive and work-efficient scans, I think global memory access was the bottleneck. GPU compute capability is much better than memory accesses, and the kernels I wrote in particular were independent of each other and consisted of only a few floating-point computations, as well as global memory reads and writes. I also used hardware-optimized branching, which should minimize divergence within warps. I was surprised at how slow the thrust library call was. It's likely that the library call is using an optimized algorithm under the hood, but may have been slowed down due to layers of other function calls, error checking, etc. It's hard to say exactly why it was slow without examining the source code.

To mitigate the bottlenecks of my GPU implementations, I could improve my code by utilizing shared memory instead of global memory. I'm also interested in whether moving the loops from the host to the device would increase performance. Also, I could refactor my work-efficient stream compaction so that there is no memory copying between host and device, which might make it faster than the CPU compaction.

#### Test Program Output
The following is a sample test program output from `main.cpp` for an array of size 2^20.

```
****************
** SCAN TESTS **
****************
[ 1 42 48 47 9 36 13 34 28 39 3 2 38 ... 1 0 ]
==== cpu scan, power-of-two ====
elapsed time: 2.4675ms (std::chrono Measured)
==== cpu scan, non-power-of-two ====
elapsed time: 2.47652ms (std::chrono Measured)
passed
==== naive scan, power-of-two ====
elapsed time: 38.5892ms (CUDA Measured)
passed
==== naive scan, non-power-of-two ====
elapsed time: 37.0342ms (CUDA Measured)
passed
==== work-efficient scan, power-of-two ====
elapsed time: 6.30717ms (CUDA Measured)
passed
==== work-efficient scan, non-power-of-two ====
elapsed time: 6.17421ms (CUDA Measured)
passed
==== thrust scan, power-of-two ====
elapsed time: 84.909ms (CUDA Measured)
passed
==== thrust scan, non-power-of-two ====
elapsed time: 78.5352ms (CUDA Measured)
passed


*****************************
** STREAM COMPACTION TESTS **
*****************************
[ 0 0 0 3 1 0 1 3 1 0 0 2 0 ... 2 0 ]
==== cpu compact without scan, power-of-two ====
elapsed time: 4.67753ms (std::chrono Measured)
passed
==== cpu compact without scan, non-power-of-two ====
elapsed time: 4.78749ms (std::chrono Measured)
passed
==== cpu compact with scan ====
elapsed time: 12.9883ms (std::chrono Measured)
passed
==== work-efficient compact, power-of-two ====
elapsed time: 13.6215ms (CUDA Measured)
passed
==== work-efficient compact, non-power-of-two ====
elapsed time: 13.4029ms (CUDA Measured)
passed
Press any key to continue . . .
```
Binary file added img/block-size.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/scan-runtime.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 66 additions & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#include <stream_compaction/thrust.h>
#include "testing_helpers.hpp"

const int SIZE = 1 << 8; // feel free to change the size of array
const int SIZE = 1 << 20; // feel free to change the size of array
//const int SIZE = 8;
const int NPOT = SIZE - 3; // Non-Power-Of-Two
int a[SIZE], b[SIZE], c[SIZE];

Expand All @@ -29,20 +30,74 @@ int main(int argc, char* argv[]) {
a[SIZE - 1] = 0;
printArray(SIZE, a, true);

// initialize b using StreamCompaction::CPU::scan you implement
// We use b for further comparison. Make sure your StreamCompaction::CPU::scan is correct.
// At first all cases passed because b && c are all zeroes.
float cpuTime = 0;
float naiveTime = 0;
float efficientTime = 0;
float thrustTime = 0;

//for (int i = 0; i < 10; i++)
//{
// genArray(SIZE - 1, a, 50, i); // Leave a 0 at the end to test that edge case
// a[SIZE - 1] = 0;
// printArray(SIZE, a, true);

// zeroArray(SIZE, b);
// //printDesc("cpu scan, power-of-two");
// StreamCompaction::CPU::scan(SIZE, b, a);
// //printElapsedTime(StreamCompaction::CPU::timer().getCpuElapsedTimeForPreviousOperation(), "(std::chrono Measured)");
// //printArray(SIZE, b, true);

// zeroArray(SIZE, c);
// //printDesc("naive scan, power-of-two");
// StreamCompaction::Naive::scan(SIZE, c, a);
// //printElapsedTime(StreamCompaction::Naive::timer().getGpuElapsedTimeForPreviousOperation(), "(CUDA Measured)");
// //printArray(SIZE, c, true);
// //printCmpResult(SIZE, b, c);

// zeroArray(SIZE, c);
// //printDesc("work-efficient scan, power-of-two");
// StreamCompaction::Efficient::scan(SIZE, c, a);
// //printElapsedTime(StreamCompaction::Efficient::timer().getGpuElapsedTimeForPreviousOperation(), "(CUDA Measured)");
// //printArray(SIZE, c, true);
// //printCmpResult(SIZE, b, c);

// zeroArray(SIZE, c);
// //printDesc("thrust scan, power-of-two");
// StreamCompaction::Thrust::scan(SIZE, c, a);
// //printElapsedTime(StreamCompaction::Thrust::timer().getGpuElapsedTimeForPreviousOperation(), "(CUDA Measured)");
// //printArray(SIZE, c, true);
// //printCmpResult(SIZE, b, c);

// cpuTime += StreamCompaction::CPU::timer().getCpuElapsedTimeForPreviousOperation();
// naiveTime += StreamCompaction::Naive::timer().getGpuElapsedTimeForPreviousOperation();
// efficientTime += StreamCompaction::Efficient::timer().getGpuElapsedTimeForPreviousOperation();
// thrustTime += StreamCompaction::Thrust::timer().getGpuElapsedTimeForPreviousOperation();
//}

//cpuTime /= 10.f;
//naiveTime /= 10.f;
//efficientTime /= 10.f;
//thrustTime /= 10.f;

//printf("CPU time: %f\n", cpuTime);
//printf("Naive time: %f\n", naiveTime);
//printf("Efficient time: %f\n", efficientTime);
//printf("Thrust time: %f\n", thrustTime);

//initialize b using StreamCompaction::CPU::scan you implement
//We use b for further comparison. Make sure your StreamCompaction::CPU::scan is correct.
//At first all cases passed because b && c are all zeroes.
zeroArray(SIZE, b);
printDesc("cpu scan, power-of-two");
StreamCompaction::CPU::scan(SIZE, b, a);
printElapsedTime(StreamCompaction::CPU::timer().getCpuElapsedTimeForPreviousOperation(), "(std::chrono Measured)");
printArray(SIZE, b, true);
//printArray(SIZE, b, true);

zeroArray(SIZE, c);
printDesc("cpu scan, non-power-of-two");
StreamCompaction::CPU::scan(NPOT, c, a);
printElapsedTime(StreamCompaction::CPU::timer().getCpuElapsedTimeForPreviousOperation(), "(std::chrono Measured)");
printArray(NPOT, b, true);
//printArray(NPOT, b, true);
printCmpResult(NPOT, b, c);

zeroArray(SIZE, c);
Expand Down Expand Up @@ -100,29 +155,29 @@ int main(int argc, char* argv[]) {

int count, expectedCount, expectedNPOT;

// initialize b using StreamCompaction::CPU::compactWithoutScan you implement
// We use b for further comparison. Make sure your StreamCompaction::CPU::compactWithoutScan is correct.
//initialize b using StreamCompaction::CPU::compactWithoutScan you implement
//We use b for further comparison. Make sure your StreamCompaction::CPU::compactWithoutScan is correct.
zeroArray(SIZE, b);
printDesc("cpu compact without scan, power-of-two");
count = StreamCompaction::CPU::compactWithoutScan(SIZE, b, a);
printElapsedTime(StreamCompaction::CPU::timer().getCpuElapsedTimeForPreviousOperation(), "(std::chrono Measured)");
expectedCount = count;
printArray(count, b, true);
//printArray(count, b, true);
printCmpLenResult(count, expectedCount, b, b);

zeroArray(SIZE, c);
printDesc("cpu compact without scan, non-power-of-two");
count = StreamCompaction::CPU::compactWithoutScan(NPOT, c, a);
printElapsedTime(StreamCompaction::CPU::timer().getCpuElapsedTimeForPreviousOperation(), "(std::chrono Measured)");
expectedNPOT = count;
printArray(count, c, true);
//printArray(count, c, true);
printCmpLenResult(count, expectedNPOT, b, c);

zeroArray(SIZE, c);
printDesc("cpu compact with scan");
count = StreamCompaction::CPU::compactWithScan(SIZE, c, a);
printElapsedTime(StreamCompaction::CPU::timer().getCpuElapsedTimeForPreviousOperation(), "(std::chrono Measured)");
printArray(count, c, true);
//printArray(count, c, true);
printCmpLenResult(count, expectedCount, b, c);

zeroArray(SIZE, c);
Expand Down
1 change: 1 addition & 0 deletions src/testing_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void zeroArray(int n, int *a) {

void genArray(int n, int *a, int maxval) {
srand(time(nullptr));
//srand(seed);

for (int i = 0; i < n; i++) {
a[i] = rand() % maxval;
Expand Down
21 changes: 19 additions & 2 deletions stream_compaction/common.cu
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ namespace StreamCompaction {
* which map to 0 will be removed, and elements which map to 1 will be kept.
*/
__global__ void kernMapToBoolean(int n, int *bools, const int *idata) {
// TODO
int index = (blockIdx.x * blockDim.x) + threadIdx.x;

if (index >= n)
{
return;
}

bools[index] = idata[index] != 0 ? 1 : 0;
}

/**
Expand All @@ -32,7 +39,17 @@ namespace StreamCompaction {
*/
__global__ void kernScatter(int n, int *odata,
const int *idata, const int *bools, const int *indices) {
// TODO
int index = (blockIdx.x * blockDim.x) + threadIdx.x;

if (index >= n)
{
return;
}

if (bools[index] == 1)
{
odata[indices[index]] = idata[index];
}
}

}
Expand Down
59 changes: 52 additions & 7 deletions stream_compaction/cpu.cu
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ namespace StreamCompaction {
* (Optional) For better understanding before starting moving to GPU, you can simulate your GPU scan in this function first.
*/
void scan(int n, int *odata, const int *idata) {
timer().startCpuTimer();
// TODO
timer().endCpuTimer();
//timer().startCpuTimer();

odata[0] = 0;

for (int i = 1; i < n; i++)
{
odata[i] = odata[i - 1] + idata[i - 1];
}

//timer().endCpuTimer();
}

/**
Expand All @@ -29,10 +36,21 @@ namespace StreamCompaction {
* @returns the number of elements remaining after compaction.
*/
int compactWithoutScan(int n, int *odata, const int *idata) {
int j = 0;

timer().startCpuTimer();
// TODO

for (int i = 0; i < n; i++)
{
if (idata[i] != 0)
{
odata[j++] = idata[i];
}
}

timer().endCpuTimer();
return -1;

return j;
}

/**
Expand All @@ -41,10 +59,37 @@ namespace StreamCompaction {
* @returns the number of elements remaining after compaction.
*/
int compactWithScan(int n, int *odata, const int *idata) {
int *scanResult = new int[n];
int j = 0;

timer().startCpuTimer();
// TODO

for (int i = 0; i < n; i++)
{
odata[i] = idata[i] == 0 ? 0 : 1;
}

scan(n, scanResult, odata);

for (int i = 0; i < n-1; i++)
{
if (odata[i] == 1)
{
odata[scanResult[i]] = idata[i];
j++;
}
}

if (odata[n - 1] == 1)
{
odata[scanResult[n - 1] + 1] = idata[n - 1];
j++;
}

timer().endCpuTimer();
return -1;

free(scanResult);
return j;
}
}
}
Loading