bool getCounterDataPrefixImage()

in libkineto/src/CuptiNvPerfMetric.cpp [234:330]


bool getCounterDataPrefixImage(
    const std::string& chipName,
    const std::vector<std::string>& metricNames,
    std::vector<uint8_t>& counterDataImagePrefix) {

  NVPW_CUDA_MetricsContext_Create_Params metricsContextCreateParams = {
      NVPW_CUDA_MetricsContext_Create_Params_STRUCT_SIZE, nullptr};
  metricsContextCreateParams.pChipName = chipName.c_str();

  if (!NVPW_CALL(
        NVPW_CUDA_MetricsContext_Create(&metricsContextCreateParams))) {
    return false;
  }

  NVPW_MetricsContext_Destroy_Params metricsContextDestroyParams = {
      NVPW_MetricsContext_Destroy_Params_STRUCT_SIZE, nullptr};
  metricsContextDestroyParams.pMetricsContext =
      metricsContextCreateParams.pMetricsContext;


  SCOPE_EXIT([&]() {
    NVPW_MetricsContext_Destroy(
        (NVPW_MetricsContext_Destroy_Params*)&metricsContextDestroyParams);
  });

  // Get all raw metrics required for given metricNames list
  std::vector<NVPA_RawMetricRequest> rawMetricRequests;

  // note: we need a variable at this functions scope to hold the string
  // pointers for underlying C char arrays.
  std::vector<std::string> rawMetricDeps;

  if (!getRawMetricRequests(
      metricsContextCreateParams.pMetricsContext,
      metricNames,
      rawMetricDeps,
      rawMetricRequests)) {
    return false;
  }

  // Setup Counter Data builder
  NVPW_CounterDataBuilder_Create_Params counterDataBuilderCreateParams = {
      NVPW_CounterDataBuilder_Create_Params_STRUCT_SIZE, nullptr};
  counterDataBuilderCreateParams.pChipName = chipName.c_str();
  if (!NVPW_CALL(
        NVPW_CounterDataBuilder_Create(&counterDataBuilderCreateParams))) {
    return false;
  }

  NVPW_CounterDataBuilder_Destroy_Params counterDataBuilderDestroyParams = {
      NVPW_CounterDataBuilder_Destroy_Params_STRUCT_SIZE, nullptr};
  counterDataBuilderDestroyParams.pCounterDataBuilder =
      counterDataBuilderCreateParams.pCounterDataBuilder;
  SCOPE_EXIT([&]() {
    NVPW_CounterDataBuilder_Destroy((
        NVPW_CounterDataBuilder_Destroy_Params*)&counterDataBuilderDestroyParams);
  });

  // Add metrics to counter data image prefix
  NVPW_CounterDataBuilder_AddMetrics_Params addMetricsParams = {
      NVPW_CounterDataBuilder_AddMetrics_Params_STRUCT_SIZE, nullptr};
  addMetricsParams.pCounterDataBuilder =
      counterDataBuilderCreateParams.pCounterDataBuilder;
  addMetricsParams.pRawMetricRequests = rawMetricRequests.data();
  addMetricsParams.numMetricRequests = rawMetricRequests.size();
  if (!NVPW_CALL(
        NVPW_CounterDataBuilder_AddMetrics(&addMetricsParams))) {
    return false;
  }

  // Get image prefix size
  NVPW_CounterDataBuilder_GetCounterDataPrefix_Params
      getCounterDataPrefixParams = {
          NVPW_CounterDataBuilder_GetCounterDataPrefix_Params_STRUCT_SIZE, nullptr};
  getCounterDataPrefixParams.pCounterDataBuilder =
      counterDataBuilderCreateParams.pCounterDataBuilder;
  getCounterDataPrefixParams.bytesAllocated = 0;
  getCounterDataPrefixParams.pBuffer = nullptr;
  if (!NVPW_CALL(
        NVPW_CounterDataBuilder_GetCounterDataPrefix(
          &getCounterDataPrefixParams))) {
    return false;
  }

  counterDataImagePrefix.resize(getCounterDataPrefixParams.bytesCopied);

  // Now write counter data image prefix
  getCounterDataPrefixParams.bytesAllocated = counterDataImagePrefix.size();
  getCounterDataPrefixParams.pBuffer = counterDataImagePrefix.data();
  if (!NVPW_CALL(
        NVPW_CounterDataBuilder_GetCounterDataPrefix(
          &getCounterDataPrefixParams))) {
    return false;
  }

  return true;
}