Getting a transpose of a matrix of size of n*n where n=2^m using multithreading
Is it worth parallelising?

When you are parallelizing code you have to ask yourself if it's worth doing that. Parallel code doesn't magically give a speedup, because there are various things that can actually slow you down when using parallelism, for example:


Spawning and waiting for threads itself costs some time.
Threads might be fighting for resources, like locks, but even just memory access might cause contention.
Code that is not trivially parallelizable might require more computations when parallelized than when running single-threaded.


In the case of transposing matrices, there are almost no computations involved, the work is completely dominated by reading and writing to memory. A good single-threaded implementation, which reads sequentially as much as possible, will likely be able to saturate the memory bus on typical desktop machines.

In your implementation, you have a huge overhead just from starting all the async work items: you call async() more often than there are elements in the matrix. So that alone will make this horribly slow. Most good parallel algorithms try to limit the number of threads to the number of CPU cores or hardware threads that are available.

Even if you didn't parallelize, but kept the structure of the code the same (just not use async()), then the recursive divide-and-conquer approach will cause a non-sequential memory access pattern.

Code review

Nested std::vector&lt;&gt;s are not efficient containers for matrices

While it's convenient to declare a 2D matrix using std::vector&lt;std::vector&lt;T&gt;&gt;, it is not very efficient, since there is a lot of indirection. It is more efficient to declare a single std::vector&lt;T&gt; of size n * n, and index the vector using [row * n + col], or use a C++ library that provides proper matrix classes.

Use std::swap()

The standard library provides std::swap(), which swaps two variables for you.