File: hmem_rocr.c

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (378 lines) | stat: -rw-r--r-- 9,601 bytes parent folder | download | duplicates (6)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * (C) Copyright 2020 Hewlett Packard Enterprise Development LP
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "hmem.h"
#include "shared.h"

#ifdef HAVE_ROCR_RUNTIME_H

#include <dlfcn.h>
#include <stdio.h>
#include <hsa/hsa.h>

struct rocr_ops {
	hsa_status_t (*hsa_memory_copy)(void *dst, const void *src,
					size_t size);
	hsa_status_t (*hsa_init)(void);
	hsa_status_t (*hsa_shut_down)(void);
	hsa_status_t (*hsa_status_string)(hsa_status_t status,
					  const char **status_string);
	hsa_status_t (*hsa_agent_get_info)(hsa_agent_t agent,
					   hsa_agent_info_t attribute,
					   void *value);
	hsa_status_t (*hsa_region_get_info)(hsa_region_t region,
					    hsa_region_info_t attribute,
					    void *value);
	hsa_status_t (*hsa_iterate_agents)
		(hsa_status_t (*cb)(hsa_agent_t agent, void* data), void *data);
	hsa_status_t (*hsa_agent_iterate_regions)
		(hsa_agent_t agent,
		 hsa_status_t (*cb)(hsa_region_t region, void* data),
		 void *data);
	hsa_status_t (*hsa_memory_allocate)(hsa_region_t region, size_t size,
					    void **ptr);
	hsa_status_t (*hsa_memory_free)(void *ptr);
	hsa_status_t (*hsa_amd_memory_fill)(void* ptr, uint32_t value,
					    size_t count);
};

static struct rocr_ops rocr_ops;
static void *rocr_handle;

static const char *hsa_status_to_string(hsa_status_t status)
{
	const char *str;
	hsa_status_t hsa_ret;

	hsa_ret = rocr_ops.hsa_status_string(status, &str);
	if (hsa_ret != HSA_STATUS_SUCCESS)
		return "unknown error";

	return str;
}

