host.cpp 2.36 KB
Newer Older
Andrei Tosa's avatar
Andrei Tosa committed
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
#include "host.hpp"
#include <iostream>
#include <fstream>

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_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_sw_results[i] = 0;
        source_hw_results[i] = 0;
    }

// OPENCL HOST CODE AREA START
	
    std::vector<cl::Device> devices = get_devices("Xilinx");
    devices.resize(1);
    cl::Device device = devices[0];

    OCL_CHECK(err, cl::Context context(device, NULL, NULL, NULL, &err));
    OCL_CHECK(err, cl::CommandQueue q(context, device, CL_QUEUE_PROFILING_ENABLE, &err));

    char* fileBuf = read_binary_file(binaryFile, fileBufSize);
    cl::Program::Binaries bins{{fileBuf, fileBufSize}};
	
    OCL_CHECK(err, cl::Program program(context, devices, bins, NULL, &err));
    OCL_CHECK(err, cl::Kernel krnl_rnd_gen(program,"rndgen", &err));

    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));

    short seed = 12345;
    OCL_CHECK(err, err = krnl_rnd_gen.setArg(0, seed));
    OCL_CHECK(err, err = krnl_rnd_gen.setArg(1, buffer_output));

	std::cout << "* * * START SIMULATION\n";
    //OCL_CHECK(err, err = q.enqueueMigrateMemObjects({seed}, 0));	
    OCL_CHECK(err, err = q.enqueueTask(krnl_rnd_gen));
	std::cout << "* * * SIMUlATION DONE\n";
    OCL_CHECK(err, err = q.enqueueMigrateMemObjects({buffer_output},CL_MIGRATE_MEM_OBJECT_HOST));
	std::cout << "* * * MEMORY OFFLOAD\n";

//    q.finish();
	std::cout << "* * * SIMULATION DONE\n";	
// OPENCL HOST CODE AREA END


    // Compare the results of the Device to the simulation
    std::ofstream fout("data.out");
    bool match = true;
    for (int i = 0 ; i < DATA_SIZE ; i++){
		std::cout << i << ' ';
        fout << source_hw_results[i] << ' ';
    }

	fout.close();
    delete[] fileBuf;

    std::cout << "TEST " << (match ? "PASSED" : "FAILED") << std::endl; 
    return (match ? EXIT_SUCCESS : EXIT_FAILURE);
}