Skip to content

Commit

Permalink
Merge branch 'compile'
Browse files Browse the repository at this point in the history
  • Loading branch information
Cloud User committed Jan 10, 2018
2 parents fd8165a + 740e850 commit 461729f
Show file tree
Hide file tree
Showing 6 changed files with 565 additions and 2 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-e .

pytest
pytest
ipython
18 changes: 17 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ def run(self):

sys.exit()

extra_compile_args = ["-DSDX_PLATFORM=xilinx:aws-vu9p-f1:4ddr-xpr-2pr:4.0",
"-D__USE_XOPEN2K8", "-I/opt/Xilinx/SDx/2017.1.op/runtime/include/1_2/",
"-I/opt/Xilinx/SDx/2017.1.op/Vivado_HLS/include/"]

extra_link_args = ["-shared", "-lxilinxopencl", "-lpthread", "-lrt",
"-L/opt/Xilinx/SDx/2017.1.op/runtime/lib/x86_64", "-lstdc++", ]

# Where the magic happens:
setup(
Expand All @@ -83,7 +89,17 @@ def run(self):
author_email=EMAIL,
url=URL,
packages=find_packages(exclude=('tests',)),
ext_modules=[Extension("_core", ["accel/_core.c", ])],
ext_modules=[Extension(
"_core",
["accel/_core.c", "src/host.cpp", "src/xcl2.cpp"],
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
language='c++14',
runtime_library_dirs=[
"/opt/Xilinx/SDx/2017.1.rte/runtime/lib/x86_64/",
],

)],
# If your package is a single module, use this instead of 'packages':
# py_modules=['slideshows'],

Expand Down
149 changes: 149 additions & 0 deletions src/host.cpp
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);
}
9 changes: 9 additions & 0 deletions src/host.hpp
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
);
Loading

0 comments on commit 461729f

Please sign in to comment.