Advertisement
Guest User

prime_numbers

a guest
Dec 7th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #include "cuda_runtime.h"
  2. #include "device_launch_parameters.h"
  3. #include <math.h>
  4. #include <stdio.h>
  5. #include <cstdio>
  6. #include <ctime>
  7.  
  8. cudaError_t addWithCuda(long long potential_prime, bool *not_prime, int threadsize, int blockssize, int border_number);
  9.  
  10. __global__ void is_prime_Kernel(long long prime, bool *not_prime, int border_number)
  11. {
  12.     int i = blockDim.x * blockIdx.x + threadIdx.x;
  13.     if (not_prime[0])
  14.     {
  15.         return;
  16.     }
  17.     if (i < border_number)
  18.     {
  19.         if (prime % (i+2) == 0)
  20.         {
  21.             not_prime[0] = true;
  22.         }
  23.     }
  24. }
  25.  
  26. int main()
  27. {
  28.     long long potential_prime;
  29.     int threadsSize;
  30.     int blocksSize;
  31.     int border_number;
  32.     bool not_prime[1];
  33.  
  34.     not_prime[0] = true;
  35.  
  36.     printf("Podaj liczbe: \n");
  37.     scanf("%llu", &potential_prime);
  38.     printf("Twoja liczba to: %llu\n", potential_prime);
  39.     border_number = sqrt(potential_prime);
  40.     if (border_number < 1024)
  41.     {
  42.         blocksSize = 1;
  43.         threadsSize = border_number;
  44.     }
  45.     else
  46.     {
  47.         blocksSize = border_number / 1024;
  48.         threadsSize = border_number % 104;
  49.     }
  50.    
  51.     printf("Watki: %i\n", threadsSize);
  52.     printf("Bloki: %i\n", blocksSize);
  53.     printf("Ilosc liczb do sprawdzenia: %i\n", border_number);
  54.  
  55.     cudaError_t cudaStatus = addWithCuda(potential_prime, not_prime, threadsSize, blocksSize, border_number);
  56.     if (cudaStatus != cudaSuccess) {
  57.         fprintf(stderr, "addWithCuda failed!");
  58.         return 1;
  59.     }
  60.     return 0;
  61. }
  62.  
  63. cudaError_t addWithCuda(long long potential_prime, bool *not_prime, int threadsize, int blockssize, int border_number)
  64. {
  65.     int *devisors = 0;
  66.     bool* prime;
  67.     float computing_time = 0;
  68.     cudaError_t cudaStatus;
  69.     cudaEvent_t start_counting, stop_counting;
  70.  
  71.     cudaStatus = cudaEventCreate(&start_counting);
  72.     cudaStatus = cudaEventCreate(&stop_counting);
  73.  
  74.     cudaEventRecord(start_counting, 0);
  75.  
  76.     cudaStatus = cudaSetDevice(0);
  77.     if (cudaStatus != cudaSuccess) {
  78.         fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
  79.         goto Error;
  80.     }
  81.  
  82.     cudaStatus = cudaMalloc((void**)&prime, 1 * sizeof(bool));
  83.     if (cudaStatus != cudaSuccess) {
  84.         fprintf(stderr, "cudaMalloc failed!");
  85.         goto Error;
  86.     }
  87.  
  88.     is_prime_Kernel <<< blockssize, threadsize >>>(potential_prime, prime, border_number);
  89.  
  90.     cudaStatus = cudaGetLastError();
  91.     if (cudaStatus != cudaSuccess) {
  92.         fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
  93.         goto Error;
  94.     }
  95.    
  96.     cudaStatus = cudaDeviceSynchronize();
  97.     if (cudaStatus != cudaSuccess) {
  98.         fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
  99.         goto Error;
  100.     }
  101.  
  102.     cudaStatus = cudaMemcpy(not_prime, prime, 1 * sizeof(bool), cudaMemcpyDeviceToHost);
  103.     if (cudaStatus != cudaSuccess) {
  104.         fprintf(stderr, "cudaMemcpy failed!");
  105.         goto Error;
  106.     }
  107.  
  108.     cudaEventRecord(stop_counting, 0);
  109.     cudaEventSynchronize(stop_counting);
  110.  
  111.     cudaEventElapsedTime(&computing_time, start_counting, stop_counting);
  112.  
  113.     cudaEventDestroy(start_counting);
  114.     cudaEventDestroy(stop_counting);
  115.  
  116.     if (!not_prime[0])
  117.     {
  118.         printf("Jest pierwsza!\n");
  119.     }
  120.     else
  121.     {
  122.         printf("Nie jest pierwsza!\n");
  123.     }
  124.     printf("Obliczono w czasie: %.4f sekund\n", computing_time/1000);
  125.  
  126. Error:
  127.     cudaFree(devisors);
  128.     cudaFree(prime);  
  129.     return cudaStatus;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement