Tuesday, March 28, 2017

Asio ThreadPool Performance Test

Updated: 7 April 2017

Category: C++, Concurrency Programming, Thread pool, Asio

I have tested Asio ThreadPool performance by comparing processing time of three concurrency methods. The first method is Standard C++ Multithreading (mthread), the second is Asio ThreadPool (asio) and the last is Microsoft Parallel Patterns Library (ppl). All of them were used for calculating PI and Fibonacci numbers on the same machine for both Windows and Linux. They all used the same code except for PPL that is only available on Windows. I have also measured serial processing times for baseline comparison. The C++ compilers used are MSVC 19.10.25017 for Windows, GCC 6.3.1 and Clang 3.9.1 for Linux. The results are as follow:

Comparing time used between MSVC, GCC and Clang

GCC is the fastest in all scenarios (not including ppl). The second is MSVC except for fibonacci with asio that Clang comes the second.

Comparing each concurrency method with serial processing

Using MSVC
There is no big difference in time used for PI among mthread, asio and ppl. However for Fabonacci, mthread and ppl approximately process at the same speed and faster than asio which is the slowest.

Using GCC
There is no big difference between mthread and asio in time used for PI. But for Fabonacci, mthread has a little bit faster than asio.

Using Clang
There is no big difference between mthread and asio for both PI and Fibonacci.

  • The concurrency processing speed depends mainly on three factors: compiler, computational type and concurrency method respectively. 
  • Asio ThreadPool does not perform well with MSVC on some computational type (Fibonacci) when comparing with GCC and Clang.
The code is here.

Saturday, March 4, 2017

Using Asio C++ library based ThreadPool class

Category: C++, Concurrency Programming, Thread pool, Asio

Prerequisites: C++, Asio C++ programming concept, Multithreading

Requirement: Asio C++ library

What is it?
It is only a C++ header file that defines ThreadPool class based on Asio C++ Library.

Why is it created?
It is created to serve the following purposes:
  1. To use C++ multithreading with thread pool.
  2. To use task based concurrency programming.
  3. Can use strictly sequential invocation of handlers.
  4. Can use C++ Exception handling.

How can you use it?
  1. Create the client class that will use thread pool.
  2. Create any tasks that you want to execute in sequential or parallel order. In our case we use sequential for simplicity.
  3. If you want to handle exception. Modify previous code, add final task to start and stop MainIoService as shown in the following code.
  4. In the main code, create ThreadPool instance. Then create client instance by using shared_ptr. Make a call from the client and try to throw an exception in the client code to test exception handling.
  5. If you use _threadPool.strand() instead of _threadPool.enqueue(), any tasks called can not be executed concurrently.
How to dowload the ThreadPool Class?
The ThreadPool Class is in ThreadPool.h header file on github.

Thursday, September 12, 2013

Introduction to AlmanacSoft Payer 1.0

Category: Windows Store app, PayPal, REST API, eCommerce

What is AlmanacSoft Payer 1.0?

AlmanacSoft Payer 1.0 is Windows Store app running on Windows 8.1 Preview or later. It is payment app that uses new PayPal's REST APIs with standards-based technologies such as OAuth and JSON for paying money on the Internet. It supports both Direct Credit Card Payment and PayPal Account Payment. The app is native code written in C++/CX and developed by Poom Malakul Na Ayudhya.

How to Install
  1. Download AlmanacSoft Payer 1.0 and you will get the file named "Payer_1.0.0.0_Win32_Test.zip".
  2. Extract it and right click on "Add-AppDevPackage.ps1" to Run with PowerShell. 
  3.  You may be asked to acquire the developer license if you don't have it yet. Use your Microsoft  account to log in and get the license for free. It will be expired in one month and you can renew it.
  4. You may be asked for Execution Policy Change, you have to answer "[Y] Yes"
  5. You may be asked for installing the signing certificate, you have to answer "[Y] Yes". 
  6. AlmanacSoft Payer will be installed successfully.

