-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
565 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
-e . | ||
|
||
pytest | ||
pytest | ||
ipython |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/********** | ||
Copyright (c) 2017, 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 "xcl2.hpp" | ||
#include <vector> | ||
|
||
using std::vector; | ||
static const int DATA_SIZE = 256; | ||
static const std::string error_message = | ||
"Error: Result mismatch:\n" | ||
"i = %d CPU result = %d Device result = %d\n"; | ||
|
||
|
||
|
||
|
||
vector<int, aligned_allocator<int>> vector_sub( | ||
vector<int,aligned_allocator<int>> source_a, | ||
vector<int,aligned_allocator<int>> source_b | ||
) { | ||
|
||
size_t size_in_bytes = DATA_SIZE * sizeof(int); | ||
vector<int,aligned_allocator<int>> source_results(DATA_SIZE); | ||
|
||
// The get_xil_devices will return vector of Xilinx Devices | ||
std::vector<cl::Device> devices = xcl::get_xil_devices(); | ||
cl::Device device = devices[0]; | ||
|
||
//Creating Context and Command Queue for selected Device | ||
cl::Context context(device); | ||
cl::CommandQueue q(context, device, CL_QUEUE_PROFILING_ENABLE); | ||
std::string device_name = device.getInfo<CL_DEVICE_NAME>(); | ||
std::cout << "Found Device=" << device_name.c_str() << std::endl; | ||
|
||
// import_binary() command will find the OpenCL binary file created using the | ||
// xocc compiler load into OpenCL Binary and return as Binaries | ||
// OpenCL and it can contain many functions which can be executed on the | ||
// device. | ||
std::string binaryFile = xcl::find_binary_file(device_name,"src/vector_subtraction"); | ||
cl::Program::Binaries bins = xcl::import_binary_file(binaryFile); | ||
devices.resize(1); | ||
cl::Program program(context, devices, bins); | ||
|
||
// These commands will allocate memory on the FPGA. The cl::Buffer objects can | ||
// be used to reference the memory locations on the device. The cl::Buffer | ||
// object cannot be referenced directly and must be passed to other OpenCL | ||
// functions. | ||
cl::Buffer buffer_a(context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_ONLY, | ||
size_in_bytes, source_a.data()); | ||
cl::Buffer buffer_b(context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_ONLY, | ||
size_in_bytes, source_b.data()); | ||
cl::Buffer buffer_result(context, CL_MEM_USE_HOST_PTR | CL_MEM_WRITE_ONLY, | ||
size_in_bytes, source_results.data()); | ||
//Separate Read/write Buffer vector is needed to migrate data between host/device | ||
std::vector<cl::Memory> inBufVec, outBufVec; | ||
inBufVec.push_back(buffer_a); | ||
inBufVec.push_back(buffer_b); | ||
outBufVec.push_back(buffer_result); | ||
|
||
|
||
// These commands will load the source_a and source_b vectors from the host | ||
// application and into the buffer_a and buffer_b cl::Buffer objects. The data | ||
// will be be transferred from system memory over PCIe to the FPGA on-board | ||
// DDR memory. | ||
q.enqueueMigrateMemObjects(inBufVec,0/* 0 means from host*/); | ||
|
||
// This call will extract a kernel out of the program we loaded in the | ||
// previous line. A kernel is an OpenCL function that is executed on the | ||
// FPGA. This function is defined in the src/vetor_addition.cl file. | ||
cl::Kernel krnl_vector_sub(program,"vector_sub"); | ||
|
||
//set the kernel Arguments | ||
int narg=0; | ||
krnl_vector_sub.setArg(narg++,buffer_result); | ||
krnl_vector_sub.setArg(narg++,buffer_a); | ||
krnl_vector_sub.setArg(narg++,buffer_b); | ||
krnl_vector_sub.setArg(narg++,DATA_SIZE); | ||
|
||
//Launch the Kernel | ||
q.enqueueTask(krnl_vector_sub); | ||
|
||
// The result of the previous kernel execution will need to be retrieved in | ||
// order to view the results. This call will write the data from the | ||
// buffer_result cl_mem object to the source_results vector | ||
q.enqueueMigrateMemObjects(outBufVec,CL_MIGRATE_MEM_OBJECT_HOST); | ||
q.finish(); | ||
|
||
return source_results; | ||
} | ||
|
||
|
||
|
||
// This example illustrates the very simple OpenCL example that performs | ||
// an addition on two vectors | ||
int main(int argc, char **argv) { | ||
if (argc != 3){ | ||
std::cout << "Please provide integers to add" << std::endl; | ||
return 1; | ||
} | ||
|
||
// Creates a vector of DATA_SIZE elements with an initial value | ||
// passed in as arguments | ||
vector<int,aligned_allocator<int>> source_a(DATA_SIZE, atoi(argv[1])); | ||
vector<int,aligned_allocator<int>> source_b(DATA_SIZE, atoi(argv[2])); | ||
vector<int,aligned_allocator<int>> source_results(DATA_SIZE); | ||
|
||
source_results = vector_sub(source_a, source_b); | ||
|
||
int match = 0; | ||
printf("Result = \n"); | ||
for (int i = 0; i < DATA_SIZE; i++) { | ||
int host_result = source_a[i] - source_b[i]; | ||
if (source_results[i] != host_result) { | ||
printf(error_message.c_str(), i, host_result, source_results[i]); | ||
match = 1; | ||
break; | ||
} else { | ||
printf("%d ", source_results[i]); | ||
if (((i + 1) % 16) == 0) printf("\n"); | ||
} | ||
} | ||
|
||
std::cout << "TEST " << (match ? "FAILED" : "PASSED") << std::endl; | ||
return (match ? EXIT_FAILURE : EXIT_SUCCESS); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include <vector> | ||
#include "xcl2.hpp" | ||
|
||
using std::vector; | ||
|
||
vector<int, aligned_allocator<int>> vector_sub( | ||
vector<int,aligned_allocator<int>> source_a, | ||
vector<int,aligned_allocator<int>> source_b | ||
); |
Oops, something went wrong.