aml.h 39.4 KB
Newer Older
Swann Perarnau's avatar
Swann Perarnau committed
1 2 3 4 5 6 7 8
/*******************************************************************************
 * 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
9
 ******************************************************************************/
Swann Perarnau's avatar
Swann Perarnau committed
10

11 12 13 14 15 16 17
/**
 * \file aml.h
 *
 * \brief Main AML header file, contains all high level
 * abstractions declarations.
 **/

18 19 20
#ifndef AML_H
#define AML_H 1

21
#include <assert.h>
22
#include <errno.h>
23 24 25 26 27 28 29
#include <inttypes.h>
#include <numa.h>
#include <numaif.h>
#include <pthread.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
30
#include <stdlib.h>
31 32
#include <string.h>
#include <strings.h>
33 34 35
#include <sys/mman.h>
#include <unistd.h>

36
#include "aml/utils/bitmap.h"
37
#include "aml/utils/error.h"
38
#include "aml/utils/inner-malloc.h"
39
#include "aml/utils/vector.h"
40
#include "aml/utils/version.h"
41
#include "aml/utils/features.h"
42

43
////////////////////////////////////////////////////////////////////////////////
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/**
 * @}
 * @defgroup aml "AML Library functions"
 * @brief Initialize/Finalize library.
 *
 * General functions of aml library.
 * Initialization of internal structures, cleanup of everything at the end.
 *
 * @see aml_error
 * @see aml_version
 * @{
 **/

////////////////////////////////////////////////////////////////////////////////

/**
61
 * Initializes the library.
62 63 64 65 66 67
 * @param argc: pointer to the main()'s argc argument; contents can get
 *        modified.
 * @param argv: pointer to the main()'s argv argument; contents can get
 *        modified.
 * @return 0 if successful; an error code otherwise.
 **/
68
int aml_init(int *argc, char **argv[]);
69 70

/**
71
 * Terminates the library.
72 73
 * @return 0 if successful; an error code otherwise.
 **/
74
int aml_finalize(void);
75

76
////////////////////////////////////////////////////////////////////////////////
77

78
/**
79 80 81 82
 * @}
 * @defgroup aml_area "AML Area"
 * @brief Area High-Level API
 *
83
 * AML areas represent places where data can be stored.
84
 * In shared memory systems, locality is a major concern for performance.
85 86
 * Being able to query memory from specific places is of major interest
 * to achieve this goal. AML areas provide low-level mmap() / munmap() functions
87
 * to query memory from specific places materialized as areas. Available area
88 89 90
 * implementations dictate the way such places can be arranged and their
 * properties. It is important to note that the functions provided through the
 * Area API are low-level and are not optimized for performance as allocators
91 92
 * are.
 *
93
 * @image html area.png "Illustration of areas in a complex system." width=700cm
94 95 96 97
 *
 * @see aml_area_linux
 *
 * @{
98 99
 **/

100 101 102 103 104 105
////////////////////////////////////////////////////////////////////////////////

/**
 * aml_area_data is an opaque handle defined by each aml_area
 * implementation. This not supposed to be used by end users.
 **/
106 107
struct aml_area_data;

108 109 110 111 112 113 114 115
/**
 * Opaque handle to pass additional options to area mmap hook.
 * This is implementation specific and cannot be used as a
 * generic interface but rather for customizing area behaviour
 * on per mmap basis.
 **/
struct aml_area_mmap_options;

116 117
/**
 * aml_area_ops is a structure containing implementations
118 119
 * of area operations.
 * Users may create or modify implementations by assembling
120 121
 * appropriate operations in such a structure.
 **/
122 123
struct aml_area_ops {
	/**
124
	 * Building block for coarse grain allocator of virtual memory.
125
	 *
126 127
	 * @param[in] data: Opaque handle to implementation specific data.
	 * @param[in] size: The minimum size of allocation.
128 129 130 131 132
	 *        Is greater than 0. Must not fail unless not enough
	 *        memory is available, or ptr argument does not point to a
	 *        suitable address.
	 *        In case of failure, aml_errno must be set to an appropriate
	 *        value.
Swann Perarnau's avatar
Swann Perarnau committed
133
	 * @param opts: Opaque handle to pass additional options to area
134
	 *        mmap hook. Can be NULL and must work with NULL opts.
135
	 * @return a pointer to allocated memory object.
136
	 **/
137
	void* (*mmap)(const struct aml_area_data  *data,
138 139
		      size_t                       size,
		      struct aml_area_mmap_options *opts);
140 141

	/**
142 143
	 * Building block for unmapping of virtual memory mapped with mmap()
	 * of the same area.
144
	 *
145 146 147 148 149 150
	 * @param data: An opaque handle to implementation specific data.
	 * @param ptr: Pointer to data mapped in physical memory. Cannot be
	 *        NULL.
	 * @param size: The size of data. Cannot be 0.
	 * @return: AML_AREA_* error code.
	 * @see mmap()
151
	 **/
152
	int (*munmap)(const struct aml_area_data *data,
153 154 155 156
		      void                       *ptr,
		      size_t                      size);
};

157
/**
158
 * An AML area is an implementation of memory operations for several types
159
 * of devices through a consistent abstraction.
160 161
 * This abstraction is meant to be implemented for several kinds of devices,
 * i.e., the same function calls allocate different kinds of devices depending
162 163
 * on the area implementation provided.
 **/
164
struct aml_area {
165
	/** Basic memory operations implementation **/
166
	struct aml_area_ops *ops;
167
	/** Implementation specific data. Set to NULL at creation. **/
168 169
	struct aml_area_data *data;
};
170 171

/**
172 173 174 175 176 177
 * Low-level function for obtaining memory from an area.
 * @param[in] area: A valid area implementing access to the target memory.
 * @param[in] size: The usable size of memory to obtain.
 * @param[in, out] opts: Opaque handle to pass additional options to the area.
 * @return a pointer to the memory range of the requested size allocated
 * within the area.
178 179
 * @return NULL on failure, with aml_errno set to the appropriate error
 * code.
180
 **/
181 182 183
void *aml_area_mmap(const struct aml_area        *area,
		    size_t                        size,
		    struct aml_area_mmap_options *opts);
184 185

/**
186 187 188 189 190 191
 * Releases memory region obtained with aml_area_mmap().
 * @param area: A valid area implementing access to the target memory.
 * @param ptr: A pointer to the memory obtained with aml_area_mmap()
 *        using the same "area" and "size" parameters.
 * @param size: The size of the memory region pointed to by "ptr".
 * @return 0 if successful, an error code otherwise.
192
 * @see aml_area_mmap()
193 194 195 196 197
 **/
int
aml_area_munmap(const struct aml_area *area,
		void                  *ptr,
		size_t                 size);
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
/**
 * @}
 * @defgroup aml_layout "AML Layout"
 * @brief Low level description of data orrganization at the byte granularity.
 *
 * Layout describes how contiguous element of a flat memory address space are
 * organized into a multidimensional array of elements of a fixed size.
 * The abstraction provide functions to build layouts, access elements,
 * reshape a layout, or subset a layout.
 *
 * Layouts are characterized by:
 * * A pointer to the data it describes
 * * A set of dimensions on which data spans.
 * * A stride in between elements of a dimension.
 * * A pitch indicating the space between contiguous elements of a dimension.
 *
 * The figure below describes a 2D layout with a sub-layout
 * (obtained with aml_layout_slice()) operation. The sub-layout has a stride
 * of 1 element along the second dimension. The slice has an offset of 1 element
 * along the same dimension, and its pitch is the pitch of the original
 * layout. Calling aml_layout_deref() on this sublayout with appropriate
 * coordinates will return a pointer to elements noted (coor_x, coord_y).
 * @see aml_layout_slice()
 *
 * @image html layout.png "2D layout with a 2D slice." width=400cm
 *
 * Access to specific elements of a layout can be done with
 * the aml_layout_deref() function. Access to an element is always done
 * relatively to the dimensions order set by at creation time.
 * However, internally, the library will store dimensions from the last
 * dimension to the first dimension such that elements along the first dimension
 * are contiguous in memory. This order is defined called with the value
 * AML_LAYOUT_ORDER_FORTRAN. Therefore, AML provides access to elements
 * without the overhead of user order choice through function suffixed
 * with "native".
 * @see aml_layout_deref()
 * @see aml_layout_deref_native()
 * @see aml_layout_dims_native()
 * @see aml_layout_slice_native()
 *
 * The layout abstraction also provides a function to reshape data
 * with a different set of dimensions. A reshaped layout will access
243
 * the same data but with different coordinates as depicted in the
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 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
 * figure below.
 * @see aml_layout_reshape()
 *
 * @image html reshape.png "2D layout turned into a 3D layout." width=700cm
 *
 * @see aml_layout_dense
 * @see aml_layout_pad
 * @{
 **/

////////////////////////////////////////////////////////////////////////////////

struct aml_layout_ops;
struct aml_layout_data;

/** Structure definition of AML layouts **/
struct aml_layout {
	/** Layout functions implementation **/
	struct aml_layout_ops *ops;
	/** Implementation specific data of a layout**/
	struct aml_layout_data *data;
};

/** List of operators implemented by layouts. **/
struct aml_layout_ops {
	/**
	 * Layout must provide a way to access a specific element
	 * according to the provided dimensions.
	 * Coordinates bounds checking is done in the generic API.
	 * Coordinates provided by the user will match the order
	 * Of the dimensions provided by the user in the constructor.
	 * However, dimensions are always stored internally in the
	 * AML_LAYOUT_ORDER_FORTRAN order.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @param coords[in]: The non-NULL coordinates on which to access data.
	 * Coordinates are checked to be valid in aml_layout_deref().
	 * @return A pointer to the dereferenced element on success.
	 * @return NULL on failure with aml_errno set to the error reason.
	 **/
	void *(*deref)(const struct aml_layout_data *data,
		       const size_t *coords);

	/**
	 * Function for derefencing elements of a layout inside the library.
	 * Layout assumes data is always stored in AML_LAYOUT_ORDER_FORTRAN
	 * order. Coordinates provided by the library will match the same
	 * order, i.e last dimension first.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @param coords[in]: The non-NULL coordinates on which to access data.
	 * The first coordinate should be the last dimensions and so on to the
	 * last, coordinate, last dimension.
	 * @return A pointer to the dereferenced element on success.
	 * @return NULL on failure with aml_errno set to the error reason.
	 **/
	void *(*deref_native)(const struct aml_layout_data *data,
			      const size_t *coords);

