Blame view

src/VPT-1/snapshot_analysis/SnapShotFrameCache.cpp 975 Bytes
e30d6793   Zou XiKun   v0.0.1
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
  #include <cuda_runtime.h>
  #include "SnapShotFrameCache.h"
  
  void SnapShotFrameCache::init_cache(unsigned int cache_len, unsigned int cache_count)
  {
  	this->cache_len = cache_len;
  	this->cache_count = cache_count;
  	alloc();
  }
  
  bool SnapShotFrameCache::alloc()
  {
  	for (size_t i = 0; i < cache_count; i++)
  	{
  		void * frame = NULL;
  		auto cudaStatus = cudaMalloc((void**)&frame, cache_len);
  		if (cudaStatus != cudaSuccess) 
  		{
  			return false;
  		}
  		cache.push(frame);
  	}
  	return true;
  }
  
  void * SnapShotFrameCache::get_frame()
  {
  	if (cache.empty())
  	{
  		alloc();
  		if (!cache.empty())
  		{
  			void * frame = cache.front();
  			cache.pop();
  			return frame;
  		}
  		else
  		{
  			return nullptr;
  		}
  	}
  	else
  	{
  		void * frame = cache.front();
  		cache.pop();
  		return frame;
  	}
  }
  
  void SnapShotFrameCache::release(void * frame)
  {
  	cache.push(frame);
  }
  
  void SnapShotFrameCache::free()
  {
  	while (!cache.empty())
  	{
  		void * frame = cache.front();
  		cudaFree(frame);
  		cache.pop();
  	}
  }