Created
April 2, 2014 00:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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