Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
2464 Discussions

Collection of simple working codes for various TBB components

sinedie
Beginner
438 Views
Hello,
In the past few days I tried learning TBB. I faced difficulties due to some C++ concepts that I was not comfortable with as well as lack of complete yet very simple examples for TBB. So I made a site and posted my toy programs. I request you kindly have a look at it and send me feedback or improvisations or corrections in the same. I hope it will be of some use to other beginners.

The site is at: http://sites.google.com/site/samplecodesproject/Home

TIA for any feedback and especially for the help that I got from here to understand TBB.

With best wishes,
-S
0 Kudos
1 Solution
Anton_M_Intel
Employee
438 Views
Quoting - sinedie
I request you kindly have a look at it and send me feedback or improvisations or corrections in the same. I hope it will be of some use to other beginners.

I took a look at some examples and I'd like to give few hints how to improve example for concurrent_hash_map.
1. You don't need hash_func explicitly defined for such a standard type as std::string. There is default hash_compare argument. But it may be fine if it shows how to write your own hash_compare class.
2. Actually, you don't need "chm_acc.release();" before the end of scope or before another call to a method with this accessor in arguments. It is necessary only to release access ASAP if the scope has other operations not related to the accessor.
3. To check existence of a key, use count() instead of find(). Using accessor here is needless overhead.
4. Performance tip: the container's interface is not perfect for serial operations, but at least, it has equal_range() that has serial semantics and thus less overhead than find() or count().

View solution in original post

0 Kudos
8 Replies
robert-reed
Valued Contributor II
438 Views
Quoting - sinedie
In the past few days I tried learning TBB. I faced difficulties due to some C++ concepts that I was not comfortable with as well as lack of complete yet very simple examples for TBB. So I made a site and posted my toy programs. I request you kindly have a look at it and send me feedback or improvisations or corrections in the same. I hope it will be of some use to other beginners.

Have you spent any time going through either the TBB tutorial (available with the distribution and from the website) or James Reinders' book on the subject? The thing that I note in quickly looking through your website that there are a lot of details explored that should not be necessary for a beginning user to be concerned about, details that would be of more use to someone intending to actually modify the library.

Much of the material on functors should be old hat to anyone familiar with the Standard Template Library. If our tutorial materials are still set too complex for new users to make sense of, we'd certainly appreciate that feedback, and with specific examples of content that you were not able to follow. Our goal is to give everyone the tools and background to be successful in parallel programming; if we're falling short on that task, we'd love to know how to make it better.

0 Kudos
sinedie
Beginner
438 Views

Have you spent any time going through either the TBB tutorial (available with the distribution and from the website) or James Reinders' book on the subject?


Yep, quite a lot in fact.

Though I have used C, and C++ that comes with STL with templates, etc. quite a lot, my knowledge was more or less confined to some popular (yet effective) books. I also help many others learn as a call on duty, but I must admit that neither C or C++ are my primary languages. Never-the-less, I was more or less sure that my knowledge should be more than adequate to get going with TBB, as it is also a library-based approach. I also had to take a few lectures on parallel computing hence I was familiar with the basic concepts (thanks to Introduction to Parallel Computing by Grama et al).

So I do understand a bit more than a naive beginner in trying to teach/learn parallel coding from scratch, and I have come to like TBB the most during the past week or two. I am going to ask folks over here to parallelize various common tasks and algorithms , and perhaps, try implementing some data structures (esp related to trees and graphs). If the implentations work, it is fine and we shall share, else, we would have learned something in the process anyway. It is towards this necessity to help me teach others, I have made the site.

I am sorry if I offended TBB teams/users, that was not the intention. I hope I clarified it to your satisfaction.

With best regards,
-S

0 Kudos
Anton_M_Intel
Employee
439 Views
Quoting - sinedie
I request you kindly have a look at it and send me feedback or improvisations or corrections in the same. I hope it will be of some use to other beginners.

I took a look at some examples and I'd like to give few hints how to improve example for concurrent_hash_map.
1. You don't need hash_func explicitly defined for such a standard type as std::string. There is default hash_compare argument. But it may be fine if it shows how to write your own hash_compare class.
2. Actually, you don't need "chm_acc.release();" before the end of scope or before another call to a method with this accessor in arguments. It is necessary only to release access ASAP if the scope has other operations not related to the accessor.
3. To check existence of a key, use count() instead of find(). Using accessor here is needless overhead.
4. Performance tip: the container's interface is not perfect for serial operations, but at least, it has equal_range() that has serial semantics and thus less overhead than find() or count().
0 Kudos
sinedie
Beginner
438 Views

I took a look at some examples and I'd like to give few hints how to improve example for concurrent_hash_map.
1. You don't need hash_func explicitly defined for such a standard type as std::string. There is default hash_compare argument. But it may be fine if it shows how to write your own hash_compare class.
2. Actually, you don't need "chm_acc.release();" before the end of scope or before another call to a method with this accessor in arguments. It is necessary only to release access ASAP if the scope has other operations not related to the accessor.
3. To check existence of a key, use count() instead of find(). Using accessor here is needless overhead.
4. Performance tip: the container's interface is not perfect for serial operations, but at least, it has equal_range() that has serial semantics and thus less overhead than find() or count().

Thanks Anton. I have updated the example with your feedback. In (4) are you referring to checking if the second iterator is not the end() is more efficient compared to find() and count()? As count() only returns 0 or 1 unlike count algorithm in STL that returns the number of times something occurs, I am unable to see why it was implemented in a more inefficient way for the purpose... Or is it that I am confusing efficiency (time) with overhead (space)?
0 Kudos
Anton_M_Intel
Employee
438 Views
Quoting - sinedie

Thanks Anton. I have updated the example with your feedback. In (4) are you referring to checking if the second iterator is not the end() is more efficient compared to find() and count()? As count() only returns 0 or 1 unlike count algorithm in STL that returns the number of times something occurs, I am unable to see why it was implemented in a more inefficient way for the purpose... Or is it that I am confusing efficiency (time) with overhead (space)?

(4) is more efficient than find() and count() because it does not lock anything - but it is for serial code only. In the same way, count() is faster than find() because it does not lock an item.
count() actually has the same purpose and semantics as STL's map::count(), it returns number of keys that are stored in the container, but since it is contaner with unique keys, it may return only 0 or 1.
So, I meant overhead by time (cycles).

0 Kudos
sinedie
Beginner
438 Views

(4) is more efficient than find() and count() because it does not lock anything - but it is for serial code only. In the same way, count() is faster than find() because it does not lock an item.
count() actually has the same purpose and semantics as STL's map::count(), it returns number of keys that are stored in the container, but since it is contaner with unique keys, it may return only 0 or 1.
So, I meant overhead by time (cycles).


Thanks Anton for the clarification. Prima facie I am unable to understand why equal_range() is more efficient than count(), or in otherwords, how does being only serial is better given the fact that count() does not lock too. I do appriciate the tip, but if it is possible to explain and you have time, please do. It is quite interesting as it is not something that is obvious.

Thanks again,
-S
0 Kudos
Anton_M_Intel
Employee
438 Views
Quoting - sinedie

Thanks Anton for the clarification. Prima facie I am unable to understand why equal_range() is more efficient than count(), or in otherwords, how does being only serial is better given the fact that count() does not lock too. I do appriciate the tip, but if it is possible to explain and you have time, please do. It is quite interesting as it is not something that is obvious.

Thanks again,
-S

There are locks on the internal structure (e.g. buckets) too. Take a look at the source code and other threads about hash map on the forum for more information.
0 Kudos
sinedie
Beginner
438 Views

There are locks on the internal structure (e.g. buckets) too. Take a look at the source code and other threads about hash map on the forum for more information.

Thanks! I shall look into the source code. :)
0 Kudos
Reply