Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <vector>
  4. #include <omp.h>
  5. #include <complex>
  6. #include <chrono>
  7.  
  8. #define N 10000;
  9.  
  10. using namespace std;
  11.  
  12. __global__ void check(int *array, int *result){
  13.  
  14.     int tid = threadIdx.x + blockIdx.x *blockDim.x;
  15.  
  16.     if(tid < N){
  17.         for(int i = 2; i < sqrt((double)array[tid]); i++){
  18.             if(array[tid] % i == 0){
  19.                 result[tid] = 0;
  20.             }
  21.         }
  22.     }
  23. }
  24.  
  25. int main(){
  26.     srand(time(nullptr));
  27.     int tab[N];
  28. //    int sum = 0;
  29.  
  30.     for(int i = 0; i < N; i++){
  31.         tab[i] = rand()%N;
  32.     }
  33.  
  34.     cudaEvent_t start, end;
  35.     float time;
  36.  
  37.     cudaEventCreate(&start);
  38.     cudaEventCreate(&end);
  39.  
  40.     int *array= nullptr;
  41.     int *check = nullptr;
  42.  
  43.     cudaMalloc(&array, N * sizeof(int));
  44.     cudaMalloc(&check, N * sizeof(int));
  45.  
  46.     cudaMemcpy(array, tab, sizeof(int)*N, cudaMemcpyHostToDevice);
  47.  
  48.     cudaEventRecord(start);
  49.     check<<<N, 1>>>(array, check);
  50.     cudaEventRecord(end);
  51.  
  52.     int result[N];
  53.     cudaMemcpy(result, check, N * sizeof(int), cudaMemcpyDeviceToHost);
  54.     cudaEventSynchronize(end);
  55.     cudaEventElapsedTime(&time, start, end);
  56.  
  57. //    for(int i = 0; i < N; i++){
  58. //        sum = sum + tab[i];
  59. //    }
  60.  
  61.     cudaFree(array);
  62.     cudaFree(check);
  63.  
  64.     cout << sum << endl;
  65.     cout << time << endl;
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement