diff --git a/documents/references.md b/documents/references.md new file mode 100644 index 0000000000000000000000000000000000000000..ab0480589d5bc0ace810f0380f54444d7e23775c --- /dev/null +++ b/documents/references.md @@ -0,0 +1,28 @@ +# Load Balancing & Isoefficiency > Documents > References.md + + +## Description +All references to external logic. Includes anything from stack overflow links to notes about logic from previous works. + + +## References + +### Parallelization +#### General Paralellization Logic +Most parallelization logic is from the book "Introduction to Parallel Programming, by Peter S Pancheco". + +#### Getting Processor Count +<https://stackoverflow.com/a/47520857> + + +### Data Types +#### Various C Data Types +<https://en.wikipedia.org/wiki/C_data_types> + +#### Data Type Limits +<https://www.tutorialspoint.com/c_standard_library/limits_h.htm> + + +### Other +#### strtol Function +<https://www.tutorialspoint.com/c_standard_library/c_function_strtol.htm> diff --git a/main.c b/main.c deleted file mode 100644 index 497999d857bb70af25a99c7737792703c1d310da..0000000000000000000000000000000000000000 --- a/main.c +++ /dev/null @@ -1,27 +0,0 @@ -/** - * An implementation of various Load Balancing schemes, to examine the concepts of Isoefficiency. - */ - -// Import headers. -#include <ctype.h> -#include <pthread.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - - -// Method Declaration. - - -// Global Variables. - - -/** - * Program's main. Initialized and runs program. - */ -int main(int argc, char* argv[]) { - printf("Initializing program.\n"); - - printf("Terminating program.\n"); -} diff --git a/makefile b/makefile index fe51b8290ab1ca7ad0cfb533fb1f71d0388bd903..fabcfd221a9d7bea1984c2d126d7cd8f07aa5afe 100644 --- a/makefile +++ b/makefile @@ -1,3 +1,3 @@ all: - gcc -Wall -Wpedantic -std=c99 main.c -g -o main.out -pthread + gcc -Wall -Wpedantic -std=c99 src/main.c -g -o main.out -pthread diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000000000000000000000000000000000000..360f4ef29ca0737c363675528168539b729c03af --- /dev/null +++ b/src/main.c @@ -0,0 +1,168 @@ +/** + * An implementation of various Load Balancing schemes, to examine the concepts of Isoefficiency. + */ + + +// Import headers. +#include <ctype.h> +#include <limits.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/sysinfo.h> +#include <unistd.h> + + +// Method Declaration. +void validate_args(); +void display_max_types(); +void simulate_load(); + + +// Global Variables. +int thread_count; +int seconds_per_index; +int indexes_per_load; +int total_loads; + + +/** + * Program's main. Initialized and runs program. + */ +int main(int argc, char* argv[]) { + printf("Initializing program.\n"); + printf("\n"); + + // Validate program args. + validate_args(argc, argv); + // If we got this far, then args are valid. Proceed with program. + int total_processors = get_nprocs(); + + // Display args. + printf("Provided values:"); + printf(" %i Total Loads\n", total_loads); + printf(" %i Indexes Per Load\n", indexes_per_load); + printf(" %i Seconds Per Index\n", seconds_per_index); + printf("\n"); + printf(" Total Expected work = TotalLoads * IndexesPerLoad * SecondsPerIndex\n"); + printf(" = %i * %i * %i\n", total_loads, indexes_per_load, seconds_per_index); + int seconds_active_work = total_loads * indexes_per_load * seconds_per_index; + printf(" = %i Seconds Active Work\n", seconds_active_work); + printf(" = %i Minutes Active Work\n", (seconds_active_work / 60)); + printf("\n"); + printf(" %i Total System Processors\n", total_processors); + printf("\n"); + printf(" Expected total execution time with ideal processor usage:\n"); + printf(" (Work / Processor Count) = %i Minutes\n", (seconds_active_work / 60 / total_processors)); + printf("\n"); + + // Run program. + display_max_types(); + // simulate_load(); + + printf("\n"); + printf("Terminating program.\n"); + exit(0); +} + + +/** + * Validate program args. + */ +void validate_args(int argc, char* argv[]) { + + if (argc == 4) { + // Expected number of params. Validate passed args. + + // Validate "seconds_per_index" value. Should be between 1 and 10. + seconds_per_index = strtol(argv[1], NULL, 10); + if ((seconds_per_index < 1) || (seconds_per_index > 10)) { + printf("Arg1 (seconds_per_index) should be int between 1 and 10.\n"); + printf("\n"); + printf("Terminating program.\n"); + exit(1); + } + + // Validate "indexes_per_load" value. Should be between 100 and 10,000. + indexes_per_load = strtol(argv[2], NULL, 10); + if ((indexes_per_load < 100) || (indexes_per_load > 10000)) { + printf("Arg2 (indexes_per_load) should be int between 100 and 10,000.\n"); + printf("\n"); + printf("Terminating program.\n"); + exit(1); + } + + // Validate "total_loads" value. Should be between 100 and 10,000. + total_loads = strtol(argv[3], NULL, 10); + if ((total_loads < 100) || (total_loads > 10000)) { + printf("Arg3 (total_loads) should be int between 100 and 10,000.\n"); + printf("\n"); + printf("Terminating program.\n"); + exit(1); + } + + } else if (argc > 4) { + // Too many args. Error. + printf("Too many args passed. Got %i. Expected 3.\n", (argc - 1)); + printf("\n"); + printf("Terminating program.\n"); + exit(1); + } else { + // Too few args. Error. + printf("Too few args passed. Got %i. Expected 3.\n", (argc - 1)); + printf("\n"); + printf("Terminating program.\n"); + exit(1); + } +} + + +/** + * Helper function to display some max values for system. + */ +void display_max_types() { + printf("\n\n"); + + printf("Int max is %i.\n", INT_MAX); + printf("Unsigned Int max is %u.\n", UINT_MAX); + printf("Long Max is %ld.\n", LONG_MAX); + printf("Unsigned Long max is %lu.\n", ULONG_MAX); + + printf("\n\n"); +} + + +/** + * Function to simulate working on a load. + */ +void simulate_load() { + + unsigned int load_counter = 0; + unsigned long index = 0; + + // Iterate through loads. Each load has indexes to simulate "work". + while (load_counter < total_loads) { + + // Increment load num. + load_counter += 1; + + // Simulate running the load. + index = 0; + while (index < indexes_per_load) { + + // Print out index value. + if ((index % 10) == 0) { + printf("load"); + } + + // Wait 1 second to simulate work. + sleep(seconds_per_index); + + // Increment index. + index += 1; + } + + } + +}