1 /* OpenCL runtime library: clEnqueueFillBuffer()
2 
3    Copyright (c) 2015 Michal Babej / Tampere University of Technology
4 
5    Permission is hereby granted, free of charge, to any person obtaining a copy
6    of this software and associated documentation files (the "Software"), to deal
7    in the Software without restriction, including without limitation the rights
8    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9    copies of the Software, and to permit persons to whom the Software is
10    furnished to do so, subject to the following conditions:
11 
12    The above copyright notice and this permission notice shall be included in
13    all copies or substantial portions of the Software.
14 
15    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21    THE SOFTWARE.
22 */
23 
24 #include "pocl_util.h"
25 #include <string.h>
26 
27 extern CL_API_ENTRY cl_int CL_API_CALL
POname(clEnqueueFillBuffer)28 POname(clEnqueueFillBuffer)(cl_command_queue  command_queue,
29                            cl_mem            buffer,
30                            const void *      pattern,
31                            size_t            pattern_size,
32                            size_t            offset,
33                            size_t            size,
34                            cl_uint           num_events_in_wait_list,
35                            const cl_event*   event_wait_list,
36                            cl_event*         event)
37 CL_API_SUFFIX__VERSION_1_2
38 {
39   int errcode = CL_SUCCESS;
40   _cl_command_node *cmd = NULL;
41 
42   POCL_RETURN_ERROR_COND ((!IS_CL_OBJECT_VALID (command_queue)),
43                           CL_INVALID_COMMAND_QUEUE);
44 
45   POCL_RETURN_ERROR_COND ((!IS_CL_OBJECT_VALID (buffer)),
46                           CL_INVALID_MEM_OBJECT);
47 
48   POCL_RETURN_ERROR_ON((buffer->type != CL_MEM_OBJECT_BUFFER), CL_INVALID_MEM_OBJECT,
49                        "buffer is not a CL_MEM_OBJECT_BUFFER\n");
50 
51   POCL_RETURN_ERROR_ON((command_queue->context != buffer->context), CL_INVALID_CONTEXT,
52                        "buffer and command_queue are not from the same context\n");
53 
54   errcode = pocl_check_event_wait_list (command_queue, num_events_in_wait_list,
55                                         event_wait_list);
56   if (errcode != CL_SUCCESS)
57     return errcode;
58 
59   errcode = pocl_buffer_boundcheck(buffer, offset, size);
60   if (errcode != CL_SUCCESS)
61     return errcode;
62 
63   /* CL_INVALID_VALUE if pattern is NULL or if pattern_size is 0
64    * or if pattern_size is not one of {1, 2, 4, 8, 16, 32, 64, 128}. */
65   POCL_RETURN_ERROR_COND((pattern == NULL), CL_INVALID_VALUE);
66   POCL_RETURN_ERROR_COND((pattern_size == 0), CL_INVALID_VALUE);
67   POCL_RETURN_ERROR_COND((pattern_size > 128), CL_INVALID_VALUE);
68 
69   POCL_RETURN_ERROR_ON((__builtin_popcount(pattern_size) > 1), CL_INVALID_VALUE,
70                        "pattern_size(%zu) must be a power-of-two value", pattern_size);
71 
72   /* CL_INVALID_VALUE if offset and size are not a multiple of pattern_size.  */
73   POCL_RETURN_ERROR_ON((offset % pattern_size), CL_INVALID_VALUE,
74                        "offset(%zu) must be a multiple of pattern_size(%zu)\n",
75                        offset, pattern_size);
76   POCL_RETURN_ERROR_ON((size % pattern_size), CL_INVALID_VALUE,
77                        "size(%zu) must be a multiple of pattern_size(%zu)\n",
78                        size, pattern_size);
79 
80   POCL_RETURN_ON_SUB_MISALIGN (buffer, command_queue);
81 
82   POCL_CONVERT_SUBBUFFER_OFFSET (buffer, offset);
83 
84   POCL_RETURN_ERROR_ON((buffer->size > command_queue->device->max_mem_alloc_size),
85                         CL_OUT_OF_RESOURCES,
86                         "buffer is larger than device's MAX_MEM_ALLOC_SIZE\n");
87 
88   char rdonly = 0;
89 
90   errcode = pocl_create_command (&cmd, command_queue, CL_COMMAND_FILL_BUFFER,
91                                  event, num_events_in_wait_list,
92                                  event_wait_list, 1, &buffer, &rdonly);
93   if (errcode != CL_SUCCESS)
94     return errcode;
95 
96   cmd->command.memfill.dst_mem_id
97       = &buffer->device_ptrs[command_queue->device->global_mem_id];
98   cmd->command.memfill.size = size;
99   cmd->command.memfill.offset = offset;
100   void *p = pocl_aligned_malloc(pattern_size, pattern_size);
101   memcpy(p, pattern, pattern_size);
102   cmd->command.memfill.pattern = p;
103   cmd->command.memfill.pattern_size = pattern_size;
104 
105   pocl_command_enqueue(command_queue, cmd);
106 
107   return CL_SUCCESS;
108 
109 }
110 POsym(clEnqueueFillBuffer)
111