Skip to content

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

@Torrencem

Description

@Torrencem

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions