• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can
5  * be found in the LICENSE file.
6  *
7  */
8 
9 //
10 //
11 //
12 
13 #include "runtime_cl_12.h"
14 #include "config_cl.h"
15 #include "common/cl/assert_cl.h"
16 
17 //
18 // PERM
19 //
20 
21 cl_mem
skc_runtime_device_perm_alloc(struct skc_runtime * const runtime,cl_mem_flags const flags,size_t const size)22 skc_runtime_device_perm_alloc(struct skc_runtime * const runtime,
23                               cl_mem_flags         const flags,
24                               size_t               const size)
25 {
26   cl_int cl_err;
27 
28   cl_mem mem = clCreateBuffer(runtime->cl.context,
29                               flags,
30                               size,
31                               NULL,
32                               &cl_err); cl_ok(cl_err);
33   return mem;
34 }
35 
36 void
skc_runtime_device_perm_free(struct skc_runtime * const runtime,cl_mem const mem)37 skc_runtime_device_perm_free(struct skc_runtime * const runtime,
38                              cl_mem               const mem)
39 {
40   cl(ReleaseMemObject(mem));
41 }
42 
43 //
44 // TEMP
45 //
46 
47 cl_mem
skc_runtime_device_temp_alloc(struct skc_runtime * const runtime,cl_mem_flags const flags,size_t const size,skc_subbuf_id_t * const subbuf_id,size_t * const subbuf_size)48 skc_runtime_device_temp_alloc(struct skc_runtime * const runtime,
49                               cl_mem_flags         const flags,
50                               size_t               const size,
51                               skc_subbuf_id_t    * const subbuf_id,
52                               size_t             * const subbuf_size)
53 {
54   if (size == 0)
55     {
56       *subbuf_id = (skc_subbuf_id_t)-1;
57 
58       if (subbuf_size != NULL)
59         *subbuf_size = 0;
60 
61       return NULL;
62     }
63 
64   cl_buffer_region br;
65 
66   br.origin = skc_suballocator_subbuf_alloc(&runtime->allocator.device.temp.suballocator,
67                                             runtime->scheduler,
68                                             size,subbuf_id,&br.size);
69 
70   if (subbuf_size != NULL)
71     *subbuf_size = br.size;
72 
73   cl_int cl_err;
74 
75   cl_mem mem = clCreateSubBuffer(runtime->allocator.device.temp.extent,
76                                  flags,
77                                  CL_BUFFER_CREATE_TYPE_REGION,
78                                  &br,
79                                  &cl_err); cl_ok(cl_err);
80 
81   return mem;
82 }
83 
84 
85 void
skc_runtime_device_temp_free(struct skc_runtime * const runtime,cl_mem const mem,skc_subbuf_id_t const subbuf_id)86 skc_runtime_device_temp_free(struct skc_runtime * const runtime,
87                              cl_mem               const mem,
88                              skc_subbuf_id_t      const subbuf_id)
89 {
90   if (mem == NULL)
91     return;
92 
93   skc_suballocator_subbuf_free(&runtime->allocator.device.temp.suballocator,subbuf_id);
94 
95   cl(ReleaseMemObject(mem));
96 }
97 
98 //
99 //
100 //
101 
102 void
skc_allocator_device_create(struct skc_runtime * const runtime)103 skc_allocator_device_create(struct skc_runtime * const runtime)
104 {
105   skc_suballocator_create(runtime,
106                           &runtime->allocator.device.temp.suballocator,
107                           "DEVICE",
108                           runtime->config->suballocator.device.subbufs,
109                           runtime->cl.align_bytes,
110                           runtime->config->suballocator.device.size);
111 
112 #ifndef NDEBUG
113 #pragma message("Get rid of CL_MEM_ALLOC_HOST_PTR as soon as the sorter is installed")
114   cl_mem_flags const flags = CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR;
115 #else
116   cl_mem_flags const flags = CL_MEM_READ_WRITE;
117 #endif
118 
119   runtime->allocator.device.temp.extent =
120     skc_runtime_device_perm_alloc(runtime,
121                                   flags,
122                                   runtime->config->suballocator.device.size);
123 }
124 
125 void
skc_allocator_device_dispose(struct skc_runtime * const runtime)126 skc_allocator_device_dispose(struct skc_runtime * const runtime)
127 {
128   skc_suballocator_dispose(runtime,&runtime->allocator.device.temp.suballocator);
129 
130   skc_runtime_device_perm_free(runtime,runtime->allocator.device.temp.extent);
131 }
132 
133 //
134 //
135 //
136