Fraxinus  2023.01.05-dev+develop.0da12
An IGT application
cxOpenCLPrinter.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxOpenCLPrinter.h"
13 
14 #include <iostream>
15 #include <boost/algorithm/string.hpp>
16 #include <boost/lexical_cast.hpp>
17 #include <boost/format.hpp>
18 
19 namespace cx
20 {
21 
23 {
24  VECTOR_CLASS<cl::Platform> platforms;
25  cl::Platform::get(&platforms);
26 
27  VECTOR_CLASS<cl::Device> devices;
28  for(unsigned int i = 0; i < platforms.size(); i++)
29  {
30  printPlatformInfo(platforms[i]);
31 
32  platforms[i].getDevices(CL_DEVICE_TYPE_ALL, &devices);
33 
34  for(unsigned int j = 0; j < devices.size(); j++)
35  {
36  printDeviceInfo(devices[j]);
37  }
38  }
39  print("Number of platforms", platforms.size());
40  print("Number of devices", devices.size());
41 }
42 
43 void OpenCLPrinter::printPlatformInfo(cl::Platform platform)
44 {
45  print("--- PlatformInfo ---", "");
46  print("Name", platform.getInfo<CL_PLATFORM_NAME>());
47  print("Vendor", platform.getInfo<CL_PLATFORM_VENDOR>());
48  print("Version", platform.getInfo<CL_PLATFORM_VERSION>());
49  print("Profile", platform.getInfo<CL_PLATFORM_PROFILE>());
50  print("Extensions", "");
51  printStringList(platform.getInfo<CL_PLATFORM_EXTENSIONS>());
52 }
53 
54 void OpenCLPrinter::printDeviceInfo(cl::Device device, bool verbose)
55 {
56  print("--- DeviceInfo ---", "");
57  print("Name", device.getInfo<CL_DEVICE_NAME>());
58  print("Vendor", device.getInfo<CL_DEVICE_VENDOR>());
59  print("Vendor id", device.getInfo<CL_DEVICE_VENDOR_ID>());
60  print("Device supports", device.getInfo<CL_DEVICE_VERSION>());
61  print("Graphics card driver", device.getInfo<CL_DRIVER_VERSION>());
62  print("Available", (device.getInfo<CL_DEVICE_AVAILABLE>() ? "Yes" : "No"));
63  print("Extensions", "");
64  printStringList(device.getInfo<CL_DEVICE_EXTENSIONS>());
65  print("\n","");
66 
67  if(!verbose)
68  return;
69 
70  //Verbose output
71  print("Compute Units", device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>());
72  print("Clock Frequency (MHz)", device.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>());
73  print("Global Memory (MB)", (double)device.getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>()/1048576);
74  print("Max Allocateable Memory (MB)", (double)device.getInfo<CL_DEVICE_MAX_MEM_ALLOC_SIZE>()/1048576);
75  print("Local Memory (KB)", device.getInfo<CL_DEVICE_LOCAL_MEM_SIZE>());
76  print("\n","");
77 
78  print("Device Profile", device.getInfo<CL_DEVICE_PROFILE>());
79  print("Error correction support", device.getInfo<CL_DEVICE_ERROR_CORRECTION_SUPPORT>() ? "Yes" : "No");
80  print("Profiling Timer Resolution", device.getInfo<CL_DEVICE_PROFILING_TIMER_RESOLUTION>());
81  print("Endian Little", device.getInfo<CL_DEVICE_ENDIAN_LITTLE>() ? "Yes" : "No");
82  print("Command queue properties", device.getInfo<CL_DEVICE_QUEUE_PROPERTIES>());
83  print("Execution capabilities", device.getInfo<CL_DEVICE_EXECUTION_CAPABILITIES>());
84  print("Host unified memory", device.getInfo<CL_DEVICE_HOST_UNIFIED_MEMORY>() ? "Yes" : "No");
85  print("\n","");
86 
87  print("Max work group size", device.getInfo<CL_DEVICE_MAX_WORK_GROUP_SIZE>());
88  int maxWorkItemDimensions = device.getInfo<CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS>();
89  print("Max work item dimensions", maxWorkItemDimensions);
90  print("\n","");
91 
92  print("Image support", device.getInfo<CL_DEVICE_IMAGE_SUPPORT>() ? "Yes" : "No");
93  print("Image2D max width", device.getInfo<CL_DEVICE_IMAGE2D_MAX_WIDTH>());
94  print("Image2D max height",device.getInfo<CL_DEVICE_IMAGE2D_MAX_HEIGHT>());
95  print("Image3D max width", device.getInfo<CL_DEVICE_IMAGE3D_MAX_WIDTH>());
96  print("Image3D max height", device.getInfo<CL_DEVICE_IMAGE3D_MAX_HEIGHT>());
97  print("Image3D max depth", device.getInfo<CL_DEVICE_IMAGE3D_MAX_DEPTH>());
98 }
99 
100 void OpenCLPrinter::printContextInfo(cl::Context context)
101 {
102  print("--- ContextInfo ---", "");
103  VECTOR_CLASS<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
104  print("Number of devices", devices.size());
105  for(int i=0; i<devices.size(); ++i)
106  printDeviceInfo(devices[i]);
107 }
108 
109 void OpenCLPrinter::printProgramInfo(cl::Program program)
110 {
111  print("--- ProgramInfo ---", "");
112  //printProgramSource(program);
113 }
114 
115 void OpenCLPrinter::printProgramSource(cl::Program program)
116 {
117  print("--- ProgramSource ---", "");
118  cl::STRING_CLASS source = program.getInfo<CL_PROGRAM_SOURCE>();
119  print("", "\n"+source);
120 }
121 
122 void OpenCLPrinter::printKernelInfo(cl::Kernel kernel)
123 {
124  print("--- KernelInfo ---", "");
125  cl::STRING_CLASS functionName = kernel.getInfo<CL_KERNEL_FUNCTION_NAME>();
126  cl::Context context = kernel.getInfo<CL_KERNEL_CONTEXT>();
127  cl::Program program = kernel.getInfo<CL_KERNEL_PROGRAM>();
128  print("Function name", functionName);
129  printContextInfo(context);
130  printProgramInfo(program);
131 
132 }
133 
134 void OpenCLPrinter::printMemoryInfo(cl::Memory memory)
135 {
136  print("--- MemoryInfo ---", "");
137  cl::Context context = memory.getInfo<CL_MEM_CONTEXT>();
138  printContextInfo(context);
139 }
140 
141 void OpenCLPrinter::printStringList(std::string list, std::string separator)
142 {
143  std::vector<std::string> strings;
144  boost::split(strings, list, boost::is_any_of(std::string(separator)));
145  std::vector<std::string>::iterator it;
146  for(it = strings.begin(); it != strings.end(); ++it)
147  print("", (*it));
148 }
149 
150 void OpenCLPrinter::print(std::string name, std::string value, int indents)
151 {
152  std::string stringIndents = getIndentation(indents);
153  std::cout << stringIndents << boost::format("%-30s %-20s\n") % name % value;
154 }
155 
156 void OpenCLPrinter::print(std::string name, int value, int indents)
157 {
158  std::string stringValue = boost::lexical_cast<std::string>(value);
159  print(name, stringValue, indents);
160 }
161 
162 std::string const OpenCLPrinter::getIndentation(unsigned int numberOfIndents)
163 {
164  std::string indentator = "\t";
165  std::string retval("");
166  for(unsigned int i=0; i < numberOfIndents; i++)
167  retval += indentator;
168  return retval;
169 }
170 
171 } //namespace cx
static void printMemoryInfo(cl::Memory memory)
static void printKernelInfo(cl::Kernel kernel)
static void printProgramSource(cl::Program program)
static void printProgramInfo(cl::Program program)
static void printDeviceInfo(cl::Device device, bool verbose=false)
static void printContextInfo(cl::Context context)
static void printPlatformInfo(cl::Platform platform)
static void printPlatformAndDeviceInfo()
Namespace for all CustusX production code.