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