Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1.  
  2. #include "cuda_runtime.h"
  3. #include "device_functions.h"
  4. #include "device_launch_parameters.h"
  5.  
  6. #include <stdio.h>
  7. #include "stdlib.h"
  8.  
  9. #define BLOCK_SIZE 8
  10. #define TH 1e-6
  11. #define N 128
  12.  
  13. __global__ void getSolCount(float *valuesX, float *valuesY, float *answers01)
  14. {
  15. int i = blockIdx.x * blockDim.x + threadIdx.x;
  16. __shared__ float temp[BLOCK_SIZE];
  17. float leftVal = __cosf(valuesX[i] + TH) * __sinf(valuesX[i] / TH);
  18. float rightVal = __cosf(valuesY[i] + TH) * __sinf(valuesY[i] / TH);
  19. if (leftVal * rightVal < 0)
  20. {
  21. temp[threadIdx.x] = 1.0;
  22. }
  23. else
  24. {
  25. temp[threadIdx.x] = 0.0;
  26. }
  27. __syncthreads();
  28.  
  29. i = blockIdx.x * blockDim.x + threadIdx.x;
  30.  
  31. answers01[i] = temp[threadIdx.x];
  32. }
  33.  
  34. __host__ int main()
  35. {
  36. float a = 0.0, b = 2 * 3.14;
  37. float *coordArrX = new float[N];
  38. float *coordArrY = new float[N];
  39. float *answers01_host = new float[N];
  40. float hi = (b - a) / N;
  41.  
  42. for (int i = 0; i < N; i++)
  43. {
  44. coordArrX[i] = i * hi;
  45. coordArrY[i] = (i + 1) * hi;
  46. printf("x=%f y=%f\n", coordArrX[i], coordArrX[i]);
  47. }
  48. system("pause");
  49. float *answers01;
  50. float *valuesX;
  51. float *valuesY;
  52. cudaMalloc((void **)&answers01, N * sizeof(float));
  53. cudaMalloc((void **)&valuesX, N * sizeof(float));
  54. cudaMalloc((void **)&valuesY, N * sizeof(float));
  55.  
  56. cudaMemcpy(valuesX, coordArrX, N * sizeof(float), cudaMemcpyHostToDevice);
  57. cudaMemcpy(valuesY, coordArrY, N * sizeof(float), cudaMemcpyHostToDevice);
  58.  
  59. dim3 gridSize = dim3(N / BLOCK_SIZE);
  60. dim3 blockSize = dim3(BLOCK_SIZE);
  61.  
  62. getSolCount <<< gridSize, blockSize>>>(valuesX, valuesY, answers01);
  63.  
  64. cudaMemcpy(answers01_host, answers01, N * sizeof(float), cudaMemcpyDeviceToHost);
  65. int sol_count = 0;
  66. for (int i = 0; i < N; i++)
  67. {
  68. total += answers01_host[i];
  69. if (answers01_host[i] == 1.0f)
  70. {
  71. sol_count++;
  72. }
  73. }
  74. printf("solutions count: %d", sol_count);
  75. system("pause");
  76.  
  77. cudaFree(valuesX);
  78. cudaFree(valuesY);
  79.  
  80. free(coordArrX);
  81. free(coordArrY);
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement