-
Notifications
You must be signed in to change notification settings - Fork 214
/
host.cpp
97 lines (78 loc) · 3.13 KB
/
host.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Copyright (C) 2019-2021 Xilinx, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may
* not use this file except in compliance with the License. A copy of the
* License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#include "cmdlineparser.h"
#include <iostream>
#include <cstring>
// XRT includes
#include "experimental/xrt_bo.h"
#include "experimental/xrt_device.h"
#include "experimental/xrt_kernel.h"
#define DATA_SIZE 4096
int main(int argc, char** argv) {
// Command Line Parser
sda::utils::CmdLineParser parser;
// Switches
//**************//"<Full Arg>", "<Short Arg>", "<Description>", "<Default>"
parser.addSwitch("--xclbin_file", "-x", "input binary file string", "");
parser.addSwitch("--device_id", "-d", "device index", "0");
parser.parse(argc, argv);
// Read settings
std::string binaryFile = parser.value("xclbin_file");
int device_index = stoi(parser.value("device_id"));
if (argc < 3) {
parser.printHelp();
return EXIT_FAILURE;
}
std::cout << "Open the device" << device_index << std::endl;
auto device = xrt::device(device_index);
std::cout << "Load the xclbin " << binaryFile << std::endl;
auto uuid = device.load_xclbin(binaryFile);
size_t vector_size_bytes = sizeof(int) * DATA_SIZE;
auto krnl = xrt::kernel(device, uuid, "vadd");
std::cout << "Allocate Buffer in Global Memory\n";
auto bo0 = xrt::bo(device, vector_size_bytes, krnl.group_id(0));
auto bo1 = xrt::bo(device, vector_size_bytes, krnl.group_id(1));
auto bo_out = xrt::bo(device, vector_size_bytes, krnl.group_id(2));
// Map the contents of the buffer object into host memory
auto bo0_map = bo0.map<int*>();
auto bo1_map = bo1.map<int*>();
auto bo_out_map = bo_out.map<int*>();
std::fill(bo0_map, bo0_map + DATA_SIZE, 0);
std::fill(bo1_map, bo1_map + DATA_SIZE, 0);
std::fill(bo_out_map, bo_out_map + DATA_SIZE, 0);
// Create the test data
int bufReference[DATA_SIZE];
for (int i = 0; i < DATA_SIZE; ++i) {
bo0_map[i] = i;
bo1_map[i] = i;
bufReference[i] = bo0_map[i] + bo1_map[i];
}
// Synchronize buffer content with device side
std::cout << "synchronize input buffer data to device global memory\n";
bo0.sync(XCL_BO_SYNC_BO_TO_DEVICE);
bo1.sync(XCL_BO_SYNC_BO_TO_DEVICE);
std::cout << "Execution of the kernel\n";
auto run = krnl(bo0, bo1, bo_out, DATA_SIZE);
run.wait();
// Get the output;
std::cout << "Get the output data from the device" << std::endl;
bo_out.sync(XCL_BO_SYNC_BO_FROM_DEVICE);
// Validate our results
if (std::memcmp(bo_out_map, bufReference, DATA_SIZE))
throw std::runtime_error("Value read back does not match reference");
std::cout << "TEST PASSED\n";
return 0;
}