problem with results

Hi,

i have the code below. I have an array, x, with size 8 and i initialize with ones. I want to initialize the x array with 11 using MPI_Gather and Scatter.

I use 2 processors, so i try to seperate the x array to 2 arrays with size 4 each and then i add +10 to each element in order to have the result i want (11 in all x array elements).

The problem is that finally the x array has only the first 4 elements equal to 11 while the other 4 (the last 4) elements take 'rubbish' numbers.

I think the problem is with MPI_Gather but i cannot figure it out. Any help please ?

the code:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

#include "mpi.h"
#include <iostream>
#include <stdlib.h>

using namespace std;

int N=8;

int main(int argc, char **argv)
{
	int rank,
		nprocs,
		*x,*r,
		count,
		*w,
		i=0;
	
	x = (int*)malloc(N*sizeof(int));
	r = (int*)malloc(N*sizeof(int));
	
	/* MPI Initialization */
	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);				// get process number for this process
	MPI_Comm_size(MPI_COMM_WORLD, &nprocs);				// number of processes
	
	count = N/nprocs;
	
	w = (int*)malloc(count*sizeof(int));
	
	if (rank == 0) {
		// Initialize the vector x
		for (i=0; i<N; i++)
			x[i]= 1;
	} // end master
	
	// distribute vector partitions among processes using MPI_Scatter
	MPI_Scatter(x, count, MPI_INT, x, N, MPI_INT, 0, MPI_COMM_WORLD);
	
	// computation (MASTER AND SLAVES)
	for (i=0;i<count;i++){
		w[i] = x[i] + 10;
	}
	
	// gather partial results from processes using MPI_Gather
	MPI_Gather(&w, count, MPI_INT, r, count, MPI_INT, 0, MPI_COMM_WORLD);
	
	if (rank == 0) {
		cout << "\n\nFINISH\n\n";
		cout << "\n\n";
		for (i=0; i < N; i++) {
			r[i] = w[i];
			cout << "r[" << i << "] = " << r[i] << "\n"; 
		}
	}  // end master
	
	MPI_Finalize();
}
There are only count items inside 'w', but you are trying to access N.
Out of bounds -> undefined behaviour.
i am actually confused with the mpi_gather and i dont really know if i use it correctly i want

the 1st processor to compute the addition (+10) of the elements 0,1,2,3 from the x array

and

2nd processor to compute the addition to the elements 4,5,6,7 from the x array.

A solution would be to set every array to size equal to the size of x, but in that way i wouldn't be able to gain the advantages from both processors.
Topic archived. No new replies allowed.