Blame view

tsl_aiplatform/ai_platform/GpuRgbMemoryFactory.cpp 1.62 KB
fccbe006   Hu Chunming   初步完成从解码到算法处理,任务管理...
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
  #include "GpuRgbMemoryFactory.h"
  
  GpuRgbMemoryFactory::GpuRgbMemoryFactory(){
      // TODO 这里起一个定时器线程,清理超过一定时间如30s没有使用过的mem,避免出现可能的高显存占用情形
      // 清理的时候注意线程安全性
  }
  
  GpuRgbMemory* GpuRgbMemoryFactory::getMemory(int size, string id, string gpuid){
      std::lock_guard<std::mutex> lck (mtx);
      // 需要加锁
      auto it = memoryMap.find(id);
      if (it != memoryMap.end()){
          GpuRgbMemory* mem = it->second;
          if (mem){
              if(mem->getId() == id && mem->getSize() == size && mem->getGpuId() == gpuid){
                  if (mem->isIsused() == false){
                      // 找到了,且未使用
                      mem->setIsused(true);
                      return mem;
                  }else{
                      // 找到了,但是在使用中
                      return nullptr;
                  }
              }else{
                  // 找到了,但是不符合现在的需求,删除原有的,重新分配
                  delete mem;
                  mem = nullptr;
                  memoryMap.erase(it);
              }
          }
      }
      
      // 没有找到,或者找到了但是不符合需求重新分配
6e2b079e   Hu Chunming   编译通过
34
      GpuRgbMemory* mem = nullptr; //new GpuRgbMemory(size, true, id, gpuid);
fccbe006   Hu Chunming   初步完成从解码到算法处理,任务管理...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
      memoryMap[id] = mem;
      return mem;
  }
  
  // 显存置为未使用状态
  void GpuRgbMemoryFactory::releaseMemory(string id){
      std::lock_guard<std::mutex> lck (mtx);
      auto it = memoryMap.find(id);
      if (it != memoryMap.end()){
          GpuRgbMemory* mem = it->second;
          if (mem){
              mem->setIsused(false);
          }
      }
  }