Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pointer Casting in C++ interface (braid_Vector vs. BraidVector) #48

Open
Torrencem opened this issue Jul 9, 2020 · 1 comment
Open

Comments

@Torrencem
Copy link
Contributor

Currently, in the C++ interface to XBraid, when the user writes their subclass of BraidApp (in braid.hpp), they are given the unhelpful incomplete type braid_Vector, which they have to cast every time to a pointer of their own BraidVector type. For example, in examples/ex-01-pp.cpp:

int MyBraidApp::Sum(double       alpha,
                      braid_Vector x_,
                      double       beta,
                      braid_Vector y_)
{
   BraidVector *x = (BraidVector*) x_;
   BraidVector *y = (BraidVector*) y_;
   (y->value) = alpha*(x->value) + beta*(y->value);
   return 0;
}

This could be fixed by adding a template type to the BraidApp super class. For example, in the generic_cpp branch where I've made the change the declaration of MyBraidApp changes only slightly:

class MyBraidApp : public BraidApp<MyBraidVector>
{
...

Then all the methods can be written much nicer using the BraidVector type (which is typedef'd in this example to MyBraidVector *):

int MyBraidApp::Sum(double       alpha,
                    BraidVector x,
                    double       beta,
                    BraidVector y)
{
   (y->value) = alpha*(x->value) + beta*(y->value);
   return 0;
}

The pointer casting then takes place in the _BraidAppX functions automatically. I think this is pretty obviously nicer for the user, since there's never really a situation where they want to use braid_Vector in C++, and the pointer casts are probably free.

The only other small change this creates though is either:

  1. BraidCore must have a template type as well:
...
// Initialize Braid Core Object and set some solver options
BraidCore<MyBraidVector> core(MPI_COMM_WORLD, &app);
core.SetPrintLevel(2);
...

or
2. BraidCore's SetSpacialCoarsenAndRefine, SetSync and SetResidual methods (which take no arguments) can be changed to boolean optional arguments to the BraidCore constructor, so unless you use those methods, no change is needed to your code. This option is rendered in my generic_cpp_2 branch.

I don't know how flexible the API for the C++ interface is, but I hope this change can be considered

@Torrencem
Copy link
Contributor Author

For convenience, here are the links to the 2 branches with working examples of the changes:

option 1
option 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant