CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > OpenFOAM Programming & Development

Inefficient implementation of UList::swap ?

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old   October 30, 2015, 18:29
Default Inefficient implementation of UList::swap ?
  #1
New Member
 
Join Date: Aug 2015
Posts: 29
Rep Power: 2
knuckles is on a distinguished road
In OF 2.4., UList::swap is implemented as a member-by-member trade, which has computational complexity O(nElements) [see definition in https://github.com/OpenFOAM/OpenFOAM.../UList/UList.C]. It seems to me that it could instead be implemented by trading the data pointers of the two ULists, as is done in std::vector::swap. This has computational complexity O(1).

Is there a reason that swap is implemented this way?
knuckles is offline   Reply With Quote

Old   October 31, 2015, 07:14
Default
  #2
Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 9,531
Blog Entries: 39
Rep Power: 97
wyldckat is just really nicewyldckat is just really nicewyldckat is just really nicewyldckat is just really nicewyldckat is just really nice
Greetings knuckles,

As far as I can figure out, the likely reasons are as follows:
  • This is a template class. It has to be valid for any number of possible types of data storage, may it either be integers/labels or complex classes.
  • With this in mind, there are classes that overload the "operator=" method and which must be called when the item is copied between two variables, in order for things to be properly tagged or updated.
  • If the objective is to swap the contents, it might make more sense to swap directly the original variables of type "UList".
Nonetheless, I'm not familiar with OpenFOAM's complete source code, so it might make some sense to either change how "swap" works or to have another method that does internal pointer swap instead, for the situations where that is applicable. In which case, I suggest you try proposing this on the bug tracker.

Best regards,
Bruno

edit: I've taken a look at the origin of this method "swap" and it exists at least since OpenFOAM 1.1. Perhaps this could use some revising...
__________________

Last edited by wyldckat; October 31, 2015 at 12:32. Reason: see "edit:"
wyldckat is offline   Reply With Quote

Old   November 1, 2015, 13:13
Default
  #3
New Member
 
Join Date: Aug 2015
Posts: 29
Rep Power: 2
knuckles is on a distinguished road
Thanks for your input wyldckat!

Perhaps references are the motivation? Client code could store a reference to element 0 of the list, but this operation would actually bind the reference to the data for element 0. The current implementation of swap() would update the data that the reference points to, maintaining that referenceToZero = list[0], but swapping the data pointers would have no impact on the previously-stored reference to element 0. In code:

Code:
List<scalar> list = ...;

scalar& refToFirstElement = list[0];

list.swap(otherList);

assert(refToFirstElement == list[0]); // Not guaranteed  if swap() just exchanges pointers
Perhaps client code would find that surprising?
knuckles is offline   Reply With Quote

Old   November 1, 2015, 16:25
Default
  #4
Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 9,531
Blog Entries: 39
Rep Power: 97
wyldckat is just really nicewyldckat is just really nicewyldckat is just really nicewyldckat is just really nicewyldckat is just really nice
Hi knuckles,

Good point! I didn't think/remember about that possibility.
OpenFOAM's code is very reliant on accessing data by reference, given that it's more efficient in many situations, so that would certainly be a good reason for having a hard data swap.

Best regards,
Bruno
wyldckat is offline   Reply With Quote

Reply

Tags
lists, optimization

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Issues in FGM combustion model implementation Lisandro Maders OpenFOAM Programming & Development 8 September 29, 2015 10:30
a little confused with the implementation of compressibleInterFoam su_junwei OpenFOAM Running, Solving & CFD 2 May 7, 2014 17:33
Free CFD code with implementation of Menter's k-omega SST model nikola_m Main CFD Forum 1 November 9, 2012 10:39
Implementation issues of fvSchemes / laplacianScheme, in particular gaussLaplacianSch thomek OpenFOAM Programming & Development 0 October 18, 2010 05:10
Please explain the implementation of species transport Eqn in reactingFoam kallipygian OpenFOAM Running, Solving & CFD 0 October 13, 2008 07:29


All times are GMT -4. The time now is 08:42.