Commit bc6e512f by Marcus Winter

examples, tutorial: changed to use tasks_cpp

parent 5b5fdc8c
...@@ -10,8 +10,8 @@ include_directories( ...@@ -10,8 +10,8 @@ include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/../../base_cpp/include ${CMAKE_CURRENT_SOURCE_DIR}/../../base_cpp/include
${CMAKE_CURRENT_BINARY_DIR}/../../base_cpp/include ${CMAKE_CURRENT_BINARY_DIR}/../../base_cpp/include
${CMAKE_CURRENT_SOURCE_DIR}/../../mtapi_c/include ${CMAKE_CURRENT_SOURCE_DIR}/../../mtapi_c/include
${CMAKE_CURRENT_SOURCE_DIR}/../../mtapi_cpp/include ${CMAKE_CURRENT_SOURCE_DIR}/../../tasks_cpp/include
${CMAKE_CURRENT_BINARY_DIR}/../../mtapi_cpp/include ${CMAKE_CURRENT_BINARY_DIR}/../../tasks_cpp/include
${CMAKE_CURRENT_SOURCE_DIR}/../../containers_cpp/include ${CMAKE_CURRENT_SOURCE_DIR}/../../containers_cpp/include
${CMAKE_CURRENT_SOURCE_DIR}/../../algorithms_cpp/include ${CMAKE_CURRENT_SOURCE_DIR}/../../algorithms_cpp/include
${CMAKE_CURRENT_SOURCE_DIR}/../../dataflow_cpp/include ${CMAKE_CURRENT_SOURCE_DIR}/../../dataflow_cpp/include
...@@ -22,6 +22,6 @@ if(CMAKE_COMPILER_IS_GNUCXX) ...@@ -22,6 +22,6 @@ if(CMAKE_COMPILER_IS_GNUCXX)
endif() endif()
add_executable(examples ${EXAMPLES_SOURCES}) add_executable(examples ${EXAMPLES_SOURCES})
target_link_libraries(examples embb_dataflow_cpp embb_algorithms_cpp embb_mtapi_cpp target_link_libraries(examples embb_dataflow_cpp embb_algorithms_cpp embb_tasks_cpp
embb_mtapi_c embb_base_cpp embb_base_c embb_containers_cpp ${compiler_libs}) embb_mtapi_c embb_base_cpp embb_base_c embb_containers_cpp ${compiler_libs})
CopyBin(BIN examples DEST ${local_install_dir}) CopyBin(BIN examples DEST ${local_install_dir})
embb::mtapi::Node& node = embb::mtapi::Node::GetInstance();
...@@ -26,31 +26,31 @@ ...@@ -26,31 +26,31 @@
#include <iostream> #include <iostream>
#include <embb/mtapi/mtapi.h> #include <embb/tasks/tasks.h>
#include "mtapi/mtapi_check_status-snippet.h" #include "mtapi/mtapi_check_status-snippet.h"
static static
#include "mtapi/mtapi_cpp_action_signature-snippet.h" #include "tasks/tasks_cpp_action_signature-snippet.h"
/* get the node instance */ /* get the node instance */
#include "mtapi/mtapi_cpp_get_node-snippet.h" #include "tasks/tasks_cpp_get_node-snippet.h"
/* calculate */ /* calculate */
#include "mtapi/mtapi_terminating_condition-snippet.h" #include "mtapi/mtapi_terminating_condition-snippet.h"
/* first recursive call spawned as task (x = fib(n - 1);) */ /* first recursive call spawned as task (x = fib(n - 1);) */
#include "mtapi/mtapi_cpp_calc_task-snippet.h" #include "tasks/tasks_cpp_calc_task-snippet.h"
/* second recursive call can be called directly (y = fib(n - 2);) */ /* second recursive call can be called directly (y = fib(n - 2);) */
#include "mtapi/mtapi_cpp_calc_direct-snippet.h" #include "tasks/tasks_cpp_calc_direct-snippet.h"
/* wait for completion */ /* wait for completion */
#include "mtapi/mtapi_cpp_wait_task-snippet.h" #include "tasks/tasks_cpp_wait_task-snippet.h"
/* add the two preceeding numbers */ /* add the two preceeding numbers */
#include "mtapi/mtapi_write_back-snippet.h" #include "mtapi/mtapi_write_back-snippet.h"
static static
int fibonacci(int n) { int fibonacci(int n) {
/* get the node instance, the node is initialized automatically */ /* get the node instance, the node is initialized automatically */
embb::mtapi::Node& node = embb::mtapi::Node::GetInstance(); embb::tasks::Node& node = embb::tasks::Node::GetInstance();
/* start calculation */ /* start calculation */
#include "mtapi/mtapi_cpp_start_task-snippet.h" #include "tasks/tasks_cpp_start_task-snippet.h"
/* wait for task completion */ /* wait for task completion */
mtapi_status_t status = task.Wait(MTAPI_INFINITE); mtapi_status_t status = task.Wait(MTAPI_INFINITE);
MTAPI_CHECK_STATUS(status); MTAPI_CHECK_STATUS(status);
......
void fibonacciActionFunction( void fibonacciActionFunction(
int n, int n,
int* result, int* result,
embb::mtapi::TaskContext & task_context embb::tasks::TaskContext & task_context
) { ) {
int a = n - 1; int a = n - 1;
int x; int x;
embb::mtapi::Task task = node.Spawn( embb::tasks::Task task = node.Spawn(
embb::base::Bind( embb::base::Bind(
embb::base::MakeFunction(fibonacciActionFunction), embb::base::MakeFunction(fibonacciActionFunction),
a, /* argument */ a, /* argument */
......
embb::tasks::Node& node = embb::tasks::Node::GetInstance();
int result; int result;
embb::mtapi::Task task = node.Spawn( embb::tasks::Task task = node.Spawn(
embb::base::Bind( embb::base::Bind(
embb::base::MakeFunction(fibonacciActionFunction), embb::base::MakeFunction(fibonacciActionFunction),
n, n,
......
...@@ -154,13 +154,12 @@ After everything is done, the action is deleted (\lstinline|mtapi_action_delete( ...@@ -154,13 +154,12 @@ After everything is done, the action is deleted (\lstinline|mtapi_action_delete(
\embb provides C++ wrappers for the MTAPI C interface. Using the example from the previous section, the signature of the action function for the C++ interface looks like this: \embb provides C++ wrappers for the MTAPI C interface. Using the example from the previous section, the signature of the action function for the C++ interface looks like this:
% %
\\\inputlisting{../examples/mtapi/mtapi_cpp_action_signature-snippet.h} %\\\inputlisting{../examples/mtapi/mtapi_cpp_action_signature-snippet.h}
% %
First, the node instance needs to be obtained. If the node is not initialized yet, this function will do it. First, the node instance needs to be initialized.
% %
\\\inputlisting{../examples/mtapi/mtapi_cpp_get_node-snippet.h} %\\\inputlisting{../examples/mtapi/mtapi_cpp_initialize_node-snippet.h}
% %
\emph{\textbf{Note:} Automatic initialization allows for easy usage of the \emph{Algorithms} and \emph{Dataflow} building blocks. For performance measurements however, explicit initialization by calling \lstinline|embb::mtapi::Node::Initialize| is imperative since the measurements will otherwise include the initialization time of MTAPI.}
Checking the arguments and the result buffer is not necessary, since everything is safely typed. However, the terminating condition of the recursion still needs to be checked: Checking the arguments and the result buffer is not necessary, since everything is safely typed. However, the terminating condition of the recursion still needs to be checked:
% %
...@@ -168,15 +167,15 @@ Checking the arguments and the result buffer is not necessary, since everything ...@@ -168,15 +167,15 @@ Checking the arguments and the result buffer is not necessary, since everything
% %
After that, the first part of the computation is launched as an MTAPI task using \lstinline|embb::mtapi::Node::Spawn()| (registering an action function with a job is done automatically): After that, the first part of the computation is launched as an MTAPI task using \lstinline|embb::mtapi::Node::Spawn()| (registering an action function with a job is done automatically):
% %
\\\inputlisting{../examples/mtapi/mtapi_cpp_calc_task-snippet.h} %\\\inputlisting{../examples/mtapi/mtapi_cpp_calc_task-snippet.h}
% %
The second part can be executed directly: The second part can be executed directly:
% %
\\\inputlisting{../examples/mtapi/mtapi_cpp_calc_direct-snippet.h} %\\\inputlisting{../examples/mtapi/mtapi_cpp_calc_direct-snippet.h}
% %
Then, completion of the MTAPI task has to be waited for using \lstinline|embb::mtapi::Task::Wait()|: Then, completion of the MTAPI task has to be waited for using \lstinline|embb::mtapi::Task::Wait()|:
% %
\\\inputlisting{../examples/mtapi/mtapi_cpp_wait_task-snippet.h} %\\\inputlisting{../examples/mtapi/mtapi_cpp_wait_task-snippet.h}
% %
Finally, the two parts can be added and written into the result buffer: Finally, the two parts can be added and written into the result buffer:
% %
...@@ -185,12 +184,10 @@ Finally, the two parts can be added and written into the result buffer: ...@@ -185,12 +184,10 @@ Finally, the two parts can be added and written into the result buffer:
The \lstinline|fibonacci()| function also gets simpler compared to the C version. The MTAPI runtime is initialized automatically, only the node instance has to be fetched: The \lstinline|fibonacci()| function also gets simpler compared to the C version. The MTAPI runtime is initialized automatically, only the node instance has to be fetched:
% %
\\\inputlisting{../examples/mtapi/mtapi_cpp_get_node-snippet.h} %\\\inputlisting{../examples/mtapi/mtapi_cpp_get_node-snippet.h}
% %
The root task can be started using \lstinline|embb::mtapi::Node::Spawn()| directly, registering with a job is done automatically: The root task can be started using \lstinline|embb::mtapi::Node::Start()| directly, registering with a job is done automatically:
% %
\\\inputlisting{../examples/mtapi/mtapi_cpp_start_task-snippet.h} %\\\inputlisting{../examples/mtapi/mtapi_cpp_start_task-snippet.h}
% %
Again, the started task has to be waited for (using \lstinline|embb::mtapi::Task::Wait()|) before the result can be returned. The runtime is shut down automatically in an \lstinline|atexit()| handler. Again, the started task has to be waited for (using \lstinline|embb::mtapi::Task::Wait()|) before the result can be returned. The runtime is shut down by calling \lstinline|embb::mtapi::Node::Finalize|.
\emph{\textbf{Note:} If the node was initialized explicitly by calling \lstinline|embb::mtapi::Node::Initialize|, the runtime must also be shut down explicitly by calling \lstinline|embb::mtapi::Node::Finalize|.}
\chapter{Tasks}
\label{cha:tasks}
\embb provides a simple task management wrapper for the MTAPI interface. Using the example from the previous section, the signature of the action function for the tasks interface looks like this:
%
\\\inputlisting{../examples/tasks/tasks_cpp_action_signature-snippet.h}
%
First, the node instance needs to be obtained. If the node is not initialized yet, this function will do it.
%
\\\inputlisting{../examples/tasks/tasks_cpp_get_node-snippet.h}
%
\emph{\textbf{Note:} Automatic initialization allows for easy usage of the \emph{Algorithms} and \emph{Dataflow} building blocks. For performance measurements however, explicit initialization by calling \lstinline|embb::tasks::Node::Initialize| is imperative since the measurements will otherwise include the initialization time of MTAPI.}
Checking the arguments and the result buffer is not necessary, since everything is safely typed. However, the terminating condition of the recursion still needs to be checked:
%
\\\inputlisting{../examples/mtapi/mtapi_terminating_condition-snippet.h}
%
After that, the first part of the computation is launched as an MTAPI task using \lstinline|embb::tasks::Node::Spawn()| (registering an action function with a job is done automatically):
%
\\\inputlisting{../examples/tasks/tasks_cpp_calc_task-snippet.h}
%
The second part can be executed directly:
%
\\\inputlisting{../examples/tasks/tasks_cpp_calc_direct-snippet.h}
%
Then, completion of the MTAPI task has to be waited for using \lstinline|embb::tasks::Task::Wait()|:
%
\\\inputlisting{../examples/tasks/tasks_cpp_wait_task-snippet.h}
%
Finally, the two parts can be added and written into the result buffer:
%
\\\inputlisting{../examples/mtapi/mtapi_write_back-snippet.h}
%
The \lstinline|fibonacci()| function also gets simpler compared to the C version. The MTAPI runtime is initialized automatically, only the node instance has to be fetched:
%
\\\inputlisting{../examples/tasks/tasks_cpp_get_node-snippet.h}
%
The root task can be started using \lstinline|embb::tasks::Node::Spawn()| directly, registering with a job is done automatically:
%
\\\inputlisting{../examples/tasks/tasks_cpp_start_task-snippet.h}
%
Again, the started task has to be waited for (using \lstinline|embb::tasks::Task::Wait()|) before the result can be returned. The runtime is shut down automatically in an \lstinline|atexit()| handler.
\emph{\textbf{Note:} If the node was initialized explicitly by calling \lstinline|embb::tasks::Node::Initialize|, the runtime must also be shut down explicitly by calling \lstinline|embb::tasks::Node::Finalize|.}
...@@ -114,6 +114,7 @@ ...@@ -114,6 +114,7 @@
% \input{content/preface} % \input{content/preface}
\input{content/introduction} \input{content/introduction}
\input{content/mtapi} \input{content/mtapi}
\input{content/tasks}
\input{content/algorithms} \input{content/algorithms}
\input{content/dataflow} \input{content/dataflow}
\input{content/containers} \input{content/containers}
......
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 sign in to comment