Flatten class and send with mpi

let say I have a header file like below.

I have problem in send buffer.why I can't write &iterator.next. what should I write instead anybody?anybody can help me?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef _FLATTEN_H_
#define _FLATTEN_H_
using namespace std;

class FlattenVector {

public:
	int n;

	vector<vector<size_t>::iterator> iStart;
	vector<vector<size_t>::iterator> iEnd;
	int currIndex;
	FlattenVector(vector<vector<size_t> >& v);
	bool hasNext();
	int next();
};

and the cpp file for this header be like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
FlattenVector::FlattenVector(vector<vector<size_t> >& v)
{

	n = v.size();
	currIndex = 0;
	iStart.resize(n);
	iEnd.resize(n);

	for (size_t i = 0; i < n; i++) {
		iStart[i] = v[i].begin();
		iEnd[i] = v[i].end();
	}
}

bool FlattenVector::hasNext()
{
	for (int i = 0; i < n; i++) {
		if (iStart[i] != iEnd[i])
			return true;
	}
	return false;
}
int FlattenVector::next()
{
	if (iStart[currIndex]== iEnd[currIndex]) {
		currIndex++;
		return next();
	}

	else
		return *iStart[currIndex]++;
}

#endif
#pragma once 

in the main


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int main(int argc, char *argv[])
{

	int num_procs, myrank;
	MPI_Init(&argc, &argv);
	MPI_Status status;
	MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
	MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
if (myrank==0)
{
    vector<vector<int> >
        v{ { 1, 2 },
           { 3 },
           { 4, 5, 6 },
           { 7, 8, 9, 10 } };
    FlattenVector iter(v);
for (int i_proc=0;i_proc<num_procs;i+proc++){


			MPI_Send(iter.next, 10, MPI_INT, i_proc, 0, MPI_COMM_WORLD);

}
}
else
{
int a[10];
MPI_Recv(a.data(), 10, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
}
}


Last edited on
@resabzr
The best thing is if you only use 1-d, flattened vectors in the first place - then you will have absolutely no need for flattening 2-d containers.
Sorry. I didn't understand,could you please explain more. In another way I defined a 1d vector from this 2d vector using push_back. And in that case I don't have any problem. I can s and receive. But I want to do it with class objects
Last edited on
A 2-d array of say a rows by b cols can be considered as a 1-d of a * b elements.

So d2[r][c] would be d1[r * b + c]
resabzr wrote:
But I want to do it with class objects


If by "do it" you mean using MPI communication then you can't. Fundamentally, it is pretty language-agnostic and it will send a 1-d buffer of data, not C++ class objects. Those who say only learn about C++ vectors and not simple arrays do you a massive disservice in terms of high-performance computing.

If you have a regular array of ni x nj elements then you only need to send the data and you get the (i,j) element as i * nj + j.

If you have a ragged array then you get the (i,j) element as nj(i) + j, where nj(i) is the number of elements preceding the ith row ... and you will have to send that collection of nj(i) separately.
but it's not a 2d array, it's a vector of vector. the problem is here

I have this class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class MPI_CALL {

public:
	int sendto;
	int receive;
	size_t Vector_size;

	MPI_CALL() = default;
	void Send_to(size_t sendto, VecIdx_t& Vector);
	void Recv_from(size_t receivefrom, VecIdx_t& Vector);
};

void MPI_CALL::Send_to(size_t sendto, VecIdx_t& Vector)
{

	size_t Vector_size = Vector.size();
	MPI_Send(&Vector[0], Vector_size, MPI_UNSIGNED, sendto, 0, MPI_COMM_WORLD);

}

void MPI_CALL::Recv_from(size_t receivefrom, VecIdx_t& Vector)
{
	MPI_Status status;
	size_t Vector_size = Vector.size();
	MPI_Recv(Vector.data(), Vector_size, MPI_UNSIGNED, receivefrom, 0, MPI_COMM_WORLD, &status);

}


now in the main I make an object of type MPI_CALL mpisend
then mpisend.Sendto(argumnets) and the same for receive, I don't underestande what is the problem in receive buffer which give me the error "Assertation failed"
Last edited on
resabzr wrote:
but it's not a 2d array, it's a vector of vector


You're not listening, @resabzr.

If that is the case then both &Vector[0] and Vector.data() will point to a std::vector, NOT to a 1-d array of data. MPI won't know what to do with it.
Last edited on
Those who say only learn about C++ vectors and not simple arrays do you a massive disservice in terms of high-performance computing.


... Should first learn vectors then c-style arrays - not c-style arrays then vectors. Same as learn std::string before c-style strings. Where there is a choice of how to do things, first learn the C++ way then the other way(s).
Last edited on
seeplus wrote:
Where there is a choice of how to do things, first learn the C++ way then the other way(s).


If you are only learning C++ then perhaps.

If you are learning "programming" then learn about fundamental entities that are as common to as many languages as possible. Standard arrays are common to many languages; std::vector is a specifically C++ entity.
> I have problem in send buffer
¿where? ¿what problem?

> why I can't write &iterator.next
¿do you get a compile error message that perhaps want to show us?

https://www.mpich.org/static/docs/latest/www3/MPI_Send.html
int MPI_Send(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm)
¿is that the function you are trying to use? ¿did you bother to read the signature?
thanks for your help. yes, the function is this,https://www.mpich.org/static/docs/latest/www3/MPI_Send.html,
but I solved it.
Topic archived. No new replies allowed.