I quit. This is the most frustrating thing I've ever had to do. Even a non dynamic int array causes segfault. But if I declare it as a float/char whatever array, it works alright.
Update: If i remove the line MPI_Scatter(A[0], N, MPI_INT, A_row, N, MPI_INT, 0, MPI_COMM_WORLD);
it works fine. Problem is I need it...
I'm working on a program but I have a bizarre problem.
The following code works fine (if we suppose that N is a multiple of p):
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
void main(int argc, char** argv)
{
int my_rank, p, N, **A, *diagonals, *A_row;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
if (my_rank == 0) {
N = 4;
int *mem = malloc(N * N * sizeof(int));
A = malloc(N * sizeof(int*));
for(int i = 0; i < N; i++)
A[i] = mem + N*i;
}
MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD);
A_row = malloc (N * sizeof(int));
MPI_Scatter(A[0], N, MPI_INT, A_row, N, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Finalize();
}
However, I need to allocate another array (diagonals), like this:
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
void main(int argc, char** argv)
{
int my_rank, p, N, **A, *diagonals, *A_row;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
if (my_rank == 0) {
N = 4;
int *mem = malloc(N * N * sizeof(int));
A = malloc(N * sizeof(int*));
for(int i = 0; i < N; i++)
A[i] = mem + N*i;
diagonals = malloc (N * sizeof(int));
}
MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD);
A_row = malloc (N * sizeof(int));
MPI_Scatter(A[0], N, MPI_INT, A_row, N, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Finalize();
}
I get this segmentation fault (if it helps at all):
[teo-VirtualBox:02582] *** Process received signal ***
[teo-VirtualBox:02582] Signal: Segmentation fault (11)
[teo-VirtualBox:02582] Signal code: Address not mapped (1)
[teo-VirtualBox:02582] Failing at address: 0x1
[teo-VirtualBox:02582] [ 0] /lib/x86_64-linux-gnu/libpthread.so.0(+0x113d0)[0x7faecc8d23d0]
[teo-VirtualBox:02582] [ 1] a[0x400c85]
[teo-VirtualBox:02582] [ 2] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7faecc511830]
[teo-VirtualBox:02582] [ 3] a[0x4009a9]
[teo-VirtualBox:02582] *** End of error message ***
Am I missing something obvious?
By the way, I'm not using free(), or doing anything specific because this is not the complete code. It's just a side file that I created for testing.