Skip to content

Commit

Permalink
Added a new tutorial example to ease the onboarding of RAJA::kernel.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhornung67 committed Nov 22, 2019
1 parent ef49fae commit b53d72a
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 30 deletions.
4 changes: 2 additions & 2 deletions examples/tut_nested-loop-reorder.cpp
Expand Up @@ -19,7 +19,7 @@
*
* RAJA features shown:
* - Index range segment
* - 'RAJA::nested' loop abstractions and execution policies
* - 'RAJA::kernel' loop abstractions and execution policies
* - Nested loop reordering
* - Strongly-typed loop indices
*/
Expand Down Expand Up @@ -122,7 +122,7 @@ int main(int RAJA_UNUSED_ARG(argc), char **RAJA_UNUSED_ARG(argv[]))
#if 0
//----------------------------------------------------------------------------//
// The following demonstrates that code will not compile if lambda argument
// types/order do not match the types/order of the strongly-typed nested::For.
// types/order do not match the types/order of the For statements.
//----------------------------------------------------------------------------//

// _nestedreorder_typemismatch_start
Expand Down
24 changes: 16 additions & 8 deletions exercises/tutorial_halfday/CMakeLists.txt
Expand Up @@ -54,17 +54,25 @@ raja_add_executable(
SOURCES ex6_stencil-offset-layout_solution.cpp)

raja_add_executable(
NAME ex7_tiled-matrix-transpose
SOURCES ex7_tiled-matrix-transpose.cpp)
NAME ex7_nested-loop-reorder
SOURCES ex7_nested-loop-reorder.cpp)

raja_add_executable(
NAME ex7_tiled-matrix-transpose_solution
SOURCES ex7_tiled-matrix-transpose_solution.cpp)
NAME ex7_nested-loop-reorder_solution
SOURCES ex7_nested-loop-reorder_solution.cpp)

raja_add_executable(
NAME ex8_matrix-transpose-local-array
SOURCES ex8_matrix-transpose-local-array.cpp)
NAME ex8_tiled-matrix-transpose
SOURCES ex8_tiled-matrix-transpose.cpp)

raja_add_executable(
NAME ex8_matrix-transpose-local-array_solution
SOURCES ex8_matrix-transpose-local-array_solution.cpp)
NAME ex8_tiled-matrix-transpose_solution
SOURCES ex8_tiled-matrix-transpose_solution.cpp)

raja_add_executable(
NAME ex9_matrix-transpose-local-array
SOURCES ex9_matrix-transpose-local-array.cpp)

raja_add_executable(
NAME ex9_matrix-transpose-local-array_solution
SOURCES ex9_matrix-transpose-local-array_solution.cpp)
12 changes: 0 additions & 12 deletions exercises/tutorial_halfday/ex1_vector-addition.cpp
Expand Up @@ -55,18 +55,6 @@ void printArray(int* v, int len);
int main(int RAJA_UNUSED_ARG(argc), char **RAJA_UNUSED_ARG(argv[]))
{

RAJA::Layout<2> layout_a(3, 5);

int i = 0;
int j = 0;

layout_a.toIndices(12, i, j);
std::cout << "\n12 : (i, j) = " << i << " , " << j << "\n";

layout_a.toIndices(20, i, j);
std::cout << "\n20 : (i, j) = " << i << " , " << j << "\n";


std::cout << "\n\nExercise #1: RAJA Vector Addition...\n";

//
Expand Down
146 changes: 146 additions & 0 deletions exercises/tutorial_halfday/ex7_nested-loop-reorder.cpp
@@ -0,0 +1,146 @@
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Copyright (c) 2016-19, Lawrence Livermore National Security, LLC
// and RAJA project contributors. See the RAJA/COPYRIGHT file for details.
//
// SPDX-License-Identifier: (BSD-3-Clause)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

#include <cstdlib>
#include <iostream>

#include "RAJA/RAJA.hpp"

/*
* EXERCISE #6: Nested Loop Reordering
*
* In this exercise, you will use RAJA::kernel execution policies
* to permute the order of loops in a triple loop nest. In particular,
* you will reorder loop statements in execution policies. The exercise
* does no actual computation and just prints out the loop indices to show
* the different orderings.
*
* To avoid the complexity of interpreting parallel output, the execution
* policies you will write will use sequential execution.
*
* RAJA features shown:
* - Index range segment
* - 'RAJA::kernel' loop abstractions and execution policies
* - Nested loop reordering
* - Strongly-typed loop indices
*/

//
// Define three named loop index types used in the triply-nested loops.
// These will trigger compilation errors if lambda index argument ordering
// and types do not match the typed range index ordering. See final
// example in this file.
//
RAJA_INDEX_VALUE(KIDX, "KIDX");
RAJA_INDEX_VALUE(JIDX, "JIDX");
RAJA_INDEX_VALUE(IIDX, "IIDX");


int main(int RAJA_UNUSED_ARG(argc), char **RAJA_UNUSED_ARG(argv[]))
{

std::cout << "\n\nExercise #7: RAJA nested loop reorder example...\n";

//
// Typed index ranges
//
RAJA::TypedRangeSegment<KIDX> KRange(2, 4);
RAJA::TypedRangeSegment<JIDX> JRange(1, 3);
RAJA::TypedRangeSegment<IIDX> IRange(0, 2);

//----------------------------------------------------------------------------//

std::cout << "\n Running loop reorder example (K-outer, J-middle, I-inner)"
<< "...\n\n" << " (I, J, K)\n" << " ---------\n";

using KJI_EXECPOL = RAJA::KernelPolicy<
RAJA::statement::For<2, RAJA::seq_exec, // k
RAJA::statement::For<1, RAJA::seq_exec, // j
RAJA::statement::For<0, RAJA::seq_exec,// i
RAJA::statement::Lambda<0>
>
>
>
>;

RAJA::kernel<KJI_EXECPOL>( RAJA::make_tuple(IRange, JRange, KRange),
[=] (IIDX i, JIDX j, KIDX k) {
printf( " (%d, %d, %d) \n", (int)(*i), (int)(*j), (int)(*k));
});


//----------------------------------------------------------------------------//

std::cout << "\n Running loop reorder example (J-outer, I-middle, K-inner)"
<< "...\n\n" << " (I, J, K)\n" << " ---------\n";

///
/// TODO...
///
/// EXERCISE: Define an execution policy (JIK_EXECPOL) that reorders the
/// loop nest so that the outer loop is the j-loop (slowest
/// running index), the inner loop is the k-loop (fastest
/// running index), and the i-loop is the middle loop.
///
/// NOTE: You will have to enable this code section to compile and run it.
///

#if 0
using JIK_EXECPOL =

RAJA::kernel<JIK_EXECPOL>( RAJA::make_tuple(IRange, JRange, KRange),
[=] (IIDX i, JIDX j, KIDX k) {
printf( " (%d, %d, %d) \n", (int)(*i), (int)(*j), (int)(*k));
});
#endif


//----------------------------------------------------------------------------//

std::cout << "\n Running loop reorder example (I-outer, K-middle, J-inner)"
<< "...\n\n" << " (I, J, K)\n" << " ---------\n";

///
/// TODO...
///
/// EXERCISE: Define an execution policy (IKJ_EXECPOL) that reorders the
/// loop nest so that the outer loop is the i-loop (slowest
/// running index), the inner loop is the j-loop (fastest
/// running index), and the k-loop is the middle loop.
///
/// NOTE: You will have to enable this code section to compile and run it.
///

#if 0
using IKJ_EXECPOL =

RAJA::kernel<IKJ_EXECPOL>( RAJA::make_tuple(IRange, JRange, KRange),
[=] (IIDX i, JIDX j, KIDX k) {
printf( " (%d, %d, %d) \n", (int)(*i), (int)(*j), (int)(*k));
});
#endif


#if 0
//----------------------------------------------------------------------------//
// The following demonstrates that code will not compile if lambda argument
// types/order do not match the types/order For statements in the execution
// policy. To see this, enable this code section and try to compile this file.
//----------------------------------------------------------------------------//

RAJA::kernel<IKJ_EXECPOL>( RAJA::make_tuple(IRange, JRange, KRange),
[=] (JIDX i, IIDX j, KIDX k) {
printf( " (%d, %d, %d) \n", (int)(*i), (int)(*j), (int)(*k));
});

#endif

std::cout << "\n DONE!...\n";

return 0;
}

136 changes: 136 additions & 0 deletions exercises/tutorial_halfday/ex7_nested-loop-reorder_solution.cpp
@@ -0,0 +1,136 @@
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Copyright (c) 2016-19, Lawrence Livermore National Security, LLC
// and RAJA project contributors. See the RAJA/COPYRIGHT file for details.
//
// SPDX-License-Identifier: (BSD-3-Clause)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

#include <cstdlib>
#include <iostream>

#include "RAJA/RAJA.hpp"

/*
* EXERCISE #6: Nested Loop Reordering
*
* In this exercise, you will use RAJA::kernel execution policies
* to permute the order of loops in a triple loop nest. In particular,
* you will reorder loop statements in execution policies. The exercise
* does no actual computation and just prints out the loop indices to show
* the different orderings.
*
* To avoid the complexity of interpreting parallel output, the execution
* policies you will write will use sequential execution.
*
* RAJA features shown:
* - Index range segment
* - 'RAJA::kernel' loop abstractions and execution policies
* - Nested loop reordering
* - Strongly-typed loop indices
*/

//
// Define three named loop index types used in the triply-nested loops.
// These will trigger compilation errors if lambda index argument ordering
// and types do not match the typed range index ordering. See final
// example in this file.
//
RAJA_INDEX_VALUE(KIDX, "KIDX");
RAJA_INDEX_VALUE(JIDX, "JIDX");
RAJA_INDEX_VALUE(IIDX, "IIDX");


int main(int RAJA_UNUSED_ARG(argc), char **RAJA_UNUSED_ARG(argv[]))
{

std::cout << "\n\nExercise #7: RAJA nested loop reorder example...\n";

//
// Typed index ranges
//
RAJA::TypedRangeSegment<KIDX> KRange(2, 4);
RAJA::TypedRangeSegment<JIDX> JRange(1, 3);
RAJA::TypedRangeSegment<IIDX> IRange(0, 2);

//----------------------------------------------------------------------------//

std::cout << "\n Running loop reorder example (K-outer, J-middle, I-inner)"
<< "...\n\n" << " (I, J, K)\n" << " ---------\n";

using KJI_EXECPOL = RAJA::KernelPolicy<
RAJA::statement::For<2, RAJA::seq_exec, // k
RAJA::statement::For<1, RAJA::seq_exec, // j
RAJA::statement::For<0, RAJA::seq_exec,// i
RAJA::statement::Lambda<0>
>
>
>
>;

RAJA::kernel<KJI_EXECPOL>( RAJA::make_tuple(IRange, JRange, KRange),
[=] (IIDX i, JIDX j, KIDX k) {
printf( " (%d, %d, %d) \n", (int)(*i), (int)(*j), (int)(*k));
});


//----------------------------------------------------------------------------//

std::cout << "\n Running loop reorder example (J-outer, I-middle, K-inner)"
<< "...\n\n" << " (I, J, K)\n" << " ---------\n";

using JIK_EXECPOL = RAJA::KernelPolicy<
RAJA::statement::For<1, RAJA::seq_exec, // j
RAJA::statement::For<0, RAJA::seq_exec, // i
RAJA::statement::For<2, RAJA::seq_exec,// k
RAJA::statement::Lambda<0>
>
>
>
>;

RAJA::kernel<JIK_EXECPOL>( RAJA::make_tuple(IRange, JRange, KRange),
[=] (IIDX i, JIDX j, KIDX k) {
printf( " (%d, %d, %d) \n", (int)(*i), (int)(*j), (int)(*k));
});


//----------------------------------------------------------------------------//

std::cout << "\n Running loop reorder example (I-outer, K-middle, J-inner)"
<< "...\n\n" << " (I, J, K)\n" << " ---------\n";

using IKJ_EXECPOL = RAJA::KernelPolicy<
RAJA::statement::For<0, RAJA::seq_exec, // i
RAJA::statement::For<2, RAJA::seq_exec, // k
RAJA::statement::For<1, RAJA::seq_exec,// j
RAJA::statement::Lambda<0>
>
>
>
>;

RAJA::kernel<IKJ_EXECPOL>( RAJA::make_tuple(IRange, JRange, KRange),
[=] (IIDX i, JIDX j, KIDX k) {
printf( " (%d, %d, %d) \n", (int)(*i), (int)(*j), (int)(*k));
});


#if 0
//----------------------------------------------------------------------------//
// The following demonstrates that code will not compile if lambda argument
// types/order do not match the types/order For statements in the execution
// policy. To see this, enable this code section and try to compile this file.
//----------------------------------------------------------------------------//

RAJA::kernel<IKJ_EXECPOL>( RAJA::make_tuple(IRange, JRange, KRange),
[=] (JIDX i, IIDX j, KIDX k) {
printf( " (%d, %d, %d) \n", (int)(*i), (int)(*j), (int)(*k));
});

#endif

std::cout << "\n DONE!...\n";

return 0;
}

Expand Up @@ -15,7 +15,7 @@
//#define ENABLE_POLICY //EXERCISE: Uncomment

/*
* EXERCISE #7: Tiled Matrix Transpose
* EXERCISE #8: Tiled Matrix Transpose
*
* In this exercise, your program will carry out the transpose of a matrix A
* using a tiling algorithm. An input matrix A of dimension N_r x N_c
Expand Down Expand Up @@ -57,7 +57,7 @@ void printResult(RAJA::View<T, RAJA::Layout<DIM>> Atview, int N_r, int N_c);
int main(int RAJA_UNUSED_ARG(argc), char **RAJA_UNUSED_ARG(argv[]))
{

std::cout << "\n\nExercise #7: RAJA Tiled Matrix Transpose...\n";
std::cout << "\n\nExercise #8: RAJA Tiled Matrix Transpose...\n";

//
// Define num rows/cols in matrix
Expand Down
Expand Up @@ -14,7 +14,7 @@
#include "memoryManager.hpp"

/*
* EXERCISE #7: Tiled Matrix Transpose
* EXERCISE #8: Tiled Matrix Transpose
*
* In this exercise, your program will carry out the transpose of a matrix A
* using a tiling algorithm. An input matrix A of dimension N_r x N_c
Expand Down Expand Up @@ -54,7 +54,7 @@ void printResult(RAJA::View<T, RAJA::Layout<DIM>> Atview, int N_r, int N_c);
int main(int RAJA_UNUSED_ARG(argc), char **RAJA_UNUSED_ARG(argv[]))
{

std::cout << "\n\nExercise #7: RAJA Tiled Matrix Transpose...\n";
std::cout << "\n\nExercise #8: RAJA Tiled Matrix Transpose...\n";

//
// Define num rows/cols in matrix
Expand Down

0 comments on commit b53d72a

Please sign in to comment.