Skip to content

Commit

Permalink
Prints GPU info.
Browse files Browse the repository at this point in the history
  • Loading branch information
matinraayai committed Apr 24, 2022
1 parent 0c7b6a7 commit e919716
Showing 1 changed file with 74 additions and 23 deletions.
97 changes: 74 additions & 23 deletions src/pymcx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,41 @@ void pyMcxInterface(const py::dict& userCfg) {
if (itemKey == "nthread") {
mcxConfig.nthread = py::reinterpret_borrow<py::int_>(item.second);
}
if (itemKey == "seed") {
mcxConfig.seed = py::reinterpret_borrow<py::int_>(item.second);
}
if (itemKey == "srcpos") {
auto srcPosArray = py::reinterpret_borrow<py::list>(item.second);
mcxConfig.srcpos = {srcPosArray[0].cast<float>(),
srcPosArray[1].cast<float>(),
srcPosArray[2].cast<float>(),
srcPosArray[3].cast<float>()
};
}
if (itemKey == "srcdir") {
auto srcPosDir = py::reinterpret_borrow<py::list>(item.second);
mcxConfig.srcdir = {srcPosDir[0].cast<float>(),
srcPosDir[1].cast<float>(),
srcPosDir[2].cast<float>(),
srcPosDir[3].cast<float>()
};
}
if (itemKey == "vol") {
auto cStyleVolume = py::array_t<unsigned int, py::array::c_style | py::array::forcecast>::ensure(item.second);
if (!cStyleVolume) {
std::cout << "Failed to cast to c!" << std::endl;
auto fStyleVolume = py::array_t<unsigned int, py::array::f_style | py::array::forcecast>::ensure(item.second);
auto bufferInfo = fStyleVolume.request();
mcxConfig.vol = static_cast<unsigned int*>(bufferInfo.ptr);
mcxConfig.dim = {static_cast<unsigned int>(bufferInfo.shape.at(0)),
static_cast<unsigned int>(bufferInfo.shape.at(1)),
static_cast<unsigned int>(bufferInfo.shape.at(2))};
}
else {
std::cout << "Works like a charm converting to f!" << std::endl;
auto fStyleVolume = py::array_t<unsigned int, py::array::f_style | py::array::forcecast>(cStyleVolume);
mcxConfig.vol = static_cast<unsigned int*>(fStyleVolume.request().ptr);
auto bufferInfo = fStyleVolume.request();
mcxConfig.dim = {static_cast<unsigned int>(bufferInfo.shape.at(0)),
static_cast<unsigned int>(bufferInfo.shape.at(1)),
static_cast<unsigned int>(bufferInfo.shape.at(2))};
}
for (int i = 0; i < mcxConfig.dim.x * mcxConfig.dim.y * mcxConfig.dim.z; i++) {
std::cout << mcxConfig.vol[i] << ", " << std::endl;
}
auto fStyleVolume = py::array_t<unsigned int, py::array::f_style | py::array::forcecast>::ensure(item.second);
auto bufferInfo = fStyleVolume.request();
mcxConfig.vol = static_cast<unsigned int*>(bufferInfo.ptr);
mcxConfig.dim = {static_cast<unsigned int>(bufferInfo.shape.at(0)),
static_cast<unsigned int>(bufferInfo.shape.at(1)),
static_cast<unsigned int>(bufferInfo.shape.at(2))
};
}

}
/** The next step, we identify gpu number and query all GPU info */
if(!(activeDev = mcx_list_gpu(&mcxConfig, &gpuInfo))) {
mcx_error(-1,"No GPU device found\n",__FILE__,__LINE__);
}
std::cout << gpuInfo->name << std::endl;

//#ifdef _OPENMP
// /**
Expand All @@ -83,10 +89,55 @@ void pyMcxInterface(const py::dict& userCfg) {
// return a pointer to the MCX output, wrapped in a std::vector
}


void printMCXUsage() {
std::cout << "PyMCX v2021.2\nUsage:\n flux, detphoton, vol, seeds = pymcx.mcx(cfg);\n\nRun 'help(pymcx.mcx)' for more details.\n";
}


py::list getGPUInfo() {
Config mcxConfig; /** mcxconfig: structure to store all simulation parameters */
GPUInfo *gpuInfo = nullptr; /** gpuinfo: structure to store GPU information */
mcx_initcfg(&mcxConfig);
mcxConfig.isgpuinfo = 3;
py::list output;
if(!(mcx_list_gpu(&mcxConfig,&gpuInfo))){
std::cerr << "No CUDA-capable device was found." << std::endl;
return output;
}

for (int i = 0; i < gpuInfo[0].devcount; i++) {
py::dict currentDeviceInfo;
currentDeviceInfo["name"] = gpuInfo[i].name;
currentDeviceInfo["id"] = gpuInfo[i].id;
currentDeviceInfo["devcount"] = gpuInfo[i].devcount;
currentDeviceInfo["major"] = gpuInfo[i].major;
currentDeviceInfo["minor"] = gpuInfo[i].minor;
currentDeviceInfo["globalmem"] = gpuInfo[i].globalmem;
currentDeviceInfo["constmem"] = gpuInfo[i].constmem;
currentDeviceInfo["sharedmem"] = gpuInfo[i].sharedmem;
currentDeviceInfo["regcount"] = gpuInfo[i].regcount;
currentDeviceInfo["clock"] = gpuInfo[i].clock;
currentDeviceInfo["sm"] = gpuInfo[i].sm;
currentDeviceInfo["core"] = gpuInfo[i].core;
currentDeviceInfo["autoblock"] = gpuInfo[i].autoblock;
currentDeviceInfo["autothread"] = gpuInfo[i].autothread;
currentDeviceInfo["maxgate"] = gpuInfo[i].maxgate;
output.append(currentDeviceInfo);
}
mcx_cleargpuinfo(&gpuInfo);
mcx_clearcfg(&mcxConfig);
return output;
}



PYBIND11_MODULE(pymcx, m) {
m.doc() = "Monte Carlo eXtreme Python Interface www.mcx.space"; // optional module docstring
m.doc() = "PyMCX: Monte Carlo eXtreme Python Interface, www.mcx.space.";

m.def("mcx", &pyMcxInterface, "Runs MCX");
m.def("mcx", &printMCXUsage, "");
m.def("gpu_info", &getGPUInfo, "Prints out the list of CUDA-capable devices attached to this system.");
}


Expand Down

0 comments on commit e919716

Please sign in to comment.