-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathhost.cpp
183 lines (154 loc) · 8.49 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**********
Copyright (c) 2018, Xilinx, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**********/
#include "host.hpp"
int main(int argc, char** argv)
{
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " <XCLBIN File>" << std::endl;
return EXIT_FAILURE;
}
std::string binaryFile = argv[1];
size_t vector_size_bytes = sizeof(int) * DATA_SIZE;
cl_int err;
unsigned fileBufSize;
// Allocate Memory in Host Memory
std::vector<int,aligned_allocator<int>> source_in1(DATA_SIZE);
std::vector<int,aligned_allocator<int>> source_in2(DATA_SIZE);
std::vector<int,aligned_allocator<int>> source_hw_results(DATA_SIZE);
std::vector<int,aligned_allocator<int>> source_sw_results(DATA_SIZE);
// Create the test data
for(int i = 0 ; i < DATA_SIZE ; i++){
source_in1[i] = rand() % DATA_SIZE;
source_in2[i] = rand() % DATA_SIZE;
source_sw_results[i] = source_in1[i] + source_in2[i];
source_hw_results[i] = 0;
}
// OPENCL HOST CODE AREA START
// ------------------------------------------------------------------------------------
// Step 1: Get All PLATFORMS, then search for Target_Platform_Vendor (CL_PLATFORM_VENDOR)
// Search for Platform: Xilinx
// Check if the current platform matches Target_Platform_Vendor
// ------------------------------------------------------------------------------------
std::vector<cl::Device> devices = get_devices("Xilinx");
devices.resize(1);
cl::Device device = devices[0];
// ------------------------------------------------------------------------------------
// Step 1: Create Context
// ------------------------------------------------------------------------------------
OCL_CHECK(err, cl::Context context(device, NULL, NULL, NULL, &err));
// ------------------------------------------------------------------------------------
// Step 1: Create Command Queue
// ------------------------------------------------------------------------------------
OCL_CHECK(err, cl::CommandQueue q(context, device, CL_QUEUE_PROFILING_ENABLE, &err));
// ------------------------------------------------------------------
// Step 1: Load Binary File from disk
// ------------------------------------------------------------------
char* fileBuf = read_binary_file(binaryFile, fileBufSize);
cl::Program::Binaries bins{{fileBuf, fileBufSize}};
// -------------------------------------------------------------
// Step 1: Create the program object from the binary and program the FPGA device with it
// -------------------------------------------------------------
OCL_CHECK(err, cl::Program program(context, devices, bins, NULL, &err));
// -------------------------------------------------------------
// Step 1: Create Kernels
// -------------------------------------------------------------
OCL_CHECK(err, cl::Kernel krnl_vector_add(program,"vadd", &err));
// ================================================================
// Step 2: Setup Buffers and run Kernels
// ================================================================
// o) Allocate Memory to store the results
// o) Create Buffers in Global Memory to store data
// ================================================================
// ------------------------------------------------------------------
// Step 2: Create Buffers in Global Memory to store data
// o) buffer_in1 - stores source_in1
// o) buffer_in2 - stores source_in2
// o) buffer_ouput - stores Results
// ------------------------------------------------------------------
// .......................................................
// Allocate Global Memory for source_in1
// .......................................................
OCL_CHECK(err, cl::Buffer buffer_in1 (context,CL_MEM_USE_HOST_PTR | CL_MEM_READ_ONLY,
vector_size_bytes, source_in1.data(), &err));
// .......................................................
// Allocate Global Memory for source_in2
// .......................................................
OCL_CHECK(err, cl::Buffer buffer_in2 (context,CL_MEM_USE_HOST_PTR | CL_MEM_READ_ONLY,
vector_size_bytes, source_in2.data(), &err));
// .......................................................
// Allocate Global Memory for sourcce_hw_results
// .......................................................
OCL_CHECK(err, cl::Buffer buffer_output(context,CL_MEM_USE_HOST_PTR | CL_MEM_WRITE_ONLY,
vector_size_bytes, source_hw_results.data(), &err));
// ============================================================================
// Step 2: Set Kernel Arguments and Run the Application
// o) Set Kernel Arguments
// ----------------------------------------------------
// Kernel Argument Description
// ----------------------------------------------------
// in1 (input) --> Input Vector1
// in2 (input) --> Input Vector2
// out (output) --> Output Vector
// size (input) --> Size of Vector in Integer
// o) Copy Input Data from Host to Global Memory on the device
// o) Submit Kernels for Execution
// o) Copy Results from Global Memory, device to Host
// ============================================================================
int size = DATA_SIZE;
OCL_CHECK(err, err = krnl_vector_add.setArg(0, buffer_in1));
OCL_CHECK(err, err = krnl_vector_add.setArg(1, buffer_in2));
OCL_CHECK(err, err = krnl_vector_add.setArg(2, buffer_output));
OCL_CHECK(err, err = krnl_vector_add.setArg(3, size));
// ------------------------------------------------------
// Step 2: Copy Input data from Host to Global Memory on the device
// ------------------------------------------------------
OCL_CHECK(err, err = q.enqueueMigrateMemObjects({buffer_in1, buffer_in2},0/* 0 means from host*/));
// ----------------------------------------
// Step 2: Submit Kernels for Execution
// ----------------------------------------
OCL_CHECK(err, err = q.enqueueTask(krnl_vector_add));
// --------------------------------------------------
// Step 2: Copy Results from Device Global Memory to Host
// --------------------------------------------------
OCL_CHECK(err, err = q.enqueueMigrateMemObjects({buffer_output},CL_MIGRATE_MEM_OBJECT_HOST));
q.finish();
// OPENCL HOST CODE AREA END
// Compare the results of the Device to the simulation
bool match = true;
for (int i = 0 ; i < DATA_SIZE ; i++){
if (source_hw_results[i] != source_sw_results[i]){
std::cout << "Error: Result mismatch" << std::endl;
std::cout << "i = " << i << " CPU result = " << source_sw_results[i]
<< " Device result = " << source_hw_results[i] << std::endl;
match = false;
break;
}
}
// ============================================================================
// Step 3: Release Allocated Resources
// ============================================================================
delete[] fileBuf;
std::cout << "TEST " << (match ? "PASSED" : "FAILED") << std::endl;
return (match ? EXIT_SUCCESS : EXIT_FAILURE);
}