24 #if !defined( BOLT_CL_CONTROL_H )
25 #define BOLT_CL_CONTROL_H
32 #include <boost/thread/mutex.hpp>
33 #include <boost/shared_ptr.hpp>
103 enum e_UseHostMode {NoUseHost, UseHost};
104 enum e_RunMode {Automatic,
109 enum e_AutoTuneMode{NoAutoTune=0x0,
111 AutoTuneWorkShape=0x2,
114 static const unsigned None=0;
115 static const unsigned Compile = 0x1;
116 static const unsigned ShowCode = 0x2;
117 static const unsigned SaveCompilerTemps = 0x4;
118 static const unsigned DebugKernelRun = 0x8;
119 static const unsigned AutoTune = 0x10;
122 enum e_WaitMode {BalancedWait,
132 const ::cl::CommandQueue& commandQueue =
getDefault().getCommandQueue(),
133 e_UseHostMode useHost=
getDefault().getUseHost(),
136 m_commandQueue(commandQueue),
138 m_forceRunMode(OpenCL),
139 m_defaultRunMode(OpenCL),
142 m_wgPerComputeUnit(
getDefault().m_wgPerComputeUnit),
143 m_compileOptions(
getDefault().m_compileOptions),
144 m_compileForAllDevices(
getDefault().m_compileForAllDevices),
150 control(
const control& ref) :
151 m_commandQueue(ref.m_commandQueue),
152 m_useHost(ref.m_useHost),
153 m_forceRunMode(ref.m_forceRunMode),
154 m_defaultRunMode(ref.m_defaultRunMode),
155 m_debug(ref.m_debug),
156 m_autoTune(ref.m_autoTune),
157 m_wgPerComputeUnit(ref.m_wgPerComputeUnit),
158 m_compileOptions(ref.m_compileOptions),
159 m_compileForAllDevices(ref.m_compileForAllDevices),
160 m_waitMode(ref.m_waitMode),
161 m_unroll(ref.m_unroll)
171 void setCommandQueue(::cl::CommandQueue commandQueue) { m_commandQueue = commandQueue; };
176 void setUseHost(e_UseHostMode useHost) { m_useHost = useHost; };
216 ::cl::CommandQueue& getCommandQueue( ) {
return m_commandQueue; };
217 const ::cl::CommandQueue& getCommandQueue( )
const {
return m_commandQueue; };
218 ::cl::Context getContext()
const {
return m_commandQueue.getInfo<CL_QUEUE_CONTEXT>();};
219 ::cl::Device getDevice()
const {
return m_commandQueue.getInfo<CL_QUEUE_DEVICE>();};
220 e_UseHostMode getUseHost()
const {
return m_useHost; };
221 e_RunMode getForceRunMode()
const {
return m_forceRunMode; };
222 e_RunMode getDefaultPathToRun()
const {
return m_defaultRunMode; };
223 unsigned getDebugMode()
const {
return m_debug;};
224 int const getWGPerComputeUnit()
const {
return m_wgPerComputeUnit; };
225 const ::std::string getCompileOptions()
const {
return m_compileOptions; };
226 e_WaitMode getWaitMode()
const {
return m_waitMode; };
227 int getUnroll()
const {
return m_unroll; };
228 bool getCompileForAllDevices()
const {
return m_compileForAllDevices; };
248 static control _defaultControl(
true );
249 return _defaultControl;
252 static void printPlatforms(
bool printDevices =
true, cl_device_type deviceType = CL_DEVICE_TYPE_ALL );
253 static void printPlatformsRange( std::vector< ::cl::Platform >::iterator begin, std::vector< ::cl::Platform >::iterator end,
254 bool printDevices =
true, cl_device_type deviceType = CL_DEVICE_TYPE_ALL );
281 m_debug(
debug::None),
282 m_autoTune(AutoTuneAll),
283 m_wgPerComputeUnit(8),
284 m_compileForAllDevices(true),
285 m_waitMode(BusyWait),
288 ::cl_device_type dType = CL_DEVICE_TYPE_CPU;
289 if(m_commandQueue() != NULL)
291 ::cl::Device device = m_commandQueue.getInfo<CL_QUEUE_DEVICE>();
292 dType = device.getInfo<CL_DEVICE_TYPE>();
294 if(dType == CL_DEVICE_TYPE_CPU || m_commandQueue() == NULL)
299 m_forceRunMode = MultiCoreCpu;
300 m_defaultRunMode = MultiCoreCpu;
302 m_forceRunMode = SerialCpu;
303 m_defaultRunMode = SerialCpu;
309 m_forceRunMode = OpenCL;
310 m_defaultRunMode = OpenCL;
314 ::cl::CommandQueue m_commandQueue;
315 e_UseHostMode m_useHost;
316 e_RunMode m_forceRunMode;
317 e_RunMode m_defaultRunMode;
318 e_AutoTuneMode m_autoTune;
320 int m_wgPerComputeUnit;
321 ::std::string m_compileOptions;
322 bool m_compileForAllDevices;
323 e_WaitMode m_waitMode;
328 ::cl::Context buffContext;
329 cl_mem_flags memFlags;
330 const void* host_ptr;
333 struct descBufferValue
337 ::cl::Buffer buffBuff;
340 struct descBufferComp
342 bool operator( )(
const descBufferKey& lhs,
const descBufferKey& rhs )
const
344 if( lhs.memFlags < rhs.memFlags )
348 else if( lhs.memFlags == rhs.memFlags )
350 if( lhs.buffContext( ) < rhs.buffContext( ) )
354 else if( lhs.buffContext( ) == rhs.buffContext( ) )
356 if( lhs.host_ptr < rhs.host_ptr )
377 typedef std::multimap< descBufferKey, descBufferValue, descBufferComp > mapBufferType;
389 mapBufferType::iterator m_iter;
394 UnlockBuffer( control& p_control, mapBufferType::iterator it ): m_iter( it ), m_control( p_control )
397 void operator( )(
const void* pBuff )
401 boost::lock_guard< boost::mutex > lock( m_control.mapGuard );
402 m_iter->second.inUse =
false;
406 friend class UnlockBuffer;
407 mapBufferType mapBuffer;
408 boost::mutex mapGuard;