TEST(CacheTest, SimplePut) { cache::lru_cache<int, int> cache_lru(1); cache_lru.put(7, 777); EXPECT_TRUE(cache_lru.exists(7)); EXPECT_EQ(777, cache_lru.get(7)); EXPECT_EQ(1, cache_lru.size()); }
void *cuda_make_ctx(CUcontext ctx, int flags) { cuda_context *res; void *p; res = malloc(sizeof(*res)); if (res == NULL) return NULL; res->ctx = ctx; res->err = CUDA_SUCCESS; res->blas_handle = NULL; res->refcnt = 1; res->flags = flags; res->enter = 0; res->freeblocks = NULL; if (detect_arch(ARCH_PREFIX, res->bin_id, &err)) { goto fail_cache; } res->extcopy_cache = cache_lru(64, 32, (cache_eq_fn)extcopy_eq, (cache_hash_fn)extcopy_hash, (cache_freek_fn)extcopy_free, (cache_freev_fn)cuda_freekernel); if (res->extcopy_cache == NULL) { goto fail_cache; } err = cuStreamCreate(&res->s, 0); if (err != CUDA_SUCCESS) { goto fail_stream; } err = cuStreamCreate(&res->mem_s, CU_STREAM_NON_BLOCKING); if (err != CUDA_SUCCESS) { goto fail_mem_stream; } err = cuMemAllocHost(&p, 16); if (err != CUDA_SUCCESS) { goto fail_errbuf; } memset(p, 0, 16); /* Need to tag for new_gpudata */ TAG_CTX(res); res->errbuf = new_gpudata(res, (CUdeviceptr)p, 16); if (res->errbuf == NULL) { err = res->err; goto fail_end; } res->errbuf->flags |= CUDA_MAPPED_PTR; return res; fail_end: cuMemFreeHost(p); fail_errbuf: cuStreamDestroy(res->mem_s); fail_mem_stream: cuStreamDestroy(res->s); fail_stream: cache_destroy(res->extcopy_cache); fail_cache: free(res); return NULL; }
TEST(CacheTest1, KeepsAllValuesWithinCapacity) { cache::lru_cache<int, int> cache_lru(TEST2_CACHE_CAPACITY); for (int i = 0; i < NUM_OF_TEST2_RECORDS; ++i) { cache_lru.put(i, i); } for (int i = 0; i < NUM_OF_TEST2_RECORDS - TEST2_CACHE_CAPACITY; ++i) { EXPECT_FALSE(cache_lru.exists(i)); } for (int i = NUM_OF_TEST2_RECORDS - TEST2_CACHE_CAPACITY; i < NUM_OF_TEST2_RECORDS; ++i) { EXPECT_TRUE(cache_lru.exists(i)); EXPECT_EQ(i, cache_lru.get(i)); } size_t size = cache_lru.size(); EXPECT_EQ(TEST2_CACHE_CAPACITY, size); }
TEST(CacheTest, MissingValue) { cache::lru_cache<int, int> cache_lru(1); EXPECT_THROW(cache_lru.get(7), std::range_error); }