mindspore\lite\examples\runtime_cpp/main方法注释1

一、代码路径

https://forgeplus.trustie.net/projects/Huawei_Technology/mindspore/tree/master/mindspore/lite/examples/runtime_cpp/main.cc

二、代码分块解析

#include <iostream>//io流
#include <cstring>//字符串函数
#include <random>//随机数
#include <fstream>//文件流
#include <thread>//线程使用
#include <algorithm>//算法
#include "include/errorcode.h"
#include "include/model.h"
#include "include/context.h"//其他文件的导入
#include "include/lite_session.h"
#include "include/version.h"

std::string RealPath(const char *path) {//定义一个路径函数
const size_t max = 4096;//定义一个常量并初始化
  if (path == nullptr) {//判断path是否为空值
    std::cerr << "path is nullptr" << std::endl;//输出错误
    return "";
  }
  if ((strlen(path)) >= max) {//判断路径的长度是否大于max
    std::cerr << "path is too long" << std::endl;//输出路径太长错误
    return "";
  }
    auto resolved_path = std::make_unique<char[]>(max);//将max地址用指针传给Resolved_path
  if (resolved_path == nullptr) {//判断该路径是否为null
    std::cerr << "new resolved_path failed" << std::endl;//输出分配空间失败
    return "";
  }
#ifdef _WIN32//当系统为win32时
  char *real_path = _fullpath(resolved_path.get(), path, 1024);//将resolved_path的路径赋给指针
#else
  char *real_path = realpath(path, resolved_path.get());//返回绝对路径
#endif
  if (real_path == nullptr || strlen(real_path) == 0) {//判断real路径是否为空或者null
    std::cerr << "file path is not valid : " << path << std::endl;//输出文件路径无效
    return "";
  }
  std::string res = resolved_path.get();//定义res得到文件路径
  return res;//返回res
}

char *ReadFile(const char *file, size_t *size) {//定义一个读取文件指针方法
  if (file == nullptr) {//判断文件是否为无效值
    std::cerr << "file is nullptr." << std::endl;//输出错误
    return nullptr;//返回null
  }

  std::ifstream ifs(file);//用ifstream来打开一个文件
  if (!ifs.good()) {检测流的状态是否正常。当错误的状态 都没被设置的时候返回true
    std::cerr << "file: " << file << " is not exist." << std::endl;//输出文件错误
    return nullptr;//返回null
  }

  if (!ifs.is_open()) {//判断文件流对象与文件绑定
    std::cerr << "file: " << file << " open failed." << std::endl;//输出错误
    return nullptr;
  }

  ifs.seekg(0, std::ios::end);//设将读指针设置位置
  *size = ifs.tellg();//读取文件指针位置
  std::unique_ptr<char[]> buf(new (std::nothrow) char[*size]);//通过原始指针创建 unique_ptr 实例buf
  if (buf == nullptr) {//判断buf
    std::cerr << "malloc buf failed, file: " << file << std::endl;
    ifs.close();
    return nullptr;
  }

  ifs.seekg(0, std::ios::beg);//设置文件指针位置
  ifs.read(buf.get(), *size);//从输入流中提取*size个字符,并把他们存在buf中
  ifs.close();//关闭文件流

  return buf.release();//返回buf,释放buf的所有权
}

template <typename T, typename Distribution>//定义模板函数类型变量T
void GenerateRandomData(int size, void *data, Distribution distribution) {//定义生成随机数据函数
  if (data == nullptr) {//判断data是否为空
    std::cerr << "data is nullptr." << std::endl;
    return;
  }
  std::mt19937 random_engine;//生成随机数
  int elements_num = size / sizeof(T);//定义元素数变量
  (void)std::generate_n(static_cast<T *>(data), elements_num,
                    [&]() { return static_cast<T>(distribution(random_engine)); });//生成序列数函数
}

std::shared_ptr<mindspore::lite::Context> CreateCPUContext() {//使用原始指针创建 shared_ptr 对象
  auto context = std::make_shared<mindspore::lite::Context>();//返回context的指针
  if (context == nullptr) {//判断context时否为null
    std::cerr << "New context failed while running." << std::endl;//输出错误
    return nullptr;
  }
  // 配置线程池中的工作线程数为2,包括主线程。
  context->thread_num_ = 2;
   // CPU 设备上下文具有默认值
  auto &cpu_device_info = context->device_list_[0].device_info_.cpu_device_info_;
  //大核心在线程和核心绑定方法中优先。 此参数将在 BindThread 中起作用
  // 接口,具体绑定效果见“运行图”部分。
  cpu_device_info.cpu_bind_mode_ = mindspore::lite::HIGHER_CPU;
  // Use float16 operator as priority.
  cpu_device_info.enable_float16_ = true;
  return context;
}

std::shared_ptr<mindspore::lite::Context> CreateGPUContext() {//定义指针方法创建GPU上下文
  auto context = std::make_shared<mindspore::lite::Context>();
  if (context == nullptr) {//判断错误
    std::cerr << "New context failed while running. " << std::endl;
    return nullptr;
  }

   // If GPU device context is set. The preferred backend is GPU, which means, if there is a GPU operator, it will run on
  // the GPU first, otherwise it will run on the CPU.
  mindspore::lite::DeviceContext gpu_device_ctx{mindspore::lite::DT_GPU, {false}};
  // GPU use float16 operator as priority.
  gpu_device_ctx.device_info_.gpu_device_info_.enable_float16_ = true;
  // The GPU device context needs to be push_back into device_list to work.
  context->device_list_.push_back(gpu_device_ctx);
  return context; 
}

std::shared_ptr<mindspore::lite::Context> CreateNPUContext() {//定义Npu创建内容方法
  auto context = std::make_shared<mindspore::lite::Context>();
  if (context == nullptr) {//判断错误
    std::cerr << "New context failed while running. " << std::endl;
    return nullptr;
  }
  mindspore::lite::DeviceContext npu_device_ctx{mindspore::lite::DT_NPU};
  npu_device_ctx.device_info_.npu_device_info_.frequency_ = 3;
  // The NPU device context needs to be push_back into device_list to work.
  context->device_list_.push_back(npu_device_ctx);
  return context;
}

int GetInputsAndSetData(mindspore::session::LiteSession *session) {定义一个获取输出设置数据方法
  auto inputs = session->GetInputs();//获取输入

  // The model has only one input tensor.
  auto in_tensor = inputs.front();//变量引用
  if (in_tensor == nullptr) {//判断输入变量是否为null
    std::cerr << "Input tensor is nullptr" << std::endl;
   return -1;
 }
  auto input_data = in_tensor->MutableData();//提取变量中的数据
  if (input_data == nullptr) {//判断数据是否有效
    std::cerr << "MallocData for inTensor failed." << std::endl;
    return -1;
  }
  GenerateRandomData<float>(in_tensor->Size(), input_data, std::uniform_real_distribution<float>(0.1f, 1.0f));//生成data个浮点类型的随机数据

  return 0;
}

int GetInputsByTensorNameAndS etData(mindspore::session::LiteSession *session) {通过多维数组名称和数据获取输入
  auto in_tensor = session->GetInputsByTensorName("2031_2030_1_construct_wrapper:x");
  if (in_tensor == nullptr) {//判断创建是否成功
    std::cerr << "Input tensor is nullptr" << std::endl;
    return -1;
  }
  auto input_data = in_tensor->MutableData();//获得输入数据
  if (input_data == nullptr) {//判断是否成功
    std::cerr << "MallocData for inTensor failed." << std::endl;
    return -1;
  }
   GenerateRandomData<float>(in_tensor->Size(), input_data, std::uniform_real_distribution<float>(0.1f, 1.0f));//生成size个随机数据
   return 0;
 }

}