fork download
  1. //==============================================================
  2. // Copyright © Intel Corporation
  3. //
  4. // SPDX-License-Identifier: MIT
  5. // =============================================================
  6. #include <CL/sycl.hpp>
  7. #include <vector>
  8. #include <iostream>
  9. #include <fstream>
  10. #include <string>
  11. #include <sstream>
  12. #include <algorithm>
  13. #include <array>
  14. #include <map>
  15. #include<chrono>
  16. #if FPGA || FPGA_EMULATOR
  17. #include <ext/intel/fpga_extensions.hpp>
  18. #endif
  19.  
  20. using namespace sycl;
  21.  
  22. // Vector type and data size for this example.
  23. size_t vector_size = 10000;
  24. typedef std::vector<int> IntVector;
  25.  
  26. const std::string train_data_path = "C:\\Users\\AMAL.T\\Downloads\\grad project\\k_nearest_neighbors_train_data.csv";
  27. const std::string train_label_path = "C:\\Users\\AMAL.T\\Downloads\\grad project\\k_nearest_neighbors_train_label.csv";
  28. const std::string test_data_path = "C:\\Users\\AMAL.T\\Downloads\\grad project\\k_nearest_neighbors_test_data.csv";
  29. const std::string test_label_path = "C:\\Users\\AMAL.T\\Downloads\\grad project\\k_nearest_neighbors_test_label.csv";
  30.  
  31. std::vector<std::vector<double>>train_data;
  32. std::vector<int>train_label;
  33.  
  34. std::vector<std::vector<double>>test_data;
  35. std::vector<int>test_label;
  36.  
  37. std::vector<int>knn_res_test_label;
  38.  
  39. // Create an exception handler for asynchronous SYCL exceptions
  40. static auto exception_handler = [](sycl::exception_list e_list) {
  41. for (std::exception_ptr const& e : e_list) {
  42. try {
  43. std::rethrow_exception(e);
  44. }
  45. catch (std::exception const& e) {
  46. #if _DEBUG
  47. std::cout << "Failure" << std::endl;
  48. #endif
  49. std::terminate();
  50. }
  51. }
  52. };
  53.  
  54. void read_csv_data(std::vector<std::vector<double>>& data_vec, std::string path) {
  55. std::vector<double> row;
  56. std::string line, word;
  57. std::fstream file(path);
  58. if (file.is_open())
  59. {
  60. while (getline(file, line))
  61. {
  62. row.clear();
  63.  
  64. std::stringstream str(line);
  65.  
  66. while (getline(str, word, ',')) {
  67. double val = std::stod(word);
  68. row.push_back(val);
  69. }
  70. data_vec.push_back(row);
  71. }
  72. file.close();
  73. }
  74. else
  75. std::cout << "Could not open the file\n";
  76. }
  77. void read_csv_label(std::vector<int>& data_vec, std::string path) {
  78. std::string line;
  79. std::fstream file(path);
  80. if (file.is_open())
  81. {
  82. while (getline(file, line))
  83. {
  84. std::stringstream str(line);
  85. data_vec.push_back(std::stoi(line));
  86. }
  87. }
  88. else
  89. std::cout << "Could not open the file\n";
  90. }
  91.  
  92. std::vector<double> distance_calculation(std::vector<std::vector<double>>& dataset, std::vector<double>& curr_test) {
  93. auto start = std::chrono::high_resolution_clock::now();
  94. std::vector<double>res;
  95. for (int i = 0; i < dataset.size(); ++i) {
  96. double dis = 0;
  97. for (int j = 0; j < dataset[i].size(); ++j) {
  98. dis += (curr_test[j] - dataset[i][j]) * (curr_test[j] - dataset[i][j]);
  99. }
  100. res.push_back(dis);
  101. }
  102. auto finish = std::chrono::high_resolution_clock::now();
  103. std::chrono::duration<double> elapsed = finish - start;
  104. std::cout << "Elapsed time: " << elapsed.count() << " s\n";
  105. return res;
  106. }
  107.  
  108. std::vector<double> distance_calculation_FPGA(queue& q, const std::vector<std::vector<double>>& dataset, const std::vector<double>& curr_test) {
  109. std::vector<double>linear_dataset;
  110. for (int i = 0; i < dataset.size(); ++i) {
  111. for (int j = 0; j < dataset[i].size(); ++j) {
  112. linear_dataset.push_back(dataset[i][j]);
  113. }
  114. }
  115. range<1> num_items{ dataset.size() };
  116. std::vector<double>res;
  117. //std::cout << "im in" << std::endl;
  118.  
  119. res.resize(dataset.size());
  120. buffer dataset_buf(linear_dataset);
  121. buffer curr_test_buf(curr_test);
  122. buffer res_buf(res.data(), num_items);
  123. {
  124. // auto start = std::chrono::high_resolution_clock::now();
  125. q.submit([&](handler& h) {
  126. accessor a(dataset_buf, h, read_only);
  127. accessor b(curr_test_buf, h, read_only);
  128.  
  129. accessor dif(res_buf, h, write_only, no_init);
  130. h.parallel_for(range<1>(num_items), [=](id<1> i) {
  131. // dif[i] = a[i].size() * 1.0;// a[i];
  132. for (int j = 0; j < 5; ++j) {
  133. dif[i] += (b[j] - a[i * 5 + j]) * (b[j] - a[i * 5 + j]);
  134.  
  135. }
  136. });
  137. }).wait();
  138. // q.wait();
  139. //auto finish = std::chrono::high_resolution_clock::now();
  140. //std::chrono::duration<double> elapsed = finish - start;
  141. // std::cout << "Elapsed time: " << elapsed.count() << " s\n";
  142.  
  143. }
  144. /*
  145.   for (int i = 0; i < dataset.size(); ++i) {
  146.   double dis = 0;
  147.   for (int j = 0; j < dataset[i].size(); ++j) {
  148.   dis += (curr_test[j] - dataset[i][j]) * (curr_test[j] - dataset[i][j]);
  149.   }
  150.   res.push_back(dis);
  151.   }
  152.   */
  153. return res;
  154. }
  155.  
  156.  
  157. int main(int argc, char* argv[]) {
  158.  
  159. if (argc > 1) vector_size = std::stoi(argv[1]);
  160. #if FPGA_EMULATOR
  161. ext::intel::fpga_emulator_selector d_selector;
  162. #elif FPGA
  163. ext::intel::fpga_selector d_selector;
  164. #else
  165. default_selector d_selector;
  166. #endif
  167.  
  168.  
  169. try {
  170. queue q(d_selector, exception_handler);
  171.  
  172. // my knn project
  173. // this is on the host definitelly
  174. read_csv_data(train_data, train_data_path);
  175. read_csv_data(test_data, test_data_path);
  176. read_csv_label(train_label, train_label_path);
  177. read_csv_label(test_label, test_label_path);
  178. //
  179.  
  180.  
  181. int k = sqrt((int)train_data.size());
  182. std::unordered_map<int, int>freq_label;
  183. std::vector<std::pair<double, int>> knn_label;
  184.  
  185. // Print out the device information used for the kernel code.
  186. std::cout << "Running on device: "
  187. << q.get_device().get_info<info::device::name>() << "\n";
  188.  
  189.  
  190. // std::vector<double> knn_dist = distance_calculation(train_data, test_data[0]);
  191.  
  192. for (int i = 0; i < (int)test_data.size(); ++i) {
  193. // std::cout << "it is alright " << i<< std::endl;
  194. std::vector<double> knn_dist = distance_calculation_FPGA(q, train_data, test_data[i]);
  195.  
  196. for (int j = 0; j < knn_dist.size(); ++j) {
  197. knn_label.push_back(std::make_pair(knn_dist[j], train_label[j]));
  198. }
  199. sort(knn_label.begin(), knn_label.end());
  200.  
  201. int mx = -1, label = -1;
  202. for (int j = 0; j < k; ++j) {
  203. // std::cout << knn_label[j].first << " " << knn_label[j].second << std::endl;
  204. int cur_label = ++freq_label[knn_label[j].second];
  205. if (cur_label > mx) {
  206. mx = cur_label;
  207. label = knn_label[j].second;
  208. }
  209. }
  210. knn_label.clear();
  211. freq_label.clear();
  212. knn_res_test_label.push_back(label);
  213. }
  214. int corr = 0;
  215. for (int i = 0; i < test_label.size(); ++i) {
  216. // std::cout << "real: " << test_label[i] << " predict: " << knn_res_test_label[i] << std::endl;
  217. if (test_label[i] == knn_res_test_label[i])
  218. corr++;
  219.  
  220. }
  221. double accuracy = corr * 1.0 / test_label.size();
  222. std::cout << corr << " " << accuracy << std::endl;
  223. //
  224. // Print out the device information used for the kernel code.
  225. std::cout << "Running on device: "
  226. << q.get_device().get_info<info::device::name>() << "\n";
  227.  
  228. }
  229. catch (exception const& e) {
  230. std::cout << "An exception is caught for vector add.\n";
  231. std::terminate();
  232. }
  233.  
  234.  
  235.  
  236. std::cout << "Vector add successfully completed on device.\n";
  237. return 0;
  238. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:6:10: fatal error: CL/sycl.hpp: No such file or directory
 #include <CL/sycl.hpp>
          ^~~~~~~~~~~~~
compilation terminated.
stdout
Standard output is empty