dma_linux_par.c 9.31 KB
Newer Older
Swann Perarnau's avatar
Swann Perarnau committed
1
2
3
4
5
6
7
8
9
10
/*******************************************************************************
 * Copyright 2019 UChicago Argonne, LLC.
 * (c.f. AUTHORS, LICENSE)
 *
 * This file is part of the AML project.
 * For more info, see https://xgitlab.cels.anl.gov/argo/aml
 *
 * SPDX-License-Identifier: BSD-3-Clause
*******************************************************************************/

11
#include "aml.h"
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
#include <assert.h>
#include <errno.h>
#include <sys/mman.h>

/*******************************************************************************
 * Linux-backed, paruential dma
 * The dma itself is organized into several different components
 * - request types: copy or move
 * - implementation of the request
 * - user API (i.e. generic request creation and call)
 * - how to init the dma
 ******************************************************************************/

/*******************************************************************************
 * Requests:
 ******************************************************************************/

int aml_dma_request_linux_par_copy_init(struct aml_dma_request_linux_par *req,
					struct aml_tiling *dt,
					void *dptr, int dtid,
					struct aml_tiling *st,
					void *sptr, int stid)
{
	assert(req != NULL);

	req->type = AML_DMA_REQUEST_TYPE_COPY;
	/* figure out pointers */
	req->dest = aml_tiling_tilestart(dt, dptr, dtid);
	req->src = aml_tiling_tilestart(st, sptr, stid);
	req->size = aml_tiling_tilesize(st, stid);
	/* TODO: assert size match */
	return 0;
}

int aml_dma_request_linux_par_copy_destroy(struct aml_dma_request_linux_par *r)
{
	assert(r != NULL);
	return 0;
}

int aml_dma_request_linux_par_move_init(struct aml_dma_request_linux_par *req,
					struct aml_area *darea,
					struct aml_tiling *tiling,
					void *startptr, int tileid)
{
	assert(req != NULL);
	struct aml_binding *binding;

	req->type = AML_DMA_REQUEST_TYPE_MOVE;
	aml_area_binding(darea, &binding);
	req->count = aml_binding_nbpages(binding, tiling, startptr, tileid);
	req->pages = calloc(req->count, sizeof(void *));
	req->nodes = calloc(req->count, sizeof(int));
	aml_binding_pages(binding, req->pages, tiling, startptr, tileid);
	aml_binding_nodes(binding, req->nodes, tiling, startptr, tileid);
	free(binding);
	return 0;
}

int aml_dma_request_linux_par_move_destroy(struct aml_dma_request_linux_par *req)
{
	assert(req != NULL);
	free(req->pages);
	free(req->nodes);
	return 0;
}

/*******************************************************************************
 * Internal functions
 ******************************************************************************/

void *aml_dma_linux_par_do_thread(void *arg)
{
	struct aml_dma_linux_par_thread_data *data =
		(struct aml_dma_linux_par_thread_data *)arg;

	if(data->req->type == AML_DMA_REQUEST_TYPE_COPY)
		data->dma->ops.do_copy(&data->dma->data, data->req, data->tid);
	else if(data->req->type == AML_DMA_REQUEST_TYPE_MOVE)
		data->dma->ops.do_move(&data->dma->data, data->req, data->tid);
	return NULL;
}

int aml_dma_linux_par_do_copy(struct aml_dma_linux_par_data *dma,
			      struct aml_dma_request_linux_par *req, int tid)
{
	assert(dma != NULL);
	assert(req != NULL);
100
101
102
103
104
105
106
107
108
109
110
111

	/* chunk memory */
	size_t nbthreads = dma->nbthreads;
	size_t chunksize = req->size / nbthreads;

	void *dest = (void*)((intptr_t)req->dest + tid * chunksize);
	void *src = (void*)((intptr_t)req->src + tid * chunksize);

	if(tid == nbthreads - 1 && req->size > chunksize * nbthreads)
		chunksize += req->size % nbthreads;

	memcpy(dest, src, chunksize);
112
113
114
115
116
117
118
119
	return 0;
}

int aml_dma_linux_par_do_move(struct aml_dma_linux_par_data *dma,
			      struct aml_dma_request_linux_par *req, int tid)
{
	assert(dma != NULL);
	assert(req != NULL);
120
121
122
123
124
125
126
127
128
129

	size_t nbthreads = dma->nbthreads;
	size_t chunksize = req->count / nbthreads;

	size_t idx = tid * chunksize;

	if(tid == nbthreads - 1 && req->count > chunksize * nbthreads)
		chunksize += req->count % nbthreads;

	int status[chunksize];
130
	int err;
131
132
	err = move_pages(0, chunksize, &req->pages[idx], &req->nodes[idx],
			 status, MPOL_MF_MOVE);
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
	if(err)
	{
		perror("move_pages:");
		return errno;
	}
	return 0;
}

struct aml_dma_linux_par_ops aml_dma_linux_par_inner_ops = {
	aml_dma_linux_par_do_thread,
	aml_dma_linux_par_do_copy,
	aml_dma_linux_par_do_move,
};

/*******************************************************************************
 * Public API
 ******************************************************************************/

int aml_dma_linux_par_create_request(struct aml_dma_data *d,
				     struct aml_dma_request **r,
				     int type, va_list ap)
{
	assert(d != NULL);
	assert(r != NULL);
	struct aml_dma_linux_par *dma =
		(struct aml_dma_linux_par *)d;

	struct aml_dma_request_linux_par *req;

162
	pthread_mutex_lock(&dma->data.lock);
163
	req = aml_vector_add(&dma->data.requests);
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

	/* init the request */
	if(type == AML_DMA_REQUEST_TYPE_COPY)
	{
		struct aml_tiling *dt, *st;
		void *dptr, *sptr;
		int dtid, stid;
		dt = va_arg(ap, struct aml_tiling *);
		dptr = va_arg(ap, void *);
		dtid = va_arg(ap, int);
		st = va_arg(ap, struct aml_tiling *);
		sptr = va_arg(ap, void *);
		stid = va_arg(ap, int);
		aml_dma_request_linux_par_copy_init(req, dt, dptr, dtid,
						    st, sptr, stid);
	}
	else if(type == AML_DMA_REQUEST_TYPE_MOVE)
	{
		struct aml_area *darea = va_arg(ap, struct aml_area *);
		struct aml_tiling *st = va_arg(ap, struct aml_tiling *);
		void *sptr = va_arg(ap, void *);
		int stid = va_arg(ap, int);
186
187
		aml_dma_request_linux_par_move_init(req, darea, st, sptr,
						    stid);
188
	}
189
	pthread_mutex_unlock(&dma->data.lock);
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

	for(int i = 0; i < dma->data.nbthreads; i++)
	{
		struct aml_dma_linux_par_thread_data *rd = &req->thread_data[i];
		rd->req = req;
		rd->dma = dma;
		rd->tid = i;
		pthread_create(&rd->thread, NULL, dma->ops.do_thread, rd);
	}
	*r = (struct aml_dma_request *)req;
	return 0;
}

int aml_dma_linux_par_destroy_request(struct aml_dma_data *d,
				      struct aml_dma_request *r)
{
	assert(d != NULL);
	assert(r != NULL);
	struct aml_dma_linux_par *dma =
		(struct aml_dma_linux_par *)d;

	struct aml_dma_request_linux_par *req =
		(struct aml_dma_request_linux_par *)r;

	/* we cancel and join, instead of killing, for a cleaner result */
	for(int i = 0; i < dma->data.nbthreads; i++)
	{
		pthread_cancel(req->thread_data[i].thread);
		pthread_join(req->thread_data[i].thread, NULL);
	}

	if(req->type == AML_DMA_REQUEST_TYPE_COPY)
		aml_dma_request_linux_par_copy_destroy(req);
	else if(req->type == AML_DMA_REQUEST_TYPE_MOVE)
		aml_dma_request_linux_par_move_destroy(req);

226
	pthread_mutex_lock(&dma->data.lock);
227
	aml_vector_remove(&dma->data.requests, req);
228
	pthread_mutex_unlock(&dma->data.lock);
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
	return 0;
}

