cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted
Explorer
Explorer
440 Views
Registered: ‎02-07-2020

Vitis Buil sw-emulation and run correct but result different than desired one

Jump to solution

Hi to all. 

I have created host and kernel code to return the addition and the subtraction of two scalars in order to be familiar with Vitis.

Seems to compile and run correctly but the result it returns is not correct.

Host code

#include "xcl2.hpp"
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include<stdlib.h>
#include <vector>
#define DATA_SIZE 2 // c[0] kai c[1]
//#define BUFFER_SIZE 1024

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 size_bytes = sizeof(int) * DATA_SIZE;
    cl_int err;
    cl::Context context;
    cl::Kernel krnl_add;
    cl::CommandQueue q;
    
     std::vector<int, aligned_allocator<int>> result(DATA_SIZE);
    
     for (int i = 0; i < DATA_SIZE; i++) {
       result[i] = 0;
         }

    
    auto devices = xcl::get_xil_devices();
    
    auto fileBuf = xcl::read_binary_file(binaryFile);
    cl::Program::Binaries bins{{fileBuf.data(), fileBuf.size()}};
    int valid_device = 0;
    for (unsigned int i = 0; i < devices.size(); i++) {
        auto device = devices[i];
        // Creating Context and Command Queue for selected Device
        OCL_CHECK(err, context = cl::Context(device, NULL, NULL, NULL, &err));
        OCL_CHECK(err,
                  q = cl::CommandQueue(
                      context, device, CL_QUEUE_PROFILING_ENABLE, &err));
        std::cout << "Trying to program device[" << i
                  << "]: " << device.getInfo<CL_DEVICE_NAME>() << std::endl;
        cl::Program program(context, {device}, bins, NULL, &err);
        if (err != CL_SUCCESS) {
            std::cout << "Failed to program device[" << i
                      << "] with xclbin file!\n";
        } else {
            std::cout << "Device[" << i << "]: program successful!\n";
            OCL_CHECK(err, krnl_add = cl::Kernel(program, "vadd", &err));
            valid_device++;
            break; // we break because we found a valid device
        }
    }
    if (valid_device == 0) {
        std::cout << "Failed to program any device found, exit!\n";
        exit(EXIT_FAILURE);
    }
   
    OCL_CHECK(err,
              cl::Buffer buffer_c(context,
            		  CL_MEM_COPY_HOST_PTR | CL_MEM_WRITE_ONLY,
                                       size_bytes,
                                       result.data(),
									   &err));

     int buffer_a = 12;
     int buffer_b = 5;

    OCL_CHECK(err, err = krnl_add.setArg(0, buffer_a));
    OCL_CHECK(err, err = krnl_add.setArg(1, buffer_b));
    OCL_CHECK(err, err = krnl_add.setArg(2, buffer_c));
    
    OCL_CHECK(err, err = q.enqueueTask(krnl_add));

    // Copy Result from Device Global Memory to Host Local Memory
    OCL_CHECK(err,
              err = q.enqueueMigrateMemObjects({buffer_c},
                                               CL_MIGRATE_MEM_OBJECT_HOST));

    q.finish();

    std::cout << "return value = " << result[0] <<std::endl;
    std::cout << "return value = " << result[1] <<std::endl;


   
    bool match = true;
    

    std::cout << "TEST " << (match ? "PASSED" : "FAILED") << std::endl;

    return (match ? EXIT_SUCCESS : EXIT_FAILURE);

}


kernel code.

#define DATA_SIZE 2


extern "C" {


void vadd(int a, int b, unsigned int *c)
{

#pragma HLS INTERFACE m_axi     port=c  offset=slave bundle=gmem
#pragma HLS INTERFACE s_axilite port=a               bundle=control
#pragma HLS INTERFACE s_axilite port=b               bundle=control
#pragma HLS INTERFACE s_axilite port=c               bundle=control
#pragma HLS INTERFACE s_axilite port=return            bundle=control

			c[0] = a+b;
			c[1] = a-b;



}

}

For integers 12 and 5 i declare in host code i receive the following console output.

Found Platform
Platform Name: Xilinx
INFO: Reading ../binary_container_1.xclbin
Loading: '../binary_container_1.xclbin'
Trying to program device[0]: xilinx_u200_xdma_201830_2
Device[0]: program successful!
return value = 0
return value = 0
TEST PASSED

where return value should be 17 and 7 instead of 0.

Any help guys?

Thanks in advance.

 

0 Kudos
Reply
1 Solution

Accepted Solutions
Highlighted
Adventurer
Adventurer
283 Views
Registered: ‎03-01-2020

If you just change the "cl::Buffer buffer_c" definition to use "CL_MEM_USE_HOST_PTR" instead of "CL_MEM_COPY_HOST_PTR", you will get correct output.

View solution in original post

4 Replies
Highlighted
Explorer
Explorer
387 Views
Registered: ‎02-07-2020

No one to help me find the error in code?

0 Kudos
Reply
Highlighted
Explorer
Explorer
362 Views
Registered: ‎02-07-2020

Any help..???

0 Kudos
Reply
Highlighted
Explorer
Explorer
316 Views
Registered: ‎02-07-2020

If i pass in kernel 

printf("Kernel result add %d\n", c[0]);
printf(" kernel result abstr %d\n", c[1]);

The results are the right ones(17 and 7) but they are not passed back to the host who still returns the value, return value=0

instead of 17 and 7.

kernel

#include <vector>
#include <stdio.h>
#include <iostream>
using namespace std;
#define DATA_SIZE 2


extern "C" {


void vadd(int a, int b,  int *c)
{




#pragma HLS INTERFACE m_axi     port=c  offset=slave bundle=gmem
#pragma HLS INTERFACE s_axilite port=a               bundle=control
#pragma HLS INTERFACE s_axilite port=b               bundle=control
#pragma HLS INTERFACE s_axilite port=c               bundle=control
//#pragma HLS INTERFACE s_axilite port=size              bundle=control
#pragma HLS INTERFACE s_axilite port=return            bundle=control

//write:
      c[0] = a+b;
      c[1] = a-b;
      printf("Kernel result add %d\n", c[0]);
      printf(" kernel result abstr %d\n", c[1]);



}

}

Results

 

Found Platform
Platform Name: Xilinx
INFO: Reading ../vadd.xclbin
Loading: '../vadd.xclbin'
Trying to program device[0]: xilinx_u200_xdma_201830_2
Device[0]: program successful!
Kernel result add 17
kernel result abstr 7
return value 0
return value 0
TEST PASSED

 

Tags (1)
0 Kudos
Reply
Highlighted
Adventurer
Adventurer
284 Views
Registered: ‎03-01-2020

If you just change the "cl::Buffer buffer_c" definition to use "CL_MEM_USE_HOST_PTR" instead of "CL_MEM_COPY_HOST_PTR", you will get correct output.

View solution in original post