diff --git a/src/load_balance_general.c b/src/load_balance_general.c index 8e836d45a4c935021c4bb10248bd058ad0397208..eb9b300676ce9688c9de7baa5b7319eaef4fe953 100644 --- a/src/load_balance_general.c +++ b/src/load_balance_general.c @@ -30,25 +30,25 @@ void main_display_status(thread_struct* thread_args_ptr, int process_num, int pr fflush(stdout); // 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(); + 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 - 1); index ++) { - // terminal_line_down(); - // } - // terminal_line_start(); - // } + if (init_run == 0) { + for (index = 0; index < (thread_args_ptr->total_processors - process_num - 1); index ++) { + terminal_line_down(); + } + terminal_line_start(); + } log("EXIT FUNCTION main_terminate_workers()"); log(""); @@ -67,11 +67,20 @@ void main_terminate_workers(thread_struct* thread_args_ptr) { log("FUNCTION main_terminate_workers()"); fflush(stdout); + // Send termination message to all worker threads. + for (index = 1; index < thread_args_ptr->total_processors; index++) { + char* log_msg = calloc(256, sizeof(char)); + sprintf(log_msg, "Sending termination message to process %i.", index); + log(log_msg); free(log_msg); + MPI_Isend(&index, 1, MPI_INT, index, tag_main_termination, MPI_COMM_WORLD, &request_var); + } + + // Send second termination message, because for some reason some workers seem to get stuck otherwise. for (index = 1; index < thread_args_ptr->total_processors; index++) { + // char* log_msg = calloc(256, sizeof(char)); + // sprintf(log_msg, "Sending termination message to process %i.", index); + // log(log_msg); free(log_msg); MPI_Isend(&index, 1, MPI_INT, index, tag_main_termination, MPI_COMM_WORLD, &request_var); - // printf("Sending termination message to process %i.\n", index); - // MPI_Ssend(&index, 1, MPI_INT, index, tag_main_termination, MPI_COMM_WORLD); - // printf("Process %i received termination message.\n", index); } log("EXIT FUNCTION main_terminate_workers()"); @@ -207,9 +216,9 @@ void worker_handle_request(thread_struct* thread_args_ptr) { // One or more messages present. Loop through and check for message from all processors. for (index = 1; index < thread_args_ptr->total_processors; index++) { - char* msg = calloc(256, sizeof(char)); - sprintf(msg, " Checking for request from process %i", index); - log(msg); free(msg); + // char* msg = calloc(256, sizeof(char)); + // sprintf(msg, " Checking for request from process %i", index); + // log(msg); free(msg); msg_status_flag = 0; MPI_Iprobe(index, tag_work_request, MPI_COMM_WORLD, &msg_status_flag, MPI_STATUS_IGNORE); if (msg_status_flag == 1) { @@ -221,15 +230,15 @@ void worker_handle_request(thread_struct* thread_args_ptr) { // Check if donating worker has work to split and send. if (thread_args_ptr->remaining_loads > 1) { // Worker has at least one load to split and send. Do so. - char* msg = calloc(256, sizeof(char)); - sprintf(msg, " orig load: %i", thread_args_ptr->remaining_loads); - log(msg); free(msg); msg = calloc(256, sizeof(char)); + // char* msg = calloc(256, sizeof(char)); + // sprintf(msg, " orig load: %i", thread_args_ptr->remaining_loads); + // log(msg); free(msg); msg = calloc(256, sizeof(char)); work_send_value = thread_args_ptr->remaining_loads / 2; - sprintf(msg, " sending value: %i", work_send_value); - log(msg); free(msg); msg = calloc(256, sizeof(char)); + // sprintf(msg, " sending value: %i", work_send_value); + // log(msg); free(msg); msg = calloc(256, sizeof(char)); thread_args_ptr->remaining_loads = work_send_value + (thread_args_ptr->remaining_loads % 2); - sprintf(msg, " new personal load value: %i", thread_args_ptr->remaining_loads); - log(msg); free(msg); + // sprintf(msg, " new personal load value: %i", thread_args_ptr->remaining_loads); + // log(msg); free(msg); MPI_Isend(&work_send_value, 1, MPI_INT, index, tag_work_response, MPI_COMM_WORLD, &request_var); // Load has been split. Immediately attempt to update main processor of change. @@ -237,10 +246,10 @@ void worker_handle_request(thread_struct* thread_args_ptr) { } else { // Worker has exactly 0 or 1 loads. Not enough to send. Reject request instead. work_send_value = -1; - char* msg = calloc(256, sizeof(char)); - sprintf(msg, " rejecting request with current load of %i", thread_args_ptr->remaining_loads); - log(msg); - free(msg); + // char* msg = calloc(256, sizeof(char)); + // sprintf(msg, " rejecting request with current load of %i", thread_args_ptr->remaining_loads); + // log(msg); + // free(msg); MPI_Isend(&work_send_value, 1, MPI_INT, index, tag_work_response, MPI_COMM_WORLD, &request_var); } diff --git a/src/load_balance_schemes.c b/src/load_balance_schemes.c index 5b26ceeed22d56869c0ab01c06181e98c325d6f5..68b0072cbe76347e779050a6bd1ff7fafa8b1b4e 100644 --- a/src/load_balance_schemes.c +++ b/src/load_balance_schemes.c @@ -40,10 +40,11 @@ void arr_run(thread_struct* thread_args_ptr) { // Wait for all processes to synchronize. // Also sleep for a second to allow time for console output to process. - // MPI_Barrier(MPI_COMM_WORLD); - // sleep(1); + MPI_Barrier(MPI_COMM_WORLD); + sleep(1); if (process_rank == 0) { + printf("\n"); printf("Finished ARR load scheme.\n"); printf("\n"); } @@ -136,6 +137,7 @@ void arr_worker(thread_struct* thread_args_ptr) { int request_flag = -1; int index; int work_value = 0; + int send_status = 1; log("Starting ARR worker."); @@ -168,10 +170,17 @@ void arr_worker(thread_struct* thread_args_ptr) { // Decriment load number. thread_args_ptr->remaining_loads -= 1; + send_status = 1; } // If we made it this far, then worker no longer has work. + // Send notification message to main, to show value of 0. + if (send_status == 1) { + worker_send_status(process_rank, thread_args_ptr->remaining_loads); + send_status = 0; + } + // Check for pending work requests. msg_status_flag = 0; MPI_Iprobe(MPI_ANY_SOURCE, tag_work_request, MPI_COMM_WORLD, &msg_status_flag, MPI_STATUS_IGNORE); diff --git a/src/structs.c b/src/structs.c index b2baae546f2f78145b58043ebf617e0da8c0b82e..1983218a528f3f8d1e0daaea61684083b60893f1 100644 --- a/src/structs.c +++ b/src/structs.c @@ -24,7 +24,7 @@ thread_struct* thread_args_ptr; void _log(char const* file, long line, char const* message) { // Print to console (debugging only). - printf(" P%3i [%s:%ld] %s\n", thread_args_ptr->thread_num, file, line, message); + // printf(" P%3i [%s:%ld] %s\n", thread_args_ptr->thread_num, file, line, message); // Log to file. fprintf(thread_args_ptr->log_file, "[%s:%ld] %s\n", file, line, message);