📜 ⬆️ ⬇️

Writing multi-threaded applications for a Windows store using Intel Threading Building Blocks - now with a DLL

This article describes how to build a simple Windows Store application using Intel Threading Building Blocks (Intel TBB).

My previous post Windows 8: Writing multi-threaded apps for a Windows store using Intel Threading Building Blocks describes experimental support for apps for a Windows store. Update 3 for Intel TBB 4.1, as well as the stable release tbb41_20130314oss, contains dynamic libraries for such applications.

To create a simple application, create a new Blank App project (XAML) using the Visual C ++> Windows Store template. This article uses the project name tbbSample0321 .

Add a couple of buttons for the main page (tbbSample0321.MainPage class). After this addition, the page's XAML file will look something like this.
')
<Page x:Class="tbbSample0321.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:tbbSample0321" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Button Name="SR" Margin="167,262,0,406" Height="100" Width="300" Content="Press to run Simple Reduction" Click="SR_Click"></Button> <Button Name="DR" Margin="559,262,0,406" Height="100" Width="300" Content="Press to run Determenistic Reduction" Click="DR_Click"></Button> </Grid> </Page> 


And add to the header file of the main page (MainPage.xaml.h) declarations of methods for handling button presses:
 #pragma once #include "MainPage.gh" namespace tbbSample0321 { public ref class MainPage sealed { public: MainPage(); protected: virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override; private: void SR_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); void DR_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); }; 

Then add the Intel TBB function calls to these event handlers. As an example, use the reduction (tbb :: parallel_reduce) and deterministic reduction (tbb :: parallel_deterministic_reduce) algorithms. To do this, add the following code to the main source main page MainPage.xaml.cpp:
 #include "tbb/tbb.h" void tbbSample0321::MainPage::SR_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) { int N=100000000; float fr = 1.0f/(float)N; float sum = tbb::parallel_reduce( tbb::blocked_range<int>(0,N), 0.0f, [=](const tbb::blocked_range<int>& r, float sum)->float { for( int i=r.begin(); i!=r.end(); ++i ) sum += fr; return sum; }, []( float x, float y )->float { return x+y; } ); SR->Content="Press to run Simple ReductionnThe answer is " + sum.ToString(); } void tbbSample0321::MainPage::DR_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) { int N=100000000; float fr = 1.0f/(float)N; float sum = tbb::parallel_deterministic_reduce( tbb::blocked_range<int>(0,N), 0.0f, [=](const tbb::blocked_range<int>& r, float sum)->float { for( int i=r.begin(); i!=r.end(); ++i ) sum += fr; return sum; }, []( float x, float y )->float { return x+y; } ); DR->Content="Press to run Deterministic ReductionnThe answer is " + sum.ToString(); } 

Then configure Intel TBB in the project properties page.

In Visual Studio, Project> Properties> Intel Performance Libraries , set Use TBB to Yes :


If you are using a stable release, configure the project manually: add the <TBB_folder> / include folder to the properties of the Additional Include Directories project and add the directory where the tbb.lib library lies in Additional Library Directories .

Then add the tbb.dll and tbbmalloc.dll dynamic libraries to the application container. To do this, add files to the project via Project> Add Existing Item ...


and set the Content property to Yes . In this case, the files will be copied to the application container (AppX) and then can be loaded during the application launch or later.


That's all! A simple Windows Store app is ready and it should be a good start to write more complex multithreaded applications using Intel Threading Building Blocks.

Source: https://habr.com/ru/post/175121/


All Articles