1*6467f958SSadaf Ebrahimi // 2*6467f958SSadaf Ebrahimi // Copyright (c) 2017 The Khronos Group Inc. 3*6467f958SSadaf Ebrahimi // 4*6467f958SSadaf Ebrahimi // Licensed under the Apache License, Version 2.0 (the "License"); 5*6467f958SSadaf Ebrahimi // you may not use this file except in compliance with the License. 6*6467f958SSadaf Ebrahimi // You may obtain a copy of the License at 7*6467f958SSadaf Ebrahimi // 8*6467f958SSadaf Ebrahimi // http://www.apache.org/licenses/LICENSE-2.0 9*6467f958SSadaf Ebrahimi // 10*6467f958SSadaf Ebrahimi // Unless required by applicable law or agreed to in writing, software 11*6467f958SSadaf Ebrahimi // distributed under the License is distributed on an "AS IS" BASIS, 12*6467f958SSadaf Ebrahimi // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*6467f958SSadaf Ebrahimi // See the License for the specific language governing permissions and 14*6467f958SSadaf Ebrahimi // limitations under the License. 15*6467f958SSadaf Ebrahimi // 16*6467f958SSadaf Ebrahimi #ifndef THREAD_POOL_H 17*6467f958SSadaf Ebrahimi #define THREAD_POOL_H 18*6467f958SSadaf Ebrahimi 19*6467f958SSadaf Ebrahimi #if defined(__APPLE__) 20*6467f958SSadaf Ebrahimi #include <OpenCL/opencl.h> 21*6467f958SSadaf Ebrahimi #else 22*6467f958SSadaf Ebrahimi #include <CL/cl.h> 23*6467f958SSadaf Ebrahimi #endif 24*6467f958SSadaf Ebrahimi 25*6467f958SSadaf Ebrahimi // 26*6467f958SSadaf Ebrahimi // An atomic add operator 27*6467f958SSadaf Ebrahimi cl_int ThreadPool_AtomicAdd(volatile cl_int *a, cl_int b); // returns old value 28*6467f958SSadaf Ebrahimi 29*6467f958SSadaf Ebrahimi // Your function prototype 30*6467f958SSadaf Ebrahimi // 31*6467f958SSadaf Ebrahimi // A function pointer to the function you want to execute in a multithreaded 32*6467f958SSadaf Ebrahimi // context. No synchronization primitives are provided, other than the atomic 33*6467f958SSadaf Ebrahimi // add above. You may not call ThreadPool_Do from your function. 34*6467f958SSadaf Ebrahimi // ThreadPool_AtomicAdd() and GetThreadCount() should work, however. 35*6467f958SSadaf Ebrahimi // 36*6467f958SSadaf Ebrahimi // job ids and thread ids are 0 based. If number of jobs or threads was 8, they 37*6467f958SSadaf Ebrahimi // will numbered be 0 through 7. Note that while every job will be run, it is 38*6467f958SSadaf Ebrahimi // not guaranteed that every thread will wake up before the work is done. 39*6467f958SSadaf Ebrahimi typedef cl_int (*TPFuncPtr)(cl_uint /*job_id*/, cl_uint /* thread_id */, 40*6467f958SSadaf Ebrahimi void *userInfo); 41*6467f958SSadaf Ebrahimi 42*6467f958SSadaf Ebrahimi // returns first non-zero result from func_ptr, or CL_SUCCESS if all are zero. 43*6467f958SSadaf Ebrahimi // Some workitems may not run if a non-zero result is returned from func_ptr(). 44*6467f958SSadaf Ebrahimi // This function may not be called from a TPFuncPtr. 45*6467f958SSadaf Ebrahimi cl_int ThreadPool_Do(TPFuncPtr func_ptr, cl_uint count, void *userInfo); 46*6467f958SSadaf Ebrahimi 47*6467f958SSadaf Ebrahimi // Returns the number of worker threads that underlie the threadpool. The value 48*6467f958SSadaf Ebrahimi // passed as the TPFuncPtrs thread_id will be between 0 and this value less one, 49*6467f958SSadaf Ebrahimi // inclusive. This is safe to call from a TPFuncPtr. 50*6467f958SSadaf Ebrahimi cl_uint GetThreadCount(void); 51*6467f958SSadaf Ebrahimi 52*6467f958SSadaf Ebrahimi // SetThreadCount() may be used to artifically set the number of worker threads 53*6467f958SSadaf Ebrahimi // If the value is 0 (the default) the number of threads will be determined 54*6467f958SSadaf Ebrahimi // based on the number of CPU cores. If it is a unicore machine, then 2 will be 55*6467f958SSadaf Ebrahimi // used, so that we still get some testing for thread safety. 56*6467f958SSadaf Ebrahimi // 57*6467f958SSadaf Ebrahimi // If count < 2 or the CL_TEST_SINGLE_THREADED environment variable is set then 58*6467f958SSadaf Ebrahimi // the code will run single threaded, but will report an error to indicate that 59*6467f958SSadaf Ebrahimi // the test is invalid. This option is intended for debugging purposes only. It 60*6467f958SSadaf Ebrahimi // is suggested as a convention that test apps set the thread count to 1 in 61*6467f958SSadaf Ebrahimi // response to the -m flag. 62*6467f958SSadaf Ebrahimi // 63*6467f958SSadaf Ebrahimi // SetThreadCount() must be called before the first call to GetThreadCount() or 64*6467f958SSadaf Ebrahimi // ThreadPool_Do(), otherwise the behavior is indefined. It may not be called 65*6467f958SSadaf Ebrahimi // from a TPFuncPtr. 66*6467f958SSadaf Ebrahimi void SetThreadCount(int count); 67*6467f958SSadaf Ebrahimi 68*6467f958SSadaf Ebrahimi 69*6467f958SSadaf Ebrahimi #endif /* THREAD_POOL_H */ 70