Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // includes, system
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- // includes, project
- #include <cuda.h>
- __global__ void stringMatcherComplex(char* subject, int subjectSize, char* pattern, int patternSize, int* numberOfOccurences)
- {
- int numberOfThreads = ceil((double)subjectSize / (double)patternSize);
- int threadId = threadIdx.x;
- int i,j;
- if (threadId == 0){
- for (j=0; j<patternSize && pattern[j]==subject[j]; ++j);
- if (j==patternSize)
- (*numberOfOccurences)++;
- }else{
- for (i=((threadId-1)*patternSize+1); i<(threadId*patternSize+1); ++i){
- for (j=0; j<patternSize && pattern[j]==subject[i+j]; ++j);
- if (j==patternSize)
- (*numberOfOccurences)++;
- }
- }
- }
- __global__ void stringMatcherExhaustive(char* subject, int subjectSize, char* pattern, int patternSize, int* numberOfOccurences)
- {
- int threadId = threadIdx.x;
- int j;
- for (j=0; j<patternSize && pattern[j]==subject[j+threadId]; ++j);
- if (j==patternSize)
- (*numberOfOccurences)++;
- }
- int main(int argc, char** argv)
- {
- //Maximum 512 threads
- char* subject = "blablabla";
- int subjectSize = strlen(subject);
- char* subjectd;
- cudaMalloc(&subjectd, subjectSize*sizeof(char));
- cudaMemcpy(subjectd, subject, subjectSize*sizeof(char), cudaMemcpyHostToDevice);
- char* pattern = "bla";
- int patternSize = strlen(pattern);
- char* patternd;
- cudaMalloc(&patternd, patternSize*sizeof(char));
- cudaMemcpy(patternd, pattern, patternSize*sizeof(char), cudaMemcpyHostToDevice);
- int numberOfThreads = ceil((double)subjectSize / (double)patternSize);
- int numberOfOccurences = 0;
- int* numberOfOccurencesd;
- cudaMalloc(&numberOfOccurencesd, sizeof(int));
- cudaMemcpy(numberOfOccurencesd, &numberOfOccurences, sizeof(int), cudaMemcpyHostToDevice);
- stringMatcherComplex<<<1, numberOfThreads>>>(subjectd, subjectSize, patternd, patternSize, numberOfOccurencesd);
- //stringMatcherExhaustive<<<1, subjectSize-patternSize+1>>>(subjectd, subjectSize, patternd, patternSize, numberOfOccurencesd);
- cudaMemcpy(&numberOfOccurences, numberOfOccurencesd, sizeof(int), cudaMemcpyDeviceToHost);
- printf("Number of occurences: %d\n",numberOfOccurences);
- cudaFree(subjectd);
- cudaFree(patternd);
- cudaFree(numberOfOccurencesd);
- }
Add Comment
Please, Sign In to add comment