atmi_runtime.h
6.42 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*===--------------------------------------------------------------------------
* ATMI (Asynchronous Task and Memory Interface)
*
* This file is distributed under the MIT License. See LICENSE.txt for details.
*===------------------------------------------------------------------------*/
#ifndef INCLUDE_ATMI_RUNTIME_H_
#define INCLUDE_ATMI_RUNTIME_H_
#include "atmi.h"
#include "hsa.h"
#include <inttypes.h>
#include <stdlib.h>
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
/** \defgroup context_functions ATMI Context Setup and Finalize
* @{
*/
/**
* @brief Initialize the ATMI runtime environment.
*
* @detal All ATMI runtime functions will fail if this function is not called
* at least once. The user may initialize difference device types at different
* regions in the program in order for optimization purposes.
*
* @retval ::ATMI_STATUS_SUCCESS The function has executed successfully.
*
* @retval ::ATMI_STATUS_ERROR The function encountered errors.
*
* @retval ::ATMI_STATUS_UNKNOWN The function encountered errors.
*/
atmi_status_t atmi_init();
/**
* @brief Finalize the ATMI runtime environment.
*
* @detail ATMI runtime functions will fail if called after finalize.
*
* @retval ::ATMI_STATUS_SUCCESS The function has executed successfully.
*
* @retval ::ATMI_STATUS_ERROR The function encountered errors.
*
* @retval ::ATMI_STATUS_UNKNOWN The function encountered errors.
*/
atmi_status_t atmi_finalize();
/** @} */
/** \defgroup module_functions ATMI Module
* @{
*/
/**
* @brief Register the ATMI code module from memory on to a specific place
* (device).
*
* @detail Currently, only GPU devices need explicit module registration because
* of their specific ISAs that require a separate compilation phase. On the
* other
* hand, CPU devices execute regular x86 functions that are compiled with the
* host program.
*
* @param[in] module_bytes A memory region that contains the GPU modules
* targeting ::AMDGCN platform types. Value cannot be NULL.
*
* @param[in] module_size Size of module region
*
* @param[in] place Denotes the execution place (device) on which the module
* should be registered and loaded.
*
* @param[in] on_deserialized_data Callback run on deserialized code object,
* before loading it
*
* @param[in] cb_state void* passed to on_deserialized_data callback
*
* @retval ::ATMI_STATUS_SUCCESS The function has executed successfully.
*
* @retval ::ATMI_STATUS_ERROR The function encountered errors.
*
* @retval ::ATMI_STATUS_UNKNOWN The function encountered errors.
*
*/
atmi_status_t atmi_module_register_from_memory_to_place(
void *module_bytes, size_t module_size, atmi_place_t place,
atmi_status_t (*on_deserialized_data)(void *data, size_t size,
void *cb_state),
void *cb_state);
/** @} */
/** \defgroup machine ATMI Machine
* @{
*/
/**
* @brief ATMI's device discovery function to get the current machine's
* topology.
*
* @detail The @p atmi_machine_t structure is a tree-based representation of the
* compute and memory elements in the current node. Once ATMI is initialized,
* this function can be called to retrieve the pointer to this global structure.
*
* @return Returns a pointer to a global structure of tyoe @p atmi_machine_t.
* Returns NULL if ATMI is not initialized.
*/
atmi_machine_t *atmi_machine_get_info();
/** @} */
/** \defgroup memory_functions ATMI Data Management
* @{
*/
/**
* @brief Allocate memory from the specified memory place.
*
* @detail This function allocates memory from the specified memory place. If
* the memory
* place belongs primarily to the CPU, then the memory will be accessible by
* other GPUs and CPUs in the system. If the memory place belongs primarily to a
* GPU,
* then it cannot be accessed by other devices in the system.
*
* @param[in] ptr The pointer to the memory that will be allocated.
*
* @param[in] size The size of the allocation in bytes.
*
* @param[in] place The memory place in the system to perform the allocation.
*
* @retval ::ATMI_STATUS_SUCCESS The function has executed successfully.
*
* @retval ::ATMI_STATUS_ERROR The function encountered errors.
*
* @retval ::ATMI_STATUS_UNKNOWN The function encountered errors.
*
*/
atmi_status_t atmi_malloc(void **ptr, size_t size, atmi_mem_place_t place);
/**
* @brief Frees memory that was previously allocated.
*
* @detail This function frees memory that was previously allocated by calling
* @p atmi_malloc. It throws an error otherwise. It is illegal to access a
* pointer after a call to this function.
*
* @param[in] ptr The pointer to the memory that has to be freed.
*
* @retval ::ATMI_STATUS_SUCCESS The function has executed successfully.
*
* @retval ::ATMI_STATUS_ERROR The function encountered errors.
*
* @retval ::ATMI_STATUS_UNKNOWN The function encountered errors.
*
*/
atmi_status_t atmi_free(void *ptr);
/**
* @brief Syncrhonously copy memory from the source to destination memory
* locations.
*
* @detail This function assumes that the source and destination regions are
* non-overlapping. The runtime determines the memory place of the source and
* the
* destination and executes the appropriate optimized data movement methodology.
*
* @param[in] dest The destination pointer previously allocated by a system
* allocator or @p atmi_malloc.
*
* @param[in] src The source pointer previously allocated by a system
* allocator or @p atmi_malloc.
*
* @param[in] size The size of the data to be copied in bytes.
*
* @retval ::ATMI_STATUS_SUCCESS The function has executed successfully.
*
* @retval ::ATMI_STATUS_ERROR The function encountered errors.
*
* @retval ::ATMI_STATUS_UNKNOWN The function encountered errors.
*
*/
atmi_status_t atmi_memcpy(hsa_signal_t sig, void *dest, const void *src,
size_t size);
static inline atmi_status_t atmi_memcpy_no_signal(void *dest, const void *src,
size_t size) {
hsa_signal_t sig;
hsa_status_t err = hsa_signal_create(0, 0, NULL, &sig);
if (err != HSA_STATUS_SUCCESS) {
return ATMI_STATUS_ERROR;
}
atmi_status_t r = atmi_memcpy(sig, dest, src, size);
hsa_status_t rc = hsa_signal_destroy(sig);
if (r != ATMI_STATUS_SUCCESS) {
return r;
}
if (rc != HSA_STATUS_SUCCESS) {
return ATMI_STATUS_ERROR;
}
return ATMI_STATUS_SUCCESS;
}
/** @} */
#ifdef __cplusplus
}
#endif
#endif // INCLUDE_ATMI_RUNTIME_H_