File: test_program_cache.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (99 lines) | stat: -rw-r--r-- 3,284 bytes parent folder | download | duplicates (18)
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
95
96
97
98
99
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://boostorg.github.com/compute for more information.
//---------------------------------------------------------------------------//

#define BOOST_TEST_MODULE TestProgramCache
#include <boost/test/unit_test.hpp>

#include <boost/compute/system.hpp>
#include <boost/compute/utility/program_cache.hpp>

#include "context_setup.hpp"

namespace compute = boost::compute;

BOOST_AUTO_TEST_CASE(setup)
{
    // get default context
    compute::context ctx = context;

    // get program cache
    boost::shared_ptr<compute::program_cache> cache =
        compute::program_cache::get_global_cache(ctx);

    // try to load a null string
    BOOST_CHECK(cache->get(std::string()) == boost::none);

    // try to load a non-existant program
    BOOST_CHECK(cache->get("nonexistant") == boost::none);

    // create and store a program
    const char p1_source[] =
        "__kernel void add(__global int *a, int x)\n"
        "{\n"
        "    a[get_global_id(0)] += x;\n"
        "}\n";
    compute::program p1 =
        compute::program::create_with_source(p1_source, ctx);
    p1.build();
    cache->insert("p1", p1);

    // try to load the program
    BOOST_CHECK(cache->get("p1") == p1);

    // create a copy of the context
    compute::context ctx_copy = ctx;

    // check that they both have the same cl_context
    BOOST_CHECK(ctx_copy.get() == ctx.get());

    // check that the cache is the same
    boost::shared_ptr<compute::program_cache> cache_copy =
        compute::program_cache::get_global_cache(ctx_copy);
    BOOST_CHECK(cache_copy == cache);

    // try to load the program again
    BOOST_CHECK(cache_copy->get("p1") == p1);
}

BOOST_AUTO_TEST_CASE(evict)
{
    // create cache with capacity of four and insert four programs
    compute::program_cache cache(4);
    cache.insert("a", compute::program());
    cache.insert("b", compute::program());
    cache.insert("c", compute::program());
    cache.insert("d", compute::program());

    // check that all four programs still reside in the cache
    BOOST_CHECK(cache.get("a") != boost::none);
    BOOST_CHECK(cache.get("b") != boost::none);
    BOOST_CHECK(cache.get("c") != boost::none);
    BOOST_CHECK(cache.get("d") != boost::none);

    // insert fifth program which should evict the oldest ("a")
    cache.insert("e", compute::program());

    // check that "a" has been evicted and that "e" is now present
    BOOST_CHECK(cache.get("a") == boost::none);
    BOOST_CHECK(cache.get("b") != boost::none);
    BOOST_CHECK(cache.get("c") != boost::none);
    BOOST_CHECK(cache.get("d") != boost::none);
    BOOST_CHECK(cache.get("e") != boost::none);

    // clear cache and ensure no program objects are found
    cache.clear();
    BOOST_CHECK(cache.get("a") == boost::none);
    BOOST_CHECK(cache.get("b") == boost::none);
    BOOST_CHECK(cache.get("c") == boost::none);
    BOOST_CHECK(cache.get("d") == boost::none);
    BOOST_CHECK(cache.get("e") == boost::none);
}

BOOST_AUTO_TEST_SUITE_END()