int aml_dma_linux_par_wait_request(struct aml_dma_data *d,
				   struct aml_dma_request *r)
{
	assert(d != NULL);
	assert(r != NULL);
	struct aml_dma_linux_par *dma = (struct aml_dma_linux_par *)d;
	struct aml_dma_request_linux_par *req =
		(struct aml_dma_request_linux_par *)r;

	for(int i = 0; i < dma->data.nbthreads; i++)
		pthread_join(req->thread_data[i].thread, NULL);

	/* destroy a completed request */
	if(req->type == AML_DMA_REQUEST_TYPE_COPY)
		aml_dma_request_linux_par_copy_destroy(req);
	else if(req->type == AML_DMA_REQUEST_TYPE_MOVE)
		aml_dma_request_linux_par_move_destroy(req);
249

250
	pthread_mutex_lock(&dma->data.lock);
251
	aml_vector_remove(&dma->data.requests, req);
252
	pthread_mutex_unlock(&dma->data.lock);
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
	return 0;
}

struct aml_dma_ops aml_dma_linux_par_ops = {
	aml_dma_linux_par_create_request,
	aml_dma_linux_par_destroy_request,
	aml_dma_linux_par_wait_request,
};

/*******************************************************************************
 * Init functions:
 ******************************************************************************/

int aml_dma_linux_par_create(struct aml_dma **d, ...)
{
	va_list ap;
	struct aml_dma *ret = NULL;
	intptr_t baseptr, dataptr;
	va_start(ap, d);

	/* alloc */
	baseptr = (intptr_t) calloc(1, AML_DMA_LINUX_PAR_ALLOCSIZE);
	dataptr = baseptr + sizeof(struct aml_dma);

	ret = (struct aml_dma *)baseptr;
	ret->data = (struct aml_dma_data *)dataptr;

	aml_dma_linux_par_vinit(ret, ap);

	va_end(ap);
	*d = ret;
	return 0;
}
int aml_dma_linux_par_vinit(struct aml_dma *d, va_list ap)
{
	d->ops = &aml_dma_linux_par_ops;
	struct aml_dma_linux_par *dma = (struct aml_dma_linux_par *)d->data;

	dma->ops = aml_dma_linux_par_inner_ops;
	/* allocate request array */
293
	size_t nbreqs = va_arg(ap, size_t);
294
	dma->data.nbthreads = va_arg(ap, size_t);
295
296
297
298
299
300

	aml_vector_init(&dma->data.requests, nbreqs,
			sizeof(struct aml_dma_request_linux_par),
			offsetof(struct aml_dma_request_linux_par, type),
			AML_DMA_REQUEST_TYPE_INVALID);
	for(int i = 0; i < nbreqs; i++)
301
	{
302
303
304
305
		struct aml_dma_request_linux_par *req =
			aml_vector_get(&dma->data.requests, i);
		req->thread_data = calloc(dma->data.nbthreads,
					  sizeof(struct aml_dma_linux_par_thread_data));
306
	}
307
	pthread_mutex_init(&dma->data.lock, NULL);
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
	return 0;
}
int aml_dma_linux_par_init(struct aml_dma *d, ...)
{
	int err;
	va_list ap;
	va_start(ap, d);
	err = aml_dma_linux_par_vinit(d, ap);
	va_end(ap);
	return err;
}

int aml_dma_linux_par_destroy(struct aml_dma *d)
{
	struct aml_dma_linux_par *dma = (struct aml_dma_linux_par *)d->data;
323
324
325
326
327
328
329
	for(int i = 0; i < aml_vector_size(&dma->data.requests); i++)
	{
		struct aml_dma_request_linux_par *req =
			aml_vector_get(&dma->data.requests, i);
		free(req->thread_data);
	}
	aml_vector_destroy(&dma->data.requests);
330
	pthread_mutex_destroy(&dma->data.lock);
331
332
	return 0;
}