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
Binary file added Debug/project2_kernel.pdb
Binary file not shown.
110 changes: 6 additions & 104 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,82 +1,7 @@
Project-2
=========

A Study in Parallel Algorithms : Stream Compaction

# INTRODUCTION
Many of the algorithms you have learned thus far in your career have typically
been developed from a serial standpoint. When it comes to GPUs, we are mainly
looking at massively parallel work. Thus, it is necessary to reorient our
thinking. In this project, we will be implementing a couple different versions
of prefix sum. We will start with a simple single thread serial CPU version,
and then move to a naive GPU version. Each part of this homework is meant to
follow the logic of the previous parts, so please do not do this homework out of
order.

This project will serve as a stream compaction library that you may use (and
will want to use) in your
future projects. For that reason, we suggest you create proper header and CUDA
files so that you can reuse this code later. You may want to create a separate
cpp file that contains your main function so that you can test the code you
write.

# OVERVIEW
Stream compaction is broken down into two parts: (1) scan, and (2) scatter.

## SCAN
Scan or prefix sum is the summation of the elements in an array such that the
resulting array is the summation of the terms before it. Prefix sum can either
be inclusive, meaning the current term is a summation of all the elements before
it and itself, or exclusive, meaning the current term is a summation of all
elements before it excluding itself.

Inclusive:

In : [ 3 4 6 7 9 10 ]

Out : [ 3 7 13 20 29 39 ]

Exclusive

In : [ 3 4 6 7 9 10 ]

Out : [ 0 3 7 13 20 29 ]

Note that the resulting prefix sum will always be n + 1 elements if the input
array is of length n. Similarly, the first element of the exclusive prefix sum
will always be 0. In the following sections, all references to prefix sum will
be to the exclusive version of prefix sum.

## SCATTER
The scatter section of stream compaction takes the results of the previous scan
in order to reorder the elements to form a compact array.

For example, let's say we have the following array:
[ 0 0 3 4 0 6 6 7 0 1 ]

We would only like to consider the non-zero elements in this zero, so we would
like to compact it into the following array:
[ 3 4 6 6 7 1 ]

We can perform a transform on input array to transform it into a boolean array:

In : [ 0 0 3 4 0 6 6 7 0 1 ]

Out : [ 0 0 1 1 0 1 1 1 0 1 ]

Performing a scan on the output, we get the following array :

In : [ 0 0 1 1 0 1 1 1 0 1 ]

Out : [ 0 0 0 1 2 2 3 4 5 5 ]

Notice that the output array produces a corresponding index array that we can
use to create the resulting array for stream compaction.

# PART 1 : REVIEW OF PREFIX SUM
Given the definition of exclusive prefix sum, please write a serial CPU version
of prefix sum. You may write this in the cpp file to separate this from the
CUDA code you will be writing in your .cu file.

# PART 2 : NAIVE PREFIX SUM
We will now parallelize this the previous section's code. Recall from lecture
Expand All @@ -89,45 +14,22 @@ you are NOT allowed to use shared memory.
* Plot a graph of the comparison and write a short explanation of the phenomenon you
see here.

# PART 3 : OPTIMIZING PREFIX SUM
In the previous section we did not take into account shared memory. In the
previous section, we kept everything in global memory, which is much slower than
shared memory.

## PART 3a : Write prefix sum for a single block
Shared memory is accessible to threads of a block. Please write a version of
prefix sum that works on a single block.
Solution: I just printed the data table for comparing the serial CPU prefix sum and GPU Naive version.
From the data table we can see in the beginning when size is pretty small(less than 10^4), the CPU version cost very little time for the prefix sum. But GPU naive one cost much more. However, with the size growing to 10^6 or 10^7, the GPU naive one seems cost as same as the CPU version. We can predict the CPU prefix sum will cost more time when the size is big enough.
It is because the CPU version use only one thread but GPU version use multiple threads. When size is big, CPU will be much slower.

## PART 3b : Generalizing to arrays of any length.
Taking the previous portion, please write a version that generalizes prefix sum
to arbitrary length arrays, this includes arrays that will not fit on one block.

### Questions
* Compare this version to the parallel prefix sum using global memory.
* Plot a graph of the comparison and write a short explanation of the phenomenon
you see here.

# PART 4 : ADDING SCATTER
First create a serial version of scatter by expanding the serial version of
prefix sum. Then create a GPU version of scatter. Combine the function call
such that, given an array, you can call stream compact and it will compact the
array for you. Finally, write a version using thrust.
Solution: The global memory is used by multiple blocks, but the shared memory is only used within the single block. The threads in the single block access the shared memory much faster than accessing the global memory. So the single block version is faster.


### Questions
* Compare your version of stream compact to your version using thrust. How do
they compare? How might you optimize yours more, or how might thrust's stream
compact be optimized.

# EXTRA CREDIT (+10)
For extra credit, please optimize your prefix sum for work parallelism and to
deal with bank conflicts. Information on this can be found in the GPU Gems
chapter listed in the references.

# SUBMISSION
Please answer all the questions in each of the subsections above and write your
answers in the README by overwriting the README file. In future projects, we
expect your analysis to be similar to the one we have led you through in this
project. Like other projects, please open a pull request and email Harmony.

