Clover Git
OpenCL 1.1 software implementation

api_event.cpp

Go to the documentation of this file.
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/commandqueue.h>
00036 #include <core/events.h>
00037 #include <core/context.h>
00038 
00039 // Event Object APIs
00040 cl_int
00041 clWaitForEvents(cl_uint             num_events,
00042                 const cl_event *    event_list)
00043 {
00044     if (!num_events || !event_list)
00045         return CL_INVALID_VALUE;
00046 
00047     // Check the events in the list
00048     cl_context global_ctx = 0;
00049 
00050     for (cl_uint i=0; i<num_events; ++i)
00051     {
00052         if (!event_list[i]->isA(Coal::Object::T_Event))
00053             return CL_INVALID_EVENT;
00054 
00055         if (event_list[i]->status() < 0)
00056             return CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST;
00057 
00058         cl_context evt_ctx = (cl_context)event_list[i]->parent()->parent();
00059         cl_command_queue evt_queue = (cl_command_queue)event_list[i]->parent();
00060 
00061         // Flush the queue
00062         evt_queue->flush();
00063 
00064         if (global_ctx == 0)
00065             global_ctx = evt_ctx;
00066         else if (global_ctx != evt_ctx)
00067             return CL_INVALID_CONTEXT;
00068     }
00069 
00070     // Wait for the events
00071     for (cl_uint i=0; i<num_events; ++i)
00072     {
00073         event_list[i]->waitForStatus(Coal::Event::Complete);
00074     }
00075 
00076     return CL_SUCCESS;
00077 }
00078 
00079 cl_int
00080 clGetEventInfo(cl_event         event,
00081                cl_event_info    param_name,
00082                size_t           param_value_size,
00083                void *           param_value,
00084                size_t *         param_value_size_ret)
00085 {
00086     if (!event->isA(Coal::Object::T_Event))
00087         return CL_INVALID_EVENT;
00088 
00089     return event->info(param_name, param_value_size, param_value,
00090                        param_value_size_ret);
00091 }
00092 
00093 cl_int
00094 clSetEventCallback(cl_event     event,
00095                    cl_int       command_exec_callback_type,
00096                    void         (CL_CALLBACK *pfn_event_notify)(cl_event event,
00097                                                                 cl_int exec_status,
00098                                                                 void *user_data),
00099                    void *user_data)
00100 {
00101     if (!event->isA(Coal::Object::T_Event))
00102         return CL_INVALID_EVENT;
00103 
00104     if (!pfn_event_notify || command_exec_callback_type != CL_COMPLETE)
00105         return CL_INVALID_VALUE;
00106 
00107     event->setCallback(command_exec_callback_type, pfn_event_notify, user_data);
00108 
00109     return CL_SUCCESS;
00110 }
00111 
00112 cl_int
00113 clRetainEvent(cl_event event)
00114 {
00115     if (!event->isA(Coal::Object::T_Event))
00116         return CL_INVALID_EVENT;
00117 
00118     event->reference();
00119 
00120     return CL_SUCCESS;
00121 }
00122 
00123 cl_int
00124 clReleaseEvent(cl_event event)
00125 {
00126     if (!event->isA(Coal::Object::T_Event))
00127         return CL_INVALID_EVENT;
00128 
00129     if (event->dereference())
00130     {
00131         event->freeDeviceData();
00132         delete event;
00133     }
00134 
00135     return CL_SUCCESS;
00136 }
00137 
00138 cl_event
00139 clCreateUserEvent(cl_context    context,
00140                   cl_int *      errcode_ret)
00141 {
00142     cl_int dummy_errcode;
00143 
00144     if (!errcode_ret)
00145         errcode_ret = &dummy_errcode;
00146 
00147     if (!context->isA(Coal::Object::T_Context))
00148     {
00149         *errcode_ret = CL_INVALID_CONTEXT;
00150         return 0;
00151     }
00152 
00153     *errcode_ret = CL_SUCCESS;
00154 
00155     Coal::UserEvent *command = new Coal::UserEvent(
00156         (Coal::Context *)context, errcode_ret
00157     );
00158 
00159     if (*errcode_ret != CL_SUCCESS)
00160     {
00161         delete command;
00162         return 0;
00163     }
00164 
00165     return (cl_event)command;
00166 }
00167 
00168 cl_int
00169 clSetUserEventStatus(cl_event   event,
00170                      cl_int     execution_status)
00171 {
00172     Coal::Event *command = (Coal::Event *)event;
00173 
00174     if (!command->isA(Coal::Object::T_Event) ||
00175         command->type() != Coal::Event::User)
00176         return CL_INVALID_EVENT;
00177 
00178     if (execution_status != CL_COMPLETE)
00179         return CL_INVALID_VALUE;
00180 
00181     if (command->status() != CL_SUBMITTED)
00182         return CL_INVALID_OPERATION;
00183 
00184     command->setStatus((Coal::Event::Status)execution_status);
00185 
00186     return CL_SUCCESS;
00187 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines