Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 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. temp += Reg[i][j] * Q[j];
  20. }
  21. newQ[i] = temp % 2;
  22. Q[i] = newQ[i];
  23. }
  24.  
  25. int main()
  26. {
  27. const clock_t begin_time = clock();
  28. int Register[L][L] = { {0,0,0,1}, {1,1,1,0}, {0,1,1,0}, {0,0,1,1} };
  29. int Q[L] = { 1,0,0,0 };
  30. //int newQ[L];
  31. bool* tab = new bool[(int)pow(2, L) - 1];
  32. bool isMax = true;
  33. int dec;
  34.  
  35. for (int i = 0; i < pow(2, L); i++) {
  36. tab[i] = false;
  37. }
  38.  
  39. for (int i = 0; i < L; i++) {
  40. for (int j = 0; j < L; j++)
  41. {
  42. cout << Register[i][j] << " ";
  43. }
  44. cout << endl;
  45. }
  46. cout << endl;
  47.  
  48. dec = 0;
  49. for (int i = 0; i < L; i++) {
  50. dec += Q[L - i - 1] * pow(2, L - i - 1);
  51. cout << Q[i] << " ";
  52. }
  53. cout << " decimal: " << dec << endl;
  54.  
  55. int(*aReg)[L];
  56. int(*aQ);
  57. int(*aDec);
  58. cudaMalloc((void**)&aReg, (L * L) * sizeof(int));
  59. cudaMalloc((void**)&aQ, L * sizeof(int));
  60. cudaMemcpy(aReg, Register, (L * L) * sizeof(int), cudaMemcpyHostToDevice);
  61. cudaMemcpy(aQ, Q, L * sizeof(int), cudaMemcpyHostToDevice);
  62.  
  63. for (int k = 0; k < pow(2, L) - 1; k++) {
  64. mulMat << <L, 1 >> > (aReg, aQ);
  65. cudaMemcpy(Q, aQ, L * sizeof(int), cudaMemcpyDeviceToHost);
  66. dec = 0;
  67. for (int i = 0; i < L; i++) {
  68. dec += Q[i] * pow(2, i);
  69. cout << Q[i] << " ";
  70. }
  71. cout << " decimal: " << dec << endl;
  72.  
  73. if (!tab[dec]) {
  74. tab[dec] = true;
  75. }
  76. else {
  77. isMax = false;
  78. break;
  79. }
  80. }
  81. cudaFree(aReg);
  82. cudaFree(aQ);
  83. if (isMax) {
  84. cout << "CYKL MAKSYMALNY";
  85. }
  86. else {
  87. cout << "CYKL NIEMAKSYMALNY";
  88. }
  89.  
  90. cout << endl;
  91. cout << float(clock() - begin_time) / CLOCKS_PER_SEC;
  92. cout << endl;
  93. system("pause");
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement