trilinos / Trilinos

Primary repository for the Trilinos Project
https://trilinos.org/
Other
1.19k stars 565 forks source link

Conversion from boost shared_ptr to Teuchos::RCP #949

Closed guoluanjing closed 7 years ago

guoluanjing commented 7 years ago

Hello there,

I just joined gitHub to ask a question about the conversion between boost shared_ptr and Teuchos::RCP.

I included the header file of "Teuchos_RCPBoostSharedPtrConversionsDecl.hpp" from the source directory, and used the following template

template<class T>
RCP<T> rcp( const boost::shared_ptr<T> &sptr );

But I've got linking errors that reads: undefined reference to `Teuchos::RCP Teuchos::rcp(boost::shared_ptr const&)'.

My question to you is: Is this the way to go for this conversion, and if yes, what do you think I am doing wrong here?

Thanks a lot in advance, Luanjing

mhoemmen commented 7 years ago

@guoluanjing May I ask a few questions?

  1. Did you enable the Boost library in Trilinos?
  2. If so, what version of Boost are you using?
  3. Could you show us your code that attempts to convert from boost::shared_ptr to RCP?
mhoemmen commented 7 years ago

@trilinos/teuchos

guoluanjing commented 7 years ago

@mhoemmen Thanks for your response.

Here's my answer to your questions:

  1. We are using Trilinos 11.12.1, and it was not configured and built with Boost.
  2. Boost is built with our code as an external library, the version is 1.58.0i.
  3. Here is the part of the code of how I am using it:

    boost::shared_ptr<MyDataClass> R_;
    Teuchos::RCP<MyDataClass> R_rcp_;
    
    R_rcp_ = Teuchos::rcp(R_);

    I also have a new development: when I changed the include file from "Teuchos_RCPBoostSharedPtrConversionsDecl.hpp" to "Teuchos_RCPBoostSharedPtrConversions.hpp", the link error went away and the code compiles fine.

Thanks, Luanjing

mhoemmen commented 7 years ago

@guoluanjing Thanks for answering my questions!

We are using Trilinos 11.12.1, and it was not configured and built with Boost.

Teuchos::RCP's interface to boost::shared_ptr will not work unless Trilinos was configured and built with Boost. This is because Teuchos::RCP protects its interface to boost::shared_ptr with macros that are defined (or undefined) at Trilinos' configure time.

If you would like to enable Trilinos' support for Boost, you must at least set the CMake option Trilinos_ENABLE_Boost:BOOL=ON when reconfiguring. You may also have to point Trilinos to the location of your installed Boost library, if it is not installed as a system library.

I also have a new development: when I changed the include file from "Teuchos_RCPBoostSharedPtrConversionsDecl.hpp" to "Teuchos_RCPBoostSharedPtrConversions.hpp", the link error went away and the code compiles fine.

It's an accident that this worked ;-) It's really better to enable Boost in Trilinos.

bartlettroscoe commented 7 years ago

If you would like to enable Trilinos' support for Boost, you must at least set the CMake option Trilinos_ENABLE_Boost:BOOL=ON when reconfiguring. You may also have to point Trilinos to the location of your installed Boost library, if it is not installed as a system library.

Just one small correction. As described here:

you need to configure with -DTeuchos_ENABLE_Boost=ON. You can also configure with -DTPL_ENABLE_Boost=ON and that will have the same effect (since it will set Teuchos_ENABLE_Boost=ON by default) . Then you need to point Trilinos to the desired version of Boost by setting -DBoost_INCLUDE_DIR=<boost-dir>. See:

guoluanjing commented 7 years ago

Thanks for your prompt responses, @mhoemmen and @bartlettroscoe , we will rebuild it with Boost and report back how it works.

Happy holidays!

Luanjing

mhoemmen commented 7 years ago

@guoluanjing wrote:

Happy holidays!

You too! We would be interested in hearing more about your use case if you wouldn't mind sharing. The Teuchos interface to boost::shared_ptr was written before C++11. There might be some interest in a refresh to support std::shared_ptr. Would that be of interest to you?

bartlettroscoe commented 7 years ago

< There might be some interest in a refresh to support std::shared_ptr. Would that be of interest to you?

Conversions between std::shared_ptr and Teuchos::RCP already exist and are enabled automatically when C++11 is enabled (which it is by in Trilinos). See:

You can see unit tests for this running in:

(look for std_shared_ptr unit tests)

guoluanjing commented 7 years ago

@mhoemmen I'm using boost::shared_ptr only because our code was built using it, but I see that RCP actually provides similar functions we use. For the part of the code I'm modifying, it is easier to do the conversion rather than changing all shared_ptr to RCP. @bartlettroscoe Thanks for the info, good to know.

Thanks again for your help, I will close this issue. -- Luanjing

mhoemmen commented 7 years ago

@guoluanjing It would be really interesting for us to know what code you're working on, but if you're not willing to share that information in public, that's OK. Glad we could help!

guoluanjing commented 7 years ago

@mhoemmen I didn't realize that you were asking that. I certainly could share that. I'm working on a simulator that models the multiphase flow and reactions through fractured media, with applications to CO2 geological sequestration. What I'm doing is to test the matrix-free method of solving the nonlinear system, which is work in progress. By the way, we have rebuilt Trilinos with boost, and it is compiling with the conversion function of Teuchos::rcp() just by including "Teuchos_RCPBoostSharedPtrConversions.hpp" header file.

mhoemmen commented 7 years ago

I didn't realize that you were asking that. I certainly could share that. I'm working on a simulator that models the multiphase flow and reactions through fractured media, with applications to CO2 geological sequestration.

Thanks for sharing! It's helpful for us to have some idea about applications that use Trilinos. Feel free to get in touch with us if you would like to come to our yearly Users' Group Meeting (end of October usually).