host.cpp 2.8 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "host.hpp"
#include "defines.hpp"
#include <iostream>
#include <fstream>
#include <chrono>

using namespace std::chrono;

int main(int argc, char** argv)
{
    uint32_t count;
    if (argc == 2) {
        count = SET_SIZE;
    }
    else if (argc == 3) {
        count = atoi(argv[2]);
    }
    else {
        std::cout << "Usage: " << argv[0] << " <XCLBIN File> *SET SIZE*" << std::endl;
        return EXIT_FAILURE;
    }

    std::string binaryFile = argv[1];
    size_t vector_size_bytes = sizeof(int) * count;
    cl_int err;
    unsigned fileBufSize;

    // Allocate Memory in Host Memory
    uint32_t seed = 2;
    std::vector<uint32_t,aligned_allocator<uint32_t>> hw_results(SET_SIZE);
    std::vector<uint32_t,aligned_allocator<uint32_t>> sw_results(SET_SIZE);


    // Create the test data
//    for(int i = 0 ; i < count ; i++)
//    	hw_results[i] = 0;
//    populate_results(sw_results, seed);

// 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,"cont", &err));

    OCL_CHECK(err, cl::Buffer buffer_output(context,CL_MEM_USE_HOST_PTR | CL_MEM_WRITE_ONLY, vector_size_bytes, hw_results.data(), &err));

    OCL_CHECK(err, err = krnl_rnd_gen.setArg(0, seed));
    OCL_CHECK(err, err = krnl_rnd_gen.setArg(1, count));
    OCL_CHECK(err, err = krnl_rnd_gen.setArg(2, buffer_output));

    std::cout << "* * * RUN STARTED\n";
    auto t1 = high_resolution_clock::now(); 
    OCL_CHECK(err, err = q.enqueueTask(krnl_rnd_gen));
    OCL_CHECK(err, err = q.enqueueMigrateMemObjects({buffer_output},CL_MIGRATE_MEM_OBJECT_HOST));
    q.finish();
    auto t2 = high_resolution_clock::now(); //clock_t c2 = clock();
    std::cout << "* * * RUN DONE\n";

// OPENCL HOST CODE AREA END

    auto d1 = duration_cast<microseconds>(t2 - t1);
    std::cout << "Task:   " << d1.count() << '\n';

    // Compare the results of the Device to the simulation

    bool match = true;
/*      
    std::ofstream fout("results/data.out");
    for (int i = 0 ; i < count; i++){
        fout << hw_results[i] << '\n';

		if (sw_results[i] != hw_results[i]) {
				match = false;
				std::cout << "Not matched: " << sw_results[i] << ' ' << hw_results[i] << '\n';
				break;
		}
    }

    fout.close(); */
    delete[] fileBuf;

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