Untitled

mail@pastecode.io avatar
unknown
actionscript
3 years ago
2.7 kB
1
Indexable
Never
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <mpi.h>
#include <vector>

using namespace std;

vector<int> vectorOfArrayToVectorOfInt(vector<int*> points, int N);
vector<int*> vectorOfIntToVectorOfArray(vector<int> one_dim, int N);
void printPoints(vector<int*> points, int N);

int main(int argc, char** argv) {
    vector<int> points, part_points;
    int a[12] = {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, b[12];
    int rank, size;


    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    //if (rank == 0) {
    //    for (int i = 0; i < 12; i++) {
    //        //int *point = new int[2] {i,i};
    //        a[i] = i;
    //        points.push_back(i);
    //        cout << " " << points[i] << endl;
    //    }
    //}
    //vector<int> one_dim_part;

    

    //vector<int> one_dim = vectorOfArrayToVectorOfInt(points, 2);

    //for (int i = 0; i < one_dim.size(); i++) {
    //    cout << " " << one_dim[i] << endl;
    //}
    //cout << "\n" << endl;

    //vector<int*> point_new = vectorOfIntToVectorOfArray(one_dim, 2);
    //printPoints(point_new, 2);
    

    part_points.resize(4);

    //MPI_Scatter(points.data(), 4, MPI_INT, part_points.data(), 4, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Scatter(a, 4, MPI_INT, b, 4, MPI_INT, 0, MPI_COMM_WORLD);

    //part_points = vectorOfIntToVectorOfArray(one_dim_part, 2);

    cout << "Array of proccess "<<rank << "("<<part_points.size() << "): " << endl;

    for (int i = 0; i < 4; i++) {
       //cout << " " << part_points[i] << endl;
        cout << " " << b[i] << endl;
    }
    //printPoints(part_points, 2);
    MPI_Finalize();

    return 0;
}

vector<int> vectorOfArrayToVectorOfInt(vector<int*> points, int N) {
    vector<int> numbers;

    for (int i = 0; i < points.size(); i++) {
        for (int j = 0; j < N; j++) {
            numbers.push_back(points[i][j]);
        }
    }

    return numbers;
}

vector<int*> vectorOfIntToVectorOfArray(vector<int> one_dim, int N) {
    int *coords = new int[N];
    vector<int*> points;
    int j = 0;
    for (int i = 0; i < one_dim.size(); i++) {
        coords[j] = one_dim[i];

        if (j == N - 1) {
            j = 0;
            points.push_back(coords);
            coords = new int[N];
        }
        else {
            j++;
        }
    }
    return points;
}

void printPoints(vector<int*> points, int N) {
    for (int i = 0; i < points.size(); i++) {
        for (int j = 0; j < N; j++) {
            cout << points[i][j] << ", ";
        }
        cout << endl;
    }
}