寒武纪神经网络设备接口--CNDEV hanxiao2019-05-16 20:20:11 回复 3 查看 调试工具
寒武纪神经网络设备接口--CNDEV

寒武纪神经网络设备接口(Cambricon Neuware Device Interface)是一套支持主机端应用程序获取寒武纪芯片硬件信息的软件接口。



1 准备

确保寒武纪MLU驱动已经安装

2 安装

     进入cndev根目录运行

./configure.sh

3 cndev使用方法
  • 包含头文件cndev.h

  • 初始化libcndev

  • 获取接口信息

  • 释放libcndev

4用例代码

示例程序主要介绍了一下几个接口的信息获取。

  • 获取libcndev版本信息

  • 初始化libcndev环境

  • 获取硬件板卡数量

  • 获取MLU利用率

  • 获取MLU每个核的利用率

  • 获取温度信息

  • 获取正在使用板卡X的进程信息

  • 释放libcndev

注:调用接口之前用户需要将当前API版本赋给每个获取信息结构体的第一个字段version。

//获取lib版本,此接口不需要初始化

cndevLibVersionInfo_t libVersion;

libVersion.version = CNDEV_VERSION_3;

cndevCheckErrors(cndevGetLibVersion(&libVersion));

  printf("cndev lib version:%d.%d.%d\n",

         libVersion.LibMajorVersion,

         libVersion.LibMinorVersion,

         libVersion.LibBuildVersion);

//初始化libcndev环境

cndevRet_t ret = cndevInit(CNDEV_VERSION_3);

 

//获取硬件板卡数量

cndevCardInfo_t cardInfo;

cardInfo.version = CNDEV_VERSION_3;

ret = cndevGetDeviceCount(&cardInfo);

//获取板卡X的整体利用率

cndevUtilizationInfo_t utilInfo;

utilInfo.version = CNDEV_VERSION_3;

       cndevCheckErrors(cndevGetDeviceUtilizationInfo(&utilInfo, card));

printf("Util:%d\n", utilInfo.BoardUtilization);

  //获取板卡X每个核的利用率

cndevCardCoreCount_t cardCoreCount;

cardCoreCount.version = CNDEV_VERSION_3;

cndevCheckErrors(cndevGetCoreCount(&cardCoreCount,

      card));

for (int i = 0; i < cardCoreCount.count; i++) {

      printf("Util CORE%d:%d%%\n", i,

 utilInfo.CoreUtilization[i]);

}

  //获取板卡X温度

     cndevTemperatureInfo_t tempInfo;

     tempInfo.version = CNDEV_VERSION_3;

     cndevCheckErrors(cndevGetTemperatureInfo(&tempInfo,

 card));

  //获取板卡X每个核的温度

     cndevCardClusterCount_t cardClusterCount;

     cardClusterCount.version = CNDEV_VERSION_3;

     cndevCheckErrors(cndevGetClusterCount(&cardClusterCount,

   card));

     printf("temperatures:board:%dC\nClusters:",

   tempInfo.Board);

     for (int i = 0; i < cardClusterCount.count; i++) {

          printf("%dC ", tempInfo.Cluster[i]);

     }

     printf("\n");

//获取板卡X编解码器的数量

    cndevCodecCount_t codecCount;

    codecCount.version = CNDEV_VERSION_3;

    cndevCheckErrors(cndevGetCodecCount(&codecCount, card));

    printf("The codec encoder unit count: %d\n",

                                                  codecCount.count);

    //获取板卡X编解码器的算力

    cndevCodecUtilization_t codecUtilization;

    codecUtilization.version = CNDEV_VERSION_3;

      cndevCheckErrors(cndevGetCodecUtilization(

                                          &codecUtilization, card));

    for(int i = 0; i < codecCount.count; i++){

        printf("decoder utilizaton:%d\n", codecUtilization.decoderUtilization[i]);

        printf("encoder utilizaton:%d\n", codecUtilization.encoderUtilization[i]);

    }

// 获取板卡PCIe 设备ID

    cndevPCIeInfo_t deviceId;

    deviceId.version = CNDEV_VERSION_3;

    cndevCheckErrors(cndevGetPCIeInfo(&deviceId, card));

printf("subsystem_id:%x\ndevice_id:%x\nvendor:%x\nsubsyste

m_vendor:%x\n", deviceId.subsystem_id,

deviceId.device_id, deviceId.vendor,

deviceId.subsystem_vendor);

     

//获取正在使用板卡X的进程信息

unsigned tCount = 10;

   cndevProcessInfo_t *procInfo = NULL;

   procInfo = (cndevProcessInfo_t *) malloc(tCount * sizeof(cndevProcessInfo_t));

   procInfo->version = CNDEV_VERSION_3;

   ret = cndevGetProcessInfo(&tCount, procInfo, 0);

 //如果 ret 为 CNDEV_ERROR_INSUFFICIENT_SPACE 需要再次获取信息

   while (ret == CNDEV_ERROR_INSUFFICIENT_SPACE) {

       procInfo = (cndevProcessInfo_t *)

  realloc(procInfo,tCount * sizeof(cndevProcessInfo_t));

       ret = cndevGetProcessInfo(&tCount, procInfo, 0);

   }

   cndevCheckErrors(ret);

 // 获取板卡序列号

    cndevCardSN_t cardSN;

    cardSN.version = CNDEV_VERSION_3;

    cndevCheckErrors(cndevGetCardSN(&cardSN, card));

    printf("The SN of the card:%012lx\n", cardSN.sn);

 //释放libcndev

   cndevRelease();


官方微博 官方微信
版权所有 © 2019 寒武纪 Cambricon 备案/许可证号:京ICP备17003415
关闭