Advertisement
Guest User

Host file

a guest
May 28th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. #define CL_USE_DEPRECATED_OPENCL_2_0_APIS // using OpenCL 1.2, some functions deprecated in OpenCL 2.0
  2. #define __CL_ENABLE_EXCEPTIONS // enable OpenCL exemptions
  3.  
  4. // C++ standard library and STL headers
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <string>
  9. #include <iostream>
  10. #include <vector>
  11. #include <fstream>
  12. #include <regex>
  13.  
  14.  
  15. // OpenCL header, depending on OS
  16. #ifdef __APPLE__
  17. #include <OpenCL/cl.hpp>
  18. #else
  19. #include <CL/cl.hpp>
  20. #endif
  21.  
  22. #include "common.h"
  23.  
  24.  
  25. int main() {
  26. cl::Platform platform; // device's platform
  27. cl::Device device; // device used
  28. cl::Context context; // context for the device
  29. cl::Program program; // OpenCL program object
  30. cl::Kernel kernel; // a single kernel object
  31. cl::CommandQueue queue; // commandqueue for a context and device
  32.  
  33. //Create data struct for Host to read plaintext.txt and store it's content.
  34. std::vector<cl_uchar> cipherTexta;
  35. std::vector<cl_uchar> cipherTextb;
  36.  
  37.  
  38. //Our shift var
  39. signed int key;
  40. //Our Cipheredtext filename.
  41. std::string fileNamea = "ciphertext2a.txt";
  42. std::string fileNameb = "ciphertext2b.txt";
  43. //For Task 2c, file names for reverse cipher on openCL device.
  44. std::string outputDeSubstitutedTextCL1 = "decrypted2a.txt";
  45. std::string outputDeSubstitutedTextCL2 = "decrypted2b.txt";
  46.  
  47.  
  48. //Read in pt file to cipered vector.
  49. std::cout << "Assignment 2 Decipher 2a:" << std::endl;
  50. std::ifstream ciphertext2a;
  51. ciphertext2a.open(fileNamea, std::ifstream::in);
  52. //char var to store and check each Character in the file.
  53. char getCharOne;
  54. if (ciphertext2a.is_open()) {
  55. while (ciphertext2a.get(getCharOne)) {
  56. if (getCharOne >= 'a' && getCharOne <= 'z') {
  57. getCharOne = getCharOne - 32;
  58. cipherTexta.push_back(getCharOne);
  59. }
  60. else if (getCharOne >= 'A' && getCharOne <= 'Z') {
  61. cipherTexta.push_back(getCharOne);
  62. }
  63. else if (isblank(getCharOne)) {
  64. cipherTexta.push_back(' ');
  65. }
  66. else if (ispunct(getCharOne)) {
  67. cipherTexta.push_back(getCharOne);
  68. }
  69. else if (isspace(getCharOne)) {
  70. cipherTexta.push_back(getCharOne);
  71. }
  72. else if (isdigit(getCharOne)) {
  73. cipherTexta.push_back(getCharOne);
  74. }
  75. }
  76. ciphertext2a.close();
  77. }
  78.  
  79.  
  80.  
  81. int ptSize = cipherTexta.size();
  82.  
  83.  
  84. //Read in ciphered file 2b to decipher.
  85. std::cout << "Assignment 2 Decipher 2b:" << std::endl;
  86. std::ifstream ciphertext2b;
  87. ciphertext2b.open(fileNameb, std::ifstream::in);
  88. //char var to store and check each Character in the file.
  89. char getCharTwo;
  90. if (ciphertext2b.is_open()) {
  91. while (ciphertext2b.get(getCharTwo)) {
  92. if (getCharTwo >= 'a' && getCharTwo <= 'z') {
  93. getCharTwo = getCharTwo - 32;
  94. cipherTextb.push_back(getCharTwo);
  95. }
  96. else if (getCharTwo >= 'A' && getCharTwo <= 'Z') {
  97. cipherTextb.push_back(getCharTwo);
  98. }
  99. else if (isblank(getCharTwo)) {
  100. cipherTextb.push_back(' ');
  101. }
  102. else if (ispunct(getCharTwo)) {
  103. cipherTextb.push_back(getCharTwo);
  104. }
  105. else if (isspace(getCharTwo)) {
  106. cipherTextb.push_back(getCharTwo);
  107. }
  108. else if (isdigit(getCharTwo)) {
  109. cipherTextb.push_back(getCharTwo);
  110. }
  111. }
  112. ciphertext2b.close();
  113. }
  114. int ptSizeb = cipherTextb.size();
  115.  
  116. //OpenCL standards > select device, create context, build prog, define buffer, create kernels, set args,
  117. //command queue
  118. try {
  119. // select an OpenCL device
  120. if (!select_one_device(&platform, &device))
  121. {
  122. // if no device selected
  123. quit_program("Device not selected.");
  124. }
  125.  
  126. // create a context from device
  127. context = cl::Context(device);
  128.  
  129.  
  130. // set modifier and work-items
  131. cl::NDRange offset(0);
  132. cl::NDRange globalSize(ptSize);
  133.  
  134.  
  135. //Create Buffer
  136. cl::Buffer substitutedBuffer, desubstitutedBuffer, substitutedBufferb, desubstitutedBufferb; //device side data obj
  137. //Create Vector
  138. std::vector<cl_uchar> originalTextVector(ptSize);
  139. std::vector<cl_uchar> originalTextVectorb(ptSizeb);
  140. std::vector<cl_uchar> substitutedTextVector(ptSize);
  141. std::vector<cl_uchar> substitutedTextVectorb(ptSizeb);
  142. std::vector<cl_uchar> outputVector(ptSize);
  143. std::vector<cl_uchar> outputVectorb(ptSizeb);
  144. //Copy the plaintext we stored previously in plainText to OriginalTextVector here.
  145. originalTextVector = cipherTexta;
  146. originalTextVectorb = cipherTextb;
  147.  
  148. // Decrypt 2a
  149. if (!build_program(&program, &context, "desubstitute.cl"))
  150. {
  151. // if OpenCL program build error
  152. quit_program("OpenCL program build error.");
  153. }
  154.  
  155. substitutedBuffer = cl::Buffer(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(cl_uchar) * ptSize, &originalTextVector[0]);
  156. desubstitutedBuffer = cl::Buffer(context, CL_MEM_WRITE_ONLY, sizeof(cl_uchar) * ptSize);
  157.  
  158.  
  159. // create a kernel
  160. kernel = cl::Kernel(program, "desubstitute");
  161.  
  162. // create command queue
  163. queue = cl::CommandQueue(context, device);
  164.  
  165. // set the kernel args index 0, 1, 2 to the corresponding buffers.
  166. kernel.setArg(0, substitutedBuffer);
  167. kernel.setArg(1, desubstitutedBuffer);
  168.  
  169.  
  170. // enqueue Kernel
  171. queue.enqueueNDRangeKernel(kernel, offset, globalSize);
  172.  
  173. std::cout << "Decryption for ciphertext2a." << std::endl;
  174. std::cout << "--------------------" << std::endl;
  175.  
  176. // enqueue command to read from device to host memory
  177. queue.enqueueReadBuffer(desubstitutedBuffer, CL_TRUE, 0, sizeof(cl_uchar) * ptSize, &outputVector[0]);
  178.  
  179.  
  180. //Saving encrypted text for task 2c.
  181. std::ofstream Output;
  182. Output.open(outputDeSubstitutedTextCL1);
  183. for (int i = 0; i < outputVector.size(); i++) {
  184. Output << outputVector[i];
  185. }
  186. std::cout << "Output Encrypted Text from OpenCL Device to: " << outputDeSubstitutedTextCL1 << std::endl;
  187. std::cout << "--------------------" << std::endl;
  188. Output.close();
  189.  
  190.  
  191.  
  192. //Decipher 2b file
  193. if (!build_program(&program, &context, "desubstituteb.cl"))
  194. {
  195. // if OpenCL program build error
  196. quit_program("OpenCL program build error.");
  197. }
  198.  
  199. substitutedBufferb = cl::Buffer(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(cl_uchar) * ptSizeb, &originalTextVectorb[0]);
  200. desubstitutedBufferb = cl::Buffer(context, CL_MEM_WRITE_ONLY, sizeof(cl_uchar) * ptSizeb);
  201.  
  202.  
  203. // create a kernel
  204. kernel = cl::Kernel(program, "desubstituteb");
  205.  
  206. // create command queue
  207. queue = cl::CommandQueue(context, device);
  208.  
  209. // set the kernel args index 0, 1, 2 to the corresponding buffers.
  210. kernel.setArg(0, substitutedBufferb);
  211. kernel.setArg(1, desubstitutedBufferb);
  212.  
  213. // enqueue Kernel
  214. queue.enqueueNDRangeKernel(kernel, offset, globalSize);
  215.  
  216. std::cout << "Decryption for ciphertext2b." << std::endl;
  217. std::cout << "--------------------" << std::endl;
  218.  
  219. // enqueue command to read from device to host memory
  220. queue.enqueueReadBuffer(desubstitutedBufferb, CL_TRUE, 0, sizeof(cl_uchar) * ptSizeb, &outputVectorb[0]);
  221.  
  222. //Saving encrypted text for task 2c.
  223. std::ofstream Output2;
  224. Output2.open(outputDeSubstitutedTextCL2);
  225. for (int i = 0; i < outputVectorb.size(); i++) {
  226. Output2 << outputVectorb[i];
  227. }
  228. std::cout << "Output Encrypted Text from OpenCL Device to: " << outputDeSubstitutedTextCL2 << std::endl;
  229. std::cout << "--------------------" << std::endl;
  230. Output2.close();
  231.  
  232.  
  233. }
  234.  
  235.  
  236. catch (cl::Error err) {
  237. handle_error(err);
  238. }
  239. }
  240.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement