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: Wenqing Wang #23

Open
wants to merge 5 commits into
base: main
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
90 changes: 84 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,90 @@ 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)
* Wenqing Wang
* [LinkedIn](https://www.linkedin.com/in/wenqingwang0910/)
* Tested on: Windows 11, i7-11370H @ 3.30GHz 16.0 GB, GTX 3050 Ti

### (TODO: Your README)
# Highlights
* This project implemented the scan (exclusive prefix sum) and string compact based on the following methods.
* CPU scan/compact (for comparision purpose)
* GPU Naïve parallel scan/compact
* GPU Work-efficient scan/compact
* Thrust scan (for comparision purpose)

* A sample output of this project would like this:

Include analysis, etc. (Remember, this is public, so don't put
anything here that you don't want to share with the world.)
(Note: Results are tested on with `blockSize = 256` and `ArraySize = 2^24` )
```
****************
** SCAN TESTS **
****************
[ 5 27 39 37 38 10 22 28 45 5 12 35 19 ... 10 0 ]
==== cpu scan, power-of-two ====
elapsed time: 28.2238ms (std::chrono Measured)
[ 0 5 32 71 108 146 156 178 206 251 256 268 303 ... 410870447 410870457 ]
==== cpu scan, non-power-of-two ====
elapsed time: 27.9835ms (std::chrono Measured)
[ 0 5 32 71 108 146 156 178 206 251 256 268 303 ... 410870378 410870402 ]
passed
==== naive scan, power-of-two ====
elapsed time: 21.7824ms (CUDA Measured)
passed
==== naive scan, non-power-of-two ====
elapsed time: 21.7999ms (CUDA Measured)
passed
==== work-efficient scan, power-of-two ====
elapsed time: 14.9641ms (CUDA Measured)
passed
==== work-efficient scan, non-power-of-two ====
elapsed time: 15.037ms (CUDA Measured)
passed
==== thrust scan, power-of-two ====
elapsed time: 1.55648ms (CUDA Measured)
passed
==== thrust scan, non-power-of-two ====
elapsed time: 1.56058ms (CUDA Measured)
passed

*****************************
** STREAM COMPACTION TESTS **
*****************************
[ 3 3 3 1 0 2 2 2 3 1 2 3 1 ... 0 0 ]
==== cpu compact without scan, power-of-two ====
elapsed time: 42.3925ms (std::chrono Measured)
[ 3 3 3 1 2 2 2 3 1 2 3 1 1 ... 1 3 ]
passed
==== cpu compact without scan, non-power-of-two ====
elapsed time: 42.6625ms (std::chrono Measured)
[ 3 3 3 1 2 2 2 3 1 2 3 1 1 ... 3 1 ]
passed
==== cpu compact with scan ====
elapsed time: 96.9729ms (std::chrono Measured)
[ 3 3 3 1 2 2 2 3 1 2 3 1 1 ... 1 3 ]
passed
==== work-efficient compact, power-of-two ====
elapsed time: 18.041ms (CUDA Measured)
passed
==== work-efficient compact, non-power-of-two ====
elapsed time: 18.0232ms (CUDA Measured)
passed
```
By comparing the time consumed by each method on different array sizes, we can see how much better the GPU performs than the CPU when processing larger data sets.

# Performance Analysis
## Scan Runtime Analysis

![Scan runtime_1](https://user-images.githubusercontent.com/33616958/190931735-eaa086bf-3206-4127-bc64-4d6149c7b746.png)

![Scan runtime_2](https://user-images.githubusercontent.com/33616958/190931738-2ec8f4ee-1242-4e22-af11-851b9f9846af.png)

* From the above diagrams, we can see that when the array size is smaller than `2^16`, the performance of CPU side scan/compact is actually better than that on GPU. This is probably because the GPU implementation involves a lot of read/write operations to global memory, and the advantages of parallel computing are not obvious when targeting smaller data sets. However, as the array size increases, the GPU starts to outperform the CPU, and the gap of their performance keeps widening. The optimized work-efficient method, which involves fewer scans operations, has a shorter execution time compared to the naive method. The trust method has the best performance on large data sets.

## Compact Runtime Analysis

![Compact runtime_1](https://user-images.githubusercontent.com/33616958/190931740-47fa15c7-d5e9-44fe-aeff-c54bc20ae95e.png)

![Compact runtime_2](https://user-images.githubusercontent.com/33616958/190931745-c0073135-8358-4a6b-b8f9-a28b72da9b8d.png)

* As can be seen from the above graphs, the performance of the stream compression algorithm trends similarly to the scanning algorithm as the array size increases.

2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#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 << 24; // 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];
Expand Down
20 changes: 20 additions & 0 deletions stream_compaction/common.cu
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ namespace StreamCompaction {
*/
__global__ void kernMapToBoolean(int n, int *bools, const int *idata) {
// TODO
int index = threadIdx.x + (blockIdx.x * blockDim.x);
if (index >= n) {
return;
}

if (idata[index] == 0) {
bools[index] = 0;
}
else {
bools[index] = 1;
}
}

/**
Expand All @@ -33,6 +44,15 @@ namespace StreamCompaction {
__global__ void kernScatter(int n, int *odata,
const int *idata, const int *bools, const int *indices) {
// TODO
int index = threadIdx.x + (blockIdx.x * blockDim.x);
if (index >= n) {
return;
}

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

}

}
Expand Down
46 changes: 44 additions & 2 deletions stream_compaction/cpu.cu
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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] = odata[k - 1] + idata[k - 1];
}
timer().endCpuTimer();
}

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

/**
Expand All @@ -43,8 +53,40 @@ namespace StreamCompaction {
int compactWithScan(int n, int *odata, const int *idata) {
timer().startCpuTimer();
// TODO

// map
int* omap = new int[n * sizeof(int)];
for (int i = 0; i < n; i++) {
if (idata[i] == 0) {
omap[i] = 0;
}
else {
omap[i] = 1;
}
}

// scan
int* oscan = new int[n * sizeof(int)];
int num = 0;
oscan[0] = 0;
for (int i = 1; i < n; i++) {
oscan[i] = oscan[i - 1] + omap[i - 1];
}

// scatter
for (int i = 0; i < n; i++) {
if (omap[i] != 0) {
odata[oscan[i]] = idata[i];
++num;
}
}

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

free(omap);
free(oscan);

return num;
}
}
}
118 changes: 115 additions & 3 deletions stream_compaction/efficient.cu
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "common.h"
#include "efficient.h"

#define blockSize 256

namespace StreamCompaction {
namespace Efficient {
using StreamCompaction::Common::PerformanceTimer;
Expand All @@ -11,14 +13,67 @@ namespace StreamCompaction {
static PerformanceTimer timer;
return timer;
}
__global__ void kernUpSweep(int n, int* data, int offset)
{
int index = threadIdx.x + (blockIdx.x * blockDim.x);
if (index >= n) {
return;
}

if (index % (2 * offset) == 0) {
int desIdx = index + (2 * offset) - 1;
int srcIdx = index + offset - 1;

data[desIdx] += data[srcIdx];
}
}

__global__ void kernDownSweep(int n, int* data, int offset)
{
int index = threadIdx.x + (blockIdx.x * blockDim.x);
if (index >= n) {
return;
}
if (index % (2 * offset) == 0) {
int t = data[index + offset - 1];
data[index + offset - 1] = data[index + offset * 2 - 1];
data[index + offset * 2 - 1] += t;
}
}

/**
* Performs prefix-sum (aka scan) on idata, storing the result into odata.
*/
void scan(int n, int *odata, const int *idata) {
void scan(int n, int *odata, const int *idata)
{
int maxDepth = ilog2ceil(n);
int maxSize = pow(2, maxDepth);
dim3 fullBlocksPerGrid((maxSize + blockSize - 1) / blockSize);

int *dev_data;
cudaMalloc((void**)&dev_data, maxSize * sizeof(int));
cudaMemcpy(dev_data, idata, maxSize * sizeof(int), cudaMemcpyHostToDevice);

timer().startGpuTimer();
// TODO
// UpSweep
for (int d = 0; d < maxDepth; d++) {
kernUpSweep << < fullBlocksPerGrid, blockSize >> > (maxSize, dev_data, pow(2, d));
}

cudaMemset(dev_data + maxSize - 1, 0, sizeof(int));

// DownSweep
for (int d = maxDepth - 1; d >= 0; d--) {
kernDownSweep << < fullBlocksPerGrid, blockSize >> > (maxSize, dev_data, pow(2, d));
}

timer().endGpuTimer();

cudaMemcpy(odata, dev_data, n * sizeof(int), cudaMemcpyDeviceToHost);

// free cuda memory
cudaFree(dev_data);
}

/**
Expand All @@ -30,11 +85,68 @@ namespace StreamCompaction {
* @param idata The array of elements to compact.
* @returns The number of elements remaining after compaction.
*/
int compact(int n, int *odata, const int *idata) {
int compact(int n, int *odata, const int *idata)
{
int *dev_idata, *dev_odata, *dev_bool, *dev_idx;

int maxDepth = ilog2ceil(n);
int maxSize = pow(2, maxDepth);

dim3 fullBlocksPerGrid((n + blockSize - 1) / blockSize);
dim3 maxBlocksPerGrid((maxSize + blockSize - 1) / blockSize);

cudaMalloc((void**)&dev_idata, maxSize * sizeof(int));
cudaMalloc((void**)&dev_odata, maxSize * sizeof(int));
cudaMalloc((void**)&dev_bool, maxSize * sizeof(int));
cudaMalloc((void**)&dev_idx, maxSize * sizeof(int));

cudaMemcpy(dev_idata, idata, n * sizeof(int), cudaMemcpyHostToDevice);

timer().startGpuTimer();
// TODO

Common::kernMapToBoolean << <fullBlocksPerGrid, blockSize >> > (n, dev_bool, dev_idata);
cudaMemcpy(dev_idx, dev_bool, maxSize * sizeof(int), cudaMemcpyDeviceToDevice);

// Scan
// UpSweep
for (int d = 0; d <= maxDepth - 1; d++) {
kernUpSweep << < maxBlocksPerGrid, blockSize >> > (maxSize, dev_idx, pow(2, d));
}

cudaMemset(dev_idx + maxSize - 1, 0, sizeof(int));

// DownSweep
for (int d = maxDepth - 1; d >= 0; d--) {
kernDownSweep << < maxBlocksPerGrid, blockSize >> > (maxSize, dev_idx, pow(2, d));
}

// Scatter
//scatter
Common::kernScatter << < fullBlocksPerGrid, blockSize >> > (maxSize, dev_odata, dev_idata, dev_bool, dev_idx);


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

cudaMemcpy(odata, dev_odata, sizeof(int) * n, cudaMemcpyDeviceToHost);

// compute num of non-zero element
int* arr = new int[maxSize];
cudaMemcpy(arr, dev_bool, sizeof(int) * maxSize, cudaMemcpyDeviceToHost);

int count = 0;
for (int i = 0; i < maxSize; i++) {
if (arr[i] == 1) {
count++;
}
}
// Free cuda memory
cudaFree(dev_idata);
cudaFree(dev_odata);
cudaFree(dev_bool);
cudaFree(dev_idx);

return count;
}
}
}
Loading