Quantcast
Channel: Intel® Cilk™ Plus
Viewing all articles
Browse latest Browse all 77

Is it possible to me to improve a simple O(n) algorithm with Cilk Plus?

$
0
0

Hi everybody!

I have a simple algorithm to print the very first unique ASCII character from a stream. In the worst case the algorithm scans through the stream twice, doing some comparisons and increments. So, roughly, my algorithm is O(n).

The algorithm0 do two main things:

  1. It scans all the characters through the stream, counting the number of their occurrences with the help of an array. The character itself is used as the index of this array, since ASCII characters are numbers from 0 to 127 (or 255, if we consider its extended version);
  2. After having filled the array that relates the characters in the stream with how many times they have appeared in it, the algorithm scans through the array, again, but this time it checks if the number of occurrence is equal to 1: if it is, then this is the first unique character; if not, it continues the search until the end of the stream is reached.

The first task runs1 in linear time: a1n + b1. So as the second task: a2n + b2. Roughly, the whole algorithm runs in a3n + b3. Where a3 = a1 + a2, and b3 = b1 + b2.

My idea2 is to try to improve its performance by splitting the first task in two using parallelism provided by Cilk Plus: I would have half of my stream being read by one function3 f1(), and the other half by f2() at the same time. With this approach, I would expect4 the first task to be executed, roughly, in (a1n + b1)/2, so that I could improve the overall performance of my algorithm even if it is just a little.

I would like to know if you think it is possible to accomplish that using Cilk Plus. If it is, I just would like to do it even if the speedup was worthless.

Cheers,

Roní.

===========================================================================================================

[0] I have attached my original code just in case I have not been clear enough in the explanation.

[1] I'm just a student and I'm not really experienced with calculating running times... However, I think I am superficially right about those claims.

[2] Again, the idea is simple and superficial. It has no rigor, by now.

[3] These two functions would operate in two different arrays to avoid competition and, then, in the end they would be joined. And I hope the time consumed by the joining the two arrays is small enough to make the splitting feasable.

[4] I know the improvement would be less than 2 because of many other factors. It is just a loose reasoning, I think.

AdjuntoTamaño
DescargarfirstUniqueChar.c982 bytes

Viewing all articles
Browse latest Browse all 77

Trending Articles



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