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

Inefficient implementation of UList::swap ?

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 30, 2015, 17:29
Default Inefficient implementation of UList::swap ?
  #1
Member
 
Join Date: Aug 2015
Posts: 37
Rep Power: 10
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, 06:14
Default
  #2
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,975
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
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 11:32. Reason: see "edit:"
wyldckat is offline   Reply With Quote

Old   November 1, 2015, 12:13
Default
  #3
Member
 
Join Date: Aug 2015
Posts: 37
Rep Power: 10
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, 15:25
Default
  #4
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,975
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
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


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 Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Free CFD code with implementation of Menter's k-omega SST model nikola_m Main CFD Forum 2 May 8, 2020 12:13
Issues in FGM combustion model implementation Lisandro Maders OpenFOAM Programming & Development 15 April 22, 2020 19:18
a little confused with the implementation of compressibleInterFoam su_junwei OpenFOAM Running, Solving & CFD 2 May 7, 2014 17:33
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 13:28.