@@ -88,7 +88,7 @@ int main(int argc, char **argv) {
8888#define NUM_ITERATIONS 100
8989 auto selector = sycl::ext::intel::fpga_selector_v;
9090#else // #if FPGA_EMULATOR
91- #define NUM_ITERATIONS 100
91+ #define NUM_ITERATIONS 10
9292 auto selector = sycl::ext::intel::fpga_emulator_selector_v;
9393#endif
9494
@@ -124,83 +124,90 @@ int main(int argc, char **argv) {
124124
125125 // hls-fpga-machine-learning insert runtime contant
126126
127+ try {
127128#if defined(IS_BSP)
128- // Allocate host memory if BSP is in use.
129- float *vals = sycl::malloc_host<float >(kInputSz , q);
130- if (vals == nullptr ) {
131- std::cerr << " ERROR: host allocation failed for input\n " ;
132- fout.close ();
133- return 1 ;
134- }
135- float *outputs = sycl::malloc_host<float >(kOutputSz , q);
136- if (output == nullptr ) {
137- std::cerr << " ERROR: host allocation failed for output\n " ;
138- fout.close ();
139- return 1 ;
140- }
129+ // Allocate host memory if BSP is in use.
130+ float *vals = sycl::malloc_host<float >(kInputSz , q);
131+ if (vals == nullptr ) {
132+ std::cerr << " ERROR: host allocation failed for input\n " ;
133+ fout.close ();
134+ return 1 ;
135+ }
136+ float *outputs = sycl::malloc_host<float >(kOutputSz , q);
137+ if (outputs == nullptr ) {
138+ std::cerr << " ERROR: host allocation failed for output\n " ;
139+ fout.close ();
140+ return 1 ;
141+ }
141142#else
142- float *vals = new float [ kInputSz ] ;
143- float *outputs = new float [ kOutputSz ] ;
143+ float *vals = sycl::malloc_shared< float >( kInputSz , q, sycl::property_list{ buffer_location (nnet:: kInputBufferLocation )}) ;
144+ float *outputs = sycl::malloc_shared< float >( kOutputSz , q, sycl::property_list{ buffer_location (nnet:: kOutputBufferLocation )}) ;
144145#endif
145146
146- if (file_valid) {
147- // Start always-run streaming kernel here, instead of inside a loop.
148- q.single_task (MyProject{});
147+ if (file_valid) {
148+ // Start always-run streaming kernel here, instead of inside a loop.
149+ q.single_task (MyProject{});
149150
150- // hls-fpga-machine-learning insert data
151+ // hls-fpga-machine-learning insert data
151152
152- // hls-fpga-machine-learning convert output
153+ // hls-fpga-machine-learning convert output
153154
154- // Print output from kernel and from prediction file.
155- for (int i = 0 ; i < num_iterations; i++) {
156- for (int j = 0 ; j < kOutLayerSize ; j++) {
157- fout << outputs[i * kOutLayerSize + j] << " " ;
158- }
159- fout << std::endl;
160- if (i % CHECKPOINT == 0 ) {
161- std::cout << " Predictions" << std::endl;
162- // hls-fpga-machine-learning insert predictions
163- for (auto predval : predictions[i]) {
164- std::cout << predval << " " ;
155+ // Print output from kernel and from prediction file.
156+ for (int i = 0 ; i < num_iterations; i++) {
157+ for (int j = 0 ; j < kOutLayerSize ; j++) {
158+ fout << outputs[i * kOutLayerSize + j] << " " ;
165159 }
166- std::cout << std::endl;
167- std::cout << " Quantized predictions" << std::endl;
168- // hls-fpga-machine-learning insert quantized
160+ fout << std::endl;
161+ if (i % CHECKPOINT == 0 ) {
162+ std::cout << " Predictions" << std::endl;
163+ // hls-fpga-machine-learning insert predictions
164+ for (auto predval : predictions[i]) {
165+ std::cout << predval << " " ;
166+ }
167+ std::cout << std::endl;
168+ std::cout << " Quantized predictions" << std::endl;
169+ // hls-fpga-machine-learning insert quantized
170+ for (int j = 0 ; j < kOutLayerSize ; j++) {
171+ std::cout << outputs[i * kOutLayerSize + j] << " " ;
172+ }
173+ std::cout << std::endl;
174+ }
175+ }
176+ } else {
177+ std::cout << " INFO: Unable to open input/predictions file, using default input with " << num_iterations
178+ << " invocations." << std::endl;
179+ q.single_task (MyProject{});
180+ // hls-fpga-machine-learning insert top-level-function
181+ // hls-fpga-machine-learning insert zero
182+ // hls-fpga-machine-learning convert output
183+ for (int i = 0 ; i < num_iterations; i++) {
169184 for (int j = 0 ; j < kOutLayerSize ; j++) {
170185 std::cout << outputs[i * kOutLayerSize + j] << " " ;
186+ fout << outputs[i * kOutLayerSize + j] << " " ;
171187 }
172188 std::cout << std::endl;
189+ fout << std::endl;
173190 }
174191 }
175- } else {
176- std::cout << " INFO: Unable to open input/predictions file, using default input with " << num_iterations
177- << " invocations." << std::endl;
178-
179- // hls-fpga-machine-learning insert top-level-function
180-
181- // hls-fpga-machine-learning insert zero
182- q.single_task (MyProject{});
183- // hls-fpga-machine-learning convert output
184- for (int i = 0 ; i < num_iterations; i++) {
185- for (int j = 0 ; j < kOutLayerSize ; j++) {
186- std::cout << outputs[i * kOutLayerSize + j] << " " ;
187- fout << outputs[i * kOutLayerSize + j] << " " ;
188- }
189- std::cout << std::endl;
190- fout << std::endl;
192+ sycl::free (vals, q);
193+ sycl::free (outputs, q);
194+ fout.close ();
195+ std::cout << " INFO: Saved inference results to file: " << RESULTS_LOG << std::endl;
196+ } catch (sycl::exception const &e) {
197+ // Catches exceptions in the host code.
198+ std::cerr << " Caught a SYCL host exception:\n "
199+ << e.what () << " \n " ;
200+
201+ // Most likely the runtime couldn't find FPGA hardware!
202+ if (e.code ().value () == CL_DEVICE_NOT_FOUND)
203+ {
204+ std::cerr << " If you are targeting an FPGA, please ensure that your "
205+ " system has a correctly configured FPGA board.\n " ;
206+ std::cerr << " Run sys_check in the oneAPI root directory to verify.\n " ;
207+ std::cerr << " If you are targeting the FPGA emulator, compile with "
208+ " -DFPGA_EMULATOR.\n " ;
191209 }
210+ std::terminate ();
192211 }
193-
194- // Free up resources.
195- #if defined(IS_BSP)
196- free (vals);
197- free (outputs);
198- #else
199- delete[] vals;
200- delete[] outputs;
201- #endif
202- fout.close ();
203- std::cout << " INFO: Saved inference results to file: " << RESULTS_LOG << std::endl;
204-
205212 return 0 ;
206213}
0 commit comments