Advertisement
Metrowy

onkgGPU

Nov 13th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include "cuda_runtime.h"
  2. #include "device_launch_parameters.h"
  3. #include <iostream>
  4. #include <string>
  5. #include <math.h>
  6. #include <ctime>
  7.  
  8. #include <stdio.h>
  9. using namespace std;
  10.  
  11. const short L = 4;
  12.  
  13. __global__ void mulMat(int Reg[L][L], int Q[L])
  14. {
  15.     int i = blockIdx.x;
  16.     int newQ[L];
  17.     int temp = 0;
  18.     for (int j = 0; j < L; j++)
  19.     {
  20.         temp += Reg[i][j] * Q[j];
  21.     }
  22.     newQ[i] = temp % 2;
  23.     Q[i] = newQ[i];
  24. }
  25.  
  26. int main()
  27. {
  28.     const clock_t begin_time = clock();
  29.     int Register[L][L] = { {0,0,0,1}, {1,1,1,0}, {0,1,1,0}, {0,0,1,1} };
  30.     int Q[L] = { 1,0,0,0 };
  31.     //int newQ[L];
  32.     bool* tab = new bool[(int)pow(2, L) - 1];
  33.     bool isMax = true;
  34.     int dec;
  35.  
  36.     for (int i = 0; i < pow(2, L); i++)
  37.     {
  38.         tab[i] = false;
  39.     }
  40.  
  41.     for (int i = 0; i < L; i++)
  42.     {
  43.         for (int j = 0; j < L; j++)
  44.         {
  45.             cout << Register[i][j] << " ";
  46.         }
  47.         cout << endl;
  48.     }
  49.     cout << endl;
  50.  
  51.     dec = 0;
  52.     for (int i = 0; i < L; i++)
  53.     {
  54.         dec += Q[L - i - 1] * pow(2, L - i - 1);
  55.         cout << Q[i] << " ";
  56.     }
  57.     cout << " decimal: " << dec << endl;
  58.  
  59.     int(*aReg)[L];
  60.     int(*aQ);
  61.     int(*aDec);
  62.     cudaMalloc((void**)&aReg, (L * L) * sizeof(int));
  63.     cudaMalloc((void**)&aQ, L * sizeof(int));
  64.     cudaMemcpy(aReg, Register, (L * L) * sizeof(int), cudaMemcpyHostToDevice);
  65.     cudaMemcpy(aQ, Q, L * sizeof(int), cudaMemcpyHostToDevice);
  66.  
  67.     for (int k = 0; k < pow(2, L) - 1; k++)
  68.     {
  69.         mulMat<<<L, 1 >> > (aReg, aQ);
  70.         cudaMemcpy(Q, aQ, L * sizeof(int), cudaMemcpyDeviceToHost);
  71.         dec = 0;
  72.         for (int i = 0; i < L; i++)
  73.         {
  74.             dec += Q[i] * pow(2, i);
  75.             cout << Q[i] << " ";
  76.         }
  77.         cout << " decimal: " << dec << endl;
  78.  
  79.         if (!tab[dec])
  80.         {
  81.             tab[dec] = true;
  82.         }
  83.         else
  84.         {
  85.             isMax = false;
  86.             break;
  87.         }
  88.     }
  89.     cudaFree(aReg);
  90.     cudaFree(aQ);
  91.     if (isMax)
  92.     {
  93.         cout << "CYKL MAKSYMALNY";
  94.     }
  95.     else
  96.     {
  97.         cout << "CYKL NIEMAKSYMALNY";
  98.     }
  99.  
  100.     cout << endl;
  101.     cout << float(clock() - begin_time) / CLOCKS_PER_SEC;
  102.     cout << endl;
  103.     system("pasue");
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement