Skip to content

Commit af665bc

Browse files
authored
Refactor memory manager. Remove buffer_info. Simplify allocs. Disable Events (#2689)
* Refactor memory manager. Remove buffer_info. Simplify allocs * Removed buffer_info from the allocation calls The buffer_info object was created so that the event synchonization was performed in the memAlloc call instead of in the memory manager. It did not have significant benefit over performing the synchronization within the memory manager and increased complexity of the API. Now that the events API is exposed, it shouldn't be necessary to include this. * Moved memory manager classes into separate files in the common directory * Use memAlloc instead of bufferAlloc in OpenCL Array<T> initializtion to provide type context to the memory manager. Avoid inconsistencies between backends. * Use objects instead of handles within the codebase
1 parent 6f5812f commit af665bc

52 files changed

Lines changed: 455 additions & 1021 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

include/af/memory.h

Lines changed: 3 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -16,147 +16,12 @@
1616

1717
#if AF_API_VERSION >= 37
1818

19-
typedef void* af_buffer_info;
20-
2119
typedef void* af_memory_manager;
2220

23-
#ifdef __cplusplus
24-
namespace af {
25-
26-
/// A simple RAII wrapper for af_buffer_info
27-
class AFAPI buffer_info {
28-
af_buffer_info p_;
29-
30-
public:
31-
buffer_info(af_buffer_info p);
32-
buffer_info(void* ptr, af_event event);
33-
~buffer_info();
34-
#if AF_COMPILER_CXX_RVALUE_REFERENCES
35-
buffer_info(buffer_info&& other);
36-
buffer_info& operator=(buffer_info&& other);
37-
#endif
38-
void* getPtr() const;
39-
void setPtr(void* ptr);
40-
af_event getEvent() const;
41-
void setEvent(af_event event);
42-
af_buffer_info get() const;
43-
af_event unlockEvent();
44-
void* unlockPtr();
45-
46-
private:
47-
buffer_info& operator=(const buffer_info& other);
48-
buffer_info(const buffer_info& other);
49-
};
50-
51-
} // namespace af
52-
#endif // __cplusplus
53-
5421
#ifdef __cplusplus
5522
extern "C" {
5623
#endif // __cplusplus
5724

58-
/**
59-
\brief Creates an \ref af_buffer_info handle from an \ref af_event event and
60-
ptr
61-
62-
\param[in] buf The \ref af_buffer_info object to be created
63-
\param[in] ptr A pointer
64-
\param[in] event An \ref af_event
65-
\returns AF_SUCCESS
66-
67-
\ingroup buffer_info
68-
*/
69-
AFAPI af_err af_create_buffer_info(af_buffer_info* buf, void* ptr,
70-
af_event event);
71-
72-
/**
73-
\brief deletes the \ref af_buffer_info and its resources
74-
75-
Deletes the \ref af_buffer_info object and its tracked resources. If buffer
76-
still holds a pointer, that pointer is freed. Does NOT enqueue a wait on the
77-
associated event
78-
79-
\param[in] buf The \ref af_buffer_info object that will be deleted
80-
\returns AF_SUCCESS
81-
82-
\ingroup buffer_info
83-
*/
84-
AFAPI af_err af_delete_buffer_info(af_buffer_info buf);
85-
86-
/**
87-
\brief Retrieves a pointer from an \ref af_buffer_info
88-
89-
\param[out] ptr The associated pointer
90-
\param[in] buf The \ref af_buffer_info object
91-
\returns AF_SUCCESS
92-
93-
\ingroup buffer_info
94-
*/
95-
AFAPI af_err af_buffer_info_get_ptr(void** ptr, af_buffer_info buf);
96-
97-
/**
98-
\brief Retrieves an \ref af_event from an \ref af_buffer_info
99-
100-
\param[out] event The associated event
101-
\param[in] buf The \ref af_buffer_info object
102-
\returns AF_SUCCESS
103-
104-
\ingroup buffer_info
105-
*/
106-
AFAPI af_err af_buffer_info_get_event(af_event* event, af_buffer_info buf);
107-
108-
/**
109-
\brief Sets a pointer on an \ref af_buffer_info
110-
111-
\param[in] buf The \ref af_buffer_info object
112-
\param[in] ptr The pointer to set
113-
\returns AF_SUCCESS
114-
115-
\ingroup buffer_info
116-
*/
117-
AFAPI af_err af_buffer_info_set_ptr(af_buffer_info buf, void* ptr);
118-
119-
/**
120-
\brief Sets an \ref af_event on an \ref af_buffer_info
121-
122-
\param[in] buf The \ref af_buffer_info object
123-
\param[in] event The \ref af_event to set
124-
\returns AF_SUCCESS
125-
126-
\ingroup buffer_info
127-
*/
128-
AFAPI af_err af_buffer_info_set_event(af_buffer_info buf, af_event event);
129-
130-
/**
131-
\brief Disassociates the \ref af_event from the \ref af_buffer_info object
132-
133-
Gets the \ref af_event and disassociated it from the af_buffer_info object.
134-
Deleting the af_buffer_info object will not affect this event.
135-
136-
\param[out] event The \ref af_event that will be disassociated. If NULL no
137-
event is returned and the event is NOT freed
138-
\param[in] buf The target \ref af_buffer_info object
139-
\returns AF_SUCCESS
140-
141-
\ingroup buffer_info
142-
*/
143-
AFAPI af_err af_unlock_buffer_info_event(af_event* event, af_buffer_info buf);
144-
145-
/**
146-
\brief Disassociates the pointer from the \ref af_buffer_info object
147-
148-
Gets the pointer and disassociated it from the \ref af_buffer_info object.
149-
Deleting the \ref af_buffer_info object will not affect this pointer.
150-
151-
\param[out] ptr The pointer that will be disassociated. If NULL no
152-
pointer is returned and the data is NOT freed.
153-
\param[in] buf The target \ref af_buffer_info object
154-
\returns AF_SUCCESS
155-
156-
\ingroup buffer_info
157-
*/
158-
AFAPI af_err af_unlock_buffer_info_ptr(void** ptr, af_buffer_info buf);
159-
16025
/**
16126
\brief Called after a memory manager is set and becomes active.
16227
@@ -181,8 +46,7 @@ typedef af_err (*af_memory_manager_shutdown_fn)(af_memory_manager handle);
18146
\brief Function pointer that will be called by ArrayFire to allocate memory.
18247
18348
\param[in] handle a pointer to the active \ref af_memory_manager handle
184-
\param[out] buffer_info a pointer to a \ref af_buffer_info containing the
185-
pointer to the allocated buffer and an associated \ref af_event
49+
\param[out] ptr pointer to the allocated buffer
18650
\param[in] bytes number of bytes to allocate
18751
\param[in] user_lock a truthy value corresponding to whether or not the
18852
memory should have a user lock associated with it
@@ -198,7 +62,7 @@ typedef af_err (*af_memory_manager_shutdown_fn)(af_memory_manager handle);
19862
\ingroup memory_manager_api
19963
*/
20064
typedef af_err (*af_memory_manager_alloc_fn)(af_memory_manager handle,
201-
af_buffer_info* buffer_info,
65+
void** ptr,
20266
/* bool */ int user_lock,
20367
const unsigned ndims, dim_t* dims,
20468
const unsigned element_size);
@@ -221,16 +85,13 @@ typedef af_err (*af_memory_manager_allocated_fn)(af_memory_manager handle,
22185
22286
\param[in] handle a pointer to the active \ref af_memory_manager handle
22387
\param[out] ptr the pointer to query
224-
\param[in] event a new \ref af_event which will be marked before the free is
225-
executed such that enqueing a wait on this event
22688
\param[in] user_unlock frees the memory from user lock
22789
\returns AF_SUCCESS
22890
22991
\ingroup memory_manager_api
23092
*/
23193
typedef af_err (*af_memory_manager_unlock_fn)(af_memory_manager handle,
232-
void* ptr, af_event event,
233-
/* bool */ int user_unlock);
94+
void* ptr, /* bool */ int user_unlock);
23495

23596
/**
23697
\brief Called to signal the memory manager should free memory if possible

include/arrayfire.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,6 @@ closures for each required function, for example:
144144
af_unset_memory_manager();
145145
\endcode
146146
147-
@defgroup buffer_info Buffer Info
148-
\brief An interface for managing information about memory (pointers and
149-
\ref af_event)
150-
151147
@defgroup native_memory_interface Native Memory Interface
152148
\brief Native alloc, native free, get device id, etc.
153149

src/api/c/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ target_sources(c_api_interface
5757
${CMAKE_CURRENT_SOURCE_DIR}/bilateral.cpp
5858
${CMAKE_CURRENT_SOURCE_DIR}/binary.cpp
5959
${CMAKE_CURRENT_SOURCE_DIR}/blas.cpp
60-
${CMAKE_CURRENT_SOURCE_DIR}/buffer_info.cpp
6160
${CMAKE_CURRENT_SOURCE_DIR}/canny.cpp
6261
${CMAKE_CURRENT_SOURCE_DIR}/cast.cpp
6362
${CMAKE_CURRENT_SOURCE_DIR}/cholesky.cpp
@@ -112,7 +111,6 @@ target_sources(c_api_interface
112111
${CMAKE_CURRENT_SOURCE_DIR}/median.cpp
113112
${CMAKE_CURRENT_SOURCE_DIR}/memory.cpp
114113
${CMAKE_CURRENT_SOURCE_DIR}/memoryapi.hpp
115-
${CMAKE_CURRENT_SOURCE_DIR}/memory_manager.hpp
116114
${CMAKE_CURRENT_SOURCE_DIR}/moddims.cpp
117115
${CMAKE_CURRENT_SOURCE_DIR}/moments.cpp
118116
${CMAKE_CURRENT_SOURCE_DIR}/morph.cpp

src/api/c/buffer_info.cpp

Lines changed: 0 additions & 119 deletions
This file was deleted.

src/api/c/events.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616

1717
using namespace detail;
1818

19-
Event &getEvent(const af_event handle) {
20-
Event &event = *(Event *)handle;
19+
Event &getEvent(af_event &handle) {
20+
Event &event = *static_cast<Event *>(handle);
2121
return event;
2222
}
2323

24-
af_event getHandle(const Event &event) { return (af_event)&event; }
24+
const Event &getEvent(const af_event &handle) {
25+
const Event &event = *static_cast<const Event *>(handle);
26+
return event;
27+
}
28+
29+
af_event getHandle(Event &event) { return static_cast<af_event>(&event); }
30+
2531

2632
af_err af_create_event(af_event *handle) {
2733
try {
@@ -35,7 +41,7 @@ af_err af_create_event(af_event *handle) {
3541

3642
af_err af_delete_event(af_event handle) {
3743
try {
38-
releaseEvent(handle);
44+
delete &getEvent(handle);
3945
}
4046
CATCHALL;
4147

src/api/c/events.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <backend.hpp>
1414
#include <af/event.h>
1515

16-
af_event getHandle(const detail::Event& event);
16+
af_event getHandle(detail::Event& event);
1717

18-
detail::Event& getEvent(const af_event eventHandle);
18+
detail::Event& getEvent(af_event &eventHandle);
19+
const detail::Event& getEvent(const af_event &eventHandle);

0 commit comments

Comments
 (0)