check transfer  0.1
Check data transfer for SDAccell OpenCL application
xcl2.cpp
Go to the documentation of this file.
1 /**********
2 Copyright (c) 2017, Xilinx, Inc.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 
11 2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14 
15 3. Neither the name of the copyright holder nor the names of its contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********/
29 
30 #include <unistd.h>
31 #include <limits.h>
32 #include <sys/stat.h>
33 #include "xcl2.hpp"
34 namespace xcl {
35 std::vector<cl::Device> get_devices(const std::string& vendor_name) {
36 
37  size_t i;
38  std::vector<cl::Platform> platforms;
39  cl::Platform::get(&platforms);
40  cl::Platform platform;
41  for (i = 0 ; i < platforms.size(); i++){
42  platform = platforms[i];
43  std::string platformName = platform.getInfo<CL_PLATFORM_NAME>();
44  std::cout << "platform Name: " << platformName.c_str() << std::endl
45  << "Vendor Name : " << vendor_name.c_str()
46  << std::endl;
47  if (platformName == vendor_name){
48  std::cout << "Found Platform" << std::endl;
49  break;
50  }
51  }
52  if (i == platforms.size()) {
53  std::cout << "Error: Failed to find Xilinx platform" << std::endl;
54  exit(EXIT_FAILURE);
55  }
56 
57  //Getting ACCELERATOR Devices and selecting 1st such device
58  std::vector<cl::Device> devices;
59  platform.getDevices(CL_DEVICE_TYPE_ACCELERATOR, &devices);
60  return devices;
61 }
62 
63 std::vector<cl::Device> get_xil_devices() {
64  return get_devices("Xilinx");
65 }
66 cl::Program::Binaries import_binary_file(std::string xclbin_file_name)
67 {
68  std::cout << "INFO: Importing " << xclbin_file_name << std::endl;
69 
70  if(access(xclbin_file_name.c_str(), R_OK) != 0) {
71  printf("ERROR: %s xclbin not available please build\n", xclbin_file_name.c_str());
72  exit(EXIT_FAILURE);
73  }
74  //Loading XCL Bin into char buffer
75  std::cout << "Loading: '" << xclbin_file_name.c_str() << "'\n";
76  std::ifstream bin_file(xclbin_file_name.c_str(), std::ifstream::binary);
77  bin_file.seekg (0, bin_file.end);
78  unsigned nb = bin_file.tellg();
79  bin_file.seekg (0, bin_file.beg);
80  char *buf = new char [nb];
81  bin_file.read(buf, nb);
82 
83  cl::Program::Binaries bins;
84  bins.push_back({buf,nb});
85  return bins;
86 }
87 
88 std::string
89 find_binary_file(const std::string& _device_name, const std::string& xclbin_name)
90 {
91  std::cout << "XCLBIN File Name: " << xclbin_name.c_str() << std::endl;
92  char *xcl_mode = getenv("XCL_EMULATION_MODE");
93  char *xcl_target = getenv("XCL_TARGET");
94  std::string mode;
95 
96  /* Fall back mode if XCL_EMULATION_MODE is not set is "hw" */
97  if(xcl_mode == NULL) {
98  mode = "hw";
99  } else {
100  /* if xcl_mode is set then check if it's equal to true*/
101  if(strcmp(xcl_mode,"true") == 0) {
102  /* if it's true, then check if xcl_target is set */
103  if(xcl_target == NULL) {
104  /* default if emulation but not specified is software emulation */
105  mode = "sw_emu";
106  } else {
107  /* otherwise, it's what ever is specified in XCL_TARGET */
108  mode = xcl_target;
109  }
110  } else {
111  /* if it's not equal to true then it should be whatever
112  * XCL_EMULATION_MODE is set to */
113  mode = xcl_mode;
114  }
115  }
116  char *xcl_bindir = getenv("XCL_BINDIR");
117 
118  // typical locations of directory containing xclbin files
119  const char *dirs[] = {
120  xcl_bindir, // $XCL_BINDIR-specified
121  "xclbin", // command line build
122  "..", // gui build + run
123  ".", // gui build, run in build directory
124  NULL
125  };
126  const char **search_dirs = dirs;
127  if (xcl_bindir == NULL) {
128  search_dirs++;
129  }
130 
131  char *device_name = strdup(_device_name.c_str());
132  if (device_name == NULL) {
133  printf("Error: Out of Memory\n");
134  exit(EXIT_FAILURE);
135  }
136 
137  // fix up device name to avoid colons and dots.
138  // xilinx:xil-accel-rd-ku115:4ddr-xpr:3.2 -> xilinx_xil-accel-rd-ku115_4ddr-xpr_3_2
139  for (char *c = device_name; *c != 0; c++) {
140  if (*c == ':' || *c == '.') {
141  *c = '_';
142  }
143  }
144 
145  char *device_name_versionless = strdup(_device_name.c_str());
146  if (device_name_versionless == NULL) {
147  printf("Error: Out of Memory\n");
148  exit(EXIT_FAILURE);
149  }
150 
151  unsigned short colons = 0;
152  for (char *c = device_name_versionless; *c != 0; c++) {
153  if (*c == ':') {
154  colons++;
155  *c = '_';
156  }
157  /* Zero out version area */
158  if (colons == 3) {
159  *c = '\0';
160  }
161  }
162 
163  const char *file_patterns[] = {
164  "%1$s/%2$s.%3$s.%4$s.xclbin", // <kernel>.<target>.<device>.xclbin
165  "%1$s/%2$s.%3$s.%5$s.xclbin", // <kernel>.<target>.<device_versionless>.xclbin
166  "%1$s/binary_container_1.xclbin", // default for gui projects
167  "%1$s/%2$s.xclbin", // <kernel>.xclbin
168  NULL
169  };
170  char xclbin_file_name[PATH_MAX];
171  memset(xclbin_file_name, 0, PATH_MAX);
172  ino_t ino = 0; // used to avoid errors if an xclbin found via multiple/repeated paths
173  for (const char **dir = search_dirs; *dir != NULL; dir++) {
174  struct stat sb;
175  if (stat(*dir, &sb) == 0 && S_ISDIR(sb.st_mode)) {
176  for (const char **pattern = file_patterns; *pattern != NULL; pattern++) {
177  char file_name[PATH_MAX];
178  memset(file_name, 0, PATH_MAX);
179  snprintf(file_name, PATH_MAX, *pattern, *dir, xclbin_name.c_str(), mode.c_str(), device_name, device_name_versionless);
180  if (stat(file_name, &sb) == 0 && S_ISREG(sb.st_mode)) {
181  char* bindir = strdup(*dir);
182  if (bindir == NULL) {
183  printf("Error: Out of Memory\n");
184  exit(EXIT_FAILURE);
185  }
186  if (*xclbin_file_name && sb.st_ino != ino) {
187  printf("Error: multiple xclbin files discovered:\n %s\n %s\n", file_name, xclbin_file_name);
188  exit(EXIT_FAILURE);
189  }
190  ino = sb.st_ino;
191  strncpy(xclbin_file_name, file_name, PATH_MAX);
192  }
193  }
194  }
195  }
196  // if no xclbin found, preferred path for error message from xcl_import_binary_file()
197  if (*xclbin_file_name == '\0') {
198  snprintf(xclbin_file_name, PATH_MAX, file_patterns[0], *search_dirs, xclbin_name.c_str(), mode.c_str(), device_name);
199  }
200 
201  free(device_name);
202  return (xclbin_file_name);
203 }
204 
206 {
207  bool ret =false;
208  char *xcl_mode = getenv("XCL_EMULATION_MODE");
209  if (xcl_mode != NULL){
210  ret = true;
211  }
212  return ret;
213 }
214 
216 {
217  bool ret =false;
218  char *xcl_mode = getenv("XCL_EMULATION_MODE");
219  if ((xcl_mode != NULL) && !strcmp(xcl_mode, "hw_emu")){
220  ret = true;
221  }
222  return ret;
223 }
224 
225 
226 };
bool is_emulation()
Definition: xcl2.cpp:205
std::string find_binary_file(const std::string &_device_name, const std::string &xclbin_name)
Definition: xcl2.cpp:89
Definition: xcl2.cpp:34
std::vector< cl::Device > get_xil_devices()
Definition: xcl2.cpp:63
std::vector< cl::Device > get_devices(const std::string &vendor_name)
Definition: xcl2.cpp:35
bool is_hw_emulation()
Definition: xcl2.cpp:215
cl::Program::Binaries import_binary_file(std::string xclbin_file_name)
Definition: xcl2.cpp:66