Blender  V2.93
memory_manager.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2017 Blender Foundation
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 #pragma once
18 
19 #include "device/device.h"
20 
21 #include "util/util_map.h"
22 #include "util/util_string.h"
23 #include "util/util_vector.h"
24 
25 #include "clew.h"
26 
28 
29 class OpenCLDevice;
30 
32  public:
33  static const int NUM_DEVICE_BUFFERS = 8;
34 
37  cl_ulong offset;
38  };
39 
40  private:
41  struct DeviceBuffer;
42 
43  struct Allocation {
44  device_memory *mem;
45 
46  DeviceBuffer *device_buffer;
47  size_t size; /* Size of actual allocation, may be larger than requested. */
48 
49  BufferDescriptor desc;
50 
51  bool needs_copy_to_device;
52 
53  Allocation() : mem(NULL), device_buffer(NULL), size(0), needs_copy_to_device(false)
54  {
55  }
56  };
57 
58  struct DeviceBuffer {
60  vector<Allocation *> allocations;
61  size_t size; /* Size of all allocations. */
62 
63  DeviceBuffer() : buffer(NULL), size(0)
64  {
65  }
66 
67  ~DeviceBuffer()
68  {
69  delete buffer;
70  buffer = NULL;
71  }
72 
73  void add_allocation(Allocation &allocation);
74 
75  void update_device_memory(OpenCLDevice *device);
76 
77  void free(OpenCLDevice *device);
78  };
79 
80  OpenCLDevice *device;
81 
82  DeviceBuffer device_buffers[NUM_DEVICE_BUFFERS];
83 
84  typedef unordered_map<string, Allocation> AllocationsMap;
85  AllocationsMap allocations;
86 
87  bool need_update;
88 
89  DeviceBuffer *smallest_device_buffer();
90 
91  public:
92  MemoryManager(OpenCLDevice *device);
93 
94  void free(); /* Free all memory. */
95 
96  void alloc(const char *name, device_memory &mem);
97  bool free(device_memory &mem);
98 
100 
102  void set_kernel_arg_buffers(cl_kernel kernel, cl_uint *narg);
103 };
104 
unsigned int uint
Definition: BLI_sys_types.h:83
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
void alloc(const char *name, device_memory &mem)
bool free(device_memory &mem)
MemoryManager(OpenCLDevice *device)
BufferDescriptor get_descriptor(string name)
void set_kernel_arg_buffers(cl_kernel kernel, cl_uint *narg)
void update_device_memory()
static const int NUM_DEVICE_BUFFERS
#define CCL_NAMESPACE_END
__kernel void ccl_constant KernelData ccl_global void ccl_global char ccl_global int ccl_global char ccl_global unsigned int ccl_global float * buffer