#define ROCR_ERR(err, fmt, ...) \
	FT_ERR(fmt ": %s", ##__VA_ARGS__, hsa_status_to_string(err))

static hsa_agent_t gpu_agent;
static hsa_region_t gpu_region;

static hsa_status_t agent_cb(hsa_agent_t agent, void *data)
{
	hsa_status_t hsa_ret;
	hsa_device_type_t hsa_dev_type;

	hsa_ret = rocr_ops.hsa_agent_get_info(agent, HSA_AGENT_INFO_DEVICE,
					      (void *) &hsa_dev_type);

	if (hsa_ret == HSA_STATUS_SUCCESS &&
	    hsa_dev_type == HSA_DEVICE_TYPE_GPU) {
		gpu_agent = agent;
		return HSA_STATUS_INFO_BREAK;
	}

	return hsa_ret;
}

static hsa_status_t region_cb(hsa_region_t region, void *data)
{
	hsa_status_t hsa_ret;
	hsa_region_segment_t hsa_segment;

	hsa_ret = rocr_ops.hsa_region_get_info(region, HSA_REGION_INFO_SEGMENT,
					       &hsa_segment);

	if (hsa_ret == HSA_STATUS_SUCCESS &&
	    hsa_segment == HSA_REGION_SEGMENT_GLOBAL) {
		gpu_region = region;
		return HSA_STATUS_INFO_BREAK;
	}

	return hsa_ret;
}

int ft_rocr_init(void)
{
	hsa_status_t hsa_ret;

	rocr_handle = dlopen("libhsa-runtime64.so", RTLD_NOW);
	if (!rocr_handle) {
		FT_ERR("Failed to dlopen libhsa-runtime64.so");
		goto err;
	}

	rocr_ops.hsa_memory_copy = dlsym(rocr_handle, "hsa_memory_copy");
	if (!rocr_ops.hsa_memory_copy) {
		FT_ERR("Failed to find hsa_memory_copy");
		goto err_dlclose_rocr;
	}

	rocr_ops.hsa_init = dlsym(rocr_handle, "hsa_init");
	if (!rocr_ops.hsa_init) {
		FT_ERR("Failed to find hsa_init");
		goto err_dlclose_rocr;
	}

	rocr_ops.hsa_shut_down = dlsym(rocr_handle, "hsa_shut_down");
	if (!rocr_ops.hsa_shut_down) {
		FT_ERR("Failed to find hsa_shut_down");
		goto err_dlclose_rocr;
	}

	rocr_ops.hsa_status_string = dlsym(rocr_handle, "hsa_status_string");
	if (!rocr_ops.hsa_status_string) {
		FT_ERR("Failed to find hsa_status_string");
		goto err_dlclose_rocr;
	}

	rocr_ops.hsa_agent_get_info = dlsym(rocr_handle, "hsa_agent_get_info");
	if (!rocr_ops.hsa_agent_get_info) {
		FT_ERR("Failed to find hsa_agent_get_info");
		goto err_dlclose_rocr;
	}

	rocr_ops.hsa_region_get_info = dlsym(rocr_handle,
					     "hsa_region_get_info");
	if (!rocr_ops.hsa_region_get_info) {
		FT_ERR("Failed to find hsa_region_get_info");
		goto err_dlclose_rocr;
	}

	rocr_ops.hsa_iterate_agents = dlsym(rocr_handle, "hsa_iterate_agents");
	if (!rocr_ops.hsa_iterate_agents) {
		FT_ERR("Failed to find hsa_iterate_agents");
		goto err_dlclose_rocr;
	}

	rocr_ops.hsa_agent_iterate_regions =
		dlsym(rocr_handle, "hsa_agent_iterate_regions");
	if (!rocr_ops.hsa_agent_iterate_regions) {
		FT_ERR("Failed to find hsa_agent_iterate_regions");
		goto err_dlclose_rocr;
	}

	rocr_ops.hsa_memory_allocate =
		dlsym(rocr_handle, "hsa_memory_allocate");
	if (!rocr_ops.hsa_memory_allocate) {
		FT_ERR("Failed to find hsa_memory_allocate");
		goto err_dlclose_rocr;
	}

	rocr_ops.hsa_memory_free = dlsym(rocr_handle, "hsa_memory_free");
	if (!rocr_ops.hsa_memory_free) {
		FT_ERR("Failed to find hsa_memory_free");
		goto err_dlclose_rocr;
	}

	rocr_ops.hsa_amd_memory_fill = dlsym(rocr_handle,
					     "hsa_amd_memory_fill");
	if (!rocr_ops.hsa_amd_memory_fill) {
		FT_ERR("Failed to find hsa_amd_memory_fill");
		goto err_dlclose_rocr;
	}

	hsa_ret = rocr_ops.hsa_init();
	if (hsa_ret != HSA_STATUS_SUCCESS) {
		ROCR_ERR(hsa_ret, "hsa_init failed");
		goto err_dlclose_rocr;
	}

	hsa_ret = rocr_ops.hsa_iterate_agents(agent_cb, NULL);
	if (hsa_ret != HSA_STATUS_INFO_BREAK) {
		FT_ERR("Failed to find GPU agent");
		goto err_dlclose_rocr;
	}

	hsa_ret = rocr_ops.hsa_agent_iterate_regions(gpu_agent, region_cb,
						     NULL);
	if (hsa_ret != HSA_STATUS_INFO_BREAK) {
		FT_ERR("Failed to find GPU region");
		goto err_dlclose_rocr;
	}

	return FI_SUCCESS;

err_dlclose_rocr:
	dlclose(rocr_handle);
err:
	return -FI_ENODATA;
}

int ft_rocr_cleanup(void)
{
	hsa_status_t hsa_ret;

	hsa_ret = rocr_ops.hsa_shut_down();
	if (hsa_ret != HSA_STATUS_SUCCESS) {
		ROCR_ERR(hsa_ret, "hsa_init failed");
		return -FI_ENODATA;
	}

	dlclose(rocr_handle);

	return FI_SUCCESS;
}

int ft_rocr_alloc(uint64_t device, void **buf, size_t size)
{
	hsa_status_t hsa_ret;

	hsa_ret = rocr_ops.hsa_memory_allocate(gpu_region, size, buf);
	if (hsa_ret == HSA_STATUS_SUCCESS)
		return FI_SUCCESS;

	ROCR_ERR(hsa_ret, "hsa_memory_allocate failed");

	return -FI_ENOMEM;
}

int ft_rocr_free(void *buf)
{
	hsa_status_t hsa_ret;

	hsa_ret = rocr_ops.hsa_memory_free(buf);
	if (hsa_ret == HSA_STATUS_SUCCESS)
		return FI_SUCCESS;

	ROCR_ERR(hsa_ret, "hsa_memory_free failed");

	return -FI_EIO;
}

#define ROCR_MEM_FILL_BYTE_ALIGNMENT 4U

int ft_rocr_memset(uint64_t device, void *buf, int value, size_t size)
{
	unsigned char set_value = value;
	void *mem_fill_ptr;
	size_t mem_fill_size;
	uint32_t mem_fill_value;
	hsa_status_t hsa_ret;
	unsigned char *ptr = buf;
	int ret;

	/* Determine if ROCR memory fill can be used to set device memory. ROCR
	 * memory fill requires 4-byte alignment.
	 */
	mem_fill_ptr = (void *) ALIGN((uintptr_t) buf,
				      ROCR_MEM_FILL_BYTE_ALIGNMENT);

	/* Use ROCR memory copy to fill the start of the buffer until the buffer
	 * is correctly aligned.
	 */
	while (ptr != mem_fill_ptr && size > 0) {
		ret = ft_rocr_memcpy(device, ptr, &set_value, sizeof(*ptr));
		if (ret != FI_SUCCESS)
			return ret;

		size--;
		ptr++;
	}

	/* Use ROCR memory fill to fill the middle of the buffer. */
	if (size >= ROCR_MEM_FILL_BYTE_ALIGNMENT) {
		mem_fill_size = ALIGN_DOWN(size, ROCR_MEM_FILL_BYTE_ALIGNMENT);

		memset(&mem_fill_value, set_value, sizeof(mem_fill_value));

		hsa_ret = rocr_ops.hsa_amd_memory_fill(mem_fill_ptr,
						       mem_fill_value,
						       mem_fill_size /
						       ROCR_MEM_FILL_BYTE_ALIGNMENT);
		if (hsa_ret != HSA_STATUS_SUCCESS) {
			ROCR_ERR(hsa_ret, "hsa_amd_memory_fill failed");
			return -FI_EIO;
		}

		size -= mem_fill_size;
		ptr += mem_fill_size;
	}

	/* Use ROCR memory copy to fill the end of the buffer. */
	while (size > 0) {
		ret = ft_rocr_memcpy(device, ptr, &set_value, sizeof(*ptr));
		if (ret != FI_SUCCESS)
			return ret;

		size--;
		ptr++;
	}

	return FI_SUCCESS;
}

int ft_rocr_memcpy(uint64_t device, void *dst, const void *src, size_t size)
{
	hsa_status_t hsa_ret;

	hsa_ret = rocr_ops.hsa_memory_copy(dst, src, size);
	if (hsa_ret == HSA_STATUS_SUCCESS)
		return FI_SUCCESS;

	ROCR_ERR(hsa_ret, "hsa_memory_copy failed");

	return -FI_EIO;
}

#else

int ft_rocr_init(void)
{
	return -FI_ENOSYS;
}

int ft_rocr_cleanup(void)
{
	return -FI_ENOSYS;
}

int ft_rocr_alloc(uint64_t device, void **buf, size_t size)
{
	return -FI_ENOSYS;
}

int ft_rocr_free(void *buf)
{
	return -FI_ENOSYS;
}

int ft_rocr_memset(uint64_t device, void *buf, int value, size_t size)
{
	return -FI_ENOSYS;
}

int ft_rocr_memcpy(uint64_t device, void *dst, const void *src, size_t size)
{
	return -FI_ENOSYS;
}

#endif /* HAVE_ROCR_RUNTIME_H */