Bolt  1.3
C++ template library with support for OpenCL
pool_alloc.h
Go to the documentation of this file.
1 /***************************************************************************
2 * © 2012,2014 Advanced Micro Devices, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 
16 ***************************************************************************/
17 
21 #if !defined( BOLT_AMP_PARALLEL_ALLOC_H )
22 #define BOLT_AMP_PARALLEL_ALLOC_H
23 
24 
25 #include <amp.h>
26 #pragma once
27 
28 namespace bolt {
29 
30  // FIXME - this bool should be independent of type, should just allocate raw bytes.
31  template<typename T>
32  class ArrayPool {
33  public:
34  struct PoolEntry {
35  enum State {e_New, e_Created, e_Reserved};
36  PoolEntry() : _state(e_New), _dBuffer(NULL), _stagingBuffer(NULL) {};
37 
38  State _state;
39  concurrency::array<T> *_dBuffer; // storage on accelerator
40  concurrency::array<T> *_stagingBuffer;
41  };
42 
43  PoolEntry &alloc(concurrency::accelerator_view av, int size)
44  {
45  using namespace concurrency;
46  // FIXME - need atomic acccess here to make this thread-safe
47  if (pool[0]._state == PoolEntry::e_New) {
48  pool[0]._state = PoolEntry::e_Reserved;
49  accelerator cpuAccelerator = accelerator(accelerator::cpu_accelerator);
50  pool[0]._stagingBuffer = new array<T,1>(size, cpuAccelerator.default_view, av); // cpu memory
51  pool[0]._dBuffer = new concurrency::array<T,1>(size, av);
52  } else if ( pool[0]._state == PoolEntry::e_Reserved) {
53  throw("OclBufferpool[0] full!");
54  }
55 
56  pool[0]._state = PoolEntry::e_Reserved;
57  return pool[0];
58  };
59 
60 #if 0
61  concurrency::array<T,1> &alloc(int size)
62  {
63  // FIXME - need atomic acccess here to make this thread-safe
64  if (pool[0]._state == PoolEntry::e_New) {
65  pool[0]._state = PoolEntry::e_Reserved;
66  pool[0]._dBuffer = new concurrency::array<T,1>(size);
67  } else if ( pool[0]._state == PoolEntry::e_Reserved) {
68  throw("OclBufferpool[0] full!");
69  }
70 
71  pool[0]._state = PoolEntry::e_Reserved;
72  return *(pool[0]._dBuffer);
73  };
74 
75  void free(/*cl::Buffer m*/)
76  {
77  // FIXME , need to find the entry, etc.
78  assert (pool[0]._state == PoolEntry::e_Reserved);
79  pool[0]._state = PoolEntry::e_Created;
80  };
81 #endif
82 
83  void free(PoolEntry &poolEntry)
84  {
85  // FIXME , need to find the entry, etc.
86  assert (poolEntry._state == PoolEntry::e_Reserved);
87  poolEntry._state = PoolEntry::e_Created;
88  };
89 
90  private:
91  PoolEntry pool[1];
92  };
93 };
94 
95 #endif