Commit 3ebcad1c authored by Tomáš Oberhuber's avatar Tomáš Oberhuber

Writting tutorial on StaticArray.

parent d0bdb3bb
......@@ -2,7 +2,7 @@
## Introduction
This tutorial introduces arrays in TNL. Array is one of the most important structure for memory management. Methods implemented in arrays are particularly useful for GPU programming. From this point of view, the reader will learn how to easily allocate memory on GPU, transfer data between GPU and CPU but also, how to initialize data allocated on GPU. In addition, the resulting code is hardware platform independent, so it can be ran on CPU without any changes.
This tutorial introduces arrays in TNL. There are three types - common arrays with dynamic allocation, static arrays allocated on stack and distributed arrays with dynamic allocation. Arrays are one of the most important structures for memory management. Methods implemented in arrays are particularly useful for GPU programming. From this point of view, the reader will learn how to easily allocate memory on GPU, transfer data between GPU and CPU but also, how to initialize data allocated on GPU. In addition, the resulting code is hardware platform independent, so it can be ran on CPU nad GPU without any changes.
## Table of Contents
1. [Arrays](#arrays)
......@@ -15,7 +15,7 @@ This tutorial introduces arrays in TNL. Array is one of the most important struc
5. [Checking the array contents](#checking_the_array_contents)
6. [IO operations with arrays](#io_operations_with-arrays)
2. [Static arrays](#static_arrays)
2. [Distributed arrays](#distributed_arrays)
3. [Distributed arrays](#distributed_arrays)
## Arrays <a name="arrays"></a>
......@@ -150,4 +150,11 @@ Output:
## Static arrays <a name="static_arrays"></a>
Static arrays are allocated on stack and thus they can be created even in CUDA kernels. Their size is fixed and it is given by a template parameter. Static array is a templated class defined in namespace `TNL::Containers` having two template parameters:
* `Size` is the array size.
* `Value` is type of data stored in the array.
The interface of StaticArray is very smillar to Array.
## Distributed arrays <a name="distributed_arrays"></a>
/***************************************************************************
StaticArrayAssignment.h - description
-------------------
begin : Aug 29, 2019
copyright : (C) 2019 by Tomas Oberhuber
email : tomas.oberhuber@fjfi.cvut.cz
***************************************************************************/
/* See Copyright Notice in tnl/Copyright */
#pragma once
#include <TNL/TypeTraits.h>
#include <TNL/StaticFor.h>
namespace TNL {
namespace Containers {
namespace Algorithms {
namespace detail {
////
// Functors used together with StaticFor for static loop unrolling in the
// implementation of the StaticArray
template< typename LeftValue, typename RightValue = LeftValue >
struct assignArrayFunctor
{
__cuda_callable__ void operator()( int i, LeftValue* data, const RightValue* v ) const
{
data[ i ] = v[ i ];
}
};
template< typename LeftValue, typename RightValue = LeftValue >
struct assignValueFunctor
{
__cuda_callable__ void operator()( int i, LeftValue* data, const RightValue v ) const
{
data[ i ] = v;
}
};
} //namespace detail
template< typename StaticArray,
typename T,
bool isStaticArrayType = IsStaticArrayType< T >::value >
struct StaticArrayAssignment;
/**
* \brief Specialization for array-array assignment.
*/
template< typename StaticArray,
typename T >
struct StaticArrayAssignment< StaticArray, T, true >
{
static void assign( StaticArray& a, const T& t )
{
static_assert( StaticArray::getSize() == T::getSize(), "Cannot assign static arrays with different size." );
StaticFor< 0, StaticArray::getSize() >::exec( detail::assignArrayFunctor< StaticArray, T >{}, a, t );
}
};
/**
* \brief Specialization for array-value assignment for other types. We assume
* that T is convertible to StaticArray::ValueType.
*/
template< typename StaticArray,
typename T >
struct StaticArrayAssignment< StaticArray, T, false >
{
static void assign( StaticArray& a, const T& t )
{
StaticFor< 0, StaticArray::getSize() >::exec( detail::assignValueFunctor< StaticArray, T >{}, a, t );
}
};
} // namespace Algorithms
} // namespace Containers
} // namespace TNL
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment