test_program_cache.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #define BOOST_TEST_MODULE TestProgramCache
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/system.hpp>
  13. #include <boost/compute/utility/program_cache.hpp>
  14. #include "context_setup.hpp"
  15. namespace compute = boost::compute;
  16. BOOST_AUTO_TEST_CASE(setup)
  17. {
  18. // get default context
  19. compute::context ctx = context;
  20. // get program cache
  21. boost::shared_ptr<compute::program_cache> cache =
  22. compute::program_cache::get_global_cache(ctx);
  23. // try to load a null string
  24. BOOST_CHECK(cache->get(std::string()) == boost::none);
  25. // try to load a non-existant program
  26. BOOST_CHECK(cache->get("nonexistant") == boost::none);
  27. // create and store a program
  28. const char p1_source[] =
  29. "__kernel void add(__global int *a, int x)\n"
  30. "{\n"
  31. " a[get_global_id(0)] += x;\n"
  32. "}\n";
  33. compute::program p1 =
  34. compute::program::create_with_source(p1_source, ctx);
  35. p1.build();
  36. cache->insert("p1", p1);
  37. // try to load the program
  38. BOOST_CHECK(cache->get("p1") == p1);
  39. // create a copy of the context
  40. compute::context ctx_copy = ctx;
  41. // check that they both have the same cl_context
  42. BOOST_CHECK(ctx_copy.get() == ctx.get());
  43. // check that the cache is the same
  44. boost::shared_ptr<compute::program_cache> cache_copy =
  45. compute::program_cache::get_global_cache(ctx_copy);
  46. BOOST_CHECK(cache_copy == cache);
  47. // try to load the program again
  48. BOOST_CHECK(cache_copy->get("p1") == p1);
  49. }
  50. BOOST_AUTO_TEST_CASE(evict)
  51. {
  52. // create cache with capacity of four and insert four programs
  53. compute::program_cache cache(4);
  54. cache.insert("a", compute::program());
  55. cache.insert("b", compute::program());
  56. cache.insert("c", compute::program());
  57. cache.insert("d", compute::program());
  58. // check that all four programs still reside in the cache
  59. BOOST_CHECK(cache.get("a") != boost::none);
  60. BOOST_CHECK(cache.get("b") != boost::none);
  61. BOOST_CHECK(cache.get("c") != boost::none);
  62. BOOST_CHECK(cache.get("d") != boost::none);
  63. // insert fifth program which should evict the oldest ("a")
  64. cache.insert("e", compute::program());
  65. // check that "a" has been evicted and that "e" is now present
  66. BOOST_CHECK(cache.get("a") == boost::none);
  67. BOOST_CHECK(cache.get("b") != boost::none);
  68. BOOST_CHECK(cache.get("c") != boost::none);
  69. BOOST_CHECK(cache.get("d") != boost::none);
  70. BOOST_CHECK(cache.get("e") != boost::none);
  71. // clear cache and ensure no program objects are found
  72. cache.clear();
  73. BOOST_CHECK(cache.get("a") == boost::none);
  74. BOOST_CHECK(cache.get("b") == boost::none);
  75. BOOST_CHECK(cache.get("c") == boost::none);
  76. BOOST_CHECK(cache.get("d") == boost::none);
  77. BOOST_CHECK(cache.get("e") == boost::none);
  78. }
  79. BOOST_AUTO_TEST_SUITE_END()