
#include <iostream>
#include <sys/time.h>
#include <unistd.h>     /* gethostname() */
#include <stdlib.h>     /* rand() */
#include <mpi.h>
using namespace std;

/*
 * taskAllocate() will divide a set of total_tasks tasks into
 * total_workers groups, as evenly as possible
 * Parameters:
 *   total_tasks   : IN  : the total number of tasks to divide up
 *   total_workers : IN  : the total number of workers to allocate tasks to (>0)
 *   this_id       : IN  : the id (0-based) of the task calling us for work
 *   first_task_id : OUT : the id (0-based) of the first task for this worker
 *   num           : OUT : the number of tasks assigned to this worker
 */
inline void taskAllocate(int total_tasks, int total_workers, int this_id,
                         int * first_task_id, int * num)
{
    int l_num;
    int leftovers = total_tasks % total_workers; /* num of "leftover" tasks */
    if (this_id < leftovers)
    {
        l_num = total_tasks / total_workers + 1; /* do one of the leftovers */
        *first_task_id = l_num * this_id;
    }
    else
    {
        l_num = total_tasks / total_workers;
        *first_task_id = l_num * this_id + leftovers;
    }
    *num = l_num;
}

int main(int argc, char * argv[])
{
    int my_rank;
    int comm_size;
    int matrix_size = 100;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &comm_size);

    for (int i = 0; i < argc; i++)
    {
        if (!strncmp(argv[i], "-s", 2))
        {
            matrix_size = atoi(strlen(argv[i]) > 2
                               ? argv[i] + 2
                               : argv[++i]);
        }
    }

    MPI_Finalize();

    return 0;
}
