Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project 2: Zach Corse #6

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
104 changes: 98 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,104 @@ CUDA Stream Compaction

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

* (TODO) YOUR NAME HERE
* (TODO) [LinkedIn](), [personal website](), [twitter](), etc.
* Tested on: (TODO) Windows 22, i7-2222 @ 2.22GHz 22GB, GTX 222 222MB (Moore 2222 Lab)
* Zach Corse
* LinkedIn: https://www.linkedin.com/in/wzcorse/
* Personal Website: https://wzcorse.com
* Twitter: @ZachCorse
* Tested on: Windows 10, i7-6700HQ @ 2.60GHz 32GB, NVIDIA GeForce GTX 970M (personal computer)

### (TODO: Your README)
## README

Include analysis, etc. (Remember, this is public, so don't put
anything here that you don't want to share with the world.)
Introduction
------------

In this project I take a stab at implementing a scan algorithm on the GPU and compare the performance with a straightforward implementation on the CPU. For those not familiar with scan, this algorithm accepts an array idata of numbers then returns an array odata such that each element in odata is the sum of all elements in idata that precede the index in consideration (specifically, this is known as an exclusive scan, because the value stored at the index in consideration in idata is not included in the sum).

I also include a parallel implementation of the compact algorithm, which uses scan as part of its implementation. This algorithm accepts an array of data, converts this array into an array of booleans according to a predefined rule (in this case, is the value zero or not), then "removes" the values that are false, thereby compacting the original array into one which preserves the array's remaining useful information.

As shown here, my implementations pass the tests designed to check accuracy and measure timing for each algorithm. Speed comparisons are discussed in detail below.

```
****************
** SCAN TESTS **
****************
[ 43 1 21 0 10 41 43 1 21 16 31 44 19 ... 6 0 ]
==== cpu scan, power-of-two ====
elapsed time: 0.00079ms (std::chrono Measured)
[ 0 43 44 65 65 75 116 159 160 181 197 228 272 ... 6155 6161 ]
==== cpu scan, non-power-of-two ====
elapsed time: 0.000395ms (std::chrono Measured)
[ 0 43 44 65 65 75 116 159 160 181 197 228 272 ... 6077 6104 ]
passed
==== naive scan, power-of-two ====
elapsed time: 0.04864ms (CUDA Measured)
passed
==== naive scan, non-power-of-two ====
elapsed time: 0.047904ms (CUDA Measured)
passed
==== work-efficient scan, power-of-two ====
elapsed time: 0.20016ms (CUDA Measured)
passed
==== work-efficient scan, non-power-of-two ====
elapsed time: 0.183488ms (CUDA Measured)
passed
==== thrust scan, power-of-two ====
elapsed time: 0.022336ms (CUDA Measured)
passed
==== thrust scan, non-power-of-two ====
elapsed time: 0.02464ms (CUDA Measured)
passed

*****************************
** STREAM COMPACTION TESTS **
*****************************
[ 3 1 3 0 2 3 1 3 3 0 1 2 3 ... 2 0 ]
==== cpu compact without scan, power-of-two ====
elapsed time: 0.001185ms (std::chrono Measured)
[ 3 1 3 2 3 1 3 3 1 2 3 1 1 ... 2 2 ]
passed
==== cpu compact without scan, non-power-of-two ====
elapsed time: 0.001185ms (std::chrono Measured)
[ 3 1 3 2 3 1 3 3 1 2 3 1 1 ... 1 3 ]
passed
==== cpu compact with scan ====
elapsed time: 0.00158ms (std::chrono Measured)
[ 3 1 3 2 3 1 3 3 1 2 3 1 1 ... 2 2 ]
passed
==== work-efficient compact, power-of-two ====
elapsed time: 0.237024ms (CUDA Measured)
passed
==== work-efficient compact, non-power-of-two ====
elapsed time: 0.276832ms (CUDA Measured)
passed
```

Scan Performance Analysis
------------

Here I compare four different implementations of the scan algorithm. The first is on the CPU, and is straightforward:

```
void scan(int n, int *odata, const int *idata) {
timer().startCpuTimer();
odata[0] = 0;
for (int k = 1; k < n; ++k) {
odata[k] = idata[k - 1] + odata[k - 1];
}
timer().endCpuTimer();
}

```

On the GPU, I compare three scan implemenations. The first is "naive." In parallel, it loops over an array log(n) times, keeping track of an offset for each loop that scales as 2^(d-1) where d is the loop index. The "work-efficient" scan uses an algorithm developed in 1978 that first applies an "upsweep" to the array (which is equivalent to parallel reduction) then follows with a downsweep, which cleverly computes the scanned array. In theory, this should be faster than the naive implementation. Finally, I call the thrust library's scan algorithm as a gold standard to which I can compare my implementation.

![graph1](img/scanCompare.png)

The results are counterintuitive but informative. The CPU implementation is actually faster for sufficiently small arrays. Thrust outperforms the CPU for sufficiently large arrays, however. My naive implementation is slightly less efficient than thrust, but scales similarly. My supposedly "work efficient" implementation, however is much less efficient than all three. I can attribute this to several factors: first and foremost, my implemenation does not use blocking and shared memory. Instead, each thread must access and write values stored in global memory, which is far slower than reading and writing to shared memory per block. Additionally, for each loop d in upsweep and downsweep, I launch a thread for each index in the array. This is highly inefficient, as the greater d is, the fewer the indices that actually affect up/downsweep, which both sum and modify indices in intervals of 2^(d+1) (a binary tree structure). These indices are also distributed across multiple blocks, further indicating that executing scan recursively per block would lead to a significant speedup.

Compact Performance Analysis
------------

As shown below, my CPU compact implementation outpeforms my parallel compact implementation which relies on my "work-efficient" scan implementation (and is therefore consistently slower). My CPU implementation that uses my CPU scan implementation rather than a more straightforward naive approach (CPU - no scan) is in fact slower, which is most likely due to the fact that scan requires that I generate two additional heap-based arrays to manage intermediate boolean and scanned boolean arrays.

![graph1](img/compactCompare.png)
Binary file added img/compactCompare.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/scanCompare.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 31 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
#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 << 19; // feel free to change the size of array
const int NPOT = SIZE - 3; // Non-Power-Of-Two
int *a = new int[SIZE];
int *b = new int[SIZE];
int *c = new int[SIZE];

int *cudaDataArrayA; // device array for read/write during scan
int *cudaDataArrayB; // device array for read/write during scan

int main(int argc, char* argv[]) {
// Scan tests

Expand All @@ -31,6 +34,14 @@ int main(int argc, char* argv[]) {
a[SIZE - 1] = 0;
printArray(SIZE, a, true);

// initialize CUDA arrays
cudaMalloc((void**)&cudaDataArrayA, SIZE * sizeof(int));
checkCUDAErrorFn("cudaMalloc tempA failed!");
cudaMalloc((void**)&cudaDataArrayB, SIZE * sizeof(int));
checkCUDAErrorFn("cudaMalloc tempB failed!");

cudaDeviceSynchronize();

// 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.
Expand All @@ -47,9 +58,10 @@ int main(int argc, char* argv[]) {
printArray(NPOT, b, true);
printCmpResult(NPOT, b, c);

cudaMemcpy(cudaDataArrayA, a, SIZE * sizeof(int), cudaMemcpyHostToDevice);
zeroArray(SIZE, c);
printDesc("naive scan, power-of-two");
StreamCompaction::Naive::scan(SIZE, c, a);
StreamCompaction::Naive::scan(SIZE, c, a, cudaDataArrayA, cudaDataArrayB);
printElapsedTime(StreamCompaction::Naive::timer().getGpuElapsedTimeForPreviousOperation(), "(CUDA Measured)");
//printArray(SIZE, c, true);
printCmpResult(SIZE, b, c);
Expand All @@ -60,27 +72,30 @@ int main(int argc, char* argv[]) {
StreamCompaction::Naive::scan(SIZE, c, a);
printArray(SIZE, c, true); */

zeroArray(SIZE, c);
cudaMemcpy(cudaDataArrayA, a, SIZE * sizeof(int), cudaMemcpyHostToDevice);
zeroArray(SIZE, c);
printDesc("naive scan, non-power-of-two");
StreamCompaction::Naive::scan(NPOT, c, a);
StreamCompaction::Naive::scan(NPOT, c, a, cudaDataArrayA, cudaDataArrayB);
printElapsedTime(StreamCompaction::Naive::timer().getGpuElapsedTimeForPreviousOperation(), "(CUDA Measured)");
//printArray(SIZE, c, true);
printCmpResult(NPOT, b, c);

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

cudaMemcpy(cudaDataArrayA, a, SIZE * sizeof(int), cudaMemcpyHostToDevice);
zeroArray(SIZE, c);
printDesc("work-efficient scan, non-power-of-two");
StreamCompaction::Efficient::scan(NPOT, c, a);
StreamCompaction::Efficient::scan(SIZE, NPOT, c, a, cudaDataArrayA);
printElapsedTime(StreamCompaction::Efficient::timer().getGpuElapsedTimeForPreviousOperation(), "(CUDA Measured)");
//printArray(NPOT, c, true);
printCmpResult(NPOT, b, c);

zeroArray(SIZE, c);
printDesc("thrust scan, power-of-two");
StreamCompaction::Thrust::scan(SIZE, c, a);
Expand Down Expand Up @@ -133,16 +148,20 @@ int main(int argc, char* argv[]) {
printArray(count, c, true);
printCmpLenResult(count, expectedCount, b, c);

cudaMemcpy(cudaDataArrayA, a, SIZE * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(cudaDataArrayB, a, SIZE * sizeof(int), cudaMemcpyHostToDevice);
zeroArray(SIZE, c);
printDesc("work-efficient compact, power-of-two");
count = StreamCompaction::Efficient::compact(SIZE, c, a);
count = StreamCompaction::Efficient::compact(SIZE, SIZE, c, a, cudaDataArrayA, cudaDataArrayB);
printElapsedTime(StreamCompaction::Efficient::timer().getGpuElapsedTimeForPreviousOperation(), "(CUDA Measured)");
//printArray(count, c, true);
printCmpLenResult(count, expectedCount, b, c);

cudaMemcpy(cudaDataArrayA, a, SIZE * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(cudaDataArrayB, a, SIZE * sizeof(int), cudaMemcpyHostToDevice);
zeroArray(SIZE, c);
printDesc("work-efficient compact, non-power-of-two");
count = StreamCompaction::Efficient::compact(NPOT, c, a);
count = StreamCompaction::Efficient::compact(SIZE, NPOT, c, a, cudaDataArrayA, cudaDataArrayB);
printElapsedTime(StreamCompaction::Efficient::timer().getGpuElapsedTimeForPreviousOperation(), "(CUDA Measured)");
//printArray(count, c, true);
printCmpLenResult(count, expectedNPOT, b, c);
Expand All @@ -151,4 +170,7 @@ int main(int argc, char* argv[]) {
delete[] a;
delete[] b;
delete[] c;

cudaFree(cudaDataArrayA);
cudaFree(cudaDataArrayB);
}
2 changes: 1 addition & 1 deletion stream_compaction/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ set(SOURCE_FILES

cuda_add_library(stream_compaction
${SOURCE_FILES}
OPTIONS -arch=sm_20
OPTIONS -arch=sm_50
)
4 changes: 2 additions & 2 deletions stream_compaction/common.cu
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ void checkCUDAErrorFn(const char *msg, const char *file, int line) {
exit(EXIT_FAILURE);
}


namespace StreamCompaction {
namespace Common {

Expand All @@ -24,6 +23,7 @@ namespace StreamCompaction {
*/
__global__ void kernMapToBoolean(int n, int *bools, const int *idata) {
// TODO
// written in efficient.cu
}

/**
Expand All @@ -33,7 +33,7 @@ namespace StreamCompaction {
__global__ void kernScatter(int n, int *odata,
const int *idata, const int *bools, const int *indices) {
// TODO
// written in efficient.cu
}

}
}
2 changes: 2 additions & 0 deletions stream_compaction/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#define FILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define checkCUDAError(msg) checkCUDAErrorFn(msg, FILENAME, __LINE__)

#define blocksize 512

/**
* Check for CUDA errors; print and exit if there was a problem.
*/
Expand Down
63 changes: 51 additions & 12 deletions stream_compaction/cpu.cu
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#include <cstdio>
#include "cpu.h"

#include "common.h"
#include "common.h"

namespace StreamCompaction {
namespace CPU {
using StreamCompaction::Common::PerformanceTimer;
PerformanceTimer& timer()
{
static PerformanceTimer timer;
return timer;
using StreamCompaction::Common::PerformanceTimer;
PerformanceTimer& timer()
{
static PerformanceTimer timer;
return timer;
}

/**
Expand All @@ -19,7 +19,10 @@ namespace StreamCompaction {
*/
void scan(int n, int *odata, const int *idata) {
timer().startCpuTimer();
// TODO
odata[0] = 0;
for (int k = 1; k < n; ++k) {
odata[k] = idata[k - 1] + odata[k - 1];
}
timer().endCpuTimer();
}

Expand All @@ -31,8 +34,15 @@ namespace StreamCompaction {
int compactWithoutScan(int n, int *odata, const int *idata) {
timer().startCpuTimer();
// TODO
int counter = 0;
for (int k = 0; k < n; ++k) {
if (idata[k] != 0) {
odata[counter] = idata[k];
counter++;
}
}
timer().endCpuTimer();
return -1;
return counter;
}

/**
Expand All @@ -41,10 +51,39 @@ namespace StreamCompaction {
* @returns the number of elements remaining after compaction.
*/
int compactWithScan(int n, int *odata, const int *idata) {
timer().startCpuTimer();
// TODO
timer().endCpuTimer();
return -1;

int *binaryArray = new int[n];
int *scanResult = new int[n];

timer().startCpuTimer();
// prepare temporary binary array
for (int k = 0; k < n; ++k) {
if (idata[k] != 0) {
binaryArray[k] = 1;
}
else {
binaryArray[k] = 0;
}
}
// scan
scanResult[0] = 0;
for (int k = 1; k < n; ++k) {
scanResult[k] = binaryArray[k - 1] + scanResult[k - 1];
}
// scatter
int counter = 0;
for (int k = 0; k < n; ++k) {
if (binaryArray[k] == 1) {
odata[scanResult[k]] = idata[k];
counter++;
}
}
timer().endCpuTimer();

delete binaryArray;
delete scanResult;

return counter;
}
}
}
Loading