Ganav Posted January 14, 2015 Report Posted January 14, 2015 Am vazut zilele trecute o intrebare care se referea la cum se poate obtine numarul de core-uri CPU. Fragmentul de mai jos returneaza numarul de nuclee si specificatiile placii grafice(in cazul in care aceasta face parte din familia Nvidia Geforce):system_specs.h:#ifndef SYSTEM_SPECS_H#define SYSTEM_SPECS_H#include "includes.h"class SystemSpecs{public: SystemSpecs(bool showInfo); ~SystemSpecs(void){}; int getNumberOfCPUCores(void); int getProcessorType(void); int getNumberOfGPUs(void); size_t getTotalConstantMemory(int deviceID); size_t getTotalGlobalMemory(int deviceID); size_t getSharedMemPerBlock(int deviceID); int getWarpSize(int deviceID); int getMaxNumberOfBlocks_x(int deviceID); int getMaxNumberOfBlocks_y(int deviceID); int getMaxNumberOfBlocks_z(int deviceID); int getMaxThreadsPerBlock(int deviceID); int getMaxThreads_x(int deviceID); int getMaxThreads_y(int deviceID); int getMaxThreads_z(int deviceID); void printGPUSpecs(void);private:#ifdef WIN32 /// query system info on windows SYSTEM_INFO system_info;#else /// query system info on linux#endif int device_count; int dev; int runtime_version, driver_version; cudaDeviceProp *device_prop; char msg[256]; char cTemp[10]; std::string sprofile_string;};#endifsytem_specs.cpp:#include "system_specs.h"SystemSpecs::SystemSpecs(bool showInfo){#ifdef WIN32 /// query sysinfo on windows GetSystemInfo(&system_info);#else /// query sysinfo on linux#endif if(showInfo) { device_count = 0; dev = 0; driver_version = 0; runtime_version = 0; sprofile_string = "deviceQuery, CUDA Driver = CUDART"; if (cudaGetDeviceCount(&device_count) != cudaSuccess) { cout << "cudaGetDeviceCount FAILED CUDA Driver and Runtime version may be mismatched.\n"<<endl; } if (device_count == 0){ cout<<"There is no device supporting CUDA"<<endl; } if(device_count == 0) { device_prop = new cudaDeviceProp[device_count + 1]; } else { device_prop = new cudaDeviceProp[device_count]; } for (dev = 0; dev < device_count; ++dev) { cudaGetDeviceProperties(&device_prop[dev], dev); if (dev == 0) { // This function call returns 9999 for both major & minor fields, if no CUDA capable devices are present if (device_prop[0].major == 9999 && device_prop[0].minor == 9999) cout<<"There is no device supporting CUDA."<<endl; else if (device_count == 1) cout<<"There is 1 device supporting CUDA."<<endl; else cout<<"There are "<<device_count<<"%d devices supporting CUDA"<<endl; } } }}int SystemSpecs::getNumberOfCPUCores(void){ #ifdef WIN32 /// windows return system_info.dwNumberOfProcessors;#else /// linux return sysconf(_SC_NPROCESSORS_ONLN);#endif}int SystemSpecs::getProcessorType(void){#ifdef WIN32 /// windows return system_info.dwProcessorType;#else /// linux return 0;#endif}void SystemSpecs::printGPUSpecs(void){ cout<<" CUDA Device Query (Runtime API) version (CUDART static linking)\n\n"<<endl; for (dev = 0; dev < device_count; ++dev) { if (dev == 0) { // This function call returns 9999 for both major & minor fields, if no CUDA capable devices are present if (device_prop[0].major == 9999 && device_prop[0].minor == 9999) cout<<"There is no device supporting CUDA."<<endl; else if (device_count == 1) cout<<"There is 1 device supporting CUDA."<<endl; else cout<<"There are "<<device_count<<"%d devices supporting CUDA"<<endl; } cout<<"\n Device "<<dev<<":"<<device_prop->name<<endl;#if CUDART_VERSION >= 2020 // Console log cudaDriverGetVersion(&driver_version); cout<<" CUDA Driver Version: "<<driver_version / 1000<<"."<<driver_version % 100<<endl; cudaRuntimeGetVersion(&runtime_version); cout<<" CUDA Runtime Version: "<<runtime_version / 1000<<"."<<runtime_version % 100<<endl;#endif cout<<" CUDA Capability Major/Minor version number: "<<device_prop[dev].major<<"."<<device_prop[dev].minor<<endl; sprintf(msg, " Total amount of global memory: %llu bytes\n", (unsigned long long) device_prop[dev].totalGlobalMem);#if CUDART_VERSION >= 2000 /*printf(" Multiprocessors x Cores/MP = Cores: %d (MP) x %d (Cores/MP) = %d (Cores)\n", device_prop->multiProcessorCount, ConvertSMVer2Cores(device_prop[dev].major, device_prop[dev].minor), ConvertSMVer2Cores(device_prop[dev].major, device_prop[dev].minor) * device_prop[dev].multiProcessorCount); */#endif cout<<" Total amount of constant memory: "<<device_prop[dev].totalConstMem<<" bytes."<<endl; cout<<" Total amount of shared memory per block: "<<device_prop[dev].sharedMemPerBlock<<" bytes."<<endl; cout<<" Total number of registers available per block: "<<device_prop[dev].regsPerBlock<<endl; cout<<" Warp size: "<<device_prop[dev].warpSize<<endl; cout<<" Maximum number of threads per block: "<<device_prop[dev].maxThreadsPerBlock<<endl; printf(" Maximum sizes of each dimension of a block: %d x %d x %d\n", device_prop[dev].maxThreadsDim[0], device_prop[dev].maxThreadsDim[1], device_prop[dev].maxThreadsDim[2]); printf(" Maximum sizes of each dimension of a grid: %d x %d x %d\n", device_prop[dev].maxGridSize[0], device_prop[dev].maxGridSize[1], device_prop[dev].maxGridSize[2]); cout<<" Maximum memory pitch: "<<device_prop[dev].memPitch<<" bytes"<<endl; cout<<" Texture alignment: "<<device_prop[dev].textureAlignment<<" bytes"<<endl; cout<<" Clock rate: "<<device_prop[dev].clockRate * 1e-6f<<"GHz"<<endl;#if CUDART_VERSION >= 2000 printf(" Concurrent copy and execution: %s\n", device_prop[dev].deviceOverlap ? "Yes" : "No");#endif#if CUDART_VERSION >= 2020 printf(" Run time limit on kernels: %s\n", device_prop[dev].kernelExecTimeoutEnabled ? "Yes" : "No"); printf(" Integrated: %s\n", device_prop[dev].integrated ? "Yes" : "No"); printf(" Support host page-locked memory mapping: %s\n", device_prop[dev].canMapHostMemory ? "Yes" : "No"); printf(" Compute mode: %s\n", device_prop[dev].computeMode == cudaComputeModeDefault ? "Default (multiple host threads can use this device simultaneously)" : device_prop[dev].computeMode == cudaComputeModeExclusive ? "Exclusive (only one host thread at a time can use this device)" : device_prop[dev].computeMode == cudaComputeModeProhibited ? "Prohibited (no host thread can use this device)" : "Unknown");#endif#if CUDART_VERSION >= 3000 printf(" Concurrent kernel execution: %s\n", device_prop[dev].concurrentKernels ? "Yes" : "No");#endif#if CUDART_VERSION >= 3010 printf(" Device has ECC support enabled: %s\n", device_prop[dev].ECCEnabled ? "Yes" : "No");#endif#if CUDART_VERSION >= 3020 printf(" Device is using TCC driver mode: %s\n", device_prop[dev].tccDriver ? "Yes" : "No");#endif } cout<<endl; // driver version sprofile_string += ", CUDA Driver Version = ";#ifdef WIN32 sprintf_s(cTemp, 10, "%d.%d", driver_version / 1000, driver_version % 100); #else sprintf(cTemp, "%d.%d", driver_version / 1000, driver_version % 100); #endif sprofile_string += cTemp; // Runtime version sprofile_string += ", CUDA Runtime Version = ";#ifdef WIN32 sprintf_s(cTemp, 10, "%d.%d", runtime_version / 1000, runtime_version % 100);#else sprintf(cTemp, "%d.%d", runtime_version / 1000, runtime_version % 100);#endif sprofile_string += cTemp; // Device count sprofile_string += ", NumDevs = ";#ifdef WIN32 sprintf_s(cTemp, 10, "%d", device_count);#else sprintf(cTemp, "%d", device_count);#endif sprofile_string += cTemp; // First 2 device names, if any for (dev = 0; dev < ((device_count > 2) ? 2 : device_count); ++dev) { cudaGetDeviceProperties(&device_prop[dev], dev); sprofile_string += ", Device = "; sprofile_string += device_prop[dev].name; } sprofile_string += "\n"; // finish cout<<"\n\nPASSED\n";}size_t SystemSpecs::getTotalConstantMemory(int deviceID) { if(deviceID > dev - 1 || deviceID < 0) { cout<<"Invalid device specified"<<endl; return 0; } else { return SystemSpecs::device_prop[deviceID].totalConstMem; }}size_t SystemSpecs::getSharedMemPerBlock(int deviceID){ if(deviceID > dev - 1 || deviceID < 0) { cout<<"Invalid device specified"<<endl; return 0; } else { return SystemSpecs::device_prop[deviceID].sharedMemPerBlock; }}size_t SystemSpecs::getTotalGlobalMemory(int deviceID){ if(deviceID > dev - 1 || deviceID < 0) { cout<<"Invalid device specified"<<endl; return 0; } else { return SystemSpecs::device_prop[deviceID].totalGlobalMem; }}int SystemSpecs::getWarpSize(int deviceID){ if(deviceID > dev - 1 || deviceID < 0) { cout<<"Invalid device specified"<<endl; return 0; } else { return SystemSpecs::device_prop[deviceID].warpSize; }}int SystemSpecs::getMaxThreadsPerBlock(int deviceID){ if(deviceID > dev - 1 || deviceID < 0) { cout<<"Invalid device specified"<<endl; return 0; } else { return SystemSpecs::device_prop[deviceID].maxThreadsPerBlock; }}int SystemSpecs::getMaxThreads_x(int deviceID){ if(deviceID > dev - 1 || deviceID < 0) { cout<<"Invalid device specified"<<endl; return 0; } else { return SystemSpecs::device_prop[deviceID].maxThreadsDim[0]; }}int SystemSpecs::getMaxThreads_y(int deviceID){ if(deviceID > dev - 1 || deviceID < 0) { cout<<"Invalid device specified"<<endl; return 0; } else { return SystemSpecs::device_prop[deviceID].maxThreadsDim[1]; }}int SystemSpecs::getMaxThreads_z(int deviceID){ if(deviceID > dev - 1 || deviceID < 0) { cout<<"Invalid device specified"<<endl; return 0; } else { return SystemSpecs::device_prop[deviceID].maxThreadsDim[2]; }}int SystemSpecs::getMaxNumberOfBlocks_x(int deviceID){ if(deviceID > dev - 1 || deviceID < 0) { cout<<"Invalid device specified"<<endl; return 0; } else { return SystemSpecs::device_prop[deviceID].maxGridSize[0]; }}int SystemSpecs::getMaxNumberOfBlocks_y(int deviceID){ if(deviceID > dev - 1 || deviceID < 0) { cout<<"Invalid device specified"<<endl; return 0; } else { return SystemSpecs::device_prop[deviceID].maxGridSize[1]; }}int SystemSpecs::getMaxNumberOfBlocks_z(int deviceID){ if(deviceID > dev - 1 || deviceID < 0) { cout<<"Invalid device specified"<<endl; return 0; } else { return SystemSpecs::device_prop[deviceID].maxGridSize[2]; }}int SystemSpecs::getNumberOfGPUs(void){ return SystemSpecs::dev;} 1 Quote