Fraxinus  17.12-rc4
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxOpenCLPrinter.h"
34 
35 #include <iostream>
36 #include <boost/algorithm/string.hpp>
37 #include <boost/lexical_cast.hpp>
38 #include <boost/format.hpp>
39 
40 namespace cx
41 {
42 
44 {
45  VECTOR_CLASS<cl::Platform> platforms;
46  cl::Platform::get(&platforms);
47 
48  VECTOR_CLASS<cl::Device> devices;
49  for(unsigned int i = 0; i < platforms.size(); i++)
50  {
51  printPlatformInfo(platforms[i]);
52 
53  platforms[i].getDevices(CL_DEVICE_TYPE_ALL, &devices);
54 
55  for(unsigned int j = 0; j < devices.size(); j++)
56  {
57  printDeviceInfo(devices[j]);
58  }
59  }
60  print("Number of platforms", platforms.size());
61  print("Number of devices", devices.size());
62 }
63 
64 void OpenCLPrinter::printPlatformInfo(cl::Platform platform)
65 {
66  print("--- PlatformInfo ---", "");
67  print("Name", platform.getInfo<CL_PLATFORM_NAME>());
68  print("Vendor", platform.getInfo<CL_PLATFORM_VENDOR>());
69  print("Version", platform.getInfo<CL_PLATFORM_VERSION>());
70  print("Profile", platform.getInfo<CL_PLATFORM_PROFILE>());
71  print("Extensions", "");
72  printStringList(platform.getInfo<CL_PLATFORM_EXTENSIONS>());
73 }
74 
75 void OpenCLPrinter::printDeviceInfo(cl::Device device, bool verbose)
76 {
77  print("--- DeviceInfo ---", "");
78  print("Name", device.getInfo<CL_DEVICE_NAME>());
79  print("Vendor", device.getInfo<CL_DEVICE_VENDOR>());
80  print("Vendor id", device.getInfo<CL_DEVICE_VENDOR_ID>());
81  print("Device supports", device.getInfo<CL_DEVICE_VERSION>());
82  print("Graphics card driver", device.getInfo<CL_DRIVER_VERSION>());
83  print("Available", (device.getInfo<CL_DEVICE_AVAILABLE>() ? "Yes" : "No"));
84  print("Extensions", "");
85  printStringList(device.getInfo<CL_DEVICE_EXTENSIONS>());
86  print("\n","");
87 
88  if(!verbose)
89  return;
90 
91  //Verbose output
92  print("Compute Units", device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>());
93  print("Clock Frequency (MHz)", device.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>());
94  print("Global Memory (MB)", (double)device.getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>()/1048576);
95  print("Max Allocateable Memory (MB)", (double)device.getInfo<CL_DEVICE_MAX_MEM_ALLOC_SIZE>()/1048576);
96  print("Local Memory (KB)", device.getInfo<CL_DEVICE_LOCAL_MEM_SIZE>());
97  print("\n","");
98 
99  print("Device Profile", device.getInfo<CL_DEVICE_PROFILE>());
100  print("Error correction support", device.getInfo<CL_DEVICE_ERROR_CORRECTION_SUPPORT>() ? "Yes" : "No");
101  print("Profiling Timer Resolution", device.getInfo<CL_DEVICE_PROFILING_TIMER_RESOLUTION>());
102  print("Endian Little", device.getInfo<CL_DEVICE_ENDIAN_LITTLE>() ? "Yes" : "No");
103  print("Command queue properties", device.getInfo<CL_DEVICE_QUEUE_PROPERTIES>());
104  print("Execution capabilities", device.getInfo<CL_DEVICE_EXECUTION_CAPABILITIES>());
105  print("Host unified memory", device.getInfo<CL_DEVICE_HOST_UNIFIED_MEMORY>() ? "Yes" : "No");
106  print("\n","");
107 
108  print("Max work group size", device.getInfo<CL_DEVICE_MAX_WORK_GROUP_SIZE>());
109  int maxWorkItemDimensions = device.getInfo<CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS>();
110  print("Max work item dimensions", maxWorkItemDimensions);
111  print("\n","");
112 
113  print("Image support", device.getInfo<CL_DEVICE_IMAGE_SUPPORT>() ? "Yes" : "No");
114  print("Image2D max width", device.getInfo<CL_DEVICE_IMAGE2D_MAX_WIDTH>());
115  print("Image2D max height",device.getInfo<CL_DEVICE_IMAGE2D_MAX_HEIGHT>());
116  print("Image3D max width", device.getInfo<CL_DEVICE_IMAGE3D_MAX_WIDTH>());
117  print("Image3D max height", device.getInfo<CL_DEVICE_IMAGE3D_MAX_HEIGHT>());
118  print("Image3D max depth", device.getInfo<CL_DEVICE_IMAGE3D_MAX_DEPTH>());
119 }
120 
121 void OpenCLPrinter::printContextInfo(cl::Context context)
122 {
123  print("--- ContextInfo ---", "");
124  VECTOR_CLASS<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
125  print("Number of devices", devices.size());
126  for(int i=0; i<devices.size(); ++i)
127  printDeviceInfo(devices[i]);
128 }
129 
130 void OpenCLPrinter::printProgramInfo(cl::Program program)
131 {
132  print("--- ProgramInfo ---", "");
133  //printProgramSource(program);
134 }
135 
136 void OpenCLPrinter::printProgramSource(cl::Program program)
137 {
138  print("--- ProgramSource ---", "");
139  cl::STRING_CLASS source = program.getInfo<CL_PROGRAM_SOURCE>();
140  print("", "\n"+source);
141 }
142 
143 void OpenCLPrinter::printKernelInfo(cl::Kernel kernel)
144 {
145  print("--- KernelInfo ---", "");
146  cl::STRING_CLASS functionName = kernel.getInfo<CL_KERNEL_FUNCTION_NAME>();
147  cl::Context context = kernel.getInfo<CL_KERNEL_CONTEXT>();
148  cl::Program program = kernel.getInfo<CL_KERNEL_PROGRAM>();
149  print("Function name", functionName);
150  printContextInfo(context);
151  printProgramInfo(program);
152 
153 }
154 
155 void OpenCLPrinter::printMemoryInfo(cl::Memory memory)
156 {
157  print("--- MemoryInfo ---", "");
158  cl::Context context = memory.getInfo<CL_MEM_CONTEXT>();
159  printContextInfo(context);
160 }
161 
162 void OpenCLPrinter::printStringList(std::string list, std::string separator)
163 {
164  std::vector<std::string> strings;
165  boost::split(strings, list, boost::is_any_of(std::string(separator)));
166  std::vector<std::string>::iterator it;
167  for(it = strings.begin(); it != strings.end(); ++it)
168  print("", (*it));
169 }
170 
171 void OpenCLPrinter::print(std::string name, std::string value, int indents)
172 {
173  std::string stringIndents = getIndentation(indents);
174  std::cout << stringIndents << boost::format("%-30s %-20s\n") % name % value;
175 }
176 
177 void OpenCLPrinter::print(std::string name, int value, int indents)
178 {
179  std::string stringValue = boost::lexical_cast<std::string>(value);
180  print(name, stringValue, indents);
181 }
182 
183 std::string const OpenCLPrinter::getIndentation(unsigned int numberOfIndents)
184 {
185  std::string indentator = "\t";
186  std::string retval("");
187  for(unsigned int i=0; i < numberOfIndents; i++)
188  retval += indentator;
189  return retval;
190 }
191 
192 } //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.