0

I have a program that using C++ MPI. I have a structure Edge like this:

#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <climits>
#include <exception>
#include <string>
#include "mpi.h"
using namespace std;

/** STEP 2: STATE data sructure */
struct Edge {
    int fromPoint;
    int toPoint;
    int cost;
    // constructor
    Edge(int fromPointIn, int toPointIn, int costIn)
    : fromPoint(fromPointIn), toPoint(toPointIn), cost(costIn) {}
};

During my main, I created a vector of Edges:

int main(int argc, char **argv) {

    /* MPI_Init ... get rank, hostname ,... */

    if (rank==0) { 
       int V=0, E=0;
       vector<Edge> adjList;

       ifstream fin(inputFile.c_str());
       fin >> V >> E;
       while (!fin.eof()) {
           int v1, v2, c;
           fin >> v1 >> v2 >> c;
           Edge temp(v1+1,v2+1,c);
           adjList.push_back(temp);

            // THIS IS WHERE I DO NOT HOW TO PASS
            //MPI_Bcast(&adjList,adjList.size()*3,MPI_INT,0,MPI_COMM_WORLD);
       }
    }

My purpose is to Broadcast the adjList vector from processor 0 to all other processors. However, this may seem not to work
MPI_Bcast(&adjList,adjList.size()*3,MPI_INT,0,MPI_COMM_WORLD);
* send data = adjList
* size = adjList.size()*3 (what I thought is each vector value has 3 int from the structure).
* data type = MPI_INT
* send from processor = 0
* MPI_COMM_WORLD

To summarize, My purpose is to Broadcast the adjList vector from processor 0 to all other processors. I would be very grateful for any suggestion

NNguyen
  • 113
  • 1
  • 6
  • 1
    You should create a structure MPI datatype, then use `MPI_Bcast(&adjList[0], adjList.size(), datatype, ...);`. – Hristo Iliev Feb 29 '16 at 08:56
  • 1
    A simple fix would be to use `adjList.data()` instead of `&adjList`, and `size=adjList.size()*sizeof(Edge)`, assuming you have correctly initialized the receive buffer on all cores. – dee-kay Feb 29 '16 at 12:03

0 Answers0