Thread: Optimizing for parallell execution... on linux

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CornedBee View Post
    - I find your names confusing. The BoardCoordinates class doesn't make sense to me.
    I can only tell you its function. It contains valid coordinates in the board to examine to place a queen. This is because I remove rows and columns as I place queens.
    When I think about it, it might have been better to just merge BoardCoordinates and Board into a single class, but... well, right now, it works, so I don't want to go there. In the future, if I reuse this, I might do it.

    Neither does GetBoard (it gets a field).
    Probably a bad name when you mention it...

    - Why is GetBoard even a bound function? Why not make Board a proper class and give it an indexing call operator, so that you can write Board(x, y)? Boost.Bind can act as an optimization barrier (it holds a function pointer), and you definitely want the field access to be inlined.
    Initially, I didn't use any function at all. I just indexed the board using the index operator directly. But then I wanted to test location and whether it could speed up the function (ie, x first or y first), so I made it into a lambda.
    But gcc 4.4 doesn't support lambdas, so I just used a dirty hack to make it compile.

    - Given that your work queue is pretty much known at compile time, static work division by index would be far more efficient than the work queue: no locking, no fetching elements from a stack.
    I didn't want static division. One thread my run faster than another. So ideally, what I wanted to do is create n threads, wait until one of them finishes, schedule more work for it.
    There is no WaitForMultipleObjects, so this is the best I figured I could do it.

    - Whether you use a work queue or computed work division, a better choice as the basis of the implementation would be the Intel Threading Building Blocks (TBB) library. It has higher-level operations, such as a parallel for loop. You could model your problem very well with a parallel for loop, I think.
    If it's free, then it's definitely something I would consider in the future.
    However, there are requirements on this task. It must use threads, condition variables and locks. No abstraction mechanisms such as OpenMP.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    I didn't want static division. One thread my run faster than another. So ideally, what I wanted to do is create n threads, wait until one of them finishes, schedule more work for it.
    There is no WaitForMultipleObjects, so this is the best I figured I could do it.
    You have absolutely the right idea there, the problem is you are working with monkey wrenches for tools. Sadly it is very hard to do scalable parallelism with threads using the commonly available primitives. As an example of doing it right look at Cilk Plus, Intel TBB or Microsoft PPL. In my opinion we are doing a huge disservice by forcing students to work at the stupidly low level of threads and mutexes. You will never write anything scalable that way, it's just too difficult. The solutions are out there, it's frustrating that people aren't using them.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do not worry. This is just the basics to make sure proper understanding of the building blocks. It is not an absolute requirement for the future.
    I just looked at Intel's Thread Building Blocks, and I must say I am disappointing. All there is is a commercial license for $100 (for students) or GPL v2, both of which sucks. Screw you Intel.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Elysia View Post
    I just looked at Intel's Thread Building Blocks, and I must say I am disappointing. All there is is a commercial license for $100 (for students) or GPL v2, both of which sucks. Screw you Intel.
    If you're doing cross-platform projects for college, GPLv2 isn't a problem. And if you're doing your own stuff, which I believe you're mostly doing for Windows, use the Microsoft PPL, which is 95% source compatible: just change the includes and recompile, and it usually just works.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #20
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Do not worry. This is just the basics to make sure proper understanding of the building blocks. It is not an absolute requirement for the future.
    I just looked at Intel's Thread Building Blocks, and I must say I am disappointing. All there is is a commercial license for $100 (for students) or GPL v2, both of which sucks. Screw you Intel.
    Can you give me idea why you think that? I can forward your concerns to some people I know on the TBB team. For what you're getting, I would think $100 a cheap price.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Compare it to the GPL version. It's free. So clearly they are interested in providing it for free to the community, and that is what the GPL version is for.
    The commercial version is clearly targeted at corporations with lots of money to pay so they can keep their source code secret. I can understand they wanting $100 or so from corporations with commercial interests. But I am not a corporation, hence $100 is a lot.

    The reasons I dislike GPL (from my limited knowledge of it) are:
    - You are required to provide source code for the program. I don't like this. It means I cannot have control over the source of my own program. I am not entirely against giving out or making source public, but I like choice. GPL does not give me any choice.
    - You are required to either provide source with the binary distribution, or provide access to source via physical media (v3 fixes this AFAIK, but this is v2 we're talking about).

    EDIT: It's also a big concern how Intel's compiler cripples AMD processors with their compiler. AFAIK, this hasn't changed in any new version. With that in mind, who would pay $100+ when they have at least one (and intend to have more in the future) AMD processors?
    Last edited by Elysia; 02-08-2012 at 11:53 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    You are required to provide source code for the program. I don't like this. It means I cannot have control over the source of my own program. I am not entirely against giving out or making source public, but I like choice. GPL does not give me any choice.
    I think that's the point though, and also the reason why reciprocal licenses are often paired for such dual licensing schemes. You do have choice: use one license or the other, with different trade-offs. It just is just that you don't like both choices, but that's too bad because you're not the copyright holder... they could, after all, have offered just one license at full price for all users.

    Quote Originally Posted by Elysia
    But I am not a corporation, hence $100 is a lot.
    Consequently, it sounds like it is not that you dislike the GPL here, but that you dislike the price of the other license, e.g., maybe if they charged $20 instead, you would be happy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    Consequently, it sounds like it is not that you dislike the GPL here, but that you dislike the price of the other license, e.g., maybe if they charged $20 instead, you would be happy.
    I dislike GPL, and I dislike the price other license too, and that is the problem.
    *shrug* We all have a choice, though. If one doesn't like it, one doesn't have to use it. So that's precisely what I am going to do, at the moment.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    The reasons I dislike GPL (from my limited knowledge of it) are:
    - You are required to provide source code for the program. I don't like this. It means I cannot have control over the source of my own program. I am not entirely against giving out or making source public, but I like choice. GPL does not give me any choice.
    - You are required to either provide source with the binary distribution, or provide access to source via physical media (v3 fixes this AFAIK, but this is v2 we're talking about).
    I'm glad I asked, because I can assuage your fear. The TBB OSS license is GPLv2 with the libstdC++ runtime exception. This is the same exception which is used for the Linux kernel to allow binary works to link dynamically against the kernel (or in this case, TBB) without falling under the usual GPL restrictions. This means that as long as you use it in the form of a DLL and do not statically link it into your code, you are good to go. See here.

    EDIT: It's also a big concern how Intel's compiler cripples AMD processors with their compiler. AFAIK, this hasn't changed in any new version. With that in mind, who would pay $100+ when they have at least one (and intend to have more in the future) AMD processors?
    TBB is totally independent of the problem you are talking about. It is true that the Intel compiler as well as their IPP libraries have some broken CPU checks in them which cause the program to take the slow path on AMD CPUs, and that ticks me off just as much as it does you. But it has no bearing on TBB.

    We all have a choice, though. If one doesn't like it, one doesn't have to use it. So that's precisely what I am going to do, at the moment.
    Rolling your own isn't a realistic option (not if you want to do it right), so you're pretty much limited to either TBB (if you want to be portable) or PPL (if you're willing to stick with MS tools only). Obviously you can choose whatever you want, though.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by brewbuck View Post
    I'm glad I asked, because I can assuage your fear. The TBB OSS license is GPLv2 with the libstdC++ runtime exception. This is the same exception which is used for the Linux kernel to allow binary works to link dynamically against the kernel (or in this case, TBB) without falling under the usual GPL restrictions. This means that as long as you use it in the form of a DLL and do not statically link it into your code, you are good to go. See here.
    I'm glad you asked. This changes things dramatically! This means I can use it incoming projects.
    Obviously keeping track of these licenses and what exactly they mean is a chore.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    [Edit]IANAL; and all that fluff.[/Edit]

    The TBB OSS license is GPLv2 with the libstdC++ runtime exception. This is the same exception which is used for the Linux kernel to allow binary works to link dynamically against the kernel (or in this case, TBB) without falling under the usual GPL restrictions. This means that as long as you use it in the form of a DLL and do not statically link it into your code, you are good to go.
    Part of that is simply incorrect.

    [Edit]
    I can't speak for "TBB" because I do not know the actual license placed on the source files at all.

    That said, the Intel "TBB" website very clearly states that the "TBB" library is licensed under the "GPLv2" with "run-time exception". If the source files themselves do not support that view, I would not trust the library, but if that is not the case (The source files reference the license and the exception.) I see no reason you couldn't use the library in a commercial work even if you link it statically. (The license exception says nothing about how the files are linked. See below.)

    To my view, the crucial bit of the license questions set is when they recommend a commercial license. They do not state it with any legal weight, but they strongly imply that you would only need to purchase a commercial license if you didn't already have one by virtue of the purchase of other Intel software that provides a license and you wanted professional services and support for the library.
    [/Edit]

    Portions of the `libstdc++' library that have the "run-time exception" (all standard components) allows client code to link the library statically and dynamically without affecting the license. Without the exception client code would be "infused" with either the GPL, LGPL, or AGPL. This is a necessary exception for the `libstdc++' library because the headers themselves contain source. The GPL, without the exception, would cause any derivative works to fall under the GPL. The LGPL would not work because code exists within the headers "infusing" the client code. Again, the exception allows client code to link with the `libstdc++' library without demanding a particular license whether linked statically or dynamically including commercial works.

    The "Linux" kernel, in general, has no such exception for kernel interfaces, even the ones targeted specifically at modules. (Certain specific extensions do have such an exception.) The derivative work issue is a murky one when considering dynamic modules because of the associations with purely public interfaces versus actual derivative works. (This is thus a purely legal issue and would be dealt with on a case by case basis.) Some modules are unquestionably derivative works. Some modules are unquestionably not derivative works. Most of them, especially binary only module distributions, fall into an alarming grey area. Those routines, markers, and interfaces that ostensibly mark the module/kernel boundary are there for the purposes of documentation/encapsulation only and have no legal strength one way or the other. This goes a long way to saying that the practice of binary only modules are allowed but not actually intended or supported. Crucially, it is the nature of the module that determines whether or not the module would legally be required to be released under the GPL due to its status as a derivative work (of the kernel or other GPL work) or not; there is no simple marker within the license or interface that guarantees legal binary only distribution. It just happens that no one cares enough to pursue the issue for most cases.

    The false exception of the "Linux" has nothing to do with the legally binding wording of the "GPL" exception specified on some `libstdc++' components and carried by some other libraries. Many, but not all, files in the "Linux" source tree are marked with a simple disclaimer of the form "User applications do not have to be covered by the "GPL"." That has nothing to do with the real license exception and has no legal strength. The notion probably comes from people originally thinking that the change to "GPL" for the kernel would require any and all applications targeting the "GNU/Linux" platform to be "GPL" for the same reason derivative works of "GPL" covered libraries must be released under the "GPL". It is extremely unlikely, but not impossible I guess, that Linus could have ever made a legal argument that such applications are derivative works even if he wanted to make that claim. Again, that wording has no legal strength; an application that did directly use "GPL" components from the kernel source tree to implement non-trivial components in the application would almost certainly be seen as a derivative work and thus would need to be placed under the "GPL".

    Soma

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, I have taken a look on the source files of TBB, and they mention that source that uses TBB is exempt from the license, which I interpret as I can use whatever license I want for my source/binaries.

    Anyway, recently I have had to work with this a little more and this time with TBB.
    This time, I get a speedup of roughly 1.8x on my dual core and 3.67x on the linux machine. However, seeing as it is supposed to have 24 cores, that is rather pitiful...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, in case this means NUMA, then yes:

    numactl --hardware
    available: 2 nodes (0-1)
    node 0 size: 22196 MB
    node 0 free: 11574 MB
    node 1 size: 22220 MB
    node 1 free: 12449 MB
    node distances:
    node 0 1
    0: 10 20
    1: 20 10

    But apart from changing to TBB, there isn't much changed since last. The threads are lock free and execute independently. There should be no reason why there is such a huge slowdown since they should access only local memory until the time comes to merge the solutions...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    So I'm guessing that node 0 and 1 both have 12 cores at their disposal. Not sure how to confirm that, maybe "numactl --dump all"?

    It would be interesting to see what happens if you compile with "NUM_THREADS=12", and run it with "numactl --cpubind=0 --membind=0 program". That should run like a normal 12 core SMP system (in theory).

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Optimizing code
    By KBriggs in forum C Programming
    Replies: 43
    Last Post: 06-05-2009, 04:09 PM
  2. optimizing
    By h_howee in forum C++ Programming
    Replies: 4
    Last Post: 01-06-2008, 02:44 AM
  3. Optimizing my code
    By Lina in forum C Programming
    Replies: 30
    Last Post: 10-22-2006, 01:31 PM
  4. execution time in C , Linux
    By vinayonnet in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 01:06 PM
  5. Optimizing my program
    By jodders in forum C++ Programming
    Replies: 7
    Last Post: 03-05-2005, 09:12 AM