我尝试使用MPI_Bcast
向其他进程发送一些数据。我有多个值,我尝试将其作为字符串发送(使用sprintf
和sscanf
来编写和读取它们),但在调用广播后,值不会发生变化。根据{{3}}中的答案,它应该工作并在所有进程中设置相同的值。或者我错过了什么?
以下是代码:
char send[128];
int pointSize;
double *x = new double;
double *sigma = new double;
double f_value;
memset(send, 0, sizeof(send));
sprintf_s(send, "%d %p %p %lf", points.front().getSize(), points.front().getX(), points.front().getSigma(), points.front().getValue());
printf("[%d]:Before bcast: %s \n", rank, send);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(&send, sizeof(send), MPI_CHAR, rank, MPI_COMM_WORLD);
sscanf_s(send, "%d %p %p %lf", &pointSize, &x, &sigma, &f_value);
printf("[%d]:After bcast: %d %p %p %lf \n", rank, pointSize, x, sigma, f_value);
输出2个进程:
[1]:Before bcast: 10 0000007DAF2D1620 0000007DAF2D0D20 0.000015
[0]:Before bcast: 10 000000482E545A20 000000482E546560 0.000020
[0]:After bcast: 10 000000482E545A20 000000482E546560 0.000020
[1]:After bcast: 10 0000007DAF2D1620 0000007DAF2D0D20 0.000015