14 #if ( SETTING_OPENCL != 0 )
39 State::State() : m_deviceID( NULL ) {
43 std::cout <<
"Searching for OpenCL device ...." << std::endl;
44 { cl_uint platformIDsLength = 0;
45 clGetPlatformIDs( 0, NULL, &platformIDsLength );
46 if ( platformIDsLength > 0 ) {
48 std::unique_ptr< cl_platform_id[] > platformIDs(
new cl_platform_id[ platformIDsLength ] );
49 clGetPlatformIDs( platformIDsLength, platformIDs.get(), NULL );
50 for (
unsigned int ii = 0; ii < platformIDsLength; ++ii ) {
52 cl_uint deviceIDsLength = 0;
53 clGetDeviceIDs( platformIDs[ ii ], CL_DEVICE_TYPE_GPU, 0, NULL, &deviceIDsLength );
54 if ( deviceIDsLength > 0 ) {
56 std::unique_ptr< cl_device_id[] > deviceIDs(
new cl_device_id[ deviceIDsLength ] );
57 clGetDeviceIDs( platformIDs[ ii ], CL_DEVICE_TYPE_GPU, deviceIDsLength, deviceIDs.get(), NULL );
58 for (
unsigned int jj = 0; jj < deviceIDsLength; ++jj ) {
61 CHECK_OPENCL( clGetDeviceInfo( deviceIDs[ jj ], CL_DEVICE_NAME, 0, NULL, &nameSize ) );
62 std::unique_ptr< cl_char[] > name(
new cl_char[ nameSize ] );
63 CHECK_OPENCL( clGetDeviceInfo( deviceIDs[ jj ], CL_DEVICE_NAME, nameSize, name.get(), NULL ) );
65 cl_bool available = CL_TRUE;
66 CHECK_OPENCL( clGetDeviceInfo( deviceIDs[ jj ], CL_DEVICE_AVAILABLE,
sizeof( available ), &available, NULL ) );
69 std::cout <<
"\tCannot use \"" << name.get() <<
"\" because it is unavailable." << std::endl;
73 cl_bool compilerAvailable = CL_TRUE;
74 CHECK_OPENCL( clGetDeviceInfo( deviceIDs[ jj ], CL_DEVICE_COMPILER_AVAILABLE,
sizeof( compilerAvailable ), &compilerAvailable, NULL ) );
75 if ( ! compilerAvailable ) {
77 std::cout <<
"\tCannot use \"" << name.get() <<
"\" because no compiler is available." << std::endl;
81 cl_bool littleEndian = CL_FALSE;
82 CHECK_OPENCL( clGetDeviceInfo( deviceIDs[ jj ], CL_DEVICE_ENDIAN_LITTLE,
sizeof( littleEndian ), &littleEndian, NULL ) );
83 if ( ! littleEndian ) {
85 std::cout <<
"\tCannot use \"" << name.get() <<
"\" because it is big-endian." << std::endl;
89 cl_bool imageSupport = CL_TRUE;
90 CHECK_OPENCL( clGetDeviceInfo( deviceIDs[ jj ], CL_DEVICE_IMAGE_SUPPORT,
sizeof( imageSupport ), &imageSupport, NULL ) );
91 if ( ! imageSupport ) {
93 std::cout <<
"\tCannot use \"" << name.get() <<
"\" because it does not support images." << std::endl;
97 if ( m_deviceID != NULL ) {
99 std::cout <<
"\tNot using \"" << name.get() <<
"\" because we have already chosen a device." << std::endl;
103 std::cout <<
"\tUsing \"" << name.get() <<
"\"." << std::endl;
104 m_deviceID = deviceIDs[ jj ];
110 if ( m_deviceID == NULL )
113 cl_int result = CL_SUCCESS;
115 m_context.reset( clCreateContext( NULL, 1, &m_deviceID, NULL, NULL, &result ) );
116 if ( result != CL_SUCCESS )
119 m_queue.reset( clCreateCommandQueue( m_context.get(), m_deviceID, 0, &result ) );
120 if ( result != CL_SUCCESS )
123 std::cout <<
"3D image formats supported by OpenCL device:" << std::endl;
124 { cl_uint imageFormatsLength = 0;
125 clGetSupportedImageFormats( m_context.get(), CL_MEM_READ_ONLY, CL_MEM_OBJECT_IMAGE3D, 0, NULL, &imageFormatsLength );
126 if ( imageFormatsLength > 0 ) {
128 std::unique_ptr< cl_image_format[] > imageFormats(
new cl_image_format[ imageFormatsLength ] );
129 clGetSupportedImageFormats( m_context.get(), CL_MEM_READ_ONLY, CL_MEM_OBJECT_IMAGE3D, imageFormatsLength, imageFormats.get(), NULL );
130 for (
unsigned int ii = 0; ii < imageFormatsLength; ++ii ) {
133 switch( imageFormats[ ii ].image_channel_order ) {
134 case CL_R: { std::cout <<
"CL_R";
break; }
135 case CL_A: { std::cout <<
"CL_A";
break; }
136 case CL_INTENSITY: { std::cout <<
"CL_INTENSITY";
break; }
137 case CL_LUMINANCE: { std::cout <<
"CL_LUMINANCE";
break; }
138 case CL_RG: { std::cout <<
"CL_RG";
break; }
139 case CL_RA: { std::cout <<
"CL_RA";
break; }
140 case CL_RGB: { std::cout <<
"CL_RGB";
break; }
141 case CL_RGBA: { std::cout <<
"CL_RGBA";
break; }
142 case CL_ARGB: { std::cout <<
"CL_ARGB";
break; }
143 case CL_BGRA: { std::cout <<
"CL_BGRA";
break; }
144 default: { std::cout <<
"<unknown channel order>";
break; }
148 switch( imageFormats[ ii ].image_channel_data_type ) {
149 case CL_SNORM_INT8: { std::cout <<
"CL_SNORM_INT8";
break; }
150 case CL_SNORM_INT16: { std::cout <<
"CL_SNORM_INT16";
break; }
151 case CL_UNORM_INT8: { std::cout <<
"CL_UNORM_INT8";
break; }
152 case CL_UNORM_INT16: { std::cout <<
"CL_UNORM_INT16";
break; }
153 case CL_UNORM_SHORT_565: { std::cout <<
"CL_UNORM_SHORT_565";
break; }
154 case CL_UNORM_SHORT_555: { std::cout <<
"CL_UNORM_SHORT_555";
break; }
155 case CL_UNORM_INT_101010: { std::cout <<
"CL_UNORM_INT_101010";
break; }
156 case CL_SIGNED_INT8: { std::cout <<
"CL_SIGNED_INT8";
break; }
157 case CL_SIGNED_INT16: { std::cout <<
"CL_SIGNED_INT16";
break; }
158 case CL_SIGNED_INT32: { std::cout <<
"CL_SIGNED_INT32";
break; }
159 case CL_UNSIGNED_INT8: { std::cout <<
"CL_UNSIGNED_INT8";
break; }
160 case CL_UNSIGNED_INT16: { std::cout <<
"CL_UNSIGNED_INT16";
break; }
161 case CL_UNSIGNED_INT32: { std::cout <<
"CL_UNSIGNED_INT32";
break; }
162 case CL_HALF_FLOAT: { std::cout <<
"CL_HALF_FLOAT";
break; }
163 case CL_FLOAT: { std::cout <<
"CL_FLOAT";
break; }
164 default: { std::cout <<
"<unknown channel data type>";
break; }
166 std::cout << std::endl;
177 clFlush( m_queue.get() );
178 clFinish( m_queue.get() );
190 #endif // SETTING_OPENCL