Markopy
Utilizing Markov Models for brute forcing attacks
cudarandom.h
Go to the documentation of this file.
1 /** @file cudarandom.h
2  * @brief Extension of Markov::Random::Marsaglia for CUDA
3  * @authors Ata Hakçıl
4  *
5  * @copydoc Markov::Random::Marsaglia
6  */
7 
8 #pragma once
9 #include "MarkovModel/src/random.h"
11 
12 /** @brief Namespace for Random engines operable under __device__ space.
13 */
14 namespace Markov::API::CUDA::Random{
15 
16  /** @brief Extension of Markov::Random::Marsaglia which is capable o working on __device__ space.
17  *
18  * @copydoc Markov::Random::Marsaglia
19  */
21  public:
22 
23  /** @brief Migrate a Marsaglia[] to VRAM as seedChunk
24  * @param MEarr Array of Marsaglia Engines
25  * @param gridSize GridSize of the CUDA Kernel, aka size of array
26  * @returns pointer to the resulting seed chunk in device VRAM.
27  */
28  static unsigned long* MigrateToVRAM(Markov::API::CUDA::Random::Marsaglia *MEarr, long int gridSize){
29  cudaError_t cudastatus;
30  unsigned long* seedChunk;
31  cudastatus = cudaMalloc((unsigned long**)&seedChunk, gridSize*3*sizeof(unsigned long));
32  CudaCheckNotifyErr(cudastatus, "Failed to allocate seed buffer");
33  unsigned long *temp = new unsigned long[gridSize*3];
34  for(int i=0;i<gridSize;i++){
35  temp[i*3] = MEarr[i].x;
36  temp[i*3+1] = MEarr[i].y;
37  temp[i*3+2] = MEarr[i].z;
38  }
39  //for(int i=0;i<gridSize*3;i++) std::cout << temp[i] << "\n";
40  cudaMemcpy(seedChunk, temp, gridSize*3*sizeof(unsigned long), cudaMemcpyHostToDevice);
41  CudaCheckNotifyErr(cudastatus, "Failed to memcpy seed buffer.");
42 
43  delete[] temp;
44  return seedChunk;
45  }
46  };
47 
48  /** @brief Marsaglia Random Generation function operable in __device__ space
49  * @param x marsaglia internal x. Not constant, (ref)
50  * @param y marsaglia internal y. Not constant, (ref)
51  * @param z marsaglia internal z. Not constant, (ref)
52  * @returns returns z
53  */
54  __device__ unsigned long devrandom(unsigned long &x, unsigned long &y, unsigned long &z){
55  unsigned long t;
56  x ^= x << 16;
57  x ^= x >> 5;
58  x ^= x << 1;
59 
60  t = x;
61  x = y;
62  y = z;
63  z = t ^ x ^ y;
64 
65  return z;
66  }
67 };