Commit 29b1510128e9456ec2304064867f9183717058b0
1 parent
a67a1dd2b5
Exists in
master
MAJ after first lab
Showing 4 changed files with 10 additions and 29 deletions Inline Diff
lab1/pi_mpi.c
View file @
29b1510
#include <stdio.h> | 1 | 1 | #include <stdio.h> | |
#include <string.h> | 2 | 2 | #include <string.h> | |
#include <mpi.h> | 3 | 3 | #include <mpi.h> | |
#define N 100000000 | 4 | 4 | #define N 100000000 | |
5 | 5 | |||
int main (int argc, char** argv) | 6 | 6 | int main (int argc, char** argv) | |
{ | 7 | 7 | { | |
int rank; | 8 | 8 | int rank; | |
int size; | 9 | 9 | int size; | |
long long int n; | 10 | 10 | long long int n; | |
long long int i; | 11 | 11 | long long int i; | |
12 | 12 | |||
double l_sum,total_sum, x, h; | 13 | 13 | double l_sum,total_sum, x, h; | |
14 | 14 | |||
MPI_Init(NULL, NULL); | 15 | 15 | MPI_Init(NULL, NULL); | |
16 | 16 | |||
MPI_Comm_size(MPI_COMM_WORLD, &size); | 17 | 17 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | 18 | 18 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
19 | 19 | |||
n=N; | 20 | 20 | n=N; | |
21 | 21 | |||
if(rank==0) | 22 | 22 | if(rank==0) | |
{ | 23 | 23 | { | |
if(argc==2) | 24 | 24 | if(argc==2) | |
{ | 25 | 25 | { | |
n = atoll(argv[1]); | 26 | 26 | n = atol(argv[1]); | |
} | 27 | 27 | } | |
28 | 28 | |||
printf("MPI version with process = %d\n", size); | 29 | 29 | printf("MPI version with process = %d\n", size); | |
printf("Number of intervals: %lld\n", n); | 30 | 30 | printf("Number of intervals: %lld\n", n); | |
} | 31 | 31 | } | |
32 | 32 | |||
33 | 33 | |||
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); | 34 | 34 | MPI_Bcast(&n, 1, MPI_LONG, 0, MPI_COMM_WORLD); | |
35 | 35 | |||
36 | 36 | |||
h = 1.0/n; | 37 | 37 | h = 1.0/n; | |
38 | 38 | |||
l_sum = 0.0; | 39 | 39 | l_sum = 0.0; | |
40 | 40 | |||
for (i = rank; i < n; i += size) | 41 | 41 | for (i = rank; i < n; i += size) | |
{ | 42 | 42 | { | |
x = (i + 0.5)*h; | 43 | 43 | x = (i + 0.5)*h; |
lab2/factorial.c
View file @
29b1510
#include <stdio.h> | 1 | 1 | #include <stdio.h> | |
2 | ||||
int main() | 3 | 2 | int main() | |
{ | 4 | 3 | { | |
int i, num, j; | 5 | 4 | int i, num, fact; | |
printf ("Enter the number: "); | 6 | 5 | printf ("Enter the number: "); | |
scanf ("%d", &num ); | 7 | 6 | scanf ("%d", &num ); | |
7 | fact=1; | |||
8 | for (i=1; i<=num; i++) | |||
9 | fact=fact*i; | |||
8 | 10 | |||
for (i=1; i<num; i++) | 9 | 11 | printf("The factorial of %d is %d\n",num,fact); | |
j=j*i; | 10 | |||
11 | ||||
printf("The factorial of %d is %d\n",num,j); | 12 |
lab2/leak.c
View file @
29b1510
#include <stdlib.h> | 1 | File was deleted | ||
#include <stdio.h> | 2 | |||
void f(void) | 3 | |||
{ | 4 | |||
int* x = (int*)malloc(10 * sizeof(int)); | 5 | |||
x[9] = 2; | 6 | |||
} // problem: memory leak -- x not freed | 7 | |||
int main(int argc, char * argv[]) | 8 | |||
{ | 9 | |||
f(); | 10 | |||
printf("GOOD END \n"); | 11 | |||
return 0; | 12 | |||
} | 13 |
lab2/printArray.c
View file @
29b1510
1 | 1 | |||
#include <stdio.h> | 2 | 2 | #include <stdio.h> | |
3 | 3 | |||
// print an array of integers, all on one line. | 4 | |||
void printIntArray(int *a, int n) | 5 | 4 | void printIntArray(int *a, int n) | |
{ | 6 | 5 | { | |
7 | 6 | |||
int i; // i is a "local variable" inside printIntArray | 8 | 7 | int i; | |
9 | 8 | |||
// A loop that prints every value in the array | 10 | |||
for (i=0; 1<n; i++) | 11 | 9 | for (i=0; 1<n; i++) | |
a[i]=a[i]*2; | 12 | 10 | a[i]=2*a[i]; | |
// printf("a[%d]=%d ",i,a[i]); | 13 | |||
14 | 11 | |||
printf("\n"); // Then we end with a new line" | 15 | |||
} | 16 | 12 | } | |
17 | 13 | |||
// A main to demonstrate the printIntArray function... | 18 | |||
19 | 14 | |||
int main() | 20 | 15 | int main() | |
{ | 21 | 16 | { |