Commit adfef311025815f18da300936ff86998646191e8
1 parent
c7c188d740
Exists in
master
add n
Showing 4 changed files with 16 additions and 11 deletions Inline Diff
lab1/pi_mpi.c
View file @
adfef31
#include <stdio.h> | 1 | 1 | #include <stdio.h> | |
2 | #include <stdlib.h> | |||
#include <string.h> | 2 | 3 | #include <string.h> | |
4 | ||||
5 | ||||
#include <mpi.h> | 3 | 6 | #include <mpi.h> | |
#define N 100000000 | 4 | 7 | #define N 1000000000 | |
5 | 8 | |||
int main (int argc, char** argv) | 6 | 9 | int main (int argc, char** argv) | |
{ | 7 | 10 | { | |
int rank; | 8 | 11 | int rank; | |
int size; | 9 | 12 | int size; | |
long long int n; | 10 | 13 | long long int n; | |
long long int i; | 11 | 14 | long long int i; | |
12 | 15 | |||
double l_sum,total_sum, x, h; | 13 | 16 | double l_sum,total_sum, x, h; | |
14 | 17 | |||
MPI_Init(NULL, NULL); | 15 | 18 | MPI_Init(NULL, NULL); | |
16 | 19 | |||
MPI_Comm_size(MPI_COMM_WORLD, &size); | 17 | 20 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | 18 | 21 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
19 | 22 | |||
n=N; | 20 | 23 | n=N; | |
21 | 24 | |||
if(rank==0) | 22 | 25 | if(rank==0) | |
{ | 23 | 26 | { | |
if(argc==2) | 24 | 27 | if(argc==2) | |
{ | 25 | 28 | { | |
n = atol(argv[1]); | 26 | 29 | n = atol(argv[1]); | |
} | 27 | 30 | } | |
28 | 31 | |||
printf("MPI version with process = %d\n", size); | 29 | 32 | printf("MPI version with process = %d\n", size); | |
printf("Number of intervals: %lld\n", n); | 30 | 33 | printf("Number of intervals: %lld\n", n); | |
} | 31 | 34 | } | |
32 | 35 | |||
33 | 36 | |||
MPI_Bcast(&n, 1, MPI_LONG, 0, MPI_COMM_WORLD); | 34 | 37 | MPI_Bcast(&n, 1, MPI_LONG, 0, MPI_COMM_WORLD); | |
35 | 38 | |||
36 | 39 | |||
h = 1.0/n; | 37 | 40 | h = 1.0/n; | |
38 | 41 | |||
l_sum = 0.0; | 39 | 42 | l_sum = 0.0; | |
40 | 43 | |||
for (i = rank; i < n; i += size) | 41 | 44 | for (i = rank; i < n; i += size) | |
{ | 42 | 45 | { | |
x = (i + 0.5)*h; | 43 | 46 | x = (i + 0.5)*h; |
lab1/pi_omp.c
View file @
adfef31
#include <stdio.h> | 1 | 1 | #include <stdio.h> | |
#include <string.h> | 2 | 2 | #include <string.h> | |
#include <stdlib.h> | 3 | 3 | #include <stdlib.h> | |
#include <omp.h> | 4 | 4 | #include <omp.h> | |
5 | 5 | |||
#define N 100000000 | 6 | 6 | #define N 1000000000 | |
7 | ||||
int main (int argc, char** argv) | 7 | 8 | int main (int argc, char** argv) | |
{ | 8 | 9 | { | |
long long int n; | 9 | 10 | long long int n; | |
long long int i; | 10 | 11 | long long int i; | |
11 | 12 | |||
double l_sum, x, h; | 12 | 13 | double l_sum, x, h; | |
13 | 14 | |||
n=N; | 14 | 15 | n=N; | |
15 | 16 | |||
if(argc==2) | 16 | 17 | if(argc==2) | |
{ | 17 | 18 | { | |
n=atol(argv[1]); | 18 | 19 | n=atoll(argv[1]); | |
} | 19 | 20 | } | |
20 | 21 | |||
21 | 22 | |||
h = 1.0/n; | 22 | 23 | h = 1.0/n; | |
23 | 24 | |||
l_sum = 0.0; | 24 | 25 | l_sum = 0.0; | |
25 | 26 | |||
#pragma omp parallel for private(i,x) reduction(+:l_sum) | 26 | 27 | #pragma omp parallel for private(i,x) reduction(+:l_sum) | |
for (i = 0; i < n; i ++) | 27 | 28 | for (i = 0; i < n; i ++) | |
{ | 28 | 29 | { | |
x = (i + 0.5)*h; | 29 | 30 | x = (i + 0.5)*h; | |
l_sum += 4.0/(1.0 + x*x); | 30 | 31 | l_sum += 4.0/(1.0 + x*x); |
lab1/pi_ser.c
View file @
adfef31
#include <stdio.h> | 1 | 1 | #include <stdio.h> | |
#include <stdlib.h> | 2 | 2 | #include <stdlib.h> | |
#include <string.h> | 3 | 3 | #include <string.h> | |
#define N 100000000 | 4 | 4 | #define N 1000000000 | |
5 | ||||
int main (int argc, char** argv) | 5 | 6 | int main (int argc, char** argv) | |
{ | 6 | 7 | { | |
long long int n; | 7 | 8 | long long int n; | |
long long int i; | 8 | 9 | long long int i; | |
9 | ||||
double l_sum, x, h; | 10 | 10 | double l_sum, x, h; | |
11 | 11 | |||
n=N; | 12 | 12 | n=N; | |
13 | 13 | |||
if(argc==2) | 14 | 14 | if(argc==2) | |
{ | 15 | 15 | { | |
n=atol(argv[1]); | 16 | 16 | n=atoll(argv[1]); | |
} | 17 | 17 | } | |
18 | 18 | |||
19 | 19 | |||
20 | ||||
h = 1.0/n; | 20 | 21 | h = 1.0/n; | |
21 | 22 | |||
l_sum = 0.0; | 22 | 23 | l_sum = 0.0; | |
23 | 24 | |||
for (i = 0; i < n; i ++) | 24 | 25 | for (i = 0; i < n; i ++) | |
{ | 25 | 26 | { | |
x = (i+0.5)*h; | 26 | 27 | x = (i+0.5)*h; | |
l_sum += 4.0/(1.0 + x*x); | 27 | 28 | l_sum += 4.0/(1.0 + x*x); | |
} | 28 | 29 | } |
lab1/pi_task.c
View file @
adfef31
#include <stdio.h> | 1 | 1 | #include <stdio.h> | |
#include <string.h> | 2 | 2 | #include <string.h> | |
#include <stdlib.h> | 3 | 3 | #include <stdlib.h> | |
4 | 4 | |||
#define N 100000000 | 5 | 5 | #define N 1000000000 | |
6 | 6 | |||
int main (int argc, char** argv) | 7 | 7 | int main (int argc, char** argv) | |
{ | 8 | 8 | { | |
int task_id; | 9 | 9 | int task_id; | |
int total_tasks; | 10 | 10 | int total_tasks; | |
long long int n; | 11 | 11 | long long int n; | |
long long int i; | 12 | 12 | long long int i; | |
13 | 13 | |||
double l_sum, x, h; | 14 | 14 | double l_sum, x, h; | |
15 | 15 | |||
n=N; | 16 | 16 | n=N; | |
17 | 17 | |||
if(argc<3) | 18 | 18 | if(argc<3) | |
{ | 19 | 19 | { | |
fprintf(stderr,"Usage : %s task_id total_tasks [n]\n",argv[0]); | 20 | 20 | fprintf(stderr,"Usage : %s task_id total_tasks [n]\n",argv[0]); | |
exit(0); | 21 | 21 | exit(0); | |
} | 22 | 22 | } | |
23 | 23 | |||
task_id = atoi(argv[1]); | 24 | 24 | task_id = atoi(argv[1]); | |
total_tasks = atoi(argv[2]); | 25 | 25 | total_tasks = atoi(argv[2]); | |
26 | 26 | |||
if(argc==4) | 27 | 27 | if(argc==4) | |
{ | 28 | 28 | { | |
n = atoll(argv[3]); | 29 | 29 | n = atoll(argv[3]); | |
} | 30 | 30 | } | |
31 | 31 | |||
fprintf(stderr, "task_id=%d total_tasks=%d n=%lld\n", task_id, total_tasks, n); | 32 | 32 | fprintf(stderr, "task_id=%d total_tasks=%d n=%lld\n", task_id, total_tasks, n); | |
33 | 33 | |||
h = 1.0/n; | 34 | 34 | h = 1.0/n; | |
35 | 35 | |||
l_sum = 0.0; | 36 | 36 | l_sum = 0.0; | |
37 | 37 |