1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/*=========================================================================
*
* Copyright UMC Utrecht and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkOpenCLWorkSize_h
#define itkOpenCLWorkSize_h
#include "itkOpenCL.h"
#include "itkSize.h"
#include <string>
namespace itk
{
/** \class OpenCLSize
* \brief The OpenCLSize class defines the size of an item of work for an OpenCL kernel.
*
* OpenCL sizes may be single-dimensional, two-dimensional, or three-dimensional:
*
* \code
* OpenCLSize one(128);
* OpenCLSize two(16, 16);
* OpenCLSize three(32, 32, 16);
* OpenCLKernel kernel = program.CreateKernel( "test" );
* kernel.SetGlobalWorkSize(one);
* kernel.SetGlobalWorkSize(two);
* kernel.SetGlobalWorkSize(three);
* \endcode
*
* \ingroup OpenCL
* \sa OpenCLKernel
*/
// Forward declaration
class OpenCLDevice;
class ITKOpenCL_EXPORT OpenCLSize
{
public:
/** Standard class typedefs. */
using Self = OpenCLSize;
using SizeType1D = Size<1>;
using SizeType2D = Size<2>;
using SizeType3D = Size<3>;
/** Null compatibility */
struct Null
{};
static const Null null;
OpenCLSize(const Null &)
: m_Dim(0)
{
this->m_Sizes[0] = 0;
this->m_Sizes[1] = 0;
this->m_Sizes[2] = 0;
}
OpenCLSize &
operator=(const Null &)
{
*this = OpenCLSize(null);
return *this;
}
bool
IsNull() const
{
return m_Dim == 0;
}
/** Constructs a default size consisting of a single dimension
* with width set to 1. */
OpenCLSize()
: m_Dim(1)
{
this->m_Sizes[0] = 1;
this->m_Sizes[1] = 1;
this->m_Sizes[2] = 1;
}
/** Constructs a single-dimensional size with width set to size.
* The height and depth will be set to 1. */
OpenCLSize(const std::size_t size)
: m_Dim(1)
{
this->m_Sizes[0] = size;
this->m_Sizes[1] = 1;
this->m_Sizes[2] = 1;
}
/** Constructs a two-dimensional size of width, height.
* The depth will be set to 1. */
OpenCLSize(const std::size_t width, const std::size_t height)
: m_Dim(2)
{
this->m_Sizes[0] = width;
this->m_Sizes[1] = height;
this->m_Sizes[2] = 1;
}
/** Constructs a three-dimensional size of width, height and depth. */
OpenCLSize(const std::size_t width, const std::size_t height, const std::size_t depth)
: m_Dim(3)
{
this->m_Sizes[0] = width;
this->m_Sizes[1] = height;
this->m_Sizes[2] = depth;
}
/** Constructs a single-dimensional size from one dimention itk::Size.
* The height and depth will be set to 1.
* \sa itk::Size */
OpenCLSize(const SizeType1D & size)
: m_Dim(1)
{
this->m_Sizes[0] = size[0];
this->m_Sizes[1] = 1;
this->m_Sizes[2] = 1;
}
/** Constructs a two-dimensional size from two-dimention itk::Size.
* The depth will be set to 1.
* \sa itk::Size */
OpenCLSize(const SizeType2D & size)
: m_Dim(2)
{
this->m_Sizes[0] = size[0];
this->m_Sizes[1] = size[1];
this->m_Sizes[2] = 1;
}
/** Constructs a three-dimensional size from three-dimention itk::Size.
* \sa itk::Size */
OpenCLSize(const SizeType3D & size)
: m_Dim(3)
{
this->m_Sizes[0] = size[0];
this->m_Sizes[1] = size[1];
this->m_Sizes[2] = size[2];
}
/** Returns the dimension for this size, 1, 2, or 3. */
cl_uint
GetDimension() const
{
return m_Dim;
}
/** Returns the width of this size. */
std::size_t
GetWidth() const
{
return this->m_Sizes[0];
}
/** Returns the height of this size. */
std::size_t
GetHeight() const
{
return this->m_Sizes[1];
}
/** Returns the depth of this size. */
std::size_t
GetDepth() const
{
return this->m_Sizes[2];
}
/** Returns a const pointer to the size array within this object. */
const std::size_t *
GetSizes() const
{
return this->m_Sizes;
}
/** Returns true if the elements are zero, otherwise returns false. */
bool
IsZero() const;
/** Access an element of the size. Elements are numbered
* 0, ..., Dim-1. No bounds checking is performed. */
std::size_t & operator[](const std::size_t dim) { return this->m_Sizes[dim]; }
/** Access an element of the size. Elements are numbered
* 0, ..., Dim-1. This version can only be an rvalue.
* No bounds checking is performed. */
std::size_t operator[](const std::size_t dim) const { return this->m_Sizes[dim]; }
/** Returns the best-fit local work size that evenly divides this work
* size and fits within the maximums defined by \a maxWorkItemSize
* and \a maxItemsPerGroup.
* This function is typically used to convert an arbitrary global
* work size on a OpenCLKernel into a compatible local work size. */
static OpenCLSize
GetLocalWorkSize(const OpenCLSize & maxWorkItemSize, const std::size_t maxItemsPerGroup);
/** Returns the best-fit local work size that evenly divides this
* work size and fits within the maximum work group size of \a device.
* This function is typically used to convert an arbitrary global
* work size on a OpenCLKernel into a compatible local work size. */
static OpenCLSize
GetLocalWorkSize(const OpenCLDevice & device);
/** Returns the result of rounding this work size up to a multiple of size. */
OpenCLSize
RoundTo(const OpenCLSize & size) const;
private:
cl_uint m_Dim;
std::size_t m_Sizes[3];
};
/** Operator ==
* Returns true if lhs and rhs are equal, otherwise returns false. */
bool ITKOpenCL_EXPORT
operator==(const OpenCLSize & lhs, const OpenCLSize & rhs);
/** Operator !=
* Returns true if lhs and rhs are different, otherwise returns false. */
bool ITKOpenCL_EXPORT
operator!=(const OpenCLSize & lhs, const OpenCLSize & rhs);
/** Stream out operator for OpenCLSize */
template <typename charT, typename traits>
inline std::basic_ostream<charT, traits> &
operator<<(std::basic_ostream<charT, traits> & strm, const OpenCLSize & size)
{
const cl_uint dim = size.GetDimension();
if (dim == 0)
{
strm << "OpenCLSize(null)";
}
else if (dim == 1)
{
strm << "OpenCLSize(" << size.GetWidth() << ')';
}
else if (dim == 2)
{
strm << "OpenCLSize(" << size.GetWidth() << ", " << size.GetHeight() << ')';
}
else
{
strm << "OpenCLSize(" << size.GetWidth() << ", " << size.GetHeight() << ", " << size.GetDepth() << ')';
}
return strm;
}
} // end namespace itk
#endif /* itkOpenCLWorkSize_h */
|