Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Finished cpu.cu
  • Loading branch information
immiao committed Sep 23, 2016
commit e249e876dc42b006db015de3ddd7d29bfcdc7c34
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,6 @@ int main(int argc, char* argv[]) {
count = StreamCompaction::Efficient::compact(NPOT, c, a);
//printArray(count, c, true);
printCmpLenResult(count, expectedNPOT, b, c);

system("pause");
}
56 changes: 53 additions & 3 deletions stream_compaction/cpu.cu
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <cstdio>
#include "stream_compaction\common.h"
#include "cpu.h"

namespace StreamCompaction {
Expand All @@ -9,7 +10,36 @@ namespace CPU {
*/
void scan(int n, int *odata, const int *idata) {
// TODO
printf("TODO\n");
if (n <= 0)
return;

memcpy(odata, idata, n * sizeof(int));
int nCeilLog = ilog2ceil(n);

int nLength = 1 << nCeilLog;

for (int d = 0; d < nCeilLog; d++)
for (int k = 0; k < nLength; k++)
{
int m = 1 << (d + 1);
if (!(k % m))
odata[k + m - 1] += odata[k + (m >> 1) - 1];
}

odata[nLength - 1] = 0;
for (int d = nCeilLog - 1; d >= 0; d--)
for (int k = 0; k < nLength; k++)
{
int m = 1 << (d + 1);
if (!(k % m))
{
int index1 = k + (m >> 1) - 1;
int index2 = k + m - 1;
int temp = odata[index1];
odata[index1] = odata[index2];
odata[index2] += temp;
}
}
}

/**
Expand All @@ -19,7 +49,15 @@ void scan(int n, int *odata, const int *idata) {
*/
int compactWithoutScan(int n, int *odata, const int *idata) {
// TODO
return -1;
if (n <= 0)
return -1;
int counter = 0;
for (int i = 0; i < n; i++)
{
if (idata[i])
odata[counter++] = idata[i];
}
return counter;
}

/**
Expand All @@ -29,7 +67,19 @@ int compactWithoutScan(int n, int *odata, const int *idata) {
*/
int compactWithScan(int n, int *odata, const int *idata) {
// TODO
return -1;
if (n <= 0)
return -1;
int counter = 0;

for (int i = 0; i < n; i++)
odata[i] = idata[i] ? 1 : 0;
scan(n, odata, odata);
for (int i = 0; i < n - 1; i++)
{
if (odata[i] != odata[i + 1])
odata[counter++] = idata[i];
}
return counter;
}

}
Expand Down