Facebook
From Ɓukasz , 6 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 260
  1. #include "cuda_runtime.h"
  2. #include "device_launch_parameters.h"
  3. #include <stdio.h>
  4. #include <iostream>
  5. #include <cstdio>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. cudaError_t addWithCuda(int *is_prime, unsigned long long int *number, unsigned long long int *sqrtt);
  11.  
  12. __global__ void addKernel(int *is_prime, unsigned long long int *number, unsigned long long int *sqrtt)
  13. {
  14.         unsigned long long int index = blockIdx.x * blockDim.x + threadIdx.x;
  15.         unsigned long long int i = index;
  16.         while (i < *sqrtt + 1)
  17.         {
  18.                 if (i >1)
  19.                 {
  20.                         if (*number%i == 0)
  21.                         {
  22.                                 *is_prime = 1;
  23.                         }
  24.                 }
  25.                 i += blockDim.x*gridDim.x;
  26.         }
  27.         cudaThreadSynchronize;
  28. }
  29.  
  30. int is_prime_number(unsigned long long int *number)
  31. {
  32.  
  33.         for (long long int i = 3; i < sqrt(*number); i += 1)
  34.         {
  35.  
  36.                 if (*number%i == 0)
  37.                 {
  38.                         cout << "(cpu)liczba nie jest pierwszan";
  39.                         return 0;
  40.                 }
  41.  
  42.         }
  43.         cout << "(cpu) liczba pierwszan";
  44.         return 1;
  45.  
  46. }
  47.  
  48. int main()
  49. {
  50.    
  51.         unsigned long long int *number =new unsigned long long int;
  52.         unsigned long long int *sqrtt = new unsigned long long int;
  53.         int *is_prime = new int;
  54.  
  55.         *number = ((unsigned long long int)1) << 61;
  56.         *number -= 1;
  57.         *sqrtt = sqrt(*number);
  58.         *is_prime = 0;
  59.  
  60.         clock_t start1 = clock();
  61.         is_prime_number(number);
  62.         printf("Czas wykonywania: %lu msn", clock() - start1);
  63.  
  64.     // Add vectors in parallel.
  65.     cudaError_t cudaStatus = addWithCuda(is_prime,number,sqrtt);
  66.     if (cudaStatus != cudaSuccess) {
  67.         fprintf(stderr, "addWithCuda failed!");
  68.                 system("pause");
  69.         return 1;
  70.     }
  71.  
  72.     //printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}n",
  73.         //c[0], c[1], c[2], c[3], c[4]);
  74.  
  75.     // cudaDeviceReset must be called before exiting in order for profiling and
  76.     // tracing tools such as Nsight and Visual Profiler to show complete traces.
  77.     cudaStatus = cudaDeviceReset();
  78.     if (cudaStatus != cudaSuccess) {
  79.         fprintf(stderr, "cudaDeviceReset failed!");
  80.                 system("pause");
  81.         return 1;
  82.     }
  83.  
  84.         system("pause");
  85.     return 0;
  86. }
  87.  
  88. // Helper function for using CUDA to add vectors in parallel.
  89. cudaError_t addWithCuda(int *is_prime, unsigned long long int *number, unsigned long long  int *sqrtt)
  90. {
  91.     unsigned long long int *dev_a = 0;
  92.     unsigned long long int *dev_b = 0;
  93.     int *dev_c = 0;
  94.     cudaError_t cudaStatus;
  95.  
  96.     // Choose which GPU to run on, change this on a multi-GPU system.
  97.     cudaStatus = cudaSetDevice(0);
  98.     if (cudaStatus != cudaSuccess) {
  99.         fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
  100.         goto Error;
  101.     }
  102.  
  103.     // Allocate GPU buffers for three vectors (two input, one output)    .
  104.     cudaStatus = cudaMalloc((void**)&dev_c, sizeof(int));
  105.     if (cudaStatus != cudaSuccess) {
  106.         fprintf(stderr, "cudaMalloc failed!");
  107.         goto Error;
  108.     }
  109.  
  110.         cudaStatus = cudaMalloc((void**)&dev_a, sizeof(unsigned long long int));
  111.     if (cudaStatus != cudaSuccess) {
  112.         fprintf(stderr, "cudaMalloc failed!");
  113.         goto Error;
  114.     }
  115.  
  116.     cudaStatus = cudaMalloc((void**)&dev_b, sizeof(unsigned long long int));
  117.     if (cudaStatus != cudaSuccess) {
  118.         fprintf(stderr, "cudaMalloc failed!");
  119.         goto Error;
  120.     }
  121.  
  122.     // Copy input vectors from host memory to GPU buffers.
  123.     cudaStatus = cudaMemcpy(dev_a, number, sizeof(unsigned long long int), cudaMemcpyHostToDevice);
  124.     if (cudaStatus != cudaSuccess) {
  125.         fprintf(stderr, "cudaMemcpy failed!");
  126.         goto Error;
  127.     }
  128.  
  129.     cudaStatus = cudaMemcpy(dev_b, sqrtt,sizeof(int), cudaMemcpyHostToDevice);
  130.     if (cudaStatus != cudaSuccess) {
  131.         fprintf(stderr, "cudaMemcpy failed!");
  132.         goto Error;
  133.     }
  134.  
  135.         clock_t start = clock();
  136.        
  137.  
  138.     // Launch a kernel on the GPU with one thread for each element.
  139.     addKernel<<<12, 512>>>(dev_c, dev_a, dev_b);
  140.  
  141.     // Check for any errors launching the kernel
  142.     cudaStatus = cudaGetLastError();
  143.     if (cudaStatus != cudaSuccess) {
  144.         fprintf(stderr, "addKernel launch failed: %sn", cudaGetErrorString(cudaStatus));
  145.         goto Error;
  146.     }
  147.    
  148.     // cudaDeviceSynchronize waits for the kernel to finish, and returns
  149.     // any errors encountered during the launch.
  150.    /* cudaStatus = cudaDeviceSynchronize();
  151.     if (cudaStatus != cudaSuccess) {
  152.         fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!n", cudaStatus);
  153.         goto Error;
  154.     }*/
  155.  
  156.     // Copy output vector from GPU buffer to host memory.
  157.     cudaStatus = cudaMemcpy(is_prime, dev_c, sizeof(int), cudaMemcpyDeviceToHost);
  158.     if (cudaStatus != cudaSuccess) {
  159.         fprintf(stderr, "cudaMemcpy failed!");
  160.         goto Error;
  161.     }
  162.  
  163.         if (*is_prime == 1) {
  164.                 cout << "(GPU)Liczba nie jest pierwsza n";
  165.  
  166.         }
  167.         else {
  168.                 cout << "(GPU)liczba pierwsza n";
  169.  
  170.         }
  171.         printf("Czas wykonywania: %lu msn", clock() - start);
  172.  
  173.  
  174. Error:
  175.     cudaFree(dev_c);
  176.     cudaFree(dev_a);
  177.     cudaFree(dev_b);
  178.    
  179.     return cudaStatus;
  180. }
  181.