Slashdot Mirror


Apple Open Sources Grand Central Dispatch

bonch writes "Apple has open sourced libdispatch, also known as Grand Central Dispatch, which is technology in Snow Leopard that makes it easier for developers to take advantage of multi-core parallelism. Kernel support is not required, but performance optimizations Apple made for supporting GCD are visible in xnu. Block support in C is required and is currently available in LLVM (note that Apple has submitted their implementation of C blocks for standardization)." Update: 09/11 15:32 GMT by KD : Drew McCormack has a post up speculating on what Apple's move means to Linux and other communities (but probably not Microsoft): "...this is also very interesting for scientific developers. It may be possible to parallelize code in the not too distant future using Grand Central Dispatch, and run that code not only on Macs, but also on clusters and supercomputers."

39 of 342 comments (clear)

  1. Awesome! by gers0667 · · Score: 5, Interesting

    I'm not too well versed in Cocoa development. I pushed some code that should have been in a separate thread into GCD, which requires you to use a block. All in all, I had to add an include, 1 line of code and a closing bracket.

    Apple has made some seriously cool stuff here.

    1. Re:Awesome! by ThePhilips · · Score: 4, Insightful

      Having done some multiprogramming, I have to tell that it is really an end-user technology. Less a tool for developer.

      One of the biggest problems on multi-CPU/core systems if to split appropriately CPUs between applications. That requires quite amount of testing and benchmarking. Then you simply configure max number of threads each application allowed to use. Obviously changing anything at later time when problem was found requires some effort too.

      With GDC that all now is much easier. Available CPU resources are presented to applications in a fashion of real-time batch queue. One doesn't need to configure thread pools per application anymore.

      That was never a problem from software development point of view - we just provide the 'max threads' parameter. But that was always problem from user/operator point of view who has to actually fill in the parameter.

      --
      All hope abandon ye who enter here.
    2. Re:Awesome! by Dog-Cow · · Score: 5, Informative

      GCD is entirely a developer technology. It's a library for crying out loud! The end-user never does anything with it.

      The whole point is to make multi-processing easy for the developer.

      I pity the fools who have to use the code you've written.

    3. Re:Awesome! by ThePhilips · · Score: 5, Insightful

      The question is... how is this different to Intel's Thread Building Bocks, or OpenMP, both of which are better supported and more widely available to non-Mac developers.

      If I'm not mistaken the technologies are for an application.

      GCD can coordinate all applications running on the same system.

      I guess having their own implemntation (for the mac) makes sense, as they can integrate it throughout the OS. I don't know anything about GCD either, but could it be used in Linux to make that a more parallel-friendly OS with less developer effort, and more standardisation of parallel execution?

      Theoretically yes.

      Apple here is in unique position.

      Most software developers care solely about their own application. Old example from the desktop. On Windows I have 7-zip archiver installed. I have dual core CPU and this 7-zip is configured to use the 2 cores. From prospective of software developers it's all what they can do: let users tell how much cores/CPUs can be used. But I also have a video encoding application installed - and also configured to use two cores. If I try to run them both in parallel, that would cause erroneous amount of context switching harming performance of both the tasks. In worst case that might make my desktop completely unresponsive. As user I'm also lazy to reconfigure every time applications how many CPUs they should use.

      Apple itself now produces number of applications which can utilize multiple CPUs (iTunes audio conversion, iMovie/QuickTime/FC video conversion, etc) and obviously they run into the problem that when applications left on their own to decide how much CPU resources they should use, system would overload leading to all the effects. Requiring user to reconfigure all the applications all the time is also kind of stupid.

      Since Apple is in control of the OS and applications - and their own software might suffer from the problem, they went out and implemented the solution: system-wide batch queue with a thread pool. They are still threads - local to the process - but they are scheduled on system-wide basis. You do not need to configure applications how many CPUs they should use - nor applications have to think about: they simply put tasks (to be threads) of the queue of GCD.

      I'm using the 'batch queue' term because this is the closest what exists now. Though classical UNIX batch queues are different in nature: those are processes and they are executed at some unknown point of time. GCD is real-time in its nature and its threads run immediately, unlike traditional batch queues which wait for system to be idle.

      --
      All hope abandon ye who enter here.
  2. GCD is task parallelism by tepples · · Score: 5, Informative

    It's a library for task parallelism using a thread pool, introduced in Mac OS X 10.6 (Snow Leopard). Wikipedia tells all.

  3. RTFA by yabos · · Score: 5, Informative

    "We recognize that libdispatch is a new technology and you likely have many questions. Here are some documentation resources for getting started:

    Introducing Blocks and Grand Central Dispatch
    Concurrency Programming Guide
    Grand Central Dispatch (GCD) Reference"

  4. Re:OK, I give up...what is it? by dave-tx · · Score: 3, Informative

    There's a decent article on wikipedia about it. Basically, it's Apple's multithreading algorithms.

    --

    >> "What would the robut do? Frame someone!"

  5. Re:OK, I give up...what is it? by Lord+Grey · · Score: 4, Informative
    Introducing Blocks and Grand Central Dispatch is what you're looking for.

    From the first paragraph:

    Grand Central Dispatch (GCD) is a revolutionary approach to multicore computing that is woven throughout the fabric of Mac OS X version 10.6 Snow Leopard. GCD combines an easy-to-use programming model with highly-efficient system services to radically simplify the code needed to make best use of multiple processors. The technologies in GCD improve the performance, efficiency, and responsiveness of Snow Leopard out of the box, and will deliver even greater benefits as more developers adopt them.

    libdispatch is the open source implementation of GCD.

    --
    // Beyond Here Lie Dragons
  6. Re:What? by Daniel_Staal · · Score: 5, Informative

    Blocks are sections of program that can be passed around between functions as arguments. They basically allow 'functional' programming in C.

    --
    'Sensible' is a curse word.
  7. Re:OK, I give up...what is it? by je+ne+sais+quoi · · Score: 3, Insightful
    I want the 5 seconds back that I just spent reading your comment. It would have taken you less time to google it and read the first few sentences of wikipedia than write that:

    Grand Central Dispatch (GCD), named for Grand Central Terminal, is an Apple technology used to optimize application support for multicore processors.[1] It is an implementation of task parallelism based on the thread pool pattern. It was first released with Mac OS X 10.6.

    --
    Gentlemen! You can't fight in here, this is the war room!
  8. Blocks and GDC by Anonymous Coward · · Score: 5, Informative

    Blocks:
    In Snow Leopard, Apple has introduced a C language extension called "blocks." Blocks add closures and anonymous functions to C and the C-derived languages C++, Objective-C, and Objective C++.
    Perhaps the simplest way to explain blocks is that they make functions another form of data. C-derived languages already have function pointers, which can be passed around like data, but these can only point to functions created at compile time. The only way to influence the behavior of such a function is by passing different arguments to the function or by setting global variables which are then accessed from within the function. Both of these approaches have big disadvantages
    Full Read: http://arstechnica.com/apple/reviews/2009/08/mac-os-x-10-6.ars/10

    Directly in line with blocks is Grand Central Dispatch (and this is, where blocks become really usefull):
    GDC is a a technology to resolve the concurrency conundrum by giving programmers a very easy way to split tasks into multiple sub-tasks which can then be loaded onto different threads/cpu. All this also works with normal threading, but GDC makes the process far easier, with the intention to prepare OSX for future multicore machines:
    http://arstechnica.com/apple/reviews/2009/08/mac-os-x-10-6.ars/12

    It does so by using blocks as separate tasks:
    http://arstechnica.com/apple/reviews/2009/08/mac-os-x-10-6.ars/13

    "When I first heard about Grand Central Dispatch, I was extremely skeptical. The greatest minds in computer science have been working for decades on the problem of how best to extract parallelism from computing workloads. Now here was Apple apparently promising to solve this problem. Ridiculous.

    But Grand Central Dispatch doesn't actually address this issue at all. It offers no help whatsoever in deciding how to split your work up into independently executable tasksâ"that is, deciding what pieces can or should be executed asynchronously or in parallel. That's still entirely up to the developer (and still a tough problem). What GCD does instead is much more pragmatic. Once a developer has identified something that can be split off into a separate task, GCD makes it as easy and non-invasive as possible to actually do so.

    The use of FIFO queues, and especially the existence of serialized queues, seems counter to the spirit of ubiquitous concurrency. But we've seen where the Platonic ideal of multithreading leads, and it's not a pleasant place for developers.

    One of Apple's slogans for Grand Central Dispatch is "islands of serialization in a sea of concurrency." That does a great job of capturing the practical reality of adding more concurrency to run-of-the-mill desktop applications. Those islands are what isolate developers from the thorny problems of simultaneous data access, deadlock, and other pitfalls of multithreading. Developers are encouraged to identify functions of their applications that would be better executed off the main thread, even if they're made up of several sequential or otherwise partially interdependent tasks. GCD makes it easy to break off the entire unit of work while maintaining the existing order and dependencies between subtasks." (source = above url)

  9. Re:What? by twoshortplanks · · Score: 4, Informative

    I'd recommend reading the relevant section of the ars technica review of mac os x 10.6

    --
    -- Sorry, I can't think of anything funny to say here.
  10. Re:OK, I give up...what is it? by cowscows · · Score: 5, Informative

    ArsTechnica always does a pretty thorough and reasonably technical review of each OSX release, and the latest one gives a pretty good explanation of GCD as well as Blocks.

    http://arstechnica.com/apple/reviews/2009/08/mac-os-x-10-6.ars

    The GCD stuff in particular starts on page 12, but the previous couple pages give a little bit of useful background on why it's important.

    --

    One time I threw a brick at a duck.

  11. Re:OK, I give up...what is it? by macshome · · Score: 4, Informative

    It's OpenCL that opens up the GPUs to general processing on 10.6. Although GCD certainly plays a role by dispatching threads to those resources.

    You should check out this astounding OpenCL demo here: http://www.macresearch.org/opencl_episode1

  12. Re:Apache and a threading framework by samkass · · Score: 3, Informative

    pthreads and fork/exec are the equivalent of assembly language for parallelism compared to GCD. The API makes it easy to create anonymous methods that can be parallelized, have dependencies, be put in serial or parallel queues, etc. Then the OS implementation can prioritize at a finely-grained level based on dynamic resource availability, relative process priority, etc., on a system-wide basis. (The OS implementation of GCD was already open-sourced as part of 10.6's Darwin xnu kernel release last week.)

    It's pretty nifty stuff. And it's good to see Apple continue MacOS X's tradition of openness and support of open source.

    --
    E pluribus unum
  13. GCD -vs- OpenMP by MobyDisk · · Score: 3, Informative

    It looks like GCD is very similar to OpenMP. I am always biased toward using an open standard, when possible. Since many compiler vendors support OpenMP, why didn't apple just implement that for Objective-C, instead of creating their own threading solution? Judging from the examples, GCD looks much cleaner and simpler. But that often comes with a price.

    1. Re:GCD -vs- OpenMP by jonesy16 · · Score: 5, Informative

      GCD and OpenMP have very little in common. OpenMP is a language extension. It requires the programmer to understand what environment their program is going to run in, what variables can be shared and how, etc. GCD merely asks you to identify blocks of code that are independent and it handles parsing them out to threads, variable replication, etc. It's the difference between providing detailed blueprints of a car (the OpenMP way) and just saying "I want a car" (the GCD way). You can *almost* think of GCD as a user-friendly frontend for OpenMP.

  14. Re:Apache and a threading framework by ThePhilips · · Score: 3, Insightful

    It's an old tech. But it's different this time around.

    Old thread pools are per process. This is a thread pool for the whole system. And that's new.

    IOW, with GCD you do not need to configure every application how much threads it should start. Applications do not need to bother with it anymore too: they simply queue batch tasks as they arrive and GCD guarantees that they will be executed. Without overloading system.

    Shortly, GCD is a system-wide replacement for old per-application thread pool configuration. Makes applications simpler and also doesn't force end-user to understand all oddities of multi-programming to get most out of their boxes.

    --
    All hope abandon ye who enter here.
  15. Re:OK, I give up...what is it? by jo_ham · · Score: 4, Insightful

    "Apple has open sourced libdispatch, also known as Grand Central Dispatch, which is technology in Snow Leopard that makes it easier for developers to take advantage of multi-core parallelism."

    First line of THE SUMMARY.

    I know it's not hip to RTFA, but it's at least a minimum requirement to read the very next line after the title, even while scrolling down eagerly to make a comment.

  16. Re:GPL violation? by Lemming+Mark · · Score: 4, Informative

    Well, I can see the logic of your concerns but Apple do actually seem to be fairly good at open sourcing infrastructure-related things. Sure they maintain a tight control on the user-facing stuff that makes Apple products distinctive - on the iPhone they even maintain a tight control on applications. But bear in mind they're working on an open source kernel, employ developers to work on the LLVM compiler (open source), open sourced an init-ish daemon (launchd) they developed, etc etc. On stuff that's "for geeks" they seem fairly enlightened wrt open source.

    It's quite surprising from a company like Apple but the fact that they manage to make surprising decisions like that looks like a strong technical management team at work, to me.

  17. Re:OK, I give up...what is it? by 99BottlesOfBeerInMyF · · Score: 4, Insightful

    Everyone who reads slashdot isn't an OSX ween and has no idea what "Grand Central Dispatch" is. Perhaps a sentence or two describing why it is important/useful would save users from following the link which doesn't provide that info either.

    Look I'm as annoyed by poor summaries as anyone, but it seems almost reflexive to complain about them these days. The summary clearly said it, "makes it easier for developers to take advantage of multi-core parallelism". I don't care if you've never heard of OS X and no nothing about it. That sentence right there tells you what effect it has and why it's useful. After that it's just details as to where and if it is applicable to some other project. I guess what I'm saying is, I thought this was a pretty decent summary, enough to know if you should read about it.

  18. Re:GPL violation? by UnknowingFool · · Score: 4, Insightful

    Apple has a long history of open source contributions since OS X. Apple has released parts of OS X as Darwin under a BSD license since they first released OS X. OS X was developed from OPENSTEP which came from NextStep which itself was based on BSD. The kernel itself is derived from XNU which was based on the Mach kernel. All of these components are covered by BSD licenses. From what I understand since Apple uses a lot of open source Unix programs like CUPS, etc, they do contribute to fixes and patches on a regular basis.

    --
    Well, there's spam egg sausage and spam, that's not got much spam in it.
  19. Re:OK, I give up...what is it? by Gilmoure · · Score: 4, Funny

    also known as Grand Central Dispatch, which is technology in Snow Leopard that makes it easier for developers to take advantage of multi-core parallelism.

    They kinda' snuck that one in.

    --
    I drank what? -- Socrates
  20. Re:Use Cilk by PacoCheezdom · · Score: 5, Insightful

    You don't think that libdispatch will be very genial to widespread usage, as it has a lot of OS-specific calls, which is an understandable position to take. But as an alternative you offer something whose "only caveat" is that it needs an entirely different compiler to build. A compiler whose most recent activity dates from two years ago.

    ... How is that a superior alternative?

  21. Re:Use Cilk by Dog-Cow · · Score: 4, Informative

    GCD is designed for small, short-running blocks of code. Read the Ars article on Snow Leopard for examples. Naturally, it will also handle longer-running threads gracefully.

    Whoever modded you informative is as ignorant as you.

  22. Re:What? by LO0G · · Score: 3, Interesting

    They're basically lambda functions which are a part of C++0x.

  23. Re:GPL violation? by Dog-Cow · · Score: 3, Insightful

    Apple actually owns CUPS now. But, yes, they still make it available under an OSS license.

  24. Re:...what is it? Check the apple web site... by Penguin+Follower · · Score: 4, Interesting

    Once developers start using GCD for their applications, you'll start noticing significant improvements in performance.

    Shoot, I already noticed the difference on my 2.5 yr old Mac Pro (1.1). First boot on 10.6 and I was like "wow, feels like a new machine again". All of the bundled apps have been recompiled (64 bit) and cleaned up (and apparently take advantage of GCD everywhere possible). I really didn't think I would see that much of a difference with 10.6 and really only upgraded because I could for $29 (I mean at that price, why not right?) I am very happy with my $29 purchase thus far. I've only had to work through a couple app incompatibilities (and as I have been able to work around them just fine, I am happy.) This is of course just my experience thus far with 10.6. I have no hard benchmark numbers for you. But I noticed right away the smoothness it brought to my older Mac Pro. And it was an easier upgrade than going from 10.4 --> 10.5.

  25. Re:Kamikaze development by Dog-Cow · · Score: 5, Insightful

    You are posting in a thread about the fact that Apple made their implementation open source and you are claiming vendor lock-in?

    Are you one of those rabid Apple-haters we see so often around here? Or are you just amazingly stupid?

  26. Re:Use Cilk by samkass · · Score: 4, Informative

    No, GDC is purely a "C with block extensions" API. The blocks are essentially anonymous methods you can pass directly in to functions. It's integrated at a much lower level than Objective-C, which is only used for the higher-level application framework in MacOS.

    Apple open-sourced the GDC API with this announcement, block extensions to C with LLVM implementation last week, and the OS support necessary as part of the xnu kernel Darwin release for 10.6.

    --
    E pluribus unum
  27. Re:GPL violation? by je+ne+sais+quoi · · Score: 3, Informative

    Here is the list of all the open source packages apple uses. These include the kernel and CUPS (under the 10.6 tab), as well as their own modified version of other open source packages like java or gcc (under the developer tab). Contrary to a lot of a the common "apple is teh proprietary satan!!1" posts on slashdot, apple acts just like you might expect a more or less decent proprietary unix distributor to act: they open source what they can but keep closed whatever they feel is necessary to maintain a competitive advantage against Microsoft or that would infringe on hardware sales.

    P.S. As in interesting tidbit, you'll notice that clamAV is posted there as well. Hmm, makes you wonder.

    --
    Gentlemen! You can't fight in here, this is the war room!
  28. Re:...what is it? Check the apple web site... by 644bd346996 · · Score: 5, Insightful

    What if you're running two applications that both are capable of monopolizing all your cpu time? How will your app know that it's only going to get 50% of the available cpu time form the OS, so it should only start threads for half the cpus?

    GCD decides how many threads a collection of tasks should be split across. If an app running on an 8-core machine wants to run 100 tasks, then they could be spread across anywhere from 1 to 8 threads, depending on what else is running. Since it's the OS that knows what else is running, it can make more intelligent decisions about how many threads should be running.

  29. The Key new feature of Grand central is by goombah99 · · Score: 5, Informative

    Grand central dispatch has many innovations, but the key feature it provides is that thread pooling is now handled by the OS not the program. This means that in a dynamic environment you don't have each application stepping on each other when they ask for too many threads --all total-- than the multi-core system can optimally handle. So if Mail asks for fifty threads and Firefox asks for fifty threads and CPU you are running on can realistically only handle 10 threads then GCD figures out how to manage things so you don't get a spinning beachball.

    It turns out a lot of tricks were required to do this including a lot of things like just in time compiling LVM and this C-Blocks stuff, but that's way over my head.

    --
    Some drink at the fountain of knowledge. Others just gargle.
    1. Re:The Key new feature of Grand central is by oatworm · · Score: 5, Informative

      Ars had a pretty good article on the subject. Fast-forward to page 8 of the review and go from there - they touch on LLVM, C blocks, and how Grand Central Dispatch works.

  30. I've been working with it in C by wandazulu · · Score: 5, Informative

    I've come to really like GCD; I haven't played with it much in Cocoa (Obj-C) but I've been moving some of the stuff I wrote a long time ago in C to use it and I think I can say that what it does is *really* *really* awesome. It helps when writing code to be run in parallel; it does is not help you in determining *what* should be done in parallel. By putting your work into queues, by way of closures (yeah, blocks, whatever...I'm sticking with the closure name), it's up to the underlying OS to determine what thread gets what work, and on what processor. Having worked with multithreaded stuff on Windows, and calling GetThreadAffinityMask or whatever it was, and being told that it's just a *hint* to the OS, which is free to ignore you, which it always did, GCD really does spread out the work evenly among my 16-proc MacPro, and then turns around and does it just as well on the dual-core mini.

    I've wanted something like this for years; a really decent OS thread scheduler that divides up the work on the other processors in a sensible fashion. I was even looking into how much effort it would take to write something like this from scratch for Linux, and now I don't even have to. Sweet!

    Caveats: This is in OS X only, so no iPhone GCD (at least, not yet...not really necessary until we have multi-core iPhones), and while I've lived with additions to C++ through the years (templates mostly), the idea of adding, well, anything to C seems strange, let alone something as run-time dependent as closures.

  31. Re:GPL violation? by 99BottlesOfBeerInMyF · · Score: 3, Insightful

    It's quite surprising from a company like Apple...

    Companies are defined by what they do. If a person surprised Apple is releasing technologies as open source projects, that just means that person has an inaccurate image of what kind of company Apple is and should pay more attention to what Apple does and less to espoused, unsupported opinions from astroturfers and zealots.

  32. Re:What? by wootest · · Score: 3, Informative

    They're an alternate implementation of lambda-like technology. Apple's Chris Lattner wrote the first public announcement on blocks and noted: "To head off the obvious question: this syntax and implementation has nothing to do with C++ lambdas. Blocks are designed to work well with C and Objective-C, and unfortunately C++ lambdas really require a language with templates to be very useful. The syntax of blocks and C++ lambdas are completely different, so we expect to eventually support both in the same compiler."

    If only due to type inference, I'd prefer the syntax of C++ lambdas, but Blocks aren't half bad at what they do within the frame of the C model.

  33. How ignorant and lazy you are by Santana · · Score: 4, Insightful

    I'm honestly surprised how ignorant and lazy the regular slashdotter has become with the years.

    Any self-respected geek should be already keeping up to date with Apple advancements which are and will be impacting techology in the years to come.

    If you people haven't noticed already, Apple has been consistently releasing libraries and server software as open source projects for the rest to pick up , use and modify, with liberal licenses.

    A friend of mine used to say (can't remember exactly... paraphrasing:)

    * Microsoft wants all software to be theirs
    * GNU wants all software to be free
    * BSD wants all software to be better

    And releasing GCD, gentlemen, is another master stroke by Apple, just like WebKit, Bonjour, LLVM, the list goes on, to share knowledge and advance technology by merit, not by forcing it down your throat thanks to the monopoly you have been handed.

    The term "block" is familiar to Ruby programmers. It's an old concept which Ruby has made easy to use and hence popular and actually useful.

    And here's another lesson which OpenBSD, Apple and Ruby have been putting to work without you noticing guys: any technology that is difficult to use, no matter how good it is, will not be used if gets in your way; the technology must be easy to deploy/use and unobstrusive to be actually used and useful.

    Just remember SELinux and how many people just disable it, no matter how good it is (which I don't think it is, but that's for another rant). Then compare it with the technology that OpenBSD has been implementing for memory protection which is unobstrusive and ready to use with no extra configuration. Same with Ruby blocks, which more programmers are using and a lot of software is benefitting from it now, even though higher order functions and closures have been around for ages.

    Having Ruby-like blocks in C and Objective-C is so COOL, you must appreciate that if you think you're serious at programming. Apple has already submitted it to be a standard. I believe MacRuby will benefit from this too, which is Ruby written in Objective-C, which implements Ruby classes as Objective-C classes, achieving incredible speed, taking advantage of Objective-C and LLVM technologies.

    Now, I want my late '90s Slashdot back please, where you could more easily find insightful and informative comments. There's a lot of garbage and Microsoft apologists nowadays.

    --
    The best way to predict the future is to invent it
  34. Re:Use Cilk by node+3 · · Score: 3, Insightful

    That's exactly my point. With cooperative multitasking, if you know everything that's going on, it can be more efficient than preemptive multitasking. Just like manually managing your threads can be more efficient than something like GCD.

    At the most fundamental level, cooperative is more efficient (if well programmed), just as manually managing threads (if well programmed) is more efficient.

    It's that "if well programmed" that's the killer. If your environment is completely known, *and* you are skilled, you have the potential to do a better job by hand. But in the real world, were your software is going to run on all sorts of computers with all sorts of different software and processing capabilities, you can't fine tune your program, so letting the system handle it works better.

    Looking at it differently, with cooperative multitasking or manually controlling threads, *if* you have complete knowledge of the system *and* you are sufficiently skilled, you can approach 100% efficiency. With cooperative multitasking, or thread management similar to GCD, you might reach only 90% efficiency. But if you have to build a cooperative multitasking or manually manages threads in a program, that is going to run on all sorts of computers, you may only manage perhaps an overall average 70% efficiency.

    And the fact that you can get that 90% efficiency with much less effort than you have to put in to get maybe around 70% (or whatever) efficiency, the benefits here are obvious.