2016-07-27 9 views
-3

私は複数のプロセスを使ってPi計算のシミュレーションをコーディングしようとしています。 ランダムなdouble x、yの数値を1から-1, まで生成する関数があり、x^2 + y^2のいずれかを計算しようとしたときに< = 1、 の結果が巨大であり、その常に1.pow(double、2)を計算すると膨大な数(C)が返されます

icpi.cppよりも大きい(メインファイル):

#include <mpi.h> 
#include <iostream> 
#include "main_header.h" 
#include "dynamic.h" 
using namespace std; 

int main(int argc,char *argv[]) 
{ 

int numprocs, myid, i, root = 0, pointsInCircle = 0, pointsOutOfCircle = 0; 
boolean pointInside = TRUE; 
double point[DIMENSION], scatterTable[NUMBER_OF_SLAVES][DIMENSION], pi, t1, t2; 
MPI_Init(&argc,&argv); 
MPI_Comm_rank(MPI_COMM_WORLD,&myid); 
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);  
MPI_Status status; 
srand(time(NULL)); 
t1 = MPI_Wtime(); 
printf("Trying to scatter..\n"); 
fflush(stdout); 
MPI_Scatter(&scatterTable[0][0], DIMENSION, MPI_DOUBLE,  // Master process sends the first tasks to the slaves. 
    &point[0], DIMENSION, MPI_DOUBLE, root, MPI_COMM_WORLD); 
printf("Scatter successful\n"); 
fflush(stdout); 
if (myid == 0) { 
    for (i = 0; i < TOTAL_NUM_OF_POINTS; i++) { 
     MPI_Recv(&pointInside, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status); 
     if (pointInside) { 
      pointsInCircle++; 
      printf("point inside\n"); 
      fflush(stdout); 
     } 
     else { 
      pointsOutOfCircle++; 
      printf("point outside\n"); 
      fflush(stdout); 
     } 
     point[0] = randomPoint();  // X component 
     point[1] = randomPoint();  // Y component 
     printf("x = %f, y = %f\n", point[0], point[1]); 
     fflush(stdout); 
     MPI_Send(&point[DIMENSION], 2, MPI_DOUBLE, status.MPI_SOURCE, 0, MPI_COMM_WORLD); 
    } 
    point[0] = point[1] = END_OF_PI_CALC; 
    for (i = 0; i < NUMBER_OF_SLAVES; i++) 
     MPI_Send(&point[DIMENSION], 2, MPI_DOUBLE, status.MPI_SOURCE, 0, MPI_COMM_WORLD); 
    t2 = MPI_Wtime(); 
    pi = calculatePi(pointsInCircle, pointsOutOfCircle); 
    printf("Pi = %f.\n Time to calculate: %f", pi, (t2-t1)); 
} 
else { 
    while (point[0] != END_OF_PI_CALC || point[1] != END_OF_PI_CALC) { 
     pointInside = withinCircle(point[0], point[1]); 
     MPI_Send(&pointInside, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); 
     MPI_Recv(&point[DIMENSION], 2, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD,  &status); 
    } 
} 

if (myid == 0) 
    MPI_Finalize(); 
return 0; 
} 

dynamic.cpp:

#include "dynamic.h" 
#include <stdio.h>; 
#include <math.h> 
boolean withinCircle(double x, double y) { 
    double result = (pow(x,(double)2) + pow(y, (double)2)); 
    printf("result = %f", result); 
    fflush(stdout); 
    if (result <= 1) 
     return TRUE; 
    return FALSE; 
} 

double randomPoint() { 
    double range = (RANDOM_MAX - RANDOM_MIN); 
    double div = RAND_MAX/range; 
    return RANDOM_MIN + (rand()/div); 
} 

double calculatePi(int pointsWithin, int pointsOutside) { 
    double pi = (pointsWithin/pointsOutside) * 4; 
    return pi; 
} 

dynamic.h:

#ifndef DYNAMIC_H 
#define DYNAMIC_H 

#include "main_header.h" 
#define TOTAL_NUM_OF_POINTS 20000 
#define NUM_OF_POINTS_TO_SEND 1 
#define RANDOM_MAX 1 
#define RANDOM_MIN -1 
#define NUMBER_OF_SLAVES 3 
#define DIMENSION 2 
#define END_OF_PI_CALC 2 

boolean withinCircle(double x, double y); 

double randomPoint(); 

double calculatePi(int pointsWithin, int pointsOutside); 

#endif DYNAMIC_H 

main_header.h:

#ifndef MAIN_H 
#define MAIN_H 

#include <time.h> 
#include <stdlib.h> 

#define TRUE 1 
#define FALSE 0 


typedef int boolean;  // Defines a boolean datatype. 

#endif MAIN_H 

例wmpiexecから生じる:

x = -0.944151, y = 0.389386 
result = 17134570711043240115048918967982918960341742262641887592820622432866394633762913354831186160392586118427086670840176838705152.000000 
point outside 

(私は実際の計算の結果は右ではないかもしれません知っている、そのわずか一例)

+1

これはあなたに役立つ情報ではありません。この症状を引き起こす可能性のある問題がたくさんあり、結果を誤解している可能性があります。あなたのコードを表示し、使用している言語を教えてください。あなたが観察した特定の動作(観察、解釈ではありません)を教えてください。 – user2357112

+0

申し訳ありません、私は質問を編集したばかりで、十分に願っています –

+2

'RANDOM_MAX'と' RANDOM_MIN'とは何ですか?できるだけ問題に無関係なコードを削除して、それを実行して問題を見ることができるくらい十分な、あなたのコードの[MCVE](http://stackoverflow.com/help/mcve)を見せてもらえますか? – user2357112

答えて

0
double calculatePi(int pointsWithin, int pointsOutside) { 
    double pi = (pointsWithin/pointsOutside) * 4; 
    return pi; 
} 

これは間違っています。何が起こっているのかは、整数除算をしてから4を掛けて、を次にをdoubleにキャストすることです。だからあなたは階下の行動を取っている。

分割する前に、pointsWithinまたはpointsOutsideのいずれかをダブルにキャストする必要があります。

(それはまた間違っ式だが、それは別の問題です。)

+0

double pi =((double)pointsWithin /(double)TOTAL_NUM_OF_POINTS)* 4.0; しかし、これは問題ではないと私は思います。 x^2 + y^2を計算すると数字が間違っています –

+0

double pi = 4.0 * pointsWithin/pointsOutside; '(キャストは避けてください) – chux

0

私はMPI_Send関数とMPI_RECVを悪用してきた、そして今そのパワー権利を計算します。

関連する問題