diff --git a/makefile b/makefile index ead3aa0de45cc841be810004d3421832716d8c6c..3aafc50d6aac34bad5804f90f7760bd7096c70c0 100644 --- a/makefile +++ b/makefile @@ -13,7 +13,8 @@ TARGET = main.out LIBRARIES = -pthread DEPENDENCIES = src/main.c src/structs.c src/simulate_loads.c src/terminal_commands.c CORES := $(shell nproc) -ARGS = `arg="$(filter-out $@,$(MAKECMDGOALS))" && echo $${arg:-${1}}` +# ARGS = `arg="$(filter-out $@,$(MAKECMDGOALS))" && echo $${arg:-${1}}` +ARGS = $(filter-out $@,$(MAKECMDGOALS)) # Compile target if any depenencies update. @@ -21,7 +22,10 @@ $(TARGET): $(DEPENDENCIES) @$(CC) $(CFLAGS) $(DEPENDENCIES) -o $(TARGET) $(LIBRARIES) -# Compile target if dependencies update. Then run. +# Compile target if dependencies update. all: $(TARGET) + + +# Compile target if dependencies update. Then run. +run: $(TARGET) @$(EXE) -n $(CORES) --oversubscribe ./$(TARGET) $(ARGS) - # @$(EXE) -n 3 --oversubscribe ./$(TARGET) $(ARGS) diff --git a/src/simulate_loads.c b/src/simulate_loads.c index 8f95747e1ccdf0dd86872bcefe0d855d131a2dcd..d0cbff066f5bb947b72e8ac6c89c6511e48b4641 100644 --- a/src/simulate_loads.c +++ b/src/simulate_loads.c @@ -12,6 +12,34 @@ #endif +void display_status(thread_struct* thread_args_ptr, int process_num, int process_load_status, int init_run) { + int index; + + // printf("init_run: %i\n", init_run); + + // Travel to desired terminal row. Skip if first loop through thread displaying. + if (init_run == 0) { + for (index = 0; index < (thread_args_ptr->total_processors - process_num); index ++) { + terminal_line_up(); + } + } + // Clear row. + terminal_line_erase(); + terminal_line_start(); + + // Display message. + printf("Thread %5i: %5i / %5i Loads Remaining\n", process_num, process_load_status, thread_args_ptr->total_loads); + + // Travel back to original terminal location. Skip if first loop through thread displaying. + if (init_run == 0) { + for (index = 0; index < (thread_args_ptr->total_processors - process_num); index ++) { + terminal_line_down(); + } + terminal_line_start(); + } +} + + /** * Logic to run "Asynchronous Round Robin" load scheme. */ @@ -31,27 +59,10 @@ void run_arr(thread_struct* thread_args_ptr) { // Handle based on process number. if (process_rank > 0) { // Child process. Handles all the work. - - int* send_array = calloc(2, sizeof(int)); - send_array[0] = process_rank; - send_array[1] = thread_args_ptr->total_loads; - MPI_Send(send_array, 2, MPI_INT, 0, 0, MPI_COMM_WORLD); - free(send_array); - + worker_arr(thread_args_ptr); } else { // Main process. Handles communication to terminal. - - // Get message from all child processors. - int index = 0; - while (index < thread_args_ptr->total_processors - 1) { - - int* recv_array = calloc(3, sizeof(int)); - MPI_Recv(recv_array, 2, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); - printf("Thread %i: %i / %i Loads Remaining\n", recv_array[0], recv_array[1], thread_args_ptr->total_loads); - free(recv_array); - - index += 1; - } + main_arr(thread_args_ptr); } // Wait for all processes to synchronize. @@ -66,6 +77,61 @@ void run_arr(thread_struct* thread_args_ptr) { } +/** + * Main processor logic for "Asynchronous Round Robin" load scheme. + */ +void main_arr(thread_struct* thread_args_ptr) { + int index = 1; + int main_loop_bool = thread_args_ptr->total_loads; + int init_run = 1; + + // Get messages from all worker processors. + while (main_loop_bool >= 0) { + + int* recv_array = calloc(3, sizeof(int)); + MPI_Recv(recv_array, 2, MPI_INT, index, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + display_status(thread_args_ptr, recv_array[0], recv_array[1], init_run); + free(recv_array); + + // Increment for next thread. + index = (index + 1) % thread_args_ptr->total_processors; + + // Check if past last processor. + if (index == 0) { + index += 1; + + init_run = 0; + main_loop_bool -= 1; + sleep(1); + } + + } +} + + +/** + * Worker processor logic for "Asynchronous Round Robin" load scheme. + */ +void worker_arr(thread_struct* thread_args_ptr) { + int process_rank = thread_args_ptr->thread_num; + int load_num = thread_args_ptr->total_loads; + + // Loop all worker processes until load count message of "0" has been sent. + while (load_num >= 0) { + + // Sent message for current load. + int* send_array = calloc(2, sizeof(int)); + send_array[0] = process_rank; + send_array[1] = load_num; + MPI_Send(send_array, 2, MPI_INT, 0, 0, MPI_COMM_WORLD); + free(send_array); + + // Decriment load number. + load_num -= 1; + } +} + + /** * Logic to run "Global Round Robin" load scheme. */ diff --git a/src/simulate_loads.h b/src/simulate_loads.h index b31363fe031239b1f521c06f8fc26f7bd3a83898..8c663eb4345db732076c55308fe0749adfe892b8 100644 --- a/src/simulate_loads.h +++ b/src/simulate_loads.h @@ -13,7 +13,10 @@ // Function Prototypes. +void display_status(); void run_arr(); // Encapsulated logic for "Asynchronous Round Robin" load scheme. +void main_arr(); +void worker_arr(); 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.