Quantcast
Channel: Intel® C++ Studio XE
Viewing all articles
Browse latest Browse all 76

Windows* 8 OS Tutorial: Writing a Multithreaded Application for the Windows Store* using Intel® Threading Building Blocks - now with DLLs.

$
0
0

This article explains how to build a simple application for the Windows Store* using Intel Threading Building Blocks (Intel® TBB).

A previous post: “Windows* 8 Tutorial: Writing a Multithreaded Application for the Windows Store* using Intel® Threading Building Blocks”, discusses experimental support for Windows 8 Store applications.  Now  Intel TBB 4.1 update 3 contains binaries for this as well as a corresponding tbb41_20130314oss stable release.

To make a simple app, create new project  Blank App (XAML) using the default template Visual C++> Windows Store. that the remainder of this tutorial uses  tbbSample0321 as the project name. 

Add a couple of buttons to the main page (tbbSample0321.MainPage class). After adding these, the XAML file of the page will look like

<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 declarations of methods that process button clicks to main page header file (MainPage.xaml.h)

#pragma once
#include "MainPage.g.h"
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);
 };
}

Next, add Intel TBB library functions in the handlers for the buttons. As an example, use reduction (tbb::parallel_reduce) and deterministic reduction (tbb::parallel_deterministic_reduce) algorithms.  To do so, add the following code to the main page source file 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 property page
From Visual Studio, go to Project > Properties > Intel Performance Libraries, and set Use TBB to Yes:

If you use an open source package, add the folder <TBB_folder>/include to the project properties Additional Include Directories and add the folder that contains the tbb.lib library file to Additional Library Directories.  

Next add the tbb.dll and tbbmalloc.dll libraries to the application container. In order 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 container (AppX) and then can be loaded during application launch as well as on demand.

That’s it! This simple application is ready and should be a good start towards writing a more complex parallel application for the Windows Store using Intel® Threading Building Blocks.

* Other names and brands may be claimed as the property of others

  • Intel Parallel Studio
  • intel threading building blocks
  • Intel TBB
  • tbb
  • C++
  • C++11
  • Developers
  • Microsoft Windows* 8
  • Windows*
  • C/C++
  • Intel® Threading Building Blocks
  • Intel® C++ Studio XE
  • Microsoft Windows* 8 Style UI
  • Open Source
  • Parallel Computing
  • URL
  • Code Sample
  • Compiler Topics
  • Libraries
  • Multithread development
  • Learning Lab

  • Viewing all articles
    Browse latest Browse all 76

    Trending Articles



    <script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>