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

Tpetra::Vector errors #13687

Closed
tread7284 opened this issue Dec 20, 2024 · 6 comments
Closed

Tpetra::Vector errors #13687

tread7284 opened this issue Dec 20, 2024 · 6 comments

Comments

@tread7284
Copy link

@tpetra

I am trying to construct Tpetra::Vector from a Tpetra::Map, but the compiler errors suggest that no matching constructor exists for this operation. Additionally, you encountered an error with accessing vector elements using operator[], which isn't supported by Tpetra::Vector.

I am getting the following errors in my model:

OA_try/main.cpp: In function ‘int main(int, char**)’:
/home/t**/OA_try/main.cpp:62:33: error: no matching function for call to ‘Tpetra::Vector::Vector(Tpetra::Map<int, long long int, Tpetra::KokkosCompat::KokkosDeviceWrapperNodeKokkos::Serial >&)’
62 | Tpetra::Vector x(map); // The state vector [pH, CO2]

  |                                 ^

note: candidate expects 0 arguments, 1 provided
/home/t**/OA_try/main.cpp:67:36: error: no matching function for call to ‘Tpetra::Vector::Vector(Tpetra::Map<int, long long int, Tpetra::KokkosCompat::KokkosDeviceWrapperNodeKokkos::Serial >&)’
67 | Tpetra::Vector dxdt(map); // Vector to store the time derivative

Any advice on how to over come these issues with Tpetra, would be greatly appreciated!

@cgcgcg
Copy link
Contributor

cgcgcg commented Dec 20, 2024

Tpetra::Vector is templated on scalar type, local and global ordinals and the node type. So you'd need to specify these. I think the constructor expects an RCP<const map_type>, not map_type. Maybe share how you are constructing the map?

@tread7284
Copy link
Author

@cgcgcg I have attached the section of code below:

#include <Sacado.hpp> // For stochastic modeling (Sacado)
#include <Teuchos_RCP.hpp>
#include <Teuchos_GlobalMPISession.hpp>
#include <Tpetra_Core.hpp>
#include <Tpetra_Vector.hpp>
#include <Tpetra_Map.hpp>
#include
#include
#include // For random number generation

// Define the stochastic parameterized variable (using Sacado)
template
class OceanAcidificationModel {
public:
OceanAcidificationModel(double emission_mean, double emission_stddev)
: emission_mean_(emission_mean), emission_stddev_(emission_stddev) {}

// Stochastic CO2 emission model (e.g., normally distributed uncertainty)
T co2_emissions(T t) const {
    // Using standard C++ random number generation instead of Sacado's random function
    static std::default_random_engine generator;
    std::normal_distribution<T> distribution(emission_mean_, emission_stddev_);
    return distribution(generator);
}

// pH model (example of a simple stochastic ODE system)
void compute_rhs(const Tpetra::Vector<double>& x, Tpetra::Vector<double>& dxdt, double t) {
    // Example dynamics for ocean acidification
    // x[0] = pH, x[1] = CO2 concentration
    auto x_data = x.getData();  // Get mutable data access
    double co2 = x_data[1];
    double pH = x_data[0];

    // Simple dynamical system with a stochastic term for CO2 emissions
    auto dxdt_data = dxdt.getDataNonConst();  // Mutable access to dxdt data
    dxdt_data[0] = -0.1 * co2 + 0.02 * co2_emissions(t); // pH change
    dxdt_data[1] = -0.05 * co2 + 0.1 * pH; // CO2 concentration change
}

private:
double emission_mean_;
double emission_stddev_;
};

int main(int argc, char* argv[]) {
// Initialize Tpetra (serial mode, no MPI)
Tpetra::ScopeGuard tpetraScope(&argc, &argv); // Corrected constructor

// Define the model parameters (mean and std deviation for stochastic emissions)
double emission_mean = 1.0;  // Mean CO2 emissions
double emission_stddev = 0.2; // Std dev of CO2 emissions
OceanAcidificationModel<double> model(emission_mean, emission_stddev);

// Use the default communicator for serial execution
Teuchos::RCP<const Teuchos::Comm<int>> comm = Tpetra::getDefaultComm();

// Set up the initial conditions for the ocean acidification model
const int numGlobalEntries = 2; // 2 variables (pH, CO2 concentration)
Tpetra::Map<int> map(numGlobalEntries, 0, comm); // No MPI, so we just use the default communicator

// Corrected: Initialize the vector with proper constructor
Tpetra::Vector<double> x(map);  // The state vector [pH, CO2]
auto x_data = x.getDataNonConst();
x_data[0] = 8.1; // Initial pH value
x_data[1] = 300; // Initial CO2 concentration (ppm)

Tpetra::Vector<double> dxdt(map); // Vector to store the time derivative

// Define the time integration parameters
double start_time = 0.0;
double end_time = 100.0; // Simulate over 100 time steps
double dt = 1.0; // Time step size

// Time-stepping loop
for (double t = start_time; t <= end_time; t += dt) {
    model.compute_rhs(x, dxdt, t);

    // Integrate the system (Euler method for simplicity)
    x.update(1.0, dxdt, dt);  // x = x + dt * dxdt

    // Output the results
    if (static_cast<int>(t) % 10 == 0) {
        auto x_data = x.getData();
        std::cout << "Time: " << t << ", pH: " << x_data[0] << ", CO2: " << x_data[1] << std::endl;
    }
}

return 0;

}

Thank you for your quick response!

@cgcgcg
Copy link
Contributor

cgcgcg commented Dec 20, 2024

auto map = Teuchos::rcp(new Tpetra::Map<int>(numGlobalEntries, 0, comm));
auto  x = Teuchos::rcp(new Tpetra::Vector<double>(map)); 

should do the trick. Tpetra might set default values for some of the template arguments, or they might get inferred from the map. Note that this changes the type of x from Tpetra::Vector<...> to Teuchos::RCP<Tpetra::Vector<...> >, i.e. to a pointer. For new code I would suggest to not use x.getDataNonConst but rather x.getLocalViewHost(Tpetra::Access::ReadWrite) as it will give you a Kokkos::View rather than a Teuchos object.

@tread7284
Copy link
Author

tread7284 commented Dec 23, 2024

@ Kokkos @cgcgcg

That fix the error I was getting in Teuchos, however I am now getting an error when I try to invoke the Kokkos::View.

This is the error, that I get:

OA_try/main.cpp:84:61: error: no match for call to ‘(Kokkos::View<const double**, Kokkos::LayoutLeft, Kokkos::Device<Kokkos::Serial, Kokkos::HostSpace>, Kokkos::Experimental::EmptyViewHooks, Kokkos::MemoryTraits<0> >) (int)’
   84 |             std::cout << "Time: " << t << ", pH: " << x_data(0) << ", CO2: " << x_data(1) << std::endl;

/OA_try/main.cpp:84:87: error: no match for call to ‘(Kokkos::View<const double**, Kokkos::LayoutLeft, Kokkos::Device<Kokkos::Serial, Kokkos::HostSpace>, Kokkos::Experimental::EmptyViewHooks, Kokkos::MemoryTraits<0> >) (int)’
   84 | Time: " << t << ", pH: " << x_data(0) << ", CO2: " << x_data(1) << std::endl;
      |        

I here is the updates sub module:

#include <Sacado.hpp>  // For stochastic modeling (Sacado)
#include <Teuchos_RCP.hpp>
#include <Teuchos_GlobalMPISession.hpp>
#include <Tpetra_Core.hpp>
#include <Tpetra_Vector.hpp>
#include <Tpetra_Map.hpp>
#include <iostream>
#include <cmath>
#include <random>  // For random number generation

// Define the stochastic parameterized variable (using Sacado)
template <typename T>
class OceanAcidificationModel {
public:
    OceanAcidificationModel(double emission_mean, double emission_stddev)
        : emission_mean_(emission_mean), emission_stddev_(emission_stddev) {}

    // Stochastic CO2 emission model (e.g., normally distributed uncertainty)
    T co2_emissions(T t) const {
        // Using standard C++ random number generation instead of Sacado's random function
        static std::default_random_engine generator;
        std::normal_distribution<T> distribution(emission_mean_, emission_stddev_);
        return distribution(generator);
    }

    // pH model (example of a simple stochastic ODE system)
    void compute_rhs(const Tpetra::Vector<double>& x, Tpetra::Vector<double>& dxdt, double t) {
        // Example dynamics for ocean acidification
        // x[0] = pH, x[1] = CO2 concentration
        auto x_data = x.getLocalViewHost(Tpetra::Access::ReadOnly);  // Get a const Kokkos::View of x data
        double co2 = x_data(1);
        double pH = x_data(0);

        // Simple dynamical system with a stochastic term for CO2 emissions
        auto dxdt_data = dxdt.getLocalViewHost(Tpetra::Access::ReadWrite);  // Get a mutable Kokkos::View for dxdt data
        dxdt_data(0) = -0.1 * co2 + 0.02 * co2_emissions(t); // pH change
        dxdt_data(1) = -0.05 * co2 + 0.1 * pH; // CO2 concentration change
    }

private:
    double emission_mean_;
    double emission_stddev_;
};

int main(int argc, char* argv[]) {
    // Initialize Tpetra (serial mode, no MPI)
    Tpetra::ScopeGuard tpetraScope(&argc, &argv);  // Corrected constructor

    // Define the model parameters (mean and std deviation for stochastic emissions)
    double emission_mean = 1.0;  // Mean CO2 emissions
    double emission_stddev = 0.2; // Std dev of CO2 emissions
    OceanAcidificationModel<double> model(emission_mean, emission_stddev);

    // Use the default communicator for serial execution
    Teuchos::RCP<const Teuchos::Comm<int>> comm = Tpetra::getDefaultComm();

    // Set up the initial conditions for the ocean acidification model
    const int numGlobalEntries = 2; // 2 variables (pH, CO2 concentration)
    Teuchos::RCP<Tpetra::Map<int>> map = Teuchos::rcp(new Tpetra::Map<int>(numGlobalEntries, 0, comm)); // Map with 2 entries

    // Corrected: Initialize the vector with proper constructor
    Teuchos::RCP<Tpetra::Vector<double>> x = Teuchos::rcp(new Tpetra::Vector<double>(map));  // The state vector [pH, CO2]
    auto x_data = x->getLocalViewHost(Tpetra::Access::ReadWrite);  // Mutable access to vector data
    x_data(0) = 8.1; // Initial pH value
    x_data(1) = 300; // Initial CO2 concentration (ppm)

    Teuchos::RCP<Tpetra::Vector<double>> dxdt = Teuchos::rcp(new Tpetra::Vector<double>(map)); // Vector to store the time derivative

    // Define the time integration parameters
    double start_time = 0.0;
    double end_time = 100.0; // Simulate over 100 time steps
    double dt = 1.0; // Time step size

    // Time-stepping loop
    for (double t = start_time; t <= end_time; t += dt) {
        model.compute_rhs(*x, *dxdt, t);

        // Integrate the system (Euler method for simplicity)
        x->update(1.0, *dxdt, dt);  // x = x + dt * dxdt

        // Output the results
        if (static_cast<int>(t) % 10 == 0) {
            auto x_data = x->getLocalViewHost(Tpetra::Access::ReadOnly);
            std::cout << "Time: " << t << ", pH: " << x_data(0) << ", CO2: " << x_data(1) << std::endl;
        }
    }

    return 0;
}

Thank you for your help in advance! Happy Holidays!

@cgcgcg
Copy link
Contributor

cgcgcg commented Dec 23, 2024

Ah, yep. Tpetra::Vector derives from Tpetra::MultiVector and hence the local views are of size localDim x 1. You can either do use

x_data(0, 0)

or

auto x_data_1d = Kokkos::subview(x_data,Kokkos::ALL(), 0);
x_data_1d(0)

@tread7284
Copy link
Author

@cgcgcg the first one fixed the issue! Thank you so much for your help!

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

No branches or pull requests

2 participants