This post originated from an RSS feed registered with Agile Buzz
by Keith Ray.
Original Post: Loops and OO and Parallelism
Feed Title: MemoRanda
Feed URL: http://homepage.mac.com/1/homepage404ErrorPage.html
Feed Description: Keith Ray's notes to be remembered on agile software development, project management, oo programming, and other topics.
A person in a dream I had this morning said that whenever you have code that has loops operating on lists, your code is not "object-oriented". Now I don't necessarily agree or disagree, but this was the example I thought of (in my dream):
app tells book 'change margins'
In some implementations, the cascade of method calls to implement that might be:
book loops through its chapters, tells each chapter to 'change margins'.
chapter loops through its pages, tells each page to 'change margins'.
But it could be different. What if the chapters share margin objects from the book, and the pages share margin objects from the chapters? If the data is shared, and not duplicated, then we don't need to loop everywhere changing it. (Though we probably still need to loop somewhere to inform all these objects that the data they depend on changed, and they need to re-layout themselves.)
app tells book to 'change margins'
book tells margins to 'change'.
margins tells all of its dependents (pages) to update themselves. (implied loop)
each page tells all of its dependents (chapters) to update themselves. (implied loop)
each chapter tells all of its dependents (the book) to update themselves.
[but what if pages are dependents of chapters? infinite loop? hmm.]
This dream was partly inspired by an article in MacTech magazine that I read, about parallelizing loops in Fortran-90 and C/C++ using some options available in Intel's compilers for MacOS X. With a couple of C pragmas (or the Fortran equivalent), the compiler takes care of splitting loop processing over multiple CPUs -- doing that yourself using Posix threads would take nearly a page of code, or somewhat smaller if you have threading classes to use.
(It also struck me that Fortran-90 is quite a different language than the Fortran I learned in the 1980's.)
Could you parallelize the looping message cascades of this book / margins example? Maybe. The loops that re-lays out each page and chapter needs one or two pieces of information from the previous page: the previous page number (so we can compute current page number = previous page number + 1). But in fact, there may be more pages than before, if the margins are made smaller. Or fewer pages if the margins are made larger. Maybe "page" is a just a flyweight object and we really only have "book" and "chapter" (or "section") where formatting is concerned. But a chapter still needs to know the last page number of the previous chapter, to get its own page numbering right.