简短的MPI程序(C代码),root中的声明变量不工作
因此,我对我的小MPI程序有一个疑问。 (我是编程的新手,所以它可能只是一些初学者的错误)
我制作了一个程序,该程序通过使用公式来计算PI:(1/n) * 4/(1+(1+(i/n)^2)的总和);
唯一的问题是,我必须定义root函数中的迭代次数,但是一旦我设置了围绕它的任何形式的牙套,程序就无法正常工作。有什么方法可以在根部外定义“ n”,但在根函数内给出一个值?还是只是一些牙套问题,如果我正确设置它们,它仍然可以正常工作。
谢谢预先的
代码:( 问题开始于“ if(process_rank == root)” - 是的,现在没有括号)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <mpi.h>
#include <math.h>
#define ROOT 0
void print_time(double time)
{
printf("TIME:%.10f\n", time * 1000.0); // Print execution time in ms
}
int main(int argc, char *argv[])
{
int communicator_size, process_rank; //n
double pi_appr; //time_elapsed
double PI_ORIGINAL = 3.141592653589793238462643; // Original pi value for comparison
double i, n; //size, error, rank
double result=0.0, sum=0.0, begin=0.0, end=0.0; //pi=0.0
MPI_Init(&argc, &argv); //error=
MPI_Comm_size(MPI_COMM_WORLD, &communicator_size); //
MPI_Comm_rank(MPI_COMM_WORLD, &process_rank); //&process_rank
//Synchronize all processes and get the begin time
MPI_Barrier(MPI_COMM_WORLD);
begin = MPI_Wtime();
if(process_rank == ROOT)
n = 1000000; // Root defines number of computation iterations
n = 1000000; //if i dont declare n here again it doesnt work
//Each process will caculate a part of the sum
for (i=process_rank; i<n; i+=communicator_size)
{
result += 4/(1+((i/n)*(i/n))); // for some reason pow() didnt work
}
//now we some up the results with MPI_Reduce
MPI_Reduce(&result, &sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
//Synchronize all processes and get the end time
MPI_Barrier(MPI_COMM_WORLD);
end = MPI_Wtime();
if (process_rank == ROOT)
{
pi_appr = (1/n)*sum; //calculate
printf("%f\n", end-begin); //we print the time by substracting the begin- from the end-time
printf("Computed pi: %.16f (Error = %.16f)\n", pi_appr, fabs(pi_appr - PI_ORIGINAL));
}
MPI_Finalize();
return 0;
}
So I have a question regarding my little MPI program. (I am new to programming so its probably just some beginners mistake)
I made a program which calculates pi by using the formula: (1/n) * the sum of 4/(1+(i/n)^2);
Only problem is, I have to define the number of iterations in the root function, but as soon as I set any kind of braces around it the program doesn't work anymore. Is there any way to define "n" outside of root but give it a value inside of the root function? Or is it just some braces problem and if I set them correctly it will still work fine.
Thanks in advance
Code: (Problem starts at "if(process_rank == ROOT)" - yes there are no braces right now)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <mpi.h>
#include <math.h>
#define ROOT 0
void print_time(double time)
{
printf("TIME:%.10f\n", time * 1000.0); // Print execution time in ms
}
int main(int argc, char *argv[])
{
int communicator_size, process_rank; //n
double pi_appr; //time_elapsed
double PI_ORIGINAL = 3.141592653589793238462643; // Original pi value for comparison
double i, n; //size, error, rank
double result=0.0, sum=0.0, begin=0.0, end=0.0; //pi=0.0
MPI_Init(&argc, &argv); //error=
MPI_Comm_size(MPI_COMM_WORLD, &communicator_size); //
MPI_Comm_rank(MPI_COMM_WORLD, &process_rank); //&process_rank
//Synchronize all processes and get the begin time
MPI_Barrier(MPI_COMM_WORLD);
begin = MPI_Wtime();
if(process_rank == ROOT)
n = 1000000; // Root defines number of computation iterations
n = 1000000; //if i dont declare n here again it doesnt work
//Each process will caculate a part of the sum
for (i=process_rank; i<n; i+=communicator_size)
{
result += 4/(1+((i/n)*(i/n))); // for some reason pow() didnt work
}
//now we some up the results with MPI_Reduce
MPI_Reduce(&result, &sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
//Synchronize all processes and get the end time
MPI_Barrier(MPI_COMM_WORLD);
end = MPI_Wtime();
if (process_rank == ROOT)
{
pi_appr = (1/n)*sum; //calculate
printf("%f\n", end-begin); //we print the time by substracting the begin- from the end-time
printf("Computed pi: %.16f (Error = %.16f)\n", pi_appr, fabs(pi_appr - PI_ORIGINAL));
}
MPI_Finalize();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在误解MPI的工作原理。它具有独立的过程和分布式内存。因此,如果您仅在一个过程中初始化变量,则不会在其他过程中初始化它。您可以使用
mpi_bcast
调用来传达从根部到其他进程的值。You are misunderstanding how MPI works. It has independent processes and distributed memory. So if you initialize a variable only in one process, it will not be initialized in other processes. You could use a
MPI_Bcast
call to communicate a value from the root to other processes.