ฉันเขียนโปรแกรมเพื่อค้นหาผลรวมขององค์ประกอบอาร์เรย์ด้วย MPI ทั้งรูทและผู้ปฏิบัติงานจะค้นหาผลรวมของส่วนและผู้ปฏิบัติงานจะส่งผลรวมบางส่วนไปยังรูทในตอนท้าย เมื่อฉันลองใช้อาเรย์ขนาดคงที่ก็ไม่มีปัญหาใดๆ แต่มันทำให้เกิดข้อผิดพลาดในการแบ่งส่วนหากฉันใช้ calloc ซอร์สโค้ดได้รับด้านล่าง:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define tag1 1 /* send from root to workers */
#define tag2 2 /* send from workers to root */
#define root 0
#define n_data 12
int main(int argc, char *argv[])
{
int total_sum, partial_sum;
int my_id, i, n_procs, n_portion;
MPI_Init(&argc, &argv);
MPI_Status status;
MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
MPI_Comm_size(MPI_COMM_WORLD, &n_procs);
n_portion=n_data/n_procs;
int *array = (int *)calloc(n_data, sizeof(int));
int *local = (int *)calloc(n_portion, sizeof(int));
if(my_id == root) {
/* initialize array */
for(i = 0; i < n_data; i++)
array[i]=i;
/* send a portion of the array to each worker */
for(i= 1; i < n_procs; i++)
MPI_Send( &array[i*n_portion], n_portion, MPI_INT,i, tag1, MPI_COMM_WORLD);
/* calculate the sum of my portion */
for(i = 0; i < n_portion; i++)
total_sum += array[i];
/* collect the partial sums from workers */
for(i= 1; i < n_procs; i++) {
MPI_Recv( &partial_sum, 1, MPI_INT, MPI_ANY_SOURCE,tag2, MPI_COMM_WORLD, &status);
total_sum += partial_sum;
}
printf("The total sum is: %d\n", total_sum);
}
else { /* I am a worker, receive data from root */
MPI_Recv( &local, n_portion, MPI_INT, root, tag1, MPI_COMM_WORLD, &status);
/* Calculate the sum of my portion of the array */
partial_sum = 0;
for(i = 0; i < n_portion; i++)
partial_sum += local[i];
/* send my partial sum to the root */
MPI_Send( &partial_sum, 1, MPI_INT, root, tag2, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}
ข้อผิดพลาดที่ฉันทำคือ:
-bash-4.1$ mpirun -np 3 distApprox
--------------------------------------------------------------------------
mpirun noticed that process rank 2 with PID 110834 on node levrek1 exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------
ขอบคุณที่ช่วยเหลือ.
n_portion
คืออะไร? - person PaulMcKenzie   schedule 16.12.2015n_portion = 4
. - person smtnkc   schedule 16.12.2015n_procs
เป็น 2 รูท จะคำนวณครึ่งหนึ่งของอาร์เรย์ และworker1
จะถูกคำนวณอีกครึ่งหนึ่ง - person smtnkc   schedule 16.12.2015calloc
พร้อมตัวแปรที่เราไม่รู้ว่ามันคืออะไร แล้วฮาร์ดโค้ด4
แทนที่จะใช้ตัวแปรล่ะ หากคุณทำเช่นนั้น คุณจะยังมีปัญหาอยู่หรือไม่? - person PaulMcKenzie   schedule 16.12.2015calloc()
บางทีมันอาจจะล้มเหลว - person John Zwinck   schedule 16.12.2015total sum
ข้อเท็จจริงนี้ทำให้ฉันเชื่อว่าคุณต้องเพิ่มระดับการเตือน เพิ่ม-wall
ลงในบรรทัดคำสั่งคอมไพเลอร์ของคุณ (สมมติว่าคุณใช้gcc
) - person Klas Lindbäck   schedule 16.12.2015local
แทนที่จะเป็น&local
ในการเรียกMPI_Recv
ใช่ไหม - person Martin Zabel   schedule 16.12.2015n_portion
เป็น 4 ดังนั้นฉันจึงเริ่มต้นtotal_sum = 0
ตามที่ Klas ชี้และเปลี่ยน&local
เป็นlocal
ตามที่ Martin ชี้ มันได้ผล! ขอบคุณ. - person smtnkc   schedule 17.12.2015