0

I have this code which uses Euler's method to solve the simple ODE - dy/dt = 2y - 2t^2 - 3. The serial code takes random values for y_0 and computes the result. I want to use MPI to send each node its own y_0 value and collect the results. Do I just need to use MPI_SEND and MPI_RECV? Here is my serial code.

#include <iostream>
#include <cmath>

using namespace std;

double tmax = 2.0; //max time value
double h = 0.00000005; //grid spacing
double imax = tmax/h;

double tarray [40000000] = {0}; //array for time values
double xarray [40000000] = {0}; //array for x values
double yarray [40000000] = {0}; //array for y values

//serial calculation for euler's method problem 1.7.1
void final171(double y0)
{
    yarray [0] = y0;

    for (int k = 0; k < imax; k++) {
        tarray[k+1] = tarray[k] + h;
        yarray[k+1] = yarray[k] + h * (2 * yarray[k] - 2 * tarray[k] * tarray[k] - 3);
    }
    cout << "Our Solution Euler: " << yarray[int(imax)];
}


int main(int argc, const char * argv[])
{
    clock_t serialStart;
    double serialTotal;
    srand((unsigned)time(0));

    serialStart = clock();
    for (int i = 0; i < 128; i++) {
        double floor = 0.5, ceiling = 3.5, range = (ceiling - floor);
        double rnd = floor + double((range * rand()) / (RAND_MAX + 1.0));
        final171(rnd);
        cout << ", for y0 = " << rnd << endl;
    }
    serialTotal = (double)(clock() - serialStart) / (double)(CLOCKS_PER_SEC);

    cout << endl << "N = " << imax << endl;
    cout << "Serial Time (s)" << serialTotal << endl;

    return 0;
}

1 Answer 1

1

Using MPI_SEND/MPI_RECV would certainly work. You can also use MPI_SCATTER as it would do the same thing, but more efficiently.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.