Skip to content

Instantly share code, notes, and snippets.

@mikealexander
Created April 2, 2014 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikealexander/9926074 to your computer and use it in GitHub Desktop.
Save mikealexander/9926074 to your computer and use it in GitHub Desktop.
Figures out starting and ending positions for each CUDA thread in each block for use by a parallel sequential searching algorithm.
#include <string>
#include <string.h>
int **starts, **ends;
const int NUM_BLOCKS = 9, THREADS_PER_BLOCK = 10;
const std::string input = "assign4input1";
// figures out the start and end position for every thread and puts them in 2D arrays.
void calculateStarts(int textLength, int patternLength)
{
int majorChunkSize = textLength / NUM_BLOCKS;
int minorChunkSize = majorChunkSize / THREADS_PER_BLOCK;
int chunkStart = 0;
starts = new int*[NUM_BLOCKS];
ends = new int*[NUM_BLOCKS];
for (int i = 0; i<NUM_BLOCKS; i++)
{
// todo: this will be created on the heap... deallocate it later!
starts[i] = new int[THREADS_PER_BLOCK];
ends[i] = new int[THREADS_PER_BLOCK];
chunkStart = i*majorChunkSize;
for(int j=0; j<THREADS_PER_BLOCK; j++)
{
starts[i][j] = chunkStart + j*minorChunkSize;
// sometimes ends will be > textLength... the matching algorithm will only check to textLength-1!
ends[i][j] = starts[i][j] + minorChunkSize + patternLength - 1;
}
// remainders are added to the end of the last chunk
int remainingForChunk = majorChunkSize % THREADS_PER_BLOCK;
ends[i][THREADS_PER_BLOCK-1] += remainingForChunk;
}
int remainingForWhole = textLength%NUM_BLOCKS;
ends[NUM_BLOCKS - 1][THREADS_PER_BLOCK - 1] += remainingForWhole;
}
int main()
{
calculateStarts(1000, 20);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment