/* Simple code for identifying some aspects of an Nvidia GPU.
 *
 * Needs CUDA installed. Compile with
 *
 * gcc -o ident ident.c -lcudart -lcuda  -lnvidia-ml
 *
 * or
 *
 * gcc -o ident ident.c -lcudart_static -lcuda -lnvidia-ml
 *
 * for better run-time portability
 *
 * or similar. MJR 11/25
 */

#include <stdio.h>
#include <string.h>

#include <cuda_runtime.h>

#include <cuda.h>
#include <nvml.h>

// Nvidia appears not to provide a function for this
const char *cc2name(int cc){
  int major;
  major=cc>>4;

  if (major==3) return "Kepler";
  if (major==5) return "Maxwell";
  if (major==6) return "Pascal";
  if (cc==0x75) return "Turing";
  if (cc==0x89) return "Ada";
  if (major==8) return "Ampere";
  if (major==9) return "Hopper";
  if ((major==10)||(major==12)) return "Blackwell";
  return "Unknown";
}

// Nvidia appears not to provide a function for this
int cc2cores(int cc){
  int major;
  major=cc>>4;

  if (major==3) return 192;
  if (major==5) return 128;
  if (cc==60) return 64;
  if (major==6) return 128;
  if ((major==7)||(cc==0x80)) return 64;
  if ((major==8)||(major==9)) return 128;
  if ((major==10)||(major==12)) return 128;
  return 0;
}


int main(int argc, char **argv) {
  int i,sms,cores_per_sm,mem_bus,ndevs,cc,verbose;
  int gpu_clk,mem_clk,pci_bus_id,pci_id;
  unsigned int pci_width,pci_gen,pci_bw,watts,boost,mem_boost,gpu_clk_cur;
  cudaError_t error_id;
  struct cudaDeviceProp props;
  nvmlDevice_t nvml_handle;
  nvmlMemory_t mem_info;
  nvmlReturn_t nvml_stat;
  char devname[NVML_DEVICE_NAME_V2_BUFFER_SIZE],*cptr;
  const char *name;

  verbose=0;
  if ((argc==2)&&(!strcmp(argv[1],"-v"))) verbose=1;
  
  error_id = cudaGetDeviceCount(&ndevs);

  if (error_id != cudaSuccess){
    printf("CUDA error: %s\n",cudaGetErrorString(error_id));
    return 1;
  }
  if (ndevs==0) return 0;

  for(i=0;i<ndevs;i++){
    cudaSetDevice(i);
    cudaGetDeviceProperties(&props, i);

    //    mem=props.totalGlobalMem;
    sms=props.multiProcessorCount;
    cc=16*props.major+props.minor;
    cores_per_sm=cc2cores(cc);

    cuDeviceGetAttribute(&gpu_clk,CU_DEVICE_ATTRIBUTE_CLOCK_RATE,i);
    cuDeviceGetAttribute(&mem_clk,CU_DEVICE_ATTRIBUTE_MEMORY_CLOCK_RATE,i);
    cuDeviceGetAttribute(&mem_bus,CU_DEVICE_ATTRIBUTE_GLOBAL_MEMORY_BUS_WIDTH,i);

    cuDeviceGetAttribute(&pci_bus_id,CU_DEVICE_ATTRIBUTE_PCI_BUS_ID,i);
    cuDeviceGetAttribute(&pci_id,CU_DEVICE_ATTRIBUTE_PCI_DEVICE_ID,i);

    pci_gen=pci_width=pci_bw=0;
    devname[0]=0;
    nvml_stat=nvmlInit_v2();
    if (nvml_stat!=NVML_SUCCESS){
      printf("NVML error: %s\n",nvmlErrorString(nvml_stat));
      return 1;
    }
    nvmlDeviceGetHandleByIndex_v2(i,&nvml_handle);
    nvmlDeviceGetClockInfo(nvml_handle,NVML_CLOCK_SM,&gpu_clk_cur);
    nvmlDeviceGetMaxPcieLinkWidth(nvml_handle,&pci_width);
    nvmlDeviceGetMaxPcieLinkGeneration(nvml_handle,&pci_gen);
    nvmlDeviceGetPcieSpeed(nvml_handle,&pci_bw);
    nvmlDeviceGetMemoryInfo(nvml_handle,&mem_info);
    nvmlDeviceGetName(nvml_handle,devname,NVML_DEVICE_NAME_V2_BUFFER_SIZE);
    nvmlDeviceGetPowerManagementDefaultLimit(nvml_handle,&watts);
    nvmlDeviceGetMaxClockInfo(nvml_handle,NVML_CLOCK_SM,&boost);
    nvmlDeviceGetMaxClockInfo(nvml_handle,NVML_CLOCK_MEM,&mem_boost);
    nvmlShutdown();

    name="GPU";
    cptr=strstr(devname,"TX");
    if ((cptr)&&(cptr>devname))
      name=cptr-1;

    printf("%s: %.0f GB (%.0fGB/s) "
	   "%d cores@%.2fGHz cc=%d%x (%s) "
	   "PCI %.0f GB/s\n",name,
	   (mem_info.total>>20)/1024.0,mem_bus*(mem_clk/4e6),
	   sms*cores_per_sm,gpu_clk/1e6,props.major,props.minor,cc2name(cc),
	   pci_bw/1024.0);
    if (verbose){
      printf("Mem: %d bits %.1fGHz",mem_bus,mem_clk/1e6);
      if (mem_boost>mem_clk*1000)
	printf(" (max boost %.1GHz)",mem_boost/1000.0);
      printf(" %s ECC\n",(props.ECCEnabled==1)?"with":"no");
      printf("L2 cache: ");
      printf("%gMB\n",((double)props.l2CacheSize)/(1<<20));
      printf("GPU: %d SMs, guessed %d cores per SM, "
	     "max boost %.1fGHz, current %.2fGHz\n",
	     sms,cores_per_sm,boost/1000.0,gpu_clk_cur/1000.0);
      printf("Max shared memory per SM: %dKB\n",
             (int)(props.sharedMemPerMultiprocessor/1024));
      printf("PCIe v%dx%d (%.1f GB/s)\n",pci_gen,pci_width,pci_bw/1024.0);
      printf("Max power: %dW\n",(int)(watts/1000));
    }

  }
}