	/**
	 * Get the order in which dimensions of the layout are
	 * supposed to be accessed by the user.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @return Order value. It is a bitmask with order bit set (or not set).
	 * Output value can be further checked against order AML_LAYOUT_ORDER
	 * flags by using the macro AML_LAYOUT_ORDER() on output value.
	 * @see AML_LAYOUT_ORDER()
	 **/
	int (*order)(const struct aml_layout_data *data);

	/**
	 * Return the layout dimensions in the user order.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @param dims[out]: The non-NULL array of dimensions to fill. It is
	 * supposed to be large enough to contain ndims() elements.
	 * @return AML_SUCCESS on success, else an AML error code.
	 **/
	int (*dims)(const struct aml_layout_data *data, size_t *dims);

	/**
	 * Return the layout dimensions in the order they are actually stored
	 * in the library.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @param dims[out]: The non-NULL array of dimensions to fill. It is
	 * supposed to be large enough to contain ndims() elements.
	 * @return AML_SUCCESS on success, else an AML error code.
	 **/
	int (*dims_native)(const struct aml_layout_data *data,
			   size_t *dims);

	/**
	 * Return the number of dimensions in a layout.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @return The number of dimensions in the layout.
	 **/
	size_t (*ndims)(const struct aml_layout_data *data);

	/**
	 * Return the size of layout elements.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @return The size of elements stored with this layout.
	 **/
	size_t (*element_size)(const struct aml_layout_data *data);

	/**
	 * Reshape the layout with different dimensions.
	 * Layout dimensions are checked in aml_layout_reshape() to store
	 * the exact same number of elements.
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @param output[out]: A non NULL pointer to a layout where to allocate
	 * a new layout resulting from the reshape operation.
	 * @param ndims[in]: The number of dimensions of the new layout.
	 * @param dims[in]: The number of elements along each dimension of
	 * the new layout.
	 * @return AML_SUCCESS on success, else an AML error code (<0).
	 **/
	int (*reshape)(const struct aml_layout_data *data,
		       struct aml_layout **output,
		       const size_t ndims,
		       const size_t *dims);

	/**
	 * Return a layout that is a subset of another layout.
	 * Slice arguments compatibility with the original layout are
	 * checked in aml_layout_slice().
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @param output[out]: A non NULL pointer to a layout where to allocate
	 * a new layout resulting from the slice operation.
	 * @param dims[in]: The number of elements of the slice along each
	 * dimension .
	 * @param offsets[in]: The index of the first element of the slice
	 * in each dimension.
	 * @param strides[in]: The displacement (in number of elements) between
	 * elements of the slice.
	 * @return A newly allocated layout with the queried subset of the
	 * original layout on succes.
	 * @return NULL on error with aml_errno set to the failure reason.
	 **/
	int (*slice)(const struct aml_layout_data *data,
		     struct aml_layout **output,
		     const size_t *dims,
		     const size_t *offsets,
		     const size_t *strides);

	/**
	 * Return a layout that is a subset of another layout, assuming
	 * dimensions are stored with AML_LAYOUT_ORDER_FORTRAN.
	 * Slice arguments compatibility with the original layout are
	 * checked in aml_layout_slice().
	 * @param data[in]: The non-NULL handle to layout internal data.
	 * @param output[out]: A non NULL pointer to a layout where to allocate
	 * a new layout resulting from the slice operation.
	 * @param dims[in]: The number of elements of the slice along each
	 * dimension .
	 * @param offsets[in]: The index of the first element of the slice
	 * in each dimension.
	 * @param strides[in]: The displacement (in number of elements) between
	 * elements of the slice.
	 * @return A newly allocated layout with the queried subset of the
	 * original layout on succes.
	 * @return NULL on error with aml_errno set to the failure reason.
	 **/
	int (*slice_native)(const struct aml_layout_data *data,
			    struct aml_layout **output,
			    const size_t *dims,
			    const size_t *offsets,
			    const size_t *strides);
};

/**
 * Tag specifying user storage of dimensions inside a layout.
 * Layout order is the first bit in an integer bitmask.
 * @see AML_LAYOUT_ORDER()
 * This tag will store dimensions in the order provided by the user,
416
 * i.e., elements of the last dimension will be contiguous in memory.
417 418 419 420 421 422 423
 **/
#define AML_LAYOUT_ORDER_C (0<<0)

/**
 * Tag specifying user storage of dimensions inside a layout.
 * Layout order is the first bit in an integer bitmask.
 * @see AML_LAYOUT_ORDER()
424 425
 * This tag will store dimensions in the reverse order to the one provided
 * by the user, i.e., elements of the first dimension will be contiguous
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
 * in memory. This storage is the actual storage used by the library
 * inside the structure.
 **/
#define AML_LAYOUT_ORDER_FORTRAN (1<<0)

/**
 * This is equivalent to AML_LAYOUT_ORDER_C.
 * @see AML_LAYOUT_ORDER_C
 **/
#define AML_LAYOUT_ORDER_COLUMN_MAJOR (0<<0)

/**
 * This is equivalent to AML_LAYOUT_ORDER_FORTRAN.
 * @see AML_LAYOUT_ORDER_FORTRAN
 **/
#define AML_LAYOUT_ORDER_ROW_MAJOR (1<<0)

/**
 * Get the order bit of an integer bitmask.
 * The value can be further checked for equality
 * with AML_LAYOUT_ORDER_* values.
 * @param x: An integer with the first bit set
 * to the order value.
 * @return An integer containing only the bit order.
 **/
#define AML_LAYOUT_ORDER(x) ((x) & (1<<0))

/**
 * Dereference an element of a layout by its coordinates.
455 456
 * @param[in] layout: An initialized layout.
 * @param[in] coords: The coordinates on which to access data.
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
 * @return A pointer to the dereferenced element on success.
 * @return NULL on failure with aml_errno set to the error reason:
 * * AML_EINVAL if coordinate are out of bound
 * * See specific implementation of layout for further information
 * on possible error codes.
 **/
void *aml_layout_deref(const struct aml_layout *layout,
		       const size_t *coords);

/**
 * Equivalent to aml_layout_deref() but with bound checking
 * on coordinates.
 * @see aml_layout_deref()
 **/
void *aml_layout_deref_safe(const struct aml_layout *layout,
			    const size_t *coords);

/**
 * Get the order in which dimensions of the layout are supposed to be
 * accessed by the user.
477
 * @param[in] layout: An initialized layout.
478 479 480
 * @return The order (>0) on success, an AML error (<0) on failure.
 * @return On success, a bitmask with order bit set (or not set).
 * Output value can be further checked against order AML_LAYOUT_ORDER
481
 * flags by using the macro AML_LAYOUT_ORDER() on the output value.
482 483 484 485 486 487
 * @see AML_LAYOUT_ORDER()
 **/
int aml_layout_order(const struct aml_layout *layout);

/**
 * Return the layout dimensions in the user order.
488 489
 * @param[in] layout: An initialized layout.
 * @param[out] dims: A non-NULL array of dimensions to fill. It is
490
 * supposed to be large enough to contain aml_layout_ndims() elements.
491 492 493 494 495 496
 * @return AML_SUCCESS on success, else an AML error code.
 **/
int aml_layout_dims(const struct aml_layout *layout, size_t *dims);

/**
 * Return the number of dimensions in a layout.
497
 * @param[in] layout: An initialized layout.
498 499 500 501 502 503
 * @return The number of dimensions in the layout.
 **/
size_t aml_layout_ndims(const struct aml_layout *layout);

/**
 * @brief Return the size of layout elements.
504
 * @param[in] layout: An initialized layout.
505 506 507 508 509 510 511 512 513 514
 * @return The size of elements stored with this layout.
 **/
size_t aml_layout_element_size(const struct aml_layout *layout);

/**
 * @brief Reshape the layout with different dimensions.
 * This function checks that the number of elements of
 * the reshaped layout matches the number of elements
 * in the original layout. Additional constraint may apply
 * depending on the layout implementation.
515 516
 * @param[in] layout: An initialized layout.
 * @param[out] reshaped_layout: A newly allocated layout
517
 * with the queried shape on succes.
518 519
 * @param[in] ndims: The number of dimensions of the new layout.
 * @param[in] dims: The number of elements along each dimension of
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
 * the new layout.
 * @return AML_SUCCESS on success.
 * @return AML_EINVAL if reshape dimensions are not compatible
 * with original layout dimensions.
 * @return AML_ENOMEM if AML failed to allocate the new structure.
 * @return Another aml_error code. Refer to the layout
 * implementation of reshape function.
 **/
int aml_layout_reshape(const struct aml_layout *layout,
		       struct aml_layout **reshaped_layout,
		       const size_t ndims,
		       const size_t *dims);

/**
 * Return a layout that is a subset of another layout.
 * The number of elements to subset along each dimension
 * must be compatible with offsets and strides.
537 538
 * This function checks that the number of elements along
 * each dimension of the slice actually fits in the original
539
 * layout.
540 541
 * @param[in] layout: An initialized layout.
 * @param[out] reshaped_layout: a pointer where to store the address of a
542 543
 * newly allocated layout with the queried subset of the
 * original layout on succes.
544
 * @param[in] dims: The number of elements of the slice along each
545
 * dimension.
546
 * @param[in] offsets: The index of the first element of the slice
547
 * in each dimension. If NULL, offset is set to 0.
548
 * @param[in] strides: The displacement (in number of elements) between
549 550 551 552 553 554 555 556 557 558 559
 * elements of the slice. If NULL, stride is set to 1.
 * @return AML_SUCCESS on success, else an AML error code (<0).
 **/
int aml_layout_slice(const struct aml_layout *layout,
		     struct aml_layout **reshaped_layout,
		     const size_t *dims,
		     const size_t *offsets,
		     const size_t *strides);

////////////////////////////////////////////////////////////////////////////////

560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
/**
 * @}
 * @defgroup aml_tiling "AML Tiling"
 * @brief Tiling Data Structure High-Level API
 *
 * Tiling is a representation of the decomposition of data structures. It
 * identifies ways a layout can be split into layouts of smaller size. As such,
 * the main function of a tiling is to provide an index into subcomponents of a
 * layout. Implementations focus on the ability to provide sublayouts of
 * different sizes at the corners, and linearization of the index range.
 * @{
 **/

////////////////////////////////////////////////////////////////////////////////

/**
 * Tag specifying user storage of dimensions inside a layout.
 * Layout order is the first bit in an integer bitmask.
 * @see AML_TILING_ORDER()
 * This tag will store dimensions in the order provided by the user,
 * i.e elements of the last dimension will be contiguous in memory.
 **/
#define AML_TILING_ORDER_C (0<<0)

/**
 * Tag specifying user storage of dimensions inside a layout.
 * Layout order is the first bit in an integer bitmask.
 * @see AML_TILING_ORDER()
 * This tag will store dimensions in the reversed order provided
 * by the user, i.e elements of the first dimension will be contiguous
 * in memory. This storage is the actual storage used by the library
 * inside the structure.
 **/
#define AML_TILING_ORDER_FORTRAN (1<<0)

/**
 * This is equivalent to AML_TILING_ORDER_C.
 * @see AML_TILING_ORDER_C
 **/
#define AML_TILING_ORDER_COLUMN_MAJOR (0<<0)

/**
 * This is equivalent to AML_TILING_ORDER_FORTRAN.
 * @see AML_TILING_ORDER_FORTRAN
 **/
#define AML_TILING_ORDER_ROW_MAJOR (1<<0)

/**
 * Get the order bit of an integer bitmask.
 * The value can be further checked for equality
 * with AML_TILING_ORDER_* values.
 * @param x: An integer with the first bit set
 * to the order value.
 * @return An integer containing only the bit order.
 **/
#define AML_TILING_ORDER(x) ((x) & (1<<0))

/**
 * aml_tiling_data is an opaque handle defined by each aml_tiling
 * implementation. This not supposed to be used by end users.
 **/
struct aml_tiling_data;

/**
 * aml_tiling_ops is a structure containing a set of operation
 * over a tiling. These operations focus on:
 * - retrieving a tile
 * - getting information about the size of tiles and the tiling itself.
 **/
struct aml_tiling_ops {
	/** retrieve a tile as a layout **/
	struct aml_layout* (*index)(const struct aml_tiling_data *t,
				    const size_t *coords);
	/** retrieve a tile as a layout with coordinates in native order  **/
	struct aml_layout* (*index_native)(const struct aml_tiling_data *t,
					   const size_t *coords);
	int (*tileid)(const struct aml_tiling_data *t, const size_t *coords);
	int (*order)(const struct aml_tiling_data *t);
	int (*tile_dims)(const struct aml_tiling_data *t, size_t *dims);
	int (*dims)(const struct aml_tiling_data *t, size_t *dims);
	int (*dims_native)(const struct aml_tiling_data *t, size_t *dims);
	size_t (*ndims)(const struct aml_tiling_data *t);
	size_t (*ntiles)(const struct aml_tiling_data *t);
};

/**
 **/
struct aml_tiling {
	/** @see aml_tiling_ops **/
	struct aml_tiling_ops *ops;
	/** @see aml_tiling_data **/
	struct aml_tiling_data *data;
};

/**
 * Get the order in which dimensions of the tiling are supposed to be
 * accessed by the user.
657
 * @param[in] tiling: An initialized tiling.
658 659
 * @return The order (>0) on success, an AML error (<0) on failure.
 * @return On success, a bitmask with order bit set (or not set).
660 661 662
 * Output value can be further checked against order AML_TILING_ORDER
 * flags by using the macro AML_TILING_ORDER() on the output value.
 * @see AML_TILING_ORDER()
663 664 665 666 667
 **/
int aml_tiling_order(const struct aml_tiling *tiling);

/**
 * Return the tiling dimensions in the user order.
668 669
 * @param[in] tiling: An initialized tiling.
 * @param[out] dims: A non-NULL array of dimensions to fill. It is
670
 * supposed to be large enough to contain aml_tiling_ndims() elements.
671 672 673 674 675 676
 * @return AML_SUCCESS on success, else an AML error code.
 **/
int aml_tiling_dims(const struct aml_tiling *tiling, size_t *dims);

/**
 * Return the dimensions of a tile in the tiling, in the user order.
677 678
 * @param[in] tiling: An initialized tiling.
 * @param[out] dims: A non-NULL array of dimensions to fill. It is
679
 * supposed to be large enough to contain aml_tiling_ndims() elements.
680 681 682 683 684 685 686
 * @return AML_SUCCESS on success, else an AML error code.
 **/
int aml_tiling_tile_dims(const struct aml_tiling *tiling, size_t *dims);

/**
 * Provide the number of dimensions in a tiling.
 * @param tiling: an initialized tiling structure.
687
 * @return the number of dimensions in the tiling.
688 689 690 691 692 693
 **/
size_t aml_tiling_ndims(const struct aml_tiling *tiling);

/**
 * Provide the number of tiles in a tiling.
 * @param tiling: an initialized tiling structure.
694
 * @return the number of tiles in the tiling.
695
 **/
696
size_t aml_tiling_ntiles(const struct aml_tiling *tiling);
697 698 699 700 701 702 703

/**
 * Return the tile at specified coordinates in the tiling
 * @param tiling: an initialized tiling
 * @param coords: the coordinates for the tile
 * @return the tile as a layout on success, NULL on error.
 **/
704
struct aml_layout *aml_tiling_index(const struct aml_tiling *tiling,
705 706 707 708 709 710 711 712
				    const size_t *coords);

/**
 * Return a unique identifier for a tile based on coordinates in the tiling
 * @param tiling: an initialized tiling
 * @param coords: the coordinates for the tile
 * @return a uuid for the tile.
 */
713
int aml_tiling_tileid(const struct aml_tiling *tiling, const size_t *coords);
714 715 716 717 718 719 720

