/*************************************************************** File: div_conquer.h Description: A program that implements a divide and conquer function computing the sum of an array. Author: Dana Vrajitoru Organization: IUSB, Computer and Information Sciences Date: September 2020 ****************************************************************/ #ifndef DIV_CONQUER_H #define DIV_CONQUER_H // Creates the threads calling the Sum_thread function. void Create_threads(pthread_t *threads, int nr_threads); // Joins all of the threads so that the program doesn;t end before // they have finished the work. void Synchronise(pthread_t *threads, int nr_threads); // Inputs the number of ships and initializes the global variables. void Init_data(int &nthreads); // A barrier function based on the number of threads implemented using // a conditional variable. It uses a global counter intialized as // 0. Each thread coming in increments the counter. If the count is // not equal to the number of threads, it goes into a wait on the // conditional variable. The thread that finds the counter equal to // the number of threads broadcasts the condition that releases all of // them. void Barrier(int nr_threads) ; // The thread function: gets an id number, sumputes the partial sum on // its part of the array, then communicates the results globally using // one of the report functions. void * Sum_thread(void *arg) ; // Copies the value of the global variable size into the local // variable n. void Global_to_local(int &n) ; // Copies the value of a global id into the reference parameter and // increments the global id. void Get_id(int &id); // Waits for a given delay specified in miliseconds. void Wait(int miliseconds); // Communicating and combining the results of the partial computations // in an asynchronous linear way. void Report_linear(int sum, int id) ; // Communicating and combining the results of the partial computations // in an order based on a binary tree with root = 0 and where at each // level, the thread id accumulates the results from itself and id+ a // power of 2 starting from 1 and doubling in each iteration. void Report_tree(int sum, int id) ; #endif