Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1.  
  2. #include "cuda_runtime.h"
  3. #include "device_launch_parameters.h"
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <iostream>
  7. #include <cstdlib>
  8.  
  9. cudaError_t CheckPrimeCUDA(bool *upsik, unsigned long long *dzielnik, unsigned long long *pitca, unsigned long long size);
  10.  
  11. __global__ void CheckPrimeKernel(bool *upsik, unsigned long long int *dzielnik, unsigned long long *pitca)
  12. {
  13. *dzielnik = 5 + (threadIdx.x + blockDim.x*blockIdx.x ) * 6;
  14. if ((*pitca % *dzielnik) == 0)
  15. *upsik = false;
  16. if ((*pitca % (*dzielnik + 2)) == 0)
  17. *upsik = false;
  18. }
  19.  
  20. int main()
  21. {
  22. unsigned long long superliczba = 2305843009213693951;
  23. unsigned long long dzielnik;
  24. bool sprawdzto = 1;
  25.  
  26. if (superliczba % 2 == 0)
  27. sprawdzto = 0;
  28. if (superliczba % 3 == 0)
  29. sprawdzto = 0;
  30. #pragma omp parallel for
  31. for (dzielnik = 5; dzielnik <= sqrt(superliczba); dzielnik += 6)
  32. {
  33. if (superliczba%dzielnik == 0)
  34. sprawdzto = 0;
  35. break;
  36. if (superliczba%dzielnik + 2 == 0)
  37. sprawdzto = 0;
  38. break;
  39. }
  40.  
  41. if (sprawdzto == 1)
  42. printf("pizza supreme\n\n");
  43. else
  44. {
  45. printf("woah\n\n");
  46. }
  47. system("pause");
  48.  
  49. // Add vectors in parallel.
  50. // cudaError_t cudaStatus = CheckPrimeKernel(sprawdzto, superliczba, dzielnik);
  51. //if (cudaStatus != cudaSuccess) {
  52. // fprintf(stderr, "addWithCuda failed!");
  53. // return 1;
  54. // }
  55.  
  56. // printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",
  57. // c[0], c[1], c[2], c[3], c[4]);
  58.  
  59. // cudaDeviceReset must be called before exiting in order for profiling and
  60. // tracing tools such as Nsight and Visual Profiler to show complete traces.
  61. // cudaStatus = cudaDeviceReset();
  62. // if (cudaStatus != cudaSuccess) {
  63. // fprintf(stderr, "cudaDeviceReset failed!");
  64. // return 1;
  65. // }
  66.  
  67. return 0;
  68. }
  69.  
  70. // Helper function for using CUDA to add vectors in parallel.
  71. cudaError_t CheckPrimeCUDA(bool *upsik, unsigned long long *dzielnik, unsigned long long *pitca, unsigned long long size)
  72. {
  73. unsigned long long int *wsk_pitca = 0;
  74. unsigned long long int *wsk_dzielnik = 0;
  75. bool * wsk_upsik = 0;
  76. size = sizeof(unsigned long long int);
  77. cudaError_t cudaStatus;
  78.  
  79. // Choose which GPU to run on, change this on a multi-GPU system.
  80. cudaStatus = cudaSetDevice(0);
  81. if (cudaStatus != cudaSuccess) {
  82. fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
  83. goto Error;
  84. }
  85.  
  86. // Allocate GPU buffers for three vectors (two input, one output) .
  87. cudaStatus = cudaMalloc((void**)&wsk_upsik, size * sizeof(int));
  88. if (cudaStatus != cudaSuccess) {
  89. fprintf(stderr, "cudaMalloc failed!");
  90. goto Error;
  91. }
  92.  
  93. cudaStatus = cudaMalloc((void**)&wsk_pitca, size * sizeof(int));
  94. if (cudaStatus != cudaSuccess) {
  95. fprintf(stderr, "cudaMalloc failed!");
  96. goto Error;
  97. }
  98.  
  99. cudaStatus = cudaMalloc((void**)&wsk_dzielnik, size * sizeof(int));
  100. if (cudaStatus != cudaSuccess) {
  101. fprintf(stderr, "cudaMalloc failed!");
  102. goto Error;
  103. }
  104.  
  105. // Copy input vectors from host memory to GPU buffers.
  106. cudaStatus = cudaMemcpy(wsk_pitca, pitca, size * sizeof(unsigned long long), cudaMemcpyHostToDevice);
  107. if (cudaStatus != cudaSuccess) {
  108. fprintf(stderr, "cudaMemcpy failed!");
  109. goto Error;
  110. }
  111.  
  112. cudaStatus = cudaMemcpy(wsk_dzielnik, dzielnik, size * sizeof(unsigned long long), cudaMemcpyHostToDevice);
  113. if (cudaStatus != cudaSuccess) {
  114. fprintf(stderr, "cudaMemcpy failed!");
  115. goto Error;
  116. }
  117.  
  118. // Launch a kernel on the GPU with one thread for each element.
  119. CheckPrimeKernel<<<2048, 2048>>>(wsk_upsik, wsk_pitca, wsk_dzielnik);
  120.  
  121. // Check for any errors launching the kernel
  122. cudaStatus = cudaGetLastError();
  123. if (cudaStatus != cudaSuccess) {
  124. fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
  125. goto Error;
  126. }
  127.  
  128. // cudaDeviceSynchronize waits for the kernel to finish, and returns
  129. // any errors encountered during the launch.
  130. cudaStatus = cudaDeviceSynchronize();
  131. if (cudaStatus != cudaSuccess) {
  132. fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
  133. goto Error;
  134. }
  135.  
  136. // Copy output vector from GPU buffer to host memory.
  137. cudaStatus = cudaMemcpy(upsik, wsk_upsik, size * sizeof(unsigned long long), cudaMemcpyDeviceToHost);
  138. if (cudaStatus != cudaSuccess) {
  139. fprintf(stderr, "cudaMemcpy failed!");
  140. goto Error;
  141. }
  142.  
  143. Error:
  144. cudaFree(wsk_pitca);
  145. cudaFree(wsk_dzielnik);
  146. cudaFree(wsk_upsik);
  147.  
  148. return cudaStatus;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement