TensorBolt
Macros | Typedefs | Enumerations | Functions
ndarray.h File Reference

File containing ndarray aka tensors metadata. This file can be extended to support multiple backends. More...

#include <stdint.h>
#include <stdarg.h>
Include dependency graph for ndarray.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define __FUNCTION_NAME__   __func__
 
#define TB_ASSERT_LOG
 
#define ASSERT(c, msg, ...)   nda_assert(c, #c, __FUNCTION_NAME__ , msg, ##__VA_ARGS__)
 
#define TB_FLOAT   0
 
#define TB_DOUBLE   1
 
#define TB_TYPE   TB_FLOAT
 

Typedefs

typedef float tb_float
 
typedef enum NDArrayLocation NDArrayLocation
 

Enumerations

enum  NDArrayLocation { NDA_LOC_HOST_MEM =0, NDA_LOC_DEVICE_MEM =0 }
 

Functions

void nda_ShapeStackInit (struct NDShapeStack *stack, struct NDShape *shape)
 Unline other factory pattern object, the stack is usually allocated on the stack (in contrast to heap) So we do need dynamically allocate it, this function only initializes the values of a fresh allocated stack. More...
 
uint8_t nda_ShapeStackCanPop (struct NDShapeStack *stack)
 Checks if a shape stack can still popped further more. More...
 
uint64_t nda_ShapeStackPop (struct NDShapeStack *stack)
 Pops the next element in the stack, does not modify the shape but updates the index in the stack. More...
 
struct NDShapenda_newShape (uint64_t rank,...)
 
struct NDShapenda_newShapeFromArray (uint64_t rank, uint64_t *dims)
 
struct NDShapenda_newShapeFromArrayCopy (uint64_t rank, uint64_t *dims)
 
void nda_debugShape (struct NDShape *shape)
 Prints tensor shape to stdout. More...
 
struct NDShapenda_copyShape (struct NDShape *shape)
 Creates and Allocates a shape with the same properties of the given one. More...
 
uint8_t nda_shapeCanBroadCast (struct NDShape *shape1, struct NDShape *shape2)
 Verifies if two shapes can be broadcasted Broadcast verifications follows numpy rules: More...
 
char * nda_shapeToString (struct NDShape *shape)
 Generate a string representation of the shape, which can be used for debugging or generating errors. More...
 
void nda_debugValue (struct NDArray *tensor)
 Prints tensor value to stdout. More...
 
uint64_t nda_getTotalSize (struct NDShape *shape)
 Calculate the total number of elements in a tensor shape. More...
 
struct NDArraynda_alloc (struct NDShape *shape)
 Creates an empty zeroed tensor. More...
 
struct NDArraynda_randomNormal (struct NDShape *shape, float mu, float sig)
 Creates an array from a Guassian Distribution. More...
 
struct NDArraynda_linspace (tb_float a, tb_float b, uint64_t n)
 Return evenly spaced numbers over a specified interval. More...
 
struct NDArraynda_ones (struct NDShape *shape)
 Creates an One initialized tensor. More...
 
struct NDArraynda_fill (struct NDShape *shape, tb_float value)
 Creates an array initialized with one value. More...
 
struct NDArraynda_copy (struct NDArray *x)
 copies an existent tensor, memory must be explicitly freed. More...
 
void nda_reshape (struct NDArray *x, struct NDShape *shape)
 reshape an ndarray, old shape is freed. More...
 
void nda_free (struct NDArray *array)
 frees an NDArray alongside its shape More...
 
tb_float nda_get (struct NDArray *array, uint64_t *index)
 Returns the value of an array. More...
 
struct NDArraynda_slice (struct NDArray *array, uint64_t *index)
 Returns a slice of an array. More...
 
tb_float nda_vget (struct NDArray *array, uint64_t *index, struct NDShape *vshape)
 Returns the value of array throughout a virtual shape. The virtual shape has the same size of the original, but padded with ones to match another shape during broadcasting. If an index has a value higher than the padded virtual shape, the index 0 is then used. #deal_with_it. More...
 
tb_float nda_get1D (struct NDArray *array, uint64_t index)
 Returns the value of an array through 1d Inde. More...
 
tb_float nda_vget1D (struct NDArray *array, uint64_t index)
 Returns the value of array throughout a 1D index. If the index has a value higher than the original size, mod is used. More...
 

Detailed Description

File containing ndarray aka tensors metadata. This file can be extended to support multiple backends.

File containing ndarray standard backend implementation aka tensors metadata.

Author
Soulaymen Chouri
Date
March 16 2019

Typedef Documentation

◆ NDArrayLocation

Array location

Enumeration Type Documentation

◆ NDArrayLocation

Array location

Enumerator
NDA_LOC_HOST_MEM 

Array is located on the host memory (RAM)

NDA_LOC_DEVICE_MEM 

Array is located on the device memory (global memory)

Function Documentation

◆ nda_alloc()

struct NDArray* nda_alloc ( struct NDShape shape)

Creates an empty zeroed tensor.

Parameters
shapeinitial shape
Returns
0 initialized tensor

◆ nda_copy()

struct NDArray* nda_copy ( struct NDArray x)

copies an existent tensor, memory must be explicitly freed.

Parameters
[in]xtensor to copy
Returns
copy of x, must be explicitly freed

◆ nda_copyShape()

struct NDShape* nda_copyShape ( struct NDShape shape)

Creates and Allocates a shape with the same properties of the given one.

Parameters
shapeShape to copy
Returns
New allocated shape

◆ nda_debugShape()

void nda_debugShape ( struct NDShape shape)

Prints tensor shape to stdout.

Parameters
[in]shapeTensorShape to display

◆ nda_debugValue()

void nda_debugValue ( struct NDArray tensor)

Prints tensor value to stdout.

Parameters
[in]tensorTensor to display

◆ nda_fill()

struct NDArray* nda_fill ( struct NDShape shape,
tb_float  value 
)

Creates an array initialized with one value.

Parameters
shape[in]initial shape
value[in]value to initialize the array with
Returns
Array whos elements are initialized by value

◆ nda_free()

void nda_free ( struct NDArray array)

frees an NDArray alongside its shape

Parameters
[in/out]array data to free

◆ nda_get()

tb_float nda_get ( struct NDArray array,
uint64_t *  index 
)

Returns the value of an array.

Parameters
[in]arrayNDArray to access
[in]indexArray of dims of the element, len(index) must be equal to to the rank of the array
Returns
array[index]

◆ nda_get1D()

tb_float nda_get1D ( struct NDArray array,
uint64_t  index 
)

Returns the value of an array through 1d Inde.

Parameters
[in]arrayNDArray to access
[in]index1D index to fetch
Returns
array[index]

◆ nda_getTotalSize()

uint64_t nda_getTotalSize ( struct NDShape shape)

Calculate the total number of elements in a tensor shape.

Parameters
[in]shapeTensor shape to process
Returns
Total number of elements in the array (Product of dimensions)

◆ nda_linspace()

struct NDArray* nda_linspace ( tb_float  a,
tb_float  b,
uint64_t  n 
)

Return evenly spaced numbers over a specified interval.

Parameters
[in]astart element
[in]bend element
[in]nnumber of elements
Returns
evenly spaced numbers over the specified interval.

◆ nda_newShape()

struct NDShape* nda_newShape ( uint64_t  rank,
  ... 
)

Creates a new shape from the given elements dimensions

Parameters
[in]ranknumber of dimensions
[in]...list of dimensions (variadic parameters)
Returns
new NDShape

◆ nda_newShapeFromArray()

struct NDShape* nda_newShapeFromArray ( uint64_t  rank,
uint64_t *  dims 
)

Creates a new shape from the given elements dimensions as array

Parameters
[in]ranknumber of dimensions
[in]arrayof dims, should be dynamically allocated as it will be freed when the shape is destroyed. len(array) must be equal to rank
Returns
new NDShape

◆ nda_newShapeFromArrayCopy()

struct NDShape* nda_newShapeFromArrayCopy ( uint64_t  rank,
uint64_t *  dims 
)

Creates a new shape from the given elements dimensions as array

Parameters
[in]ranknumber of dimensions
[in]arrayof dims, this array will be copied and the copy will be assigned to the new shape. len(array) must be equal to rank
Returns
new NDShape

◆ nda_ones()

struct NDArray* nda_ones ( struct NDShape shape)

Creates an One initialized tensor.

Parameters
shape[in]initial shape
Returns
1-initialized tensor

◆ nda_randomNormal()

struct NDArray* nda_randomNormal ( struct NDShape shape,
float  mu,
float  sig 
)

Creates an array from a Guassian Distribution.

Parameters
shapeinitial shape
[in]muGussian's Mu parameters
[in]sigGuaussian\s Sigma parameter
Returns
randomly initialized array

◆ nda_reshape()

void nda_reshape ( struct NDArray x,
struct NDShape shape 
)

reshape an ndarray, old shape is freed.

Parameters
[in/out]x ndarray to reshape
[in]shapenew shape

◆ nda_shapeCanBroadCast()

uint8_t nda_shapeCanBroadCast ( struct NDShape shape1,
struct NDShape shape2 
)

Verifies if two shapes can be broadcasted Broadcast verifications follows numpy rules:

  1. If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded with ones on its leading (left) side.
  2. If the shape of the two arrays does not match in any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  3. If in any dimension the sizes disagree and neither is equal to 1, an error is raised.
Parameters
[in]shape1Shape of the first array
[in]shape2Shape of the second array
Returns
true if shapes can be broadcasted, false otherwise.

◆ nda_ShapeStackCanPop()

uint8_t nda_ShapeStackCanPop ( struct NDShapeStack stack)

Checks if a shape stack can still popped further more.

Parameters
[in]stackStack to check
Returns
Boolean, true if the stack can be popped further more, false otherwise.

◆ nda_ShapeStackInit()

void nda_ShapeStackInit ( struct NDShapeStack stack,
struct NDShape shape 
)

Unline other factory pattern object, the stack is usually allocated on the stack (in contrast to heap) So we do need dynamically allocate it, this function only initializes the values of a fresh allocated stack.

Parameters
[in/out]stack Allocated stack to initialize
[in]shapeShape to bind to the stack

◆ nda_ShapeStackPop()

uint64_t nda_ShapeStackPop ( struct NDShapeStack stack)

Pops the next element in the stack, does not modify the shape but updates the index in the stack.

Parameters
[in/out]stack Stack from which to pop the element
Returns
Dimension of the shape in the stack index

◆ nda_shapeToString()

char* nda_shapeToString ( struct NDShape shape)

Generate a string representation of the shape, which can be used for debugging or generating errors.

Parameters
[in]shapeNDShape to process
Returns
string representation of the shape

◆ nda_slice()

struct NDArray* nda_slice ( struct NDArray array,
uint64_t *  index 
)

Returns a slice of an array.

Parameters
[in]arrayNDArray to access
[in]indexArray of slices of the elements, must be in the format start_dim_1, end_dim_1, start_dim_2, end_dim_2, ... start_dim_n, end_dim_n
Returns
array[index]

◆ nda_vget()

tb_float nda_vget ( struct NDArray array,
uint64_t *  index,
struct NDShape vshape 
)

Returns the value of array throughout a virtual shape. The virtual shape has the same size of the original, but padded with ones to match another shape during broadcasting. If an index has a value higher than the padded virtual shape, the index 0 is then used. #deal_with_it.

Parameters
[in]arrayNDArray to access
[in]indexArray of dims of the element, len(index) must be equal to to the rank of the array
[in]vshapeVirtual padded shape
Returns
array[indexvshape]

◆ nda_vget1D()

tb_float nda_vget1D ( struct NDArray array,
uint64_t  index 
)

Returns the value of array throughout a 1D index. If the index has a value higher than the original size, mod is used.

Parameters
[in]arrayNDArray to access
[in]index1D index to fetch
Returns
array[indexvshape]