aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/compute/skc/platforms/cl_12/allocator_device_cl.c
blob: aa44f36e872c3aba2dfbbe7f8e3cd43c2a17fc3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright 2018 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can
 * be found in the LICENSE file.
 *
 */

//
//
//

#include "runtime_cl_12.h"
#include "config_cl.h"
#include "common/cl/assert_cl.h"

//
// PERM
//

cl_mem
skc_runtime_device_perm_alloc(struct skc_runtime * const runtime,
                              cl_mem_flags         const flags,
                              size_t               const size)
{
  cl_int cl_err;

  cl_mem mem = clCreateBuffer(runtime->cl.context,
                              flags,
                              size,
                              NULL,
                              &cl_err); cl_ok(cl_err);
  return mem;
}

void
skc_runtime_device_perm_free(struct skc_runtime * const runtime,
                             cl_mem               const mem)
{
  cl(ReleaseMemObject(mem));
}

//
// TEMP
//

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)
{
  if (size == 0)
    {
      *subbuf_id = (skc_subbuf_id_t)-1;

      if (subbuf_size != NULL)
        *subbuf_size = 0;
      
      return NULL;
    }

  cl_buffer_region br;

  br.origin = skc_suballocator_subbuf_alloc(&runtime->allocator.device.temp.suballocator,
                                            runtime->scheduler,
                                            size,subbuf_id,&br.size);

  if (subbuf_size != NULL)
    *subbuf_size = br.size;

  cl_int cl_err;

  cl_mem mem = clCreateSubBuffer(runtime->allocator.device.temp.extent,
                                 flags,
                                 CL_BUFFER_CREATE_TYPE_REGION,
                                 &br,
                                 &cl_err); cl_ok(cl_err);

  return mem;
}


void
skc_runtime_device_temp_free(struct skc_runtime * const runtime, 
                             cl_mem               const mem,
                             skc_subbuf_id_t      const subbuf_id)
{
  if (mem == NULL)
    return;

  skc_suballocator_subbuf_free(&runtime->allocator.device.temp.suballocator,subbuf_id);

  cl(ReleaseMemObject(mem));  
}

//
//
//

void
skc_allocator_device_create(struct skc_runtime * const runtime)
{
  skc_suballocator_create(runtime,
                          &runtime->allocator.device.temp.suballocator,
                          "DEVICE",
                          runtime->config->suballocator.device.subbufs,
                          runtime->cl.base_align,
                          runtime->config->suballocator.device.size);

#ifndef NDEBUG
#pragma message("Get rid of CL_MEM_ALLOC_HOST_PTR as soon as the sorter is installed")
  cl_mem_flags const flags = CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR;
#else
  cl_mem_flags const flags = CL_MEM_READ_WRITE;
#endif

  runtime->allocator.device.temp.extent = 
    skc_runtime_device_perm_alloc(runtime,
                                  flags,
                                  runtime->config->suballocator.device.size);
}

void
skc_allocator_device_dispose(struct skc_runtime * const runtime)
{
  skc_suballocator_dispose(runtime,&runtime->allocator.device.temp.suballocator);

  skc_runtime_device_perm_free(runtime,runtime->allocator.device.temp.extent);
}

//
//
//