39 main(
int argc,
char *argv[])
44 int points, mine, i, count, total_count;
46 MPI_Init(&argc, &argv);
47 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
48 MPI_Comm_size(MPI_COMM_WORLD, &size);
49 srand((
unsigned int)rank);
53 fprintf(stderr,
"specify an input file\n");
55 MPI_Abort(MPI_COMM_WORLD, 1);
59 ifp = fopen(argv[1],
"r");
60 if (fgets(line,
sizeof(line), ifp) == NULL) {
61 fprintf(stderr,
"failed to read a file\n");
62 MPI_Abort(MPI_COMM_WORLD, 1);
68 MPI_Bcast(&points, 1, MPI_INT, 0, MPI_COMM_WORLD);
70 if (rank == size - 1) {
71 mine += points % size;
75 for (i = 0; i < mine; i++) {
76 float x = (float)rand() / ((float)RAND_MAX + 1.0F);
77 float y = (float)rand() / ((float)RAND_MAX + 1.0F);
78 if ( x * x + y * y < 1.0) {
82 MPI_Reduce(&count, &total_count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
85 char *ofilename = malloc(strlen(argv[1]) + 5);
86 strncpy(ofilename, argv[1], strlen(argv[1]) + 1);
87 strncat(ofilename,
".out", 4);
89 ofp = fopen(ofilename,
"w");
90 fprintf(ofp,
"%d/%d\n", total_count, points);
int main(int argc, char *argv[])
Main function.
#define LINELEN
Maximum length of a line of data.