aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/compute/skc/cq_pool_cl.c
blob: 80cfe34cf8986c883083a8d1ec2bc364249f0edd (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can
 * be found in the LICENSE file.
 *
 */

//
//
//

#ifndef NDEBUG
#include <stdio.h>
#endif

//
//
//

#include <string.h>

//
//
//

#include "runtime_cl_12.h"

//
// This implementation is probably excessive.
//
// The command queue pool could easily be replaced with simply an LRU
// or even round-robin reuse pool.  Even a small number of aliased
// command queues can probably enough concurrency.
//

#define SKC_CQ_POOL_EXPAND 1

//
//
//

void
skc_cq_pool_create(struct skc_runtime * const runtime,
                   struct skc_cq_pool * const pool,
                   skc_uint             const type,
                   skc_uint             const size)
{
  pool->type   = type;
  pool->size   = size + 1; // an empty spot
  pool->reads  = 0;
  pool->writes = size;
  pool->cq     = skc_runtime_host_perm_alloc(runtime,SKC_MEM_FLAGS_READ_WRITE,pool->size * sizeof(*pool->cq));

  for (skc_uint ii=0; ii<size; ii++) {
    pool->cq[ii] = skc_runtime_cl_create_cq(&runtime->cl,pool->type);
  }
  pool->cq[size] = NULL;
}

//
//
//

void
skc_cq_pool_dispose(struct skc_runtime * const runtime,
                    struct skc_cq_pool *       pool)
{
  //
  // FIXME -- release the command queues after waiting for the ring to
  // be full with pool.size queues?
  //
  skc_runtime_host_perm_free(runtime,pool->cq);
}

//
//
//

static 
void
skc_cq_pool_write(struct skc_cq_pool * const pool,
                  cl_command_queue           cq)
{
  pool->cq[pool->writes++ % pool->size] = cq;
}

//
// only expand when completely empty
//

static
void
skc_cq_pool_expand(struct skc_runtime * const runtime,
                   struct skc_cq_pool * const pool,
                   skc_uint                   expand)
{
#ifndef NDEBUG
  fprintf(stderr,"Expanding the cq_pool by: %u (%u)\n",expand,pool->size);
#endif

  // free old
  skc_runtime_host_perm_free(runtime,pool->cq);

  // the ring is empty
  pool->size  += expand;
  pool->cq     = skc_runtime_host_perm_alloc(runtime,SKC_MEM_FLAGS_READ_WRITE,pool->size * sizeof(*pool->cq));
  pool->reads  = 0;
  pool->writes = expand;

  for (skc_uint ii=0; ii<expand; ii++)
    pool->cq[ii] = skc_runtime_cl_create_cq(&runtime->cl,pool->type);
}

//
//
//

static 
cl_command_queue
skc_cq_pool_read(struct skc_runtime * const runtime,
                 struct skc_cq_pool * const pool)
{
  // any command queues left?
  if (pool->reads == pool->writes)
    skc_cq_pool_expand(runtime,pool,SKC_CQ_POOL_EXPAND);

  cl_command_queue cq = pool->cq[pool->reads++ % pool->size];

  return cq;
}

//
//
//

cl_command_queue
skc_runtime_acquire_cq_in_order(struct skc_runtime * const runtime)
{
  return skc_cq_pool_read(runtime,&runtime->cq_pool);
}

void
skc_runtime_release_cq_in_order(struct skc_runtime * const runtime, 
                                cl_command_queue           cq)
{
  skc_cq_pool_write(&runtime->cq_pool,cq);
}

//
//
//