dense.c 14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*******************************************************************************
 * 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
 ******************************************************************************/

#include "aml.h"
#include "aml/layout/native.h"
#include "aml/layout/dense.h"

15 16
static int aml_layout_dense_alloc(struct aml_layout **ret,
				  const size_t ndims)
17 18
{
	struct aml_layout *layout;
19
	struct aml_layout_dense *data;
20

21 22
	layout = AML_INNER_MALLOC_EXTRA(struct aml_layout,
					struct aml_layout_dense,
23
					size_t, 3*ndims);
24
	if (layout == NULL) {
25 26
		*ret = NULL;
		return -AML_ENOMEM;
27 28
	}

29 30 31 32
	data = AML_INNER_MALLOC_NEXTPTR(layout,
					struct aml_layout,
					struct aml_layout_dense);
	layout->data = (struct aml_layout_data *) data;
33 34 35 36

	data->ptr = NULL;
	data->ndims = ndims;

37 38 39 40 41 42 43 44
	data->dims = AML_INNER_MALLOC_EXTRA_NEXTPTR(layout,
						    struct aml_layout,
						    struct aml_layout_dense,
						    size_t, 0);
	data->stride = AML_INNER_MALLOC_EXTRA_NEXTPTR(layout,
						      struct aml_layout,
						      struct aml_layout_dense,
						      size_t, ndims);
45 46 47
	for (size_t i = 0; i < ndims; i++)
		data->stride[i] = 1;

48 49 50
	data->cpitch = AML_INNER_MALLOC_EXTRA_NEXTPTR(layout,
						      struct aml_layout,
						      struct aml_layout_dense,
51
						      size_t, ndims*2);
52 53
	*ret = layout;
	return AML_SUCCESS;
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
}

static
void aml_layout_dense_init_cpitch(struct aml_layout *layout,
				  void *ptr,
				  const size_t ndims,
				  const size_t *dims,
				  const size_t *stride,
				  const size_t *cpitch)
{
	struct aml_layout_dense *data =
		(struct aml_layout_dense *)layout->data;
	data->ptr = ptr;
	memcpy(data->dims, dims, ndims * sizeof(size_t));
	memcpy(data->stride, stride, ndims * sizeof(size_t));
69
	memcpy(data->cpitch, cpitch, ndims * sizeof(size_t));
70 71 72 73 74 75 76 77 78 79 80 81 82 83
}

int aml_layout_dense_create(struct aml_layout **layout,
			    void *ptr,
			    const int order,
			    const size_t element_size,
			    const size_t ndims,
			    const size_t *dims,
			    const size_t *stride,
			    const size_t *pitch)
{

	struct aml_layout *l;
	struct aml_layout_dense *data;
84
	int err;
85

86 87
	if (layout == NULL || ptr == NULL || !element_size || !ndims ||
	    dims == NULL)
88 89
		return -AML_EINVAL;

90 91 92
	err = aml_layout_dense_alloc(&l, ndims);
	if (err)
		return err;
93 94 95 96

	data = (struct aml_layout_dense *)l->data;
	data->ptr = ptr;
	data->cpitch[0] = element_size;
97
	size_t _pitch[ndims];
98 99 100 101 102 103 104 105 106 107

	switch (AML_LAYOUT_ORDER(order)) {

	case AML_LAYOUT_ORDER_ROW_MAJOR:
		l->ops = &aml_layout_row_ops;
		for (size_t i = 0; i < ndims; i++) {
			data->dims[i] = dims[ndims-i-1];
			if (stride)
				data->stride[i] = stride[ndims-i-1];
			if (pitch)
108
				_pitch[i] = pitch[ndims-i-1];
109
			else
110
				_pitch[i] = dims[ndims-i-1];
111 112 113 114 115 116 117 118 119
		}
		break;

	case AML_LAYOUT_ORDER_COLUMN_MAJOR:
		l->ops = &aml_layout_column_ops;
		memcpy(data->dims, dims, ndims * sizeof(size_t));
		if (stride)
			memcpy(data->stride, stride, ndims * sizeof(size_t));
		if (pitch)
120
			memcpy(_pitch, pitch, ndims * sizeof(size_t));
121
		else
122
			memcpy(_pitch, dims, ndims * sizeof(size_t));
123 124 125 126 127 128
		break;
	default:
		free(l);
		return -AML_EINVAL;

	}
129 130
	for (size_t i = 1; i < ndims; i++)
		data->cpitch[i] = data->cpitch[i-1]*_pitch[i-1];
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

	*layout = l;
	return AML_SUCCESS;
}

void aml_layout_dense_destroy(struct aml_layout **l)
{
	if (l == NULL || *l == NULL)
		return;
	free(*l);
	*l = NULL;
}

/*******************************************************************************
 * COLUMN OPERATORS:
 ******************************************************************************/

void *aml_layout_column_deref(const struct aml_layout_data *data,
			      const size_t *coords)
{
	char *ptr;
	const struct aml_layout_dense *d;

	d = (const struct aml_layout_dense *) data;
	ptr = (char *) d->ptr;
	for (size_t i = 0; i < d->ndims; i++)
		ptr += coords[i]*d->cpitch[i]*d->stride[i];
	return (void *)ptr;
}

int aml_layout_column_order(const struct aml_layout_data *data)
{
	(void)data;
	return AML_LAYOUT_ORDER_COLUMN_MAJOR;
}

int aml_layout_column_dims(const struct aml_layout_data *data, size_t *dims)
{
	const struct aml_layout_dense *d;

	d = (const struct aml_layout_dense *) data;
	assert(d != NULL);
	assert(dims != NULL);
	memcpy((void *)dims, (void *)d->dims, sizeof(size_t)*d->ndims);
	return 0;
}

size_t aml_layout_dense_ndims(const struct aml_layout_data *data)
{
	const struct aml_layout_dense *d;

	d = (const struct aml_layout_dense *) data;
	return d->ndims;
}

size_t aml_layout_dense_element_size(const struct aml_layout_data *data)
{
	const struct aml_layout_dense *d;

	d = (const struct aml_layout_dense *)data;
	// element size is the pitch along the 1st dim.
	return d->cpitch[0];
}


/* Given a layout parameters (ndim, dims, stride, and cpitch), returns
 * the representation of this layout that uses the least dimensions.
 * The new parameter are returned in (new_ndims, new_dims, new_stride,
 * and new_cpitch.
 */
static void merge_dims(const size_t ndims,
		       const size_t *dims,
		       const size_t *stride,
		       const size_t *cpitch,
		       size_t *new_ndims,
		       size_t *new_dims,
		       size_t *new_stride,
		       size_t *new_cpitch)
{
	size_t dim_index = 0;
	size_t new_dim_index = 0;

	/* Greedy algorithm that tries to merge dimensions starting with the
	 * first */
	new_dims[new_dim_index] = dims[dim_index];
	new_cpitch[new_dim_index] = cpitch[dim_index];
	new_stride[new_dim_index] = stride[dim_index];
	/* While we haven't consumed all dimensions */
	while (dim_index < ndims - 1) {
		/* Check if current dimension can be merged with the next.
		 * ie: current dimension is not padded && next dimension has
		 * no stride */
		if (dims[dim_index] * stride[dim_index] * cpitch[dim_index] ==
		    cpitch[dim_index + 1] && stride[dim_index + 1] == 1) {
			new_dims[new_dim_index] *= dims[dim_index + 1];
		/* Else add a new dimension with the same characteristic
		 * as the dimensions we were trying to merge */
		} else {
			new_dim_index++;
			new_dims[new_dim_index] = dims[dim_index + 1];
			new_cpitch[new_dim_index] = cpitch[dim_index + 1];
			new_stride[new_dim_index] = stride[dim_index + 1];
		}
		dim_index++;
	}
236
	new_cpitch[new_dim_index + 1] = 0;
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	*new_ndims = new_dim_index + 1;
}


/* Try to change the indexing dimensions of a layout to the
 * given number of dimensions and dimensions. If the description
 * is incompatible with the original layout returns -AML_EINVAL.
 * Else returns AML_SUCCESS as well as new stride and cumulative pitch
 * in n_stride and n_cpitch respectively.
 */
static int reshape_dims(const struct aml_layout_dense *d,
			 const size_t ndims,
			 const size_t *dims,
			 size_t *n_stride,
			 size_t *n_cpitch)
{
	size_t m_ndims;
	size_t m_dims[d->ndims];
	size_t m_stride[d->ndims];
256
	/* for simplicity, the underlying algorithm needs one more slot */
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
	size_t m_cpitch[d->ndims + 1];

	/* First obtain a canonical representation of the layout
	 * that uses the least amount of dimensions. */
	merge_dims(d->ndims, d->dims, d->stride, d->cpitch,
		   &m_ndims, m_dims, m_stride, m_cpitch);

	size_t m_dim_index = 0;

	/* Greedy algorithm that tries to split the canonical
	 * representation into the given new dimensions starting from the
	 * first. The canonical representation is destroyed in the process. */
	n_cpitch[0] = m_cpitch[m_dim_index];
	for (size_t i = 0; i < ndims; i++) {
		/* If the new dimension perfectly fits in the current merged
		 * dimensions, then the new stride and cumulative pitch are
		 * copied from the current merged dimension. The next merged
		 * dimension becomes current. */
		if (m_dims[m_dim_index] == dims[i]) {
			n_stride[i] = m_stride[m_dim_index];
			n_cpitch[i + 1] = m_cpitch[m_dim_index + 1];
			m_dim_index++;
		/* Else if the current merged dimension can be evenly split by
		 * the new dimension, we divide the current merged dimension by
		 * the new dimension, merged stride is consumed and becomes 1
		 * and cumulative pitch is computed from the new stride and
		 * dimensions. */
		} else if (m_dims[m_dim_index] % dims[i] == 0) {
			m_dims[m_dim_index] /= dims[i];
			n_stride[i] = m_stride[m_dim_index];
			n_cpitch[i + 1] =
				n_cpitch[i] * dims[i] * m_stride[m_dim_index];
			m_stride[m_dim_index] = 1;
		/* Else the new description is incompatible. */
		} else {
			return -AML_EINVAL;
		}
	}
	return AML_SUCCESS;
}

int aml_layout_column_reshape(const struct aml_layout_data *data,
			      struct aml_layout **output,
			      size_t ndims,
			      const size_t *dims)
{
	int err;
	struct aml_layout *layout;
	const struct aml_layout_dense *d;
	size_t stride[ndims];
307
	/* for simplicity, the underlying algorithm needs one more slot */
308 309 310 311
	size_t cpitch[ndims + 1];

	d = (const struct aml_layout_dense *)data;

312 313 314
	err = aml_layout_dense_alloc(&layout, ndims);
	if (err)
		return err;
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

	err = reshape_dims(d, ndims, dims, stride, cpitch);
	if (err != AML_SUCCESS) {
		free(layout);
		return err;
	}

	aml_layout_dense_init_cpitch(layout,
				     d->ptr,
				     ndims,
				     dims,
				     stride,
				     cpitch);
	layout->ops = &aml_layout_column_ops;

	*output = layout;
	return AML_SUCCESS;
}

int aml_layout_column_slice(const struct aml_layout_data *data,
			    struct aml_layout **output,
			    const size_t *offsets,
337
			    const size_t *dims,
338 339 340 341 342
			    const size_t *strides)
{
	struct aml_layout *layout;
	const struct aml_layout_dense *d;
	void *ptr;
343
	int err;
344 345 346 347

	d = (const struct aml_layout_dense *)data;
	ptr = aml_layout_column_deref(data, offsets);

348 349 350
	err = aml_layout_dense_alloc(&layout, d->ndims);
	if (err)
		return err;
351

352
	size_t cpitch[d->ndims];
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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
	size_t new_strides[d->ndims];

	for (size_t i = 0; i < d->ndims; i++) {
		cpitch[i] = d->cpitch[i];
		new_strides[i] = strides[i] * d->stride[i];
	}

	aml_layout_dense_init_cpitch(layout,
				     ptr,
				     d->ndims,
				     dims,
				     new_strides,
				     cpitch);
	layout->ops = &aml_layout_column_ops;

	*output = layout;
	return AML_SUCCESS;
}

struct aml_layout_ops aml_layout_column_ops = {
	aml_layout_column_deref,
	aml_layout_column_deref,
	aml_layout_column_order,
	aml_layout_column_dims,
	aml_layout_column_dims,
	aml_layout_dense_ndims,
	aml_layout_dense_element_size,
	aml_layout_column_reshape,
	aml_layout_column_slice,
	aml_layout_column_slice,
};

/*******************************************************************************
 * ROW OPERATORS:
 ******************************************************************************/

void *aml_layout_row_deref(const struct aml_layout_data *data,
			   const size_t *coords)
{
	const struct aml_layout_dense *d;
	char *ptr;

	d = (const struct aml_layout_dense *)data;
	ptr = (char *) d->ptr;

	for (size_t i = 0; i < d->ndims; i++) {
		ptr +=
			coords[i] *
			d->cpitch[d->ndims - i - 1] *
			d->stride[d->ndims - i - 1];
	}
	return (void *) ptr;
}

int aml_layout_row_order(const struct aml_layout_data *data)
{
	(void) data;
	return AML_LAYOUT_ORDER_ROW_MAJOR;
}

int aml_layout_row_dims(const struct aml_layout_data *data, size_t *dims)
{
	const struct aml_layout_dense *d;

	d = (const struct aml_layout_dense *)data;
	for (size_t i = 0; i < d->ndims; i++)
		dims[i] = d->dims[d->ndims - i - 1];
	return 0;
}

int aml_layout_row_reshape(const struct aml_layout_data *data,
			   struct aml_layout **output,
			   const size_t ndims,
			   const size_t *dims)
{
	struct aml_layout *layout;
	const struct aml_layout_dense *d;
	size_t stride[ndims];
431
	/* for simplicity, the underlying algorithm needs one more slot */
432 433 434 435 436
	size_t cpitch[ndims + 1];
	size_t n_dims[ndims];
	int err;

	d = (const struct aml_layout_dense *)data;
437 438 439
	err = aml_layout_dense_alloc(&layout, ndims);
	if (err)
		return err;
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

	for (size_t i = 0; i < ndims; i++)
		n_dims[ndims - i - 1] = dims[i];

	err = reshape_dims(d, ndims, n_dims, stride, cpitch);
	if (err != AML_SUCCESS) {
		free(layout);
		return err;
	}

	aml_layout_dense_init_cpitch(layout,
				     d->ptr,
				     ndims,
				     n_dims,
				     stride,
				     cpitch);

	layout->ops = &aml_layout_row_ops;
	*output = layout;
	return AML_SUCCESS;
}

int aml_layout_row_slice(const struct aml_layout_data *data,
			 struct aml_layout **output,
			 const size_t *offsets,
465
			 const size_t *dims,
466 467 468 469 470
			 const size_t *strides)
{
	struct aml_layout *layout;
	const struct aml_layout_dense *d;
	void *ptr;
471
	int err;
472 473 474

	d = (const struct aml_layout_dense *)data;

475
	size_t cpitch[d->ndims];
476 477 478 479
	size_t n_offsets[d->ndims];
	size_t n_dims[d->ndims];
	size_t n_strides[d->ndims];

480 481 482
	err = aml_layout_dense_alloc(&layout, d->ndims);
	if (err)
		return err;
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516

	for (size_t i = 0; i < d->ndims; i++) {
		n_offsets[i] = offsets[d->ndims - i - 1];
		n_dims[i] = dims[d->ndims - i - 1];
		n_strides[i] = strides[d->ndims - i - 1];
	}

	for (size_t i = 0; i < d->ndims; i++) {
		cpitch[i] = d->cpitch[i];
		n_strides[i] *= d->stride[i];
	}

	ptr = aml_layout_column_deref(data, n_offsets);
	aml_layout_dense_init_cpitch(layout,
				     ptr,
				     d->ndims,
				     n_dims,
				     n_strides,
				     cpitch);
	layout->ops = &aml_layout_row_ops;

	*output = layout;
	return AML_SUCCESS;
}

int aml_layout_row_slice_native(const struct aml_layout_data *data,
				struct aml_layout **output,
				const size_t *offsets,
				const size_t *dims,
				const size_t *strides)
{
	struct aml_layout *layout;
	const struct aml_layout_dense *d;
	void *ptr;
517
	int err;
518 519 520

	d = (const struct aml_layout_dense *)data;

521
	size_t cpitch[d->ndims];
522 523
	size_t new_strides[d->ndims];

524 525 526
	err = aml_layout_dense_alloc(&layout, d->ndims);
	if (err)
		return err;
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550

	for (size_t i = 0; i < d->ndims; i++) {
		cpitch[i] = d->cpitch[i];
		new_strides[i] = strides[i] * d->stride[i];
	}

	ptr = aml_layout_column_deref(data, offsets);
	aml_layout_dense_init_cpitch(layout,
				     ptr,
				     d->ndims,
				     dims,
				     new_strides,
				     cpitch);
	layout->ops = &aml_layout_row_ops;
	*output = layout;

	return AML_SUCCESS;
}

struct aml_layout_ops aml_layout_row_ops = {
	aml_layout_row_deref,
	aml_layout_column_deref,
	aml_layout_row_order,
	aml_layout_row_dims,
551
	aml_layout_column_dims,
552 553 554 555 556 557 558
	aml_layout_dense_ndims,
	aml_layout_dense_element_size,
	aml_layout_row_reshape,
	aml_layout_row_slice,
	aml_layout_row_slice_native
};