# REFERENCES
"Parallel Prefix Sum (Scan) with CUDA." GPU Gems 3.
Solution: There are some compile error in the thrust method I implemented. But I guess the given thrust method of stream compact will be faster.
Binary file added StreamCompaction.sdf
Binary file not shown.
20 changes: 20 additions & 0 deletions StreamCompaction.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StreamCompaction", "StreamCompaction\StreamCompaction.vcxproj", "{2CD2760F-4832-4806-8672-CA4B72058A00}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2CD2760F-4832-4806-8672-CA4B72058A00}.Debug|Win32.ActiveCfg = Debug|Win32
{2CD2760F-4832-4806-8672-CA4B72058A00}.Debug|Win32.Build.0 = Debug|Win32
{2CD2760F-4832-4806-8672-CA4B72058A00}.Release|Win32.ActiveCfg = Release|Win32
{2CD2760F-4832-4806-8672-CA4B72058A00}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Binary file added StreamCompaction.suo
Binary file not shown.
Binary file added StreamCompaction.v11.suo
Binary file not shown.
Binary file added StreamCompaction/Debug/CL.read.1.tlog
Binary file not shown.
Binary file added StreamCompaction/Debug/CL.write.1.tlog
Binary file not shown.
15 changes: 15 additions & 0 deletions StreamCompaction/Debug/StreamCompaction.Build.CppClean.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
C:\USERS\JIAWEIWANG\DESKTOP\CS565\CS565PROJECT-2\STREAMCOMPACTION\STREAMCOMPACTION\DEBUG\VC100.PDB
C:\Users\JiaweiWang\Desktop\cs565\cs565Project-2\StreamCompaction\StreamCompaction\Debug\prefixSum.cu.cache
C:\Users\JiaweiWang\Desktop\cs565\cs565Project-2\StreamCompaction\StreamCompaction\Debug\project2_kernel.cu.cache
C:\Users\JiaweiWang\Desktop\cs565\cs565Project-2\StreamCompaction\StreamCompaction\Debug\scatter.cu.cache
C:\Users\JiaweiWang\Desktop\cs565\cs565Project-2\StreamCompaction\StreamCompaction\Debug\streamCompact.cu.cache
C:\Users\JiaweiWang\Desktop\cs565\cs565Project-2\StreamCompaction\StreamCompaction\Debug\StreamCompaction.cu.cache
C:\Users\JiaweiWang\Desktop\cs565\cs565Project-2\StreamCompaction\StreamCompaction\Debug\prefixSum.cu.obj
C:\Users\JiaweiWang\Desktop\cs565\cs565Project-2\StreamCompaction\StreamCompaction\Debug\project2_kernel.cu.obj
C:\Users\JiaweiWang\Desktop\cs565\cs565Project-2\StreamCompaction\StreamCompaction\Debug\scatter.cu.obj
C:\Users\JiaweiWang\Desktop\cs565\cs565Project-2\StreamCompaction\StreamCompaction\Debug\streamCompact.cu.obj
C:\Users\JiaweiWang\Desktop\cs565\cs565Project-2\StreamCompaction\StreamCompaction\Debug\StreamCompaction.cu.obj
C:\Users\JiaweiWang\Desktop\cs565\cs565Project-2\StreamCompaction\StreamCompaction\Debug\cl.command.1.tlog
C:\Users\JiaweiWang\Desktop\cs565\cs565Project-2\StreamCompaction\StreamCompaction\Debug\CL.read.1.tlog
C:\Users\JiaweiWang\Desktop\cs565\cs565Project-2\StreamCompaction\StreamCompaction\Debug\CL.write.1.tlog
C:\Users\JiaweiWang\Desktop\cs565\cs565Project-2\StreamCompaction\StreamCompaction\Debug\vc100.idb
49 changes: 49 additions & 0 deletions StreamCompaction/Debug/StreamCompaction.cu.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Identity=StreamCompaction.cu
AdditionalCompilerOptions=
AdditionalCompilerOptions=
AdditionalDependencies=
AdditionalDeps=
AdditionalLibraryDirectories=
AdditionalOptions=
AdditionalOptions=
CInterleavedPTX=false
CodeGeneration=compute_10,sm_10
CodeGeneration=compute_10,sm_10
CompileOut=Debug\StreamCompaction.cu.obj
CudaRuntime=Static
CudaToolkitCustomDir=
Defines=;WIN32;_DEBUG;_CONSOLE;_UNICODE;UNICODE;
Emulation=false
FastMath=false
GenerateLineInfo=false
GenerateRelocatableDeviceCode=false
GPUDebugInfo=true
GPUDebugInfo=true
HostDebugInfo=true
Include=;;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include
Inputs=
Keep=false
KeepDir=Debug
LinkOut=
MaxRegCount=0
NvccCompilation=compile
NvccPath=
Optimization=Od
Optimization=Od
PerformDeviceLink=
PtxAsOptionV=false
RequiredIncludes=
Runtime=MDd
Runtime=MDd
RuntimeChecks=RTC1
RuntimeChecks=RTC1
TargetMachinePlatform=32
TargetMachinePlatform=32
TypeInfo=
TypeInfo=
UseHostDefines=true
UseHostInclude=true
UseHostLibraryDependencies=
UseHostLibraryDirectories=
Warning=W3
Warning=W3
Loading