1
C++에서 OpenMPI를 사용하려고 했으므로 숫자 통합을 수행 할 작은 코드를 작성했습니다. 내 문제는 모두가 제대로 발생 곳이 줄을 실행하는 것 같지 DEOS이다 : C++에서 MPI를 실행할 때 함수 호출
integral = trapezintegration(local_a, local_b, local_n);
는 지금은 MPI는이 라인 옆에 올바르게 작동하는지 매우 확신합니다. local_a, local_b, local_n 및 rank_world을 인쇄 할 때 내가 얻을 :
0 3.75 2.5e+09 0
3.75 7.5 2.5e+09 1
7.5 11.25 2.5e+09 2
11.25 15 2.5e+09 3
내가 기대했던 excactly입니다. 필자가 integral_world를 인쇄 할 때. 내가 얻을 :
17.5781 2
17.5781 3
17.5781 1
17.5781 0
이 핵심 = 17.5781의 값을 가져야 만 rank_world = 0, 나에게 이상한 것 같다 부분입니다. 내 질문에, 어떻게 MPI 함수 호출을 만들 수 있습니다, 그래서 순위 dosen't 모든 rank_world == 0 값을 얻을? 대신
integral = integral + f(a+i*h);
이어야한다
#include <mpi.h>
#include <iostream>
double f(const double x){
return x*x;
}
double trapezintegration(const double a, const double b, const double n){
"a = start value of integration range";
"b = end value of integration range";
"n = number of integration slices";
double integral=0.0, h=(b-a)/n;
long loopbound = (long)n;
"integral = the value of the numeric integral";
"h = width of the numeric integration";
integral = -(f(a)+f(b))/2.0;
for (long i=1;i<=loopbound;i++){
integral = integral + f(i*h);
}
integral = integral*(b-a)/n;
return integral;
}
int main(){
// The MPI enviroment need to be initialized
MPI_Init(NULL, NULL);
// The program need to know how many processors that are avaible
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// The processors index is also needed to be known
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
// Now the execution of the program can be done
// If no processor index (rank) is specfied the code will be
// executed for all the processes
const double a=0.0, b=15.0, n=1e+10;
double integral, total_integral;
// Right now all of the processes have the same a and b
// now different a and b will be assigned to the processes
// The rank is the index of the processor going from
// 0 to WORLD_SIZE-1, all of the processes will now get
// different local_a and local_b
double local_a = (b - a)/world_size*world_rank;
double local_b = (b - a)/world_size*(world_rank+1);
double local_n = n/world_size;
std::cout << local_a << ' '<< local_b << ' ' << local_n << ' ' << world_rank << '\n';
integral = trapezintegration(local_a, local_b, local_n);
// All of the processes have now run the numerical integration
// for their given interval. All of the integrated parts need
// to be collected to get the total integration.
// Lets collect the result in Rank 0
std::cout << integral << ' ' << world_rank << '\n';
if (world_rank != 0){
MPI_Send(&integral,1,MPI_DOUBLE,0,555+world_rank,MPI_COMM_WORLD);
}
if (world_rank == 0){
total_integral = integral;
for (int i=1; i<world_size; i++){
MPI_Recv(&integral,1,MPI_DOUBLE,i,555+i,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
total_integral = total_integral + integral;
}
}
// if rank is different from rank 0, the result need to be send
if (world_rank == 0){
std::cout << total_integral << '\n';
}
// The MPI enviroment need to be closed when the calculation is finished
MPI_Finalize();
}
에 단일 통화, 무슨 부끄러운 실수를 통해입니다. MPI_Reduce()에 대한 힌트를 주셔서 감사합니다 –
여러분 환영합니다! 때때로, 모든 사람들은 여분의 두 눈을 필요로합니다 :-) –