Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1.  
  2. #include "cuda_runtime.h"
  3. #include "device_launch_parameters.h"
  4.  
  5. #include <stdio.h>
  6.  
  7. using namespace std;
  8.  
  9. cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);
  10.  
  11. void mojaFcja(bool *czyPierwsza, long int *sprawdzana, unsigned int size);
  12.  
  13.  
  14. __global__ void addKernel(int *c, const int *a, const int *b)
  15. {
  16.     int i = threadIdx.x;
  17.     c[i] = a[i] + b[i];
  18. }
  19.  
  20. __global__ void checkKernel(long int* bliczba, bool *pierwsza)
  21. {
  22.    
  23.     if (*bliczba % 2 != 0)
  24.     {
  25.         int dzielnik = (threadIdx.x * 2) + 1;
  26.        
  27.         if (dzielnik == 1)
  28.             pierwsza[threadIdx.x] = true;
  29.         else if (*bliczba % dzielnik != 0)
  30.         {
  31.             pierwsza[threadIdx.x] = true;
  32.         }
  33.  
  34.     }
  35.     else
  36.     {
  37.         pierwsza[threadIdx.x] = false;
  38.     }
  39. }
  40.  
  41. int main()
  42. {
  43.     const int arraySize = 5;
  44.     const int a[arraySize] = { 1, 2, 3, 4, 5 };
  45.     const int b[arraySize] = { 10, 20, 30, 40, 50 };
  46.     int c[arraySize] = { 0 };
  47.  
  48.    
  49.     long int sprawdzana;
  50.     scanf("%i", &sprawdzana);
  51.  
  52.  
  53.     int size = sprawdzana / 2 - 1;;
  54.    
  55.     //bool *czyPierwsza = new bool[size];
  56.     bool czyPierwsza[1000];
  57.  
  58.  
  59.     // Add vectors in parallel.
  60.     mojaFcja(czyPierwsza, &sprawdzana, size);
  61.        
  62.     bool wynik = true;
  63.     for (int i = 0; i < size; i++)
  64.     {
  65.         if (!czyPierwsza[i])
  66.             wynik = false;
  67.     }
  68.  
  69.     if (wynik)
  70.         printf("pierwsza");
  71.     else
  72.         printf("nie");
  73.  
  74.     getchar();
  75.     getchar();
  76.  
  77.     // cudaDeviceReset must be called before exiting in order for profiling and
  78.     // tracing tools such as Nsight and Visual Profiler to show complete traces.
  79.    
  80.  
  81.     return 0;
  82. }
  83.  
  84. // Helper function for using CUDA to add vectors in parallel.
  85. void mojaFcja(bool *czyPierwsza,long int *sprawdzana, unsigned int size)
  86. {
  87.    
  88.     bool *dev_czyP;
  89.     long int *dev_s;
  90.  
  91.     // Choose which GPU to run on, change this on a multi-GPU system.
  92.     cudaSetDevice(0);
  93.    
  94.     // Allocate GPU buffers for three vectors (two input, one output)    .
  95.     cudaMalloc((void**)&dev_czyP, size * sizeof(bool));
  96.    
  97.     cudaMalloc((void**)&dev_s, sizeof(long int));
  98.    
  99.    
  100.  
  101.     // Copy input vectors from host memory to GPU buffers.
  102.     cudaMemcpy(dev_czyP, czyPierwsza, size * sizeof(bool), cudaMemcpyHostToDevice);
  103.    
  104.  
  105.     cudaMemcpy(dev_s, sprawdzana, sizeof(long int), cudaMemcpyHostToDevice);
  106.    
  107.     // Launch a kernel on the GPU with one thread for each element.
  108.     checkKernel<<<1, size >>>(dev_s, dev_czyP);
  109.  
  110.     // Check for any errors launching the kernel
  111.     cudaGetLastError();
  112.    
  113.    
  114.     // cudaDeviceSynchronize waits for the kernel to finish, and returns
  115.     // any errors encountered during the launch.
  116.     cudaDeviceSynchronize();
  117.    
  118.  
  119.     // Copy output vector from GPU buffer to host memory.
  120.     cudaMemcpy(czyPierwsza, dev_czyP, size * sizeof(bool), cudaMemcpyDeviceToHost);
  121.  
  122.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement