Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/time.h>
  4.  
  5. #define N 1024
  6. #define BLOCK_SIZE 1024
  7.  
  8. float hArray[N];
  9. float *dArray;
  10. int blocks;
  11.  
  12. float* result;
  13. float* dResult;
  14.  
  15. void prologue(void) {
  16. cudaMalloc((void**) &dArray, sizeof(hArray));
  17. cudaMemcpy(dArray, hArray, sizeof(hArray), cudaMemcpyHostToDevice);
  18. cudaMalloc((void**) &dResult, sizeof(result));
  19. cudaMemcpy(dResult, result, sizeof(result), cudaMemcpyHostToDevice);
  20. }
  21.  
  22. void epilogue(void) {
  23. cudaMemcpy(hArray, dArray, sizeof(hArray), cudaMemcpyDeviceToHost);
  24. cudaFree(dArray);
  25. cudaMemcpy(result, dResult, sizeof(result), cudaMemcpyDeviceToHost);
  26. cudaFree(dResult);
  27. }
  28.  
  29. __global__ void calcDevice(float *A, float* result, float value) {
  30. int x = blockDim.x * blockIdx.x + threadIdx.x;
  31.  
  32. if (x < N) {
  33. if (A[x] == value)
  34. *result = A[x];
  35. }
  36. }
  37.  
  38. int compare(const void* a, const void* b)
  39. {
  40. if (*(float*)a < *(float*)b)
  41. return -1;
  42. else if (*(float*)a > *(float*)b)
  43. return 1;
  44. else
  45. return 0;
  46. }
  47.  
  48. void findMax(void) {
  49. srand(time(NULL));
  50. for (int i = 0; i < N; i++) {
  51. hArray[i] = (float) rand() / RAND_MAX;
  52. }
  53. qsort(hArray, N, sizeof(float), compare);
  54.  
  55. float found = 0.0F;
  56. result = &found;
  57.  
  58. float value = hArray[22];
  59. float* bFound = (float*) bsearch(&value, hArray, N, sizeof(float), compare);
  60. printf("Value = %f\n", value);
  61. printf("bsearch() = %f\n", *bFound);
  62.  
  63. prologue();
  64.  
  65. blocks = N / BLOCK_SIZE;
  66. if (N % BLOCK_SIZE)
  67. blocks++;
  68.  
  69. calcDevice<<<blocks, BLOCK_SIZE>>>(dArray, dResult, value);
  70.  
  71. cudaThreadSynchronize();
  72. epilogue();
  73.  
  74. printf("Result = %f\n", found);
  75. }
  76.  
  77. int main(int argc, char** argv) {
  78. int devCnt;
  79.  
  80. cudaGetDeviceCount(&devCnt);
  81. if (devCnt == 0) {
  82. perror("No CUDA devices available -- exiting.");
  83. return 1;
  84. }
  85.  
  86. findMax();
  87.  
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement