Skip to content

Commit

Permalink
Add more logs to clinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Galarius committed Aug 12, 2024
1 parent f1d8f4f commit d591371
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 61 deletions.
129 changes: 70 additions & 59 deletions src/clinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,11 @@ json GetKernels(const std::string& strKernels)

json::object_t GetDeviceJSONInfo(const cl::Device& device)
{
logger()->trace("Getting device info...");
json info;
for (auto& property : deviceProperties)
{
logger()->trace("Getting device property '{}' ...", property.name);
try
{
switch (property.type)
Expand Down Expand Up @@ -354,14 +356,15 @@ json::object_t GetDeviceJSONInfo(const cl::Device& device)
}
catch (const cl::Error& err)
{
logger()->error("Failed to get info for the device, {}", err.what());
logger()->error("Failed to get info for the property '{}', {}", property.name, err.what());
}
}
return info;
}

uint32_t CalculateDeviceID(const cl::Device& device)
{
logger()->trace("Calculating device ID...");
try
{
auto name = device.getInfo<CL_DEVICE_NAME>();
Expand All @@ -381,20 +384,9 @@ uint32_t CalculateDeviceID(const cl::Device& device)
return 0;
}

json GetDevicesJSONInfo(const std::vector<cl::Device>& devices)
{
json jsonDevices;
for (auto& device : devices)
{
auto info = GetDeviceJSONInfo(device);
info["DEVICE_ID"] = CalculateDeviceID(device);
jsonDevices.push_back(info);
}
return jsonDevices;
}

uint32_t CalculatePlatformID(const cl::Platform& platform)
{
logger()->trace("Calculating platform ID...");
try
{
const auto name = platform.getInfo<CL_PLATFORM_NAME>();
Expand All @@ -411,50 +403,13 @@ uint32_t CalculatePlatformID(const cl::Platform& platform)
return 0;
}

json GetPlatformJSONInfo(const cl::Platform& platform)
{
json info;
for (auto& property : platformProperties)
{
try
{
std::string value;
platform.getInfo(property.field, &value);
info[property.name] = value;
}
catch (const cl::Error& err)
{
logger()->error("Failed to get information for the platform, {}", err.what());
}
}

info["PLATFORM_ID"] = CalculatePlatformID(platform);

auto extensions = platform.getInfo<CL_PLATFORM_EXTENSIONS>();
info["CL_PLATFORM_EXTENSIONS"] = GetExtensions(extensions);

try
{
std::vector<cl::Device> devices;
platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);
info["DEVICES"] = GetDevicesJSONInfo(devices);
}
catch (const cl::Error& err)
{
logger()->error("Failed to get platform's devices, {}", err.what());
}

return info;
}

std::vector<cl::Platform> GetPlatforms()
{
logger()->trace("Searching for OpenCL platforms...");
logger()->trace("Getting platforms...");
std::vector<cl::Platform> platforms;
try
{
cl::Platform::get(&platforms);
logger()->trace("Found OpenCL platforms: {}", platforms.size());
}
catch (cl::Error& err)
{
Expand Down Expand Up @@ -484,6 +439,7 @@ std::string GetPlatformDescription(const cl::Platform& platform)

size_t GetDevicePowerIndex(const cl::Device& device)
{
logger()->trace("Getting device power index...");
try
{
const size_t maxComputeUnits = device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>();
Expand Down Expand Up @@ -519,6 +475,61 @@ std::string GetDeviceDescription(const cl::Device& device)
return "unknown";
}

json GetDevicesJSONInfo(const std::vector<cl::Device>& devices)
{
logger()->trace("Getting devices info...");
if( devices.size() == 0 ) {
return json::array({});
}

json jsonDevices;
for (auto& device : devices)
{
const auto description = GetDeviceDescription(device);
logger()->debug("Device {}", description);
auto info = GetDeviceJSONInfo(device);
info["DEVICE_ID"] = CalculateDeviceID(device);
jsonDevices.push_back(info);
}
return jsonDevices;
}

json GetPlatformJSONInfo(const cl::Platform& platform)
{
logger()->trace("Getting platform info...");
json info;
info["PLATFORM_ID"] = CalculatePlatformID(platform);
for (auto& property : platformProperties)
{
logger()->trace("Getting platform property '{}' ...", property.name);
try
{
std::string value;
platform.getInfo(property.field, &value);
info[property.name] = value;
}
catch (const cl::Error& err)
{
logger()->error("Failed to get information for the platform, {}", err.what());
}
}
auto extensions = platform.getInfo<CL_PLATFORM_EXTENSIONS>();
info["CL_PLATFORM_EXTENSIONS"] = GetExtensions(extensions);
try
{
std::vector<cl::Device> devices;
platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);
logger()->debug("Found devices: {}", devices.size());
info["DEVICES"] = GetDevicesJSONInfo(devices);
}
catch (const cl::Error& err)
{
logger()->error("Failed to get platform's devices, {}", err.what());
}

return info;
}

// --- CLInfo ---

class CLInfo final : public ICLInfo
Expand All @@ -528,13 +539,12 @@ class CLInfo final : public ICLInfo
{
std::vector<nlohmann::json> jsonPlatforms;
const auto platforms = GetPlatforms();
if (platforms.size() > 0)
logger()->debug("Found platforms: {}", platforms.size());
for (const auto& platform : platforms)
{
for (const auto& platform : platforms)
{
logger()->trace("{}", GetPlatformDescription(platform));
jsonPlatforms.emplace_back(GetPlatformJSONInfo(platform));
}
const auto description = GetPlatformDescription(platform);
logger()->debug("Platform {}", description);
jsonPlatforms.emplace_back(GetPlatformJSONInfo(platform));
}
return nlohmann::json {{"PLATFORMS", jsonPlatforms}};
}
Expand All @@ -546,7 +556,8 @@ class CLInfo final : public ICLInfo
const auto platforms = GetPlatforms();
for (const auto& platform : platforms)
{
logger()->trace("Platform {}", GetPlatformDescription(platform));
const auto description = GetPlatformDescription(platform);
logger()->trace("Platform {}", description);
logger()->trace("Searching for platform's devices...");

try
Expand All @@ -557,7 +568,7 @@ class CLInfo final : public ICLInfo
if (logger()->level() <= spdlog::level::trace)
{
std::stringstream traceLog;
traceLog << "Found OpenCL devices: " << platformDevices.size() << "\n";
traceLog << "Found devices: " << platformDevices.size() << "\n";
for (const auto& device : platformDevices)
{
traceLog << "Device " << GetDeviceDescription(device) << "\n";
Expand Down
11 changes: 9 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ using namespace ocls;

namespace {

auto logger()
{
return spdlog::get(ocls::LogName::main);
}

struct SubCommand
{
SubCommand(CLI::App& app, std::string name, std::string description) : cmd {app.add_subcommand(name, description)}
Expand All @@ -54,8 +59,10 @@ struct CLInfoSubCommand final : public SubCommand
int Execute(const std::shared_ptr<ICLInfo>& clinfo)
{
const auto jsonBody = clinfo->json();
const int indentation = prettyPrint ? 4 : -1;
std::cout << jsonBody.dump(indentation) << std::endl;
const auto indentation = prettyPrint ? 4 : -1;
const auto info = jsonBody.dump(indentation);
logger()->trace("Result: {}", info);
std::cout << info << std::endl;
return EXIT_SUCCESS;
}

Expand Down

0 comments on commit d591371

Please sign in to comment.