Clover Git
OpenCL 1.1 software implementation
|
00001 /* 00002 * Copyright (c) 2011, Denis Steckelmacher <steckdenis@yahoo.fr> 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * * Redistributions of source code must retain the above copyright 00008 * notice, this list of conditions and the following disclaimer. 00009 * * Redistributions in binary form must reproduce the above copyright 00010 * notice, this list of conditions and the following disclaimer in the 00011 * documentation and/or other materials provided with the distribution. 00012 * * Neither the name of the copyright holder nor the 00013 * names of its contributors may be used to endorse or promote products 00014 * derived from this software without specific prior written permission. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00017 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00018 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY 00020 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00022 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00023 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00024 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00025 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 */ 00027 00033 #include "CL/cl.h" 00034 00035 #include <core/program.h> 00036 #include <core/kernel.h> 00037 00038 // Kernel Object APIs 00039 cl_kernel 00040 clCreateKernel(cl_program program, 00041 const char * kernel_name, 00042 cl_int * errcode_ret) 00043 { 00044 cl_int dummy_errcode; 00045 00046 if (!errcode_ret) 00047 errcode_ret = &dummy_errcode; 00048 00049 if (!kernel_name) 00050 { 00051 *errcode_ret = CL_INVALID_VALUE; 00052 return 0; 00053 } 00054 00055 if (!program->isA(Coal::Object::T_Program)) 00056 { 00057 *errcode_ret = CL_INVALID_PROGRAM; 00058 return 0; 00059 } 00060 00061 if (program->state() != Coal::Program::Built) 00062 { 00063 *errcode_ret = CL_INVALID_PROGRAM_EXECUTABLE; 00064 return 0; 00065 } 00066 00067 Coal::Kernel *kernel = program->createKernel(kernel_name, errcode_ret); 00068 00069 if (*errcode_ret != CL_SUCCESS) 00070 { 00071 delete kernel; 00072 return 0; 00073 } 00074 00075 return (cl_kernel)kernel; 00076 } 00077 00078 cl_int 00079 clCreateKernelsInProgram(cl_program program, 00080 cl_uint num_kernels, 00081 cl_kernel * kernels, 00082 cl_uint * num_kernels_ret) 00083 { 00084 cl_int rs = CL_SUCCESS; 00085 00086 if (!program->isA(Coal::Object::T_Program)) 00087 return CL_INVALID_PROGRAM; 00088 00089 if (program->state() != Coal::Program::Built) 00090 return CL_INVALID_PROGRAM_EXECUTABLE; 00091 00092 std::vector<Coal::Kernel *> ks = program->createKernels(&rs); 00093 00094 if (rs != CL_SUCCESS) 00095 { 00096 while (ks.size()) 00097 { 00098 delete ks.back(); 00099 ks.pop_back(); 00100 } 00101 00102 return rs; 00103 } 00104 00105 // Check that the kernels will fit in the array, if needed 00106 if (num_kernels_ret) 00107 *num_kernels_ret = ks.size(); 00108 00109 if (kernels && num_kernels < ks.size()) 00110 { 00111 while (ks.size()) 00112 { 00113 delete ks.back(); 00114 ks.pop_back(); 00115 } 00116 00117 return CL_INVALID_VALUE; 00118 } 00119 00120 if (!kernels) 00121 { 00122 // We don't need the kernels in fact 00123 while (ks.size()) 00124 { 00125 delete ks.back(); 00126 ks.pop_back(); 00127 } 00128 } 00129 else 00130 { 00131 // Copy the kernels 00132 for (size_t i=0; i<ks.size(); ++i) 00133 { 00134 kernels[i] = (cl_kernel)ks[i]; 00135 } 00136 } 00137 00138 return CL_SUCCESS; 00139 } 00140 00141 cl_int 00142 clRetainKernel(cl_kernel kernel) 00143 { 00144 if (!kernel->isA(Coal::Object::T_Kernel)) 00145 return CL_INVALID_KERNEL; 00146 00147 kernel->reference(); 00148 00149 return CL_SUCCESS; 00150 } 00151 00152 cl_int 00153 clReleaseKernel(cl_kernel kernel) 00154 { 00155 if (!kernel->isA(Coal::Object::T_Kernel)) 00156 return CL_INVALID_KERNEL; 00157 00158 if (kernel->dereference()) 00159 delete kernel; 00160 00161 return CL_SUCCESS; 00162 } 00163 00164 cl_int 00165 clSetKernelArg(cl_kernel kernel, 00166 cl_uint arg_indx, 00167 size_t arg_size, 00168 const void * arg_value) 00169 { 00170 if (!kernel->isA(Coal::Object::T_Kernel)) 00171 return CL_INVALID_KERNEL; 00172 00173 return kernel->setArg(arg_indx, arg_size, arg_value); 00174 } 00175 00176 cl_int 00177 clGetKernelInfo(cl_kernel kernel, 00178 cl_kernel_info param_name, 00179 size_t param_value_size, 00180 void * param_value, 00181 size_t * param_value_size_ret) 00182 { 00183 if (!kernel->isA(Coal::Object::T_Kernel)) 00184 return CL_INVALID_KERNEL; 00185 00186 return kernel->info(param_name, param_value_size, param_value, 00187 param_value_size_ret); 00188 } 00189 00190 cl_int 00191 clGetKernelWorkGroupInfo(cl_kernel kernel, 00192 cl_device_id device, 00193 cl_kernel_work_group_info param_name, 00194 size_t param_value_size, 00195 void * param_value, 00196 size_t * param_value_size_ret) 00197 { 00198 if (!kernel->isA(Coal::Object::T_Kernel)) 00199 return CL_INVALID_KERNEL; 00200 00201 return kernel->workGroupInfo((Coal::DeviceInterface *)device, param_name, 00202 param_value_size, param_value, 00203 param_value_size_ret); 00204 }