首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要解释MPI_Scatter()

需要解释MPI_Scatter()
EN

Stack Overflow用户
提问于 2013-07-24 14:43:00
回答 1查看 272关注 0票数 0

我试着用MPI来解决蒙特卡罗问题,假设我们产生x个兰特。num介于0和1之间,然后将n长度的数字发送到每个处理器。我正在使用散布函数,但我的代码运行不正常,它可以编译,但它不要求输入。我不明白MPI是如何在没有循环的情况下自动循环的,我的代码出了什么问题?

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "mpi.h"

main(int argc, char* argv[]) {
int         my_rank;       /* rank of process      */
int         p;             /* number of processes  */
int         source;        /* rank of sender       */
int         dest;          /* rank of receiver     */
int         tag = 0;       /* tag for messages     */
char        message[100];  /* storage for message  */
MPI_Status  status;        /* return status for    */
double *total_xr, *p_xr, total_size_xr, p_size_xr;  /* receive              */

/* Start up MPI */
MPI_Init(&argc, &argv);

/* Find out process rank  */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

/* Find out number of processes */
MPI_Comm_size(MPI_COMM_WORLD, &p);

double temp;
int i, partial_sum, x, total_sum, ratio_p, area;
total_size_xr = 0;
partial_sum = 0;
if(my_rank == 0){
    while(total_size_xr <= 0){
        printf("How many random numbers should each process get?: ");
        scanf("%f", &p_size_xr);
    }
    total_size_xr = p*p_size_xr;
    total_xr = malloc(total_size_xr*sizeof(double));

    //xr generator will generate numbers between 1 and 0
    srand(time(NULL));
    for(i=0; i<total_size_xr; i++)
    {
        temp = 2.0 * rand()/(RAND_MAX+1.0) -1.0;
        //this will make sure if any number computer stays in the boundry of 0 and 1, doesn't go over into the negative
        while(temp < 0.0)
        {
            temp = 2.0 * rand()/(RAND_MAX+1.0) -1.0;
        }
        //array set to total random numbers generated to be scatter into processors
        total_xr[i] = temp;
    }

}
else{
//this will be the buffer for the processors to hold their own numbers to add
p_xr = malloc(p_size_xr*sizeof(double));
printf("\n\narray set\n\n");
//scatter xr into processors
MPI_Scatter(total_xr, total_size_xr, MPI_DOUBLE, p_xr, p_size_xr, MPI_DOUBLE, 0, MPI_COMM_WORLD);
//while in processor the partial sum will be caluclated by using xr and the formula sqrt(1-x*x)
for(i=0; i<p_size_xr; i++)
{
    x = p_xr[i];
    temp = sqrt(1 - (x*x));
    partial_sum = partial_sum + temp;
}
//}


//we will send the partial sums to master processor which is processor 0 and add them and place 
//the result in total_sum
MPI_Reduce(&partial_sum, &total_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);

//once we have all of the sums we need to multiply the total sum and multiply it with 1/N
//N being the number of processors, the area should contain the value of pi.
ratio_p = 1/p;
area = total_sum*ratio_p;

printf("\n\nThe area under the curve of f(x) = sqrt(1-x*x), between 0 and 1 is, %f\n\n", area);

/* Shut down MPI */
MPI_Finalize();
} /* main */
EN

回答 1

Stack Overflow用户

发布于 2013-07-24 21:57:29

通常,在MPI程序中依赖STDIN/STDOUT是不好的。MPI实现可能会在启动作业的节点之外的其他节点上设置0级。在这种情况下,您必须担心转发是否正确。虽然这在大多数情况下都会起作用,但通常不是一个好主意。

更好的做法是将用户输入放在应用程序可以读取的文件中,或者通过命令行变量。这些将会更加便携。

我不明白你说的没有循环的MPI循环是什么意思。如果你仍然需要一个答案,也许你可以澄清这个评论。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17826954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档