diff --git a/makefile b/makefile index fabcfd221a9d7bea1984c2d126d7cd8f07aa5afe..f98ed0e6d24ee6cd380919480945fe4b9a060abe 100644 --- a/makefile +++ b/makefile @@ -1,3 +1,3 @@ all: - gcc -Wall -Wpedantic -std=c99 src/main.c -g -o main.out -pthread + gcc -Wall -Wpedantic -std=c99 src/main.c src/structs.c src/simulate_loads.c -g -o main.out -pthread diff --git a/src/main.c b/src/main.c index d456259fca9ad65988531624af30631a7a64ac07..6c86b1ae7d0e5297ffaa498543129a0f24c62717 100644 --- a/src/main.c +++ b/src/main.c @@ -3,7 +3,7 @@ */ -// Import headers. +// System Import Headers. #include <ctype.h> #include <limits.h> #include <pthread.h> @@ -13,16 +13,22 @@ #include <sys/sysinfo.h> #include <unistd.h> +// User Import Headers. +#ifndef user_headers + #define user_headers + #include "simulate_loads.h" + #include "structs.h" +#endif + // Method Declaration. void validate_args(); // Validates passed user args. void display_max_types(); // Displays a few data type max values. void run_program(); // Main program logic. Handles various Load Balance schemas. -void simulate_load(); // Simulates running one load. // Global Variables. -int thread_count; +int total_processors; int seconds_per_index; int indexes_per_load; int total_loads; @@ -38,7 +44,7 @@ int main(int argc, char* argv[]) { // Validate program args. validate_args(argc, argv); // If we got this far, then args are valid. Proceed with program. - int total_processors = get_nprocs(); + total_processors = get_nprocs(); // Display args. printf("Provided values:\n"); @@ -59,8 +65,8 @@ int main(int argc, char* argv[]) { printf("\n"); // Run program. - display_max_types(); - run_program(total_processors, argv); + // display_max_types(); + run_program(total_processors); printf("\n"); printf("Terminating program.\n"); @@ -138,50 +144,15 @@ void display_max_types() { * Main program logic. * Entry point for running various Load Balancing schemes. */ -void run_program(int total_processors, char* argv[]) { +void run_program(int total_processors) { printf("Running core program logic."); printf("\n"); + run_arr(total_processors, seconds_per_index, indexes_per_load, total_loads); + // Test load logic with no parallelization. Debugging only. // simulate_load("main"); printf("\n"); printf("Core program logic complete."); } - - -/** - * Function to simulate working on a load. - */ -void simulate_load(char* thread_name) { - printf("Simulating load processing on thread \"%s\".\n", thread_name); - - int load_counter = 0; - int index = 0; - - // Iterate through loads. Each load has indexes to simulate "work". - while (load_counter < total_loads) { - - // Increment load num. - load_counter += 1; - printf("Simulating load %i on thread \"%s\".\n", load_counter, thread_name); - - // Simulate running the load. - index = 0; - while (index < indexes_per_load) { - - // Print out index value. - if ((index % 10) == 0) { - printf("load %i / %i index %i / %i\n", load_counter, total_loads, index, indexes_per_load); - } - - // Wait 1 second to simulate work. - sleep(1); - - // Increment index. - index += 1; - } - - } - -} diff --git a/src/simulate_loads.c b/src/simulate_loads.c new file mode 100644 index 0000000000000000000000000000000000000000..ea3a6d4494d7525040299ebf93ac8a832abe98d0 --- /dev/null +++ b/src/simulate_loads.c @@ -0,0 +1,167 @@ +/** + * File for load simulation logic. + */ + + +// User Import Headers. +#ifndef user_headers + #define user_headers + #include "simulate_loads.h" + #include "structs.h" +#endif + + +/** + * Logic to run "Asynchronous Round Robin" load scheme. + */ +void run_arr(int total_processors, int seconds_per_index, int indexes_per_load, int total_loads) { + printf("Starting ARR load scheme."); + printf("\n"); + + // Initialize threading. + pthread_t* thread_pool = calloc((total_processors - 1), sizeof(pthread_t)); + int* thread_results = calloc((total_processors - 1), sizeof(int)); + + // Launch threads. + for (int index = 0; index < (total_processors - 1); index++) { + + // // Initialize thread args. + thread_struct* thread_args = initialize_thread_struct( + &thread_pool, &thread_results, total_processors, seconds_per_index, indexes_per_load, total_loads, index + ); + + // Create current thread. + pthread_create(&thread_pool[index], NULL, simulate_arr_load, (void*) thread_args); + } + + // Wait for threads to finish. + for (int index = 0; index < (total_processors - 1); index++) { + pthread_join(thread_pool[index], NULL); + } + + // Free variables. + free(thread_pool); + free(thread_results); + + printf("\n"); + printf("ARR load scheme. Finished."); +} + + +/** + * Logic to run "Global Round Robin" load scheme. + */ +void run_grr(int total_processors) { + printf("Starting GRR load scheme."); + printf("\n"); + + pthread_t* thread_pool = NULL; + + printf("\n"); + printf("GRR load scheme. Finished."); +} + + +/** + * Logic to run "Random Poling" load scheme. + */ +void run_rp(int total_processors) { + printf("Starting RP load scheme."); + printf("\n"); + + pthread_t* thread_pool = NULL; + + printf("\n"); + printf("RP load scheme. Finished."); +} + + + +/** + * Logic to run "Nearest Neighbor" load scheme. + */ +void run_nn(int total_processors) { + printf("Starting NN load scheme."); + printf("\n"); + + pthread_t* thread_pool = NULL; + + printf("\n"); + printf("NN load scheme. Finished."); +} + + +// /** +// * Function to simulate working on a load. +// */ +// void simulate_load(char* thread_name) { +// printf("Simulating load processing on thread \"%s\".\n", thread_name); + +// int load_counter = 0; +// int index = 0; + +// // Iterate through loads. Each load has indexes to simulate "work". +// while (load_counter < total_loads) { + +// // Increment load num. +// load_counter += 1; +// printf("Simulating load %i on thread \"%s\".\n", load_counter, thread_name); + +// // Simulate running the load. +// index = 0; +// while (index < indexes_per_load) { + +// // Print out index value. +// if ((index % 10) == 0) { +// printf("load %i / %i index %i / %i\n", load_counter, total_loads, index, indexes_per_load); +// } + +// // Wait 1 second to simulate work. +// sleep(1); + +// // Increment index. +// index += 1; +// } + +// } + +// } + + +/** + * Function to simulate working on an ARR load. + */ +void* simulate_arr_load(thread_struct* thread_struct_ptr) { + printf("Simulating load processing on thread \"%i\".\n", thread_struct_ptr->thread_num); + + // int load_counter = 0; + // int index = 0; + + // // Iterate through loads. Each load has indexes to simulate "work". + // while (load_counter < total_loads) { + + // // Increment load num. + // load_counter += 1; + // printf("Simulating load %i on thread \"%s\".\n", load_counter, thread_name); + + // // Simulate running the load. + // index = 0; + // while (index < indexes_per_load) { + + // // Print out index value. + // if ((index % 10) == 0) { + // printf("load %i / %i index %i / %i\n", load_counter, total_loads, index, indexes_per_load); + // } + + // // Wait 1 second to simulate work. + // sleep(1); + + // // Increment index. + // index += 1; + // } + + // } + + free(thread_struct_ptr); +} + diff --git a/src/simulate_loads.h b/src/simulate_loads.h new file mode 100644 index 0000000000000000000000000000000000000000..ee07d54052b26e261e4f035f6c0954ea6ac15591 --- /dev/null +++ b/src/simulate_loads.h @@ -0,0 +1,23 @@ +/** + * Header file for load simulation logic. + */ + + +// System Import Headers. +#include <ctype.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +// Function Prototypes. +void run_arr(); // Encapsulated logic for "Asynchronous Round Robin" load scheme. +void run_grr(); // Encapsulated logic for "Global Round Robin" load scheme. +void run_rp(); // Encapsulated logic for "Random Poling" load scheme. +void run_nn(); // Encapsulated logic for "Nearest Neighbor" load scheme. +void simulate_load(); // Simulates running one load. +void* simulate_arr_load(); // Thread entrypoint for an "Asynchronous Round Robin" load. +void* simulate_grr_load(); // Thread entrypoint for a "Global Round Robin" load. +void* simulate_rp_load(); // Thread entrypoint for a "Random Polling" load. +void* simulate_nn_load(); // Thread entrypoint for a "Nearest Neigher" load. diff --git a/src/structs.c b/src/structs.c new file mode 100644 index 0000000000000000000000000000000000000000..51f6bf02c8df84175ba7d1af96ec1c5e7c472bc1 --- /dev/null +++ b/src/structs.c @@ -0,0 +1,48 @@ +/** + * File for struct logic. + */ + + +// User Import Headers. +#ifndef user_headers + #define user_headers + #include "simulate_loads.h" + #include "structs.h" +#endif + + +/** + * Initializes a "thread_struct" object. + */ +thread_struct* initialize_thread_struct( + pthread_t** thread_pool_ptr, + int** thread_results_ptr, + int total_processors, + int seconds_per_index, + int indexes_per_load, + int total_loads, + int thread_num + ) { + + // Create struct. + thread_struct* new_struct = calloc(1, sizeof(thread_struct)); + + // Populate fields. + new_struct->thread_pool_ptr = thread_pool_ptr; + new_struct->thread_results_ptr = thread_results_ptr; + new_struct->total_processors = total_processors; + new_struct->seconds_per_index = seconds_per_index; + new_struct->indexes_per_load = indexes_per_load; + new_struct->total_loads = total_loads; + new_struct->thread_num = thread_num; + + return new_struct; +} + + +/** + * Destroyes passed "thread_struct" object and all associated fields. + */ +void free_thread_struct(thread_struct* a_struct) { + free(a_struct); +} diff --git a/src/structs.h b/src/structs.h new file mode 100644 index 0000000000000000000000000000000000000000..a199ef68e74e8cbc13da2786e23c11243459f2d8 --- /dev/null +++ b/src/structs.h @@ -0,0 +1,35 @@ +/** + * Header file for project struct logic. + */ + + +// System Import Headers. +#include <ctype.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +/** + * Data for a single thread runtime. + */ +typedef struct { + + // "Global" thread values. + pthread_t** thread_pool_ptr; + int** thread_results_ptr; + int total_processors; + int seconds_per_index; + int indexes_per_load; + int total_loads; + + // "Local" thread values. + int thread_num; + +} thread_struct; + + +// Function Prototypes. +thread_struct* initialize_thread_struct(); +void free_thread_struct();