commit e5c9fe7c0e33368f87c73185d00fed0205e179d5 Author: Peter Babič Date: Tue Oct 20 20:58:05 2015 +0200 sample1 edited and working with unknown file lenght diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..b634d85 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.pdf filter=lfs diff=lfs merge=lfs -text diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bbf313b --- /dev/null +++ b/.gitignore @@ -0,0 +1,32 @@ +# Object files +*.o +*.ko +*.obj +*.elf + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..727c4d3 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +program: + gcc readfile.c -o readfile diff --git a/readfile b/readfile new file mode 100755 index 0000000..d66011d Binary files /dev/null and b/readfile differ diff --git a/readfile.c b/readfile.c new file mode 100644 index 0000000..c1ffe18 --- /dev/null +++ b/readfile.c @@ -0,0 +1,71 @@ +#include +#include + +#define INPUTFILE "sample1_e.in" +// Avoids aggresive memory reallocation at the beginning of buffer growth sequence +#define INIT_COUNT 8 +// Optimal exponential buffer growth factor, 2 is sometimes used to +#define GROWTH_FACTOR 1.5 + +void *my_malloc(size_t size); +void *my_realloc(void *p, size_t size); +FILE *my_fopen(const char *filename, const char *mode); + +int main(int argc, char *argv[]){ + + int number, count = 0, max = INIT_COUNT; + // Allocate enough memory for INIT_COUNT numbers + int *numbers = my_malloc(max * sizeof(int *)); + FILE *file = my_fopen(INPUTFILE, "r"); + + // Read lines of numbers until an EOF or a character is read + while (fscanf(file, "%hd", &number) == 1) { + // Buffer space check + if (max == count) { + // Grow buffer exponentially + max *= GROWTH_FACTOR; + numbers = my_realloc(numbers, max * sizeof(int *)); + } + + // Store the read number to the array in memory + numbers[count++] = number; + } + + //printf("count: %d, max: %d\n", count, max); + int i; + for (i = 0; i < count; i++) { + printf("%d ", numbers[i]); + } + + return 0; +} + + +void *my_malloc(size_t size) { + void *p = malloc(size); + if (p == NULL) { + printf("Memory allocation unsuccessful.\n"); + exit(EXIT_FAILURE); + } + return p; +} + + +void *my_realloc(void *p, size_t size) { + void *temp = realloc(p, size); + if (temp == NULL) { + printf("Insufficient memory; can't add more items.\n"); + exit(EXIT_FAILURE); + } + return temp; +} + + +FILE *my_fopen(const char *filename, const char *mode) { + FILE *file = fopen(filename, mode); + if (file == NULL) { + printf("File %s could not be opened.\n", filename); + exit(EXIT_FAILURE); + } + return file; +} diff --git a/run b/run new file mode 100755 index 0000000..39218fe --- /dev/null +++ b/run @@ -0,0 +1,4 @@ +#!/bin/bash + +mpicc -Wall "$1.c" -o "$1" +mpirun -v -np "$2" "$1" diff --git a/sample1 b/sample1 new file mode 100755 index 0000000..bae6913 Binary files /dev/null and b/sample1 differ diff --git a/sample1.c b/sample1.c new file mode 100644 index 0000000..4334e4e --- /dev/null +++ b/sample1.c @@ -0,0 +1,79 @@ +#include +#include + +#define BUFSIZE 10 + +int main(int argc, char *argv[]) { + int size, rank; + int slave; + int buf[BUFSIZE]; + int n, value; + float rval; + MPI_Status status; + + MPI_Init(&argc, &argv); + MPI_Comm_size(MPI_COMM_WORLD, &size); + if (size == 3) { + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + if (rank == 0) { + buf[0] = 5; + buf[1] = 1; + buf[2] = 8; + buf[3] = 7; + buf[4] = 6; + buf[5] = 5; + buf[6] = 4; + buf[7] = 2; + buf[8] = 3; + buf[9] = 1; + printf("\n Sending the values {5,1,8,7,6,5,4,2,3,1}"); + printf("\n -----------------------------"); + for (slave = 1; slave < size; slave++) { + printf("\n from master %d to slave %d", rank, slave); + MPI_Send(buf, 10, MPI_INT, slave, 1, MPI_COMM_WORLD); + } + printf("\n\n Receiving the results from slaves"); + printf("\n ---------------------------------"); + MPI_Recv(&value, 1, MPI_INT, 1, 11, MPI_COMM_WORLD, &status); + printf("\n Minimum %4d from slave 1", value); + MPI_Recv(&value, 1, MPI_INT, 2, 21, MPI_COMM_WORLD, &status); + printf("\n Sum\t %4d from slave 2", value); + MPI_Recv(&value, 1, MPI_INT, 1, 12, MPI_COMM_WORLD, &status); + printf("\n Maximum %4d from slave 1", value); + MPI_Recv(&rval, 1, MPI_FLOAT, 2, 22, MPI_COMM_WORLD, &status); + printf("\n Average %4.2f from slave 2\n", rval); + } + else { + if (rank == 1) { + MPI_Recv(buf, 10, MPI_INT, 0, 1, MPI_COMM_WORLD, &status); + value = 100; + for (n = 0; n < BUFSIZE; n++) { + if (value > buf[n]) { + value = buf[n]; + } + } + MPI_Send(&value, 1, MPI_INT, 0, 11, MPI_COMM_WORLD); + value = 0; + for (n = 0; n < BUFSIZE; n++) { + if (value < buf[n]) { + value = buf[n]; + } + } + MPI_Send(&value, 1, MPI_INT, 0, 12, MPI_COMM_WORLD); + } + else { + MPI_Recv(buf, 10, MPI_INT, 0, 1, MPI_COMM_WORLD, &status); + value = 0; + for (n = 0; n < BUFSIZE; n++) { + value = value + buf[n]; + } + MPI_Send(&value, 1, MPI_INT, 0, 21, MPI_COMM_WORLD); + rval = (float) value / BUFSIZE; + MPI_Send(&rval, 1, MPI_FLOAT, 0, 22, MPI_COMM_WORLD); + } + } + } + MPI_Finalize(); + return (0); +} diff --git a/sample1_e b/sample1_e new file mode 100755 index 0000000..b2f1d30 Binary files /dev/null and b/sample1_e differ diff --git a/sample1_e.c b/sample1_e.c new file mode 100644 index 0000000..b42e6a0 --- /dev/null +++ b/sample1_e.c @@ -0,0 +1,165 @@ +#include +#include +#include +#include +#include + +#define INPUTFILE "sample1_e.in" +// Avoids aggresive memory reallocation at the beginning of buffer growth sequence +#define INIT_COUNT 8 +// Optimal exponential buffer growth factor, 2 is sometimes used to +#define GROWTH_FACTOR 1.5 + +// Macro for counting the lenght of an array +#define COUNT(x) ((int) (sizeof(x) / sizeof(x[0]))) + + +void *my_malloc(size_t size); +void *my_realloc(void *p, size_t size); +FILE *my_fopen(const char *filename, const char *mode); +bool is_mpi_defined(int n); + +int main(int argc, char *argv[]) { + int size, rank, slave, i, n, value, count = 0; + float fvalue; + + MPI_Status status; + + MPI_Init(&argc, &argv); + MPI_Comm_size(MPI_COMM_WORLD, &size); + + if (size == 4) { + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + if (rank == 0) { + + int number, max = INIT_COUNT; + // Allocate enough memory for INIT_COUNT numbers + int *numbers = my_malloc(max * sizeof(int *)); + //int num[10]; + FILE *file = my_fopen(INPUTFILE, "r"); + + // Read lines of numbers until an EOF or a character is read + while (fscanf(file, "%d", &number) == 1) { + // Buffer space check + if (max == count) { + // Grow buffer exponentially + max *= GROWTH_FACTOR; + numbers = my_realloc(numbers, max * sizeof(int *)); + } + + // Store the read number to the array in memory + numbers[count++] = number; + } + + printf(" Sending numbers: "); + for (i = 0; i < count; i++) + printf("%d ", numbers[i]); + + + printf("\n -----------------------------"); + for (slave = 1; slave < size; slave++) { + printf("\n from master %d to slave %d", rank, slave); + MPI_Send(numbers, count, MPI_INT, slave, 1, MPI_COMM_WORLD); + } + printf("\n\n Receiving the results from slaves"); + printf("\n ---------------------------------"); + MPI_Recv(&value, 1, MPI_INT, 1, 11, MPI_COMM_WORLD, &status); + printf("\n Minimum %4d from slave 1", value); + MPI_Recv(&value, 1, MPI_INT, 2, 21, MPI_COMM_WORLD, &status); + printf("\n Sum\t %4d from slave 2", value); + MPI_Recv(&value, 1, MPI_INT, 1, 12, MPI_COMM_WORLD, &status); + printf("\n Maximum %4d from slave 1", value); + MPI_Recv(&fvalue, 1, MPI_FLOAT, 2, 22, MPI_COMM_WORLD, &status); + printf("\n Average %4.2f from slave 2", fvalue); + MPI_Recv(&fvalue, 1, MPI_FLOAT, 3, 31, MPI_COMM_WORLD, &status); + printf("\n H. mean %4.2f from slave 3\n", fvalue); + } + else { + MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status); + MPI_Get_count(&status, MPI_INT, &n); + + is_mpi_defined(n); + int numbers[n]; + count = COUNT(numbers); + + if (rank == 1) { + MPI_Recv(numbers, n, MPI_INT, 0, 1, MPI_COMM_WORLD, &status); + //MPI_Recv(numbers, n, MPI_INT, status.MPI_SOURCE, status.MPI_TAG, MPI_COMM_WORLD, &status); + value = INT_MAX; + for (i = 0; i < count; i++) { + if (value > numbers[i]) { + value = numbers[i]; + } + } + MPI_Send(&value, 1, MPI_INT, 0, 11, MPI_COMM_WORLD); + value = 0; + for (i = 0; i < count; i++) { + if (value < numbers[i]) { + value = numbers[i]; + } + } + MPI_Send(&value, 1, MPI_INT, 0, 12, MPI_COMM_WORLD); + } + else if (rank == 2) { + MPI_Recv(numbers, n, MPI_INT, 0, 1, MPI_COMM_WORLD, &status); + value = 0; + for (i = 0; i < count; i++) { + value = value + numbers[i]; + } + MPI_Send(&value, 1, MPI_INT, 0, 21, MPI_COMM_WORLD); + fvalue = (float) value / count; + MPI_Send(&fvalue, 1, MPI_FLOAT, 0, 22, MPI_COMM_WORLD); + } + else { + MPI_Recv(numbers, n, MPI_INT, 0, 1, MPI_COMM_WORLD, &status); + fvalue = 0.0; + for (i = 0; i < count; i++) { + fvalue += 1.0 / numbers[i]; + } + fvalue = (float) count / fvalue; + MPI_Send(&fvalue, 1, MPI_FLOAT, 0, 31, MPI_COMM_WORLD); + } + } + } + MPI_Finalize(); + return (0); +} + + +void *my_malloc(size_t size) { + void *p = malloc(size); + if (p == NULL) { + printf("Memory allocation unsuccessful.\n"); + exit(EXIT_FAILURE); + } + return p; +} + + +void *my_realloc(void *p, size_t size) { + void *temp = realloc(p, size); + if (temp == NULL) { + printf("Insufficient memory; can't add more items.\n"); + exit(EXIT_FAILURE); + } + return temp; +} + + +FILE *my_fopen(const char *filename, const char *mode) { + FILE *file = fopen(filename, mode); + if (file == NULL) { + printf("File %s could not be opened.\n", filename); + exit(EXIT_FAILURE); + } + return file; +} + +bool is_mpi_defined(int n) { + if (n == MPI_UNDEFINED) { + printf("MPI encountered undefined integer and terminated."); + exit(EXIT_FAILURE); + } + return true; +} diff --git a/sample1_e.in b/sample1_e.in new file mode 100644 index 0000000..a81e5cb --- /dev/null +++ b/sample1_e.in @@ -0,0 +1,10 @@ +5 +1 +8 +7 +6 +5 +4 +3 +2 +1 diff --git a/testt b/testt new file mode 100755 index 0000000..5cfe26b Binary files /dev/null and b/testt differ diff --git a/testt.c b/testt.c new file mode 100644 index 0000000..3fe2e8a --- /dev/null +++ b/testt.c @@ -0,0 +1,28 @@ +#include +#include + +int main(int argc, char **argv) { + int rank, size, root_process; + + MPI_Status status; + + MPI_Init(&argc, &argv); + + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Comm_size(MPI_COMM_WORLD, &size); + + if( rank == 0 ) { + printf("I am the first process\n"); + } + else if( rank == 1 ) { + printf("I am the second process\n"); + } + else if( rank == 2 ) { + printf("I am the third process\n"); + } + else { + printf("I am a remaning process\n"); + } + + MPI_Finalize(); +}