I am trying to calculate the sum of an array A using the reduce command; note here that A is only visible to the master node/root (0).
I am getting the following error and I can't seem to figure out why. Also, broadcasting the part (N) still produces the same error.
Error:
[kali:74924:0:74924] Caught signal 11 (Segmentation fault: address not mapped to object at address (nil)) ==== backtrace (tid: 74924) ==== 0 /lib/x86_64-linux-gnu/libucs.so.0(ucs_handle_error+0x2dc) [0x7f14b5486a9c] 1 /lib/x86_64-linux-gnu/libucs.so.0(+0x28c8f) [0x7f14b5486c8f] 2 /lib/x86_64-linux-gnu/libucs.so.0(+0x28e4a) [0x7f14b5486e4a] 3 /lib/x86_64-linux-gnu/libc.so.6(+0x3c910) [0x7f14b564e910] 4 ./parts(+0x14f1) [0x557de43984f1] 5 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xcd) [0x7f14b56397ed] 6 ./parts(+0x113a) [0x557de439813a] ================================= [kali:74925:0:74925] Caught signal 11 (Segmentation fault: address not mapped to object at address (nil)) ==== backtrace (tid: 74925) ==== 0 /lib/x86_64-linux-gnu/libucs.so.0(ucs_handle_error+0x2dc) [0x7fb3324b0a9c] 1 /lib/x86_64-linux-gnu/libucs.so.0(+0x28c8f) [0x7fb3324b0c8f] 2 /lib/x86_64-linux-gnu/libucs.so.0(+0x28e4a) [0x7fb3324b0e4a] 3 /lib/x86_64-linux-gnu/libc.so.6(+0x3c910) [0x7fb332678910] 4 ./parts(+0x14f1) [0x5581e42d44f1] 5 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xcd) [0x7fb3326637ed] 6 ./parts(+0x113a) [0x5581e42d413a] ================================= [kali:74926:0:74926] Caught signal 11 (Segmentation fault: address not mapped to object at address (nil)) ==== backtrace (tid: 74926) ==== 0 /lib/x86_64-linux-gnu/libucs.so.0(ucs_handle_error+0x2dc) [0x7f7e8e8f9a9c] 1 /lib/x86_64-linux-gnu/libucs.so.0(+0x28c8f) [0x7f7e8e8f9c8f] 2 /lib/x86_64-linux-gnu/libucs.so.0(+0x28e4a) [0x7f7e8e8f9e4a] 3 /lib/x86_64-linux-gnu/libc.so.6(+0x3c910) [0x7f7e8eac1910] 4 ./parts(+0x14f1) [0x558b09e094f1] 5 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xcd) [0x7f7e8eaac7ed] 6 ./parts(+0x113a) [0x558b09e0913a] ================================= =================================================================================== = BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES = PID 74924 RUNNING AT kali = EXIT CODE: 11 = CLEANING UP REMAINING PROCESSES = YOU CAN IGNORE THE BELOW CLEANUP MESSAGES =================================================================================== YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Segmentation fault (signal 11) This typically refers to a problem with your application. Please see the FAQ page for debugging suggestions Codes:
w/out broadcast
#include <stdio.h> #include <mpi.h> #include <stdlib.h> #include <time.h> int main (int argc, char** argv) { int rank; int size; int sum = 0; int grand_sum = 0; int i; int *A; int N; int part; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); if (rank == 0) { do { printf("Enter the array size: "); scanf("%d", &N); } while (N <= 0); A = malloc(N * sizeof(int)); if (!A) { printf("Array too big!\nExiting the program...\n"); return -1; } part = N / size; srand(10); for (i = 0; i < N; i++) { A[i] = rand() % 10 + 1; grand_sum += A[i]; printf("A[%d] = %d\n", i, A[i]); } for (i = 1; i < size; i++) { MPI_Send(&part, 1, MPI_INT, i, 0, MPI_COMM_WORLD); MPI_Send(&A[i * part], part, MPI_INT, i, 0, MPI_COMM_WORLD); } } else { int part; MPI_Recv(&part, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); int Aw[part]; MPI_Recv(&Aw, part, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); for (i = 0; i < part; i++) { sum += A[i]; } } MPI_Reduce(&sum, &grand_sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0) { for (i = size * part; i < N; i++) { grand_sum += A[i]; } printf("\nThe grand sum is: %d", grand_sum); } MPI_Finalize(); } w/broadcast
#include <stdio.h> #include <mpi.h> #include <stdlib.h> #include <time.h> int main (int argc, char** argv) { int rank; int size; int sum = 0; int grand_sum = 0; int i; int *A; int N; int part; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); if (rank == 0) { do { printf("Enter the array size: "); scanf("%d", &N); } while (N <= 0); A = malloc(N * sizeof(int)); if (!A) { printf("Array too big!\nExiting the program...\n"); return -1; } part = N / size; srand(10); for (i = 0; i < N; i++) { A[i] = rand() % 10 + 1; printf("A[%d] = %d\n", i, A[i]); } for (i = 1; i < size; i++) { //MPI_Send(&part, 1, MPI_INT, i, 0, MPI_COMM_WORLD); MPI_Send(&A[i * part], part, MPI_INT, i, 0, MPI_COMM_WORLD); } } MPI_Bcast(&part, 1, MPI_INT, 0, MPI_COMM_WORLD); if (rank != 0) { //MPI_Recv(&part, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); int Aw[part]; MPI_Recv(&Aw, part, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); for (i = 0; i < part; i++) { sum += A[i]; } } MPI_Reduce(&sum, &grand_sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0) { for (i = size * part; i < N; i++) { grand_sum += A[i]; } printf("\nThe grand sum is: %d", grand_sum); } MPI_Finalize(); } 51 Answer
After rereading my code, I've spotted a mistake at the for loop, of the else statement:
for (i = 0; i < part; i++) { sum += A[i]; } A is not visible here; thus the error. Aw should have been used here instead.
Correct code:
for (i = 0; i < part; i++) { sum += Aw[i]; } 1