/**
 * Return the tile with this identifier
 * @param tiling: an initialized tiling
 * @param uuid: a unique identifier for this tile
 * @return the tiling as a layout on success, NULL on error.
 */
721 722
struct aml_layout *aml_tiling_index_byid(const struct aml_tiling *tiling,
					 const int uuid);
723 724 725 726


////////////////////////////////////////////////////////////////////////////////

727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
/**
 * @}
 * @defgroup aml_dma "AML DMA"
 * @brief Management of low-level memory movements.
 *
 * AML DMA (inspired by Direct Memory Access engines) is an abstraction over the
 * ability to move data between places. A DMAs presents an interface that allows
 * clients to create an asynchronous request to move data and to wait for this
 * request to complete. Depending on the exact operation it is configured to do,
 * the DMA might transform the data during the operation.
 *
 * Implementations are mostly responsible for providing access to various types
 * of execution engine for data movement itself.
 *
 * @image html dma.png width=600
 * @{
 **/

////////////////////////////////////////////////////////////////////////////////

/**
 * Internal macros used for tracking DMA request types.
 * Invalid request type.  Used for marking inactive requests in the vector.
 **/
#define AML_DMA_REQUEST_TYPE_INVALID -1

/**
 * The request is in the format (dest layout, src layout)
 **/
#define AML_DMA_REQUEST_TYPE_LAYOUT 0

/**
 * aml_dma is mainly used to asynchronously move data.
 * aml_dma_request is an opaque structure containing information
 * about ongoing request for data movement in a dma operation.
 * @see aml_dma_ops
 * @see aml_dma_async_copy()
 **/
struct aml_dma_request;

/**
 * Opaque handle implemented by each aml_dma implementations.
 * Should not be used by end-users.
 **/
struct aml_dma_data;

773 774 775 776
/**
 * Type of the function used to perform the DMA between two layouts.
 * @param dst: destination layout
 * @param src: source layout
777
 * @param arg: extra argument needed by the operator
778 779
 **/
typedef int (*aml_dma_operator)(struct aml_layout *dst,
780
				const struct aml_layout *src, void *arg);
781

782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
/**
   aml_dma_ops is a structure containing operations for a specific
   * aml_dma implementation.
   * These operation are operation are detailed in the structure.
   * They are specific in:
   * - the type of aml_area source and destination,
   * - the progress engine performing the operation,
   * - the type of of source and destination data structures.
   *
   * Each different combination of these three points may require a different
   * set of dma operations.
   **/
struct aml_dma_ops {
	/**
	 * Initiate a data movement, from a source pointer to a destination
	 * pointer, and output a request handler for managing the transfer.
	 * @param dma: dma_implementation internal data.
	 * @param req[out]: the request handle to manage termination
	 *        of the movement.
801 802
	 * @param dest: layout describing the destination.
	 * @param src: layout describing the source.
803 804 805 806
	 * @return an AML error code.
	 **/
	int (*create_request)(struct aml_dma_data *dma,
			      struct aml_dma_request **req,
807
			      struct aml_layout *dest,
808
			      struct aml_layout *src,
809
			      aml_dma_operator op, void *op_arg);
810 811 812 813 814 815 816 817 818 819

	/**
	 * Destroy the request handle. If the data movement is still ongoing,
	 * then cancel it.
	 *
	 * @param dma: dma_implementation internal data.
	 * @param req: the request handle to manage termination of the movement.
	 * @return an AML error code.
	 **/
	int (*destroy_request)(struct aml_dma_data *dma,
Swann Perarnau's avatar
Swann Perarnau committed
820
			       struct aml_dma_request **req);
821 822 823 824 825 826 827 828 829 830

	/**
	 * Wait for termination of a data movement and destroy the request
	 * handle.
	 *
	 * @param dma: dma_implementation internal data.
	 * @param req: the request handle to manage termination of the movement.
	 * @return an AML error code.
	 **/
	int (*wait_request)(struct aml_dma_data *dma,
Swann Perarnau's avatar
Swann Perarnau committed
831
			    struct aml_dma_request **req);
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
};

/**
 * aml_dma is an abstraction for (asynchronously) moving data
 * from one area to another. The implementation of dma to use
 * is depends on the source and destination areas. The appropriate
 * dma choice is delegated to the user.
 * @see struct aml_area.
 **/
struct aml_dma {
	/** @see aml_dma_ops **/
	struct aml_dma_ops *ops;
	/** @see aml_dma_data **/
	struct aml_dma_data *data;
};

/**
 * Requests a synchronous data copy between two different buffers.
 * @param dma: an initialized DMA structure.
851 852
 * @param dest: layout describing the destination.
 * @param src: layout describing the source.
853
 * @param op: optional custom operator for this dma
854
 * @param op_arg: optional argument to the operator
855 856
 * @return 0 if successful; an error code otherwise.
 **/
857
int aml_dma_copy_custom(struct aml_dma *dma, struct aml_layout *dest,
858
		 struct aml_layout *src, aml_dma_operator op, void *op_arg);
859 860 861 862 863 864 865

/**
 * Requests a data copy between two different buffers.This is an asynchronous
 * version of aml_dma_copy().
 * @param dma: an initialized DMA structure.
 * @param req: an address where the pointer to the newly assigned DMA request
 *        will be stored.
866 867
 * @param dest: layout describing the destination.
 * @param src: layout describing the source.
868
 * @param op: optional custom operator for this dma
869
 * @param op_arg: optional argument to the operator
870 871
 * @return 0 if successful; an error code otherwise.
 **/
872
int aml_dma_async_copy_custom(struct aml_dma *dma, struct aml_dma_request **req,
873
		       struct aml_layout *dest,
874
		       struct aml_layout *src,
875
		       aml_dma_operator op, void *op_arg);
876

877
#define aml_dma_copy(dma, d, s) aml_dma_copy_custom(dma, d, s, NULL, NULL)
878
#define aml_dma_async_copy(dma, r, d, s) \
879
	aml_dma_async_copy_custom(dma, r, d, s, NULL, NULL)
880 881 882 883 884 885 886

/**
 * Waits for an asynchronous DMA request to complete.
 * @param dma: an initialized DMA structure.
 * @param req: a DMA request obtained using aml_dma_async_*() calls.
 * @return 0 if successful; an error code otherwise.
 **/
Swann Perarnau's avatar
Swann Perarnau committed
887
int aml_dma_wait(struct aml_dma *dma, struct aml_dma_request **req);
888 889 890 891 892 893 894

/**
 * Tears down an asynchronous DMA request before it completes.
 * @param dma: an initialized DMA structure.
 * @param req: a DMA request obtained using aml_dma_async_*() calls.
 * @return 0 if successful; an error code otherwise.
 **/
Swann Perarnau's avatar
Swann Perarnau committed
895
int aml_dma_cancel(struct aml_dma *dma, struct aml_dma_request **req);
896 897 898

/**
 * Generic helper to copy from one layout to another.
899 900 901
 * @param[out] dst: destination layout
 * @param[in] src: source layout
 * @param[in] arg: unused (should be NULL)
902 903
 */
int aml_copy_layout_generic(struct aml_layout *dst,
904
			    const struct aml_layout *src, void *arg);
905 906 907 908


////////////////////////////////////////////////////////////////////////////////

909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
/**
 * @}
 * @defgroup aml_scratch "AML Scratchpad"
 * @brief Stage-in, Stage-out High Level Abstraction.
 *
 * Scratchpad in an abstraction fro moving data back and forth from
 * a data representation in an area to another data representation in another
 * areas. This is especially usefull from moving to user data representation
 * to an architecure optimized representation for heavy computational work,
 * then returning the to user representation.
 * Data movement is performed with two dma engines from one area and tiling to
 * another area and tiling.
 *
 * @image html scratch.png width=600
 * @see aml_dma
 * @{
 **/

////////////////////////////////////////////////////////////////////////////////

/**
 * Scratch is mainly used to asynchronously move data back and forth between
 * two areas. aml_scratch_request is an opaque structure containing information
 * about ongoing requests for data movement.
 **/
934
struct aml_scratch_request;
935 936 937 938 939

/**
 * Opaque handle implemented by each scratches implementation.
 * Should not be used by end users.
 **/
940 941
struct aml_scratch_data;

942 943 944 945
/**
 * Scratchpad request types.
 * Invalid request type.  Used for marking inactive requests in the vector.
 **/
946
#define AML_SCRATCH_REQUEST_TYPE_INVALID -1
947 948 949 950 951

/**
 * Scratchpad request types.
 * Push from the scratchpad to regular memory.
 **/
952
#define AML_SCRATCH_REQUEST_TYPE_PUSH 0
953 954 955 956 957

/**
 * Scratchpad request types.
 * Pull from regular memory to the scratchpad.
 **/
958
#define AML_SCRATCH_REQUEST_TYPE_PULL 1
959 960 961 962 963

/**
 * Scratchpad request types.
 * No-op/empty request
 **/
964
#define AML_SCRATCH_REQUEST_TYPE_NOOP 2
965

966 967 968 969 970 971 972 973 974
/**
 * aml_scratch_ops contain a scratch implementation specific operations.
 * These operations implementation may vary depending on the source and
 * destination of data, and thus scratch implementations use different
 * operations.
 * Aware users may create or modify implementation by assembling
 * appropriate operations in such a structure.
 * @see struct aml_scratch
 **/
975
struct aml_scratch_ops {
976 977 978
	/**
	 * \todo Doc
	 **/
979 980
	int (*create_request)(struct aml_scratch_data *scratch,
			      struct aml_scratch_request **req, int type,
981 982
			      struct aml_layout **dest, int *destid,
			      struct aml_layout *src, int srcid);
983 984 985
	/**
	 * \todo Doc
	 **/
986 987
	int (*destroy_request)(struct aml_scratch_data *scratch,
			       struct aml_scratch_request *req);
988 989 990
	/**
	 * \todo Doc
	 **/
991 992
	int (*wait_request)(struct aml_scratch_data *scratch,
			    struct aml_scratch_request *req);
993 994 995
	/**
	 * \todo Doc
	 **/
996
	int (*release)(struct aml_scratch_data *scratch, int scratchid);
997 998
};

999 1000 1001 1002 1003 1004
/**
 * An aml_scratch is abstraction aimed toward temporary use of a data structures
 * in a different area than the one where data currently resides. Scratches in
 * AML take care of asynchornously allocating and moving the data back and forth
 * between areas.
 **/
1005
struct aml_scratch {
1006
	/** @see aml_scratch_ops **/
1007
	struct aml_scratch_ops *ops;
1008
	/** @see aml_scratch_data **/
1009 1010 1011
	struct aml_scratch_data *data;
};

1012
/**
1013
 * Requests a synchronous pull from regular memory to the scratchpad.
1014
 * @param scratch: an initialized scratchpad structure.
1015
 * @param dest: destination layout (on the scratch)
1016 1017
 * @param scratchid: an argument of type int*; gets filled with the scratch tile
 *        identifier where the data will be pulled into.
1018
 * @param src: the source layout.
1019 1020 1021 1022
 * @param srcid: an argument of type int; the source tile identifier.
 * @see aml_scratch_baseptr()
 * @return 0 if successful; an error code otherwise.
 **/
1023 1024 1025
int aml_scratch_pull(struct aml_scratch *scratch,
		     struct aml_layout **dest, int *scratchid,
		     struct aml_layout *src, int srcid);
1026 1027 1028

/**
 * Requests a pull from regular memory to the scratchpad. This is an
1029
 * asynchronous version of aml_scratch_pull().
1030 1031 1032
 * @param scratch: an initialized scratchpad structure.
 * @param req: an address where the pointer to the newly assigned scratch
 *        request will be stored.
1033 1034 1035 1036 1037
 * @param scratch_layout: the layout on the scratch
 * @param scratchid: an argument of type int*; gets filled with the scratch tile
 *        identifier where the data will be pulled into.
 * @param src_layout: the source layout to pull.
 * @param srcid: an argument of type int; the source tile identifier.
1038 1039 1040
 * @return 0 if successful; an error code otherwise.
 * @see aml_scratch_pull()
 **/
1041
int aml_scratch_async_pull(struct aml_scratch *scratch,
1042 1043 1044
			   struct aml_scratch_request **req,
			   struct aml_layout **scratch_layout, int *scratchid,
			   struct aml_layout *src_layout, int srcid);
1045
/**
1046
 * Requests a synchronous push from the scratchpad to regular memory.
1047
 * @param scratch: an initialized scratchpad structure.
1048 1049 1050 1051 1052
 * @param dest_layout: the destination layout
 * @param destid: an argument of type int*; gets filled with the destination
 * tile identifier where the data will be pushed into (and where it was pulled
 * from in the first place).
 * @param scratch_layout: the source layout on the scratch
1053 1054 1055 1056
 * @param scratchid: an argument of type int; the scratchpad tile identifier.
 * @return 0 if successful; an error code otherwise.
 * @see aml_scratch_baseptr()
 **/
1057 1058 1059
int aml_scratch_push(struct aml_scratch *scratch,
		     struct aml_layout **dest_layout, int *destid,
		     struct aml_layout *scratch_layout, int scratchid);
1060 1061

/**
1062 1063
 * Requests a push from the scratchpad to regular memory.  This is an
 * asynchronous version of aml_scratch_push().
1064 1065 1066
 * @param scratch: an initialized scratchpad structure.
 * @param req: an address where the pointer to the newly assigned scratch
 *        request will be stored.
1067 1068 1069 1070 1071 1072
 * @param dest_layout: the destination layout
 * @param destid: an argument of type int*; gets filled with the destination
 * tile identifier where the data will be pushed into (and where it was pulled
 * from in the first place).
 * @param scratch_layout: the source layout on the scratch
 * @param scratchid: an argument of type int; the scratchpad tile identifier.
1073 1074 1075
 * @return 0 if successful; an error code otherwise.
 * @see aml_scratch_push()
 **/
1076
int aml_scratch_async_push(struct aml_scratch *scratch,
1077 1078 1079
			   struct aml_scratch_request **req,
			   struct aml_layout **dest_layout, int *destid,
			   struct aml_layout *scratch_layout, int scratchid);
1080
/**
1081
 * Waits for an asynchronous scratch request to complete.
1082 1083 1084 1085
 * @param scratch: an initialized scratchpad structure.
 * @param req: a scratch request obtained using aml_scratch_async_*() calls.
 * @return 0 if successful; an error code otherwise.
 **/
1086 1087
int aml_scratch_wait(struct aml_scratch *scratch,
		     struct aml_scratch_request *req);
1088

1089
/**
1090
 * Tears down an asynchronous scratch request before it completes.
1091 1092 1093 1094
 * @param scratch: an initialized scratchpad structure.
 * @param req: a scratch request obtained using aml_scratch_async_*() calls.
 * @return 0 if successful; an error code otherwise.
 **/
1095 1096
int aml_scratch_cancel(struct aml_scratch *scratch,
		       struct aml_scratch_request *req);
1097
/**
1098
 * Provides the location of the scratchpad.
1099 1100 1101
 * @param scratch: an initialized scratchpad structure.
 * @return a base pointer to the scratchpad memory buffer.
 **/
1102
void *aml_scratch_baseptr(const struct aml_scratch *scratch);
1103

1104
/**
1105
 * Release a scratch tile for immediate reuse.
1106 1107 1108 1109
 * @param scratch: an initialized scratchpad structure.
 * @param scratchid: a scratchpad tile identifier.
 * @return 0 if successful; an error code otherwise.
 **/
1110 1111
int aml_scratch_release(struct aml_scratch *scratch, int scratchid);

1112 1113 1114 1115 1116 1117
////////////////////////////////////////////////////////////////////////////////

/**
 * @}
 **/

1118
#endif