1 //---------------------------------------------------------------------------// 2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com> 3 // 4 // Distributed under the Boost Software License, Version 1.0 5 // See accompanying file LICENSE_1_0.txt or copy at 6 // http://www.boost.org/LICENSE_1_0.txt 7 // 8 // See http://boostorg.github.com/compute for more information. 9 //---------------------------------------------------------------------------// 10 11 #define BOOST_TEST_MODULE TestProgramCache 12 #include <boost/test/unit_test.hpp> 13 14 #include <boost/compute/system.hpp> 15 #include <boost/compute/utility/program_cache.hpp> 16 17 #include "context_setup.hpp" 18 19 namespace compute = boost::compute; 20 BOOST_AUTO_TEST_CASE(setup)21BOOST_AUTO_TEST_CASE(setup) 22 { 23 // get default context 24 compute::context ctx = context; 25 26 // get program cache 27 boost::shared_ptr<compute::program_cache> cache = 28 compute::program_cache::get_global_cache(ctx); 29 30 // try to load a null string 31 BOOST_CHECK(cache->get(std::string()) == boost::none); 32 33 // try to load a non-existant program 34 BOOST_CHECK(cache->get("nonexistant") == boost::none); 35 36 // create and store a program 37 const char p1_source[] = 38 "__kernel void add(__global int *a, int x)\n" 39 "{\n" 40 " a[get_global_id(0)] += x;\n" 41 "}\n"; 42 compute::program p1 = 43 compute::program::create_with_source(p1_source, ctx); 44 p1.build(); 45 cache->insert("p1", p1); 46 47 // try to load the program 48 BOOST_CHECK(cache->get("p1") == p1); 49 50 // create a copy of the context 51 compute::context ctx_copy = ctx; 52 53 // check that they both have the same cl_context 54 BOOST_CHECK(ctx_copy.get() == ctx.get()); 55 56 // check that the cache is the same 57 boost::shared_ptr<compute::program_cache> cache_copy = 58 compute::program_cache::get_global_cache(ctx_copy); 59 BOOST_CHECK(cache_copy == cache); 60 61 // try to load the program again 62 BOOST_CHECK(cache_copy->get("p1") == p1); 63 } 64 BOOST_AUTO_TEST_CASE(evict)65BOOST_AUTO_TEST_CASE(evict) 66 { 67 // create cache with capacity of four and insert four programs 68 compute::program_cache cache(4); 69 cache.insert("a", compute::program()); 70 cache.insert("b", compute::program()); 71 cache.insert("c", compute::program()); 72 cache.insert("d", compute::program()); 73 74 // check that all four programs still reside in the cache 75 BOOST_CHECK(cache.get("a") != boost::none); 76 BOOST_CHECK(cache.get("b") != boost::none); 77 BOOST_CHECK(cache.get("c") != boost::none); 78 BOOST_CHECK(cache.get("d") != boost::none); 79 80 // insert fifth program which should evict the oldest ("a") 81 cache.insert("e", compute::program()); 82 83 // check that "a" has been evicted and that "e" is now present 84 BOOST_CHECK(cache.get("a") == boost::none); 85 BOOST_CHECK(cache.get("b") != boost::none); 86 BOOST_CHECK(cache.get("c") != boost::none); 87 BOOST_CHECK(cache.get("d") != boost::none); 88 BOOST_CHECK(cache.get("e") != boost::none); 89 90 // clear cache and ensure no program objects are found 91 cache.clear(); 92 BOOST_CHECK(cache.get("a") == boost::none); 93 BOOST_CHECK(cache.get("b") == boost::none); 94 BOOST_CHECK(cache.get("c") == boost::none); 95 BOOST_CHECK(cache.get("d") == boost::none); 96 BOOST_CHECK(cache.get("e") == boost::none); 97 } 98 99 BOOST_AUTO_TEST_SUITE_END() 100