Guest User

Untitled

a guest
Apr 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. // includes, system
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. // includes, project
  8. #include <cuda.h>
  9.  
  10. __global__ void stringMatcherComplex(char* subject, int subjectSize, char* pattern, int patternSize, int* numberOfOccurences)
  11. {
  12. int numberOfThreads = ceil((double)subjectSize / (double)patternSize);
  13. int threadId = threadIdx.x;
  14. int i,j;
  15. if (threadId == 0){
  16. for (j=0; j<patternSize && pattern[j]==subject[j]; ++j);
  17. if (j==patternSize)
  18. (*numberOfOccurences)++;
  19. }else{
  20. for (i=((threadId-1)*patternSize+1); i<(threadId*patternSize+1); ++i){
  21. for (j=0; j<patternSize && pattern[j]==subject[i+j]; ++j);
  22. if (j==patternSize)
  23. (*numberOfOccurences)++;
  24. }
  25. }
  26. }
  27.  
  28. __global__ void stringMatcherExhaustive(char* subject, int subjectSize, char* pattern, int patternSize, int* numberOfOccurences)
  29. {
  30. int threadId = threadIdx.x;
  31. int j;
  32. for (j=0; j<patternSize && pattern[j]==subject[j+threadId]; ++j);
  33. if (j==patternSize)
  34. (*numberOfOccurences)++;
  35. }
  36.  
  37. int main(int argc, char** argv)
  38. {
  39. //Maximum 512 threads
  40. char* subject = "blablabla";
  41. int subjectSize = strlen(subject);
  42. char* subjectd;
  43. cudaMalloc(&subjectd, subjectSize*sizeof(char));
  44. cudaMemcpy(subjectd, subject, subjectSize*sizeof(char), cudaMemcpyHostToDevice);
  45.  
  46. char* pattern = "bla";
  47. int patternSize = strlen(pattern);
  48. char* patternd;
  49. cudaMalloc(&patternd, patternSize*sizeof(char));
  50. cudaMemcpy(patternd, pattern, patternSize*sizeof(char), cudaMemcpyHostToDevice);
  51.  
  52. int numberOfThreads = ceil((double)subjectSize / (double)patternSize);
  53.  
  54. int numberOfOccurences = 0;
  55. int* numberOfOccurencesd;
  56. cudaMalloc(&numberOfOccurencesd, sizeof(int));
  57. cudaMemcpy(numberOfOccurencesd, &numberOfOccurences, sizeof(int), cudaMemcpyHostToDevice);
  58.  
  59. stringMatcherComplex<<<1, numberOfThreads>>>(subjectd, subjectSize, patternd, patternSize, numberOfOccurencesd);
  60. //stringMatcherExhaustive<<<1, subjectSize-patternSize+1>>>(subjectd, subjectSize, patternd, patternSize, numberOfOccurencesd);
  61. cudaMemcpy(&numberOfOccurences, numberOfOccurencesd, sizeof(int), cudaMemcpyDeviceToHost);
  62.  
  63. printf("Number of occurences: %d\n",numberOfOccurences);
  64.  
  65. cudaFree(subjectd);
  66. cudaFree(patternd);
  67. cudaFree(numberOfOccurencesd);
  68. }
Add Comment
Please, Sign In to add comment