How to use

For Merchants
  1. You have to register at PayPal for PayPal merchant account.
  2. Log in at https://developer.paypal.com/ with your merchant account. Then create an application to get merchant's Client Id and Secret.
  3. Run AlmanacSoft Payer and select credentials page. Use your Client ID and Secret to apply and export your encrypted merchant data file (MDF).
  4. Send encrypted MDF to your customers by e-mail or let them download from your website.
  5. Tell your customers to use AlmanacSoft Payer to import or download your MDF and use it to make payment for you.
For Customers
  1. Run AlmanacSoft Payer. If you are behind proxy server, set your proxy credentials first.
  2. Using AlmanacSoft Payer to import or download encrypted MDF provided by the merchant you want to pay.
  3. Select appropriate payment method for merchant in the countries supported by PayPal.

Wednesday, March 27, 2013

C++ AMP: How fast is it?

Category: Windows Store app, C++ AMP, GPU Programming
Prerequisites: C++, C++ AMP

Full Text in Thai (PDF 1.13 MB)

This study measures time used in millisecond for calculating square matrix multiplication at different dimension sizes starting from 256x256 to 2048x2048. The C++ AMP tested engines are two GPUs (Intel HD Graphics 4000 and NVIDIA Geforce GT-650M) and one software engine (Microsoft Basic Render Driver).  Two C++ AMP methods (simple and tiling) are used.  The study also measures time used by normal sequential code for using as a baseline comparison. The testing software is C++ Windows Store app running on Intel i7 RAM 8 MB.

The figure above shows Windows Store app used in this study. It also has 3D rotating cube in background for testing with DirectX.

This is the result table of time used measured in milliseconds. The table also shows computed ratio (in red) comparing between MS Basic Render Driver and Sequential code and also between both GPUs and MS Basic Render Driver. The size means the matrix dimension starting from 256x256 to 2048x2048.

From the above figure, MS Render Driver speed is around five to ten times comparing with sequential code when using simple method and five to twenty times when using tile method.

When using simple method, NVIDIA's speed is ten to thirty times comparing with MS Render Driver while Intel's speed is around five times comparing with MS Render Driver.

When using tile method, NVIDIA's speed is from fifteen to twenty five times comparing with MS Render Driver while Intel's speed is around five times comparing with MS Render Driver.

Friday, February 15, 2013

Run-time Data Binding

Category: Windows Store App Developing
Prerequisites: XAML, C++, C++/CX, Simple Data Binding

Data binding lets you synchronise UI control elements in XAML with data source that can be dataset, data object or any primitive data types. Usually you just set binding property for any UI control elements and then set BindableAttribute attribute for the ref class in the code behind. And when you compile your code, the compiler will do the rest and all properties in your class will be bindable.

The Problem
Sometimes you may need data class that uses dynamic properties. That means you don’t know at compile time what and how many data properties and types the class should have. This is a common situation such as when you retrieve data set from the SQL server that you usually specify data fields at run time. In these cases, BindableAttribute attribute doesn't help.

The Solution
My solution is to implement ICustomPropertyProvider for the ref class. In this article I call this technique as Run-time Data Binding. The steps are as follows:

1. First, create new ref class with inheriting and implementing ICustomProperty. This class will be used for representing run-time property you will create later.

2. Create another new ref class with inheriting and implementing ICustomPropertyProvider. If you want to be notified when your property changed you can also inherit and implement INotifyPropertyChanged here. Don't forget to inherit from DependencyObject, this is mandatory.

In this class I used Map Collection to store my CustomProperty objects and created two public methods for getting and setting value for CustomProperty target.

Now you will have two ref class that can be used for run-time data binding.

3. Create new ref class to be used for run-time binding which inherit from CustomPropertyProvider ref class. In this case I'll create Person Class that will have name property created at run-time.

4. Insert name property at rum-time.