From de4d4a996878776665b955bc17229a539ee20cf2 Mon Sep 17 00:00:00 2001 From: josh Date: Sat, 22 Nov 2008 23:35:51 +0000 Subject: [PATCH] added taskAllocate() git-svn-id: svn://anubis/gvsu@242 45c1a28c-8058-47b2-ae61-ca45b979098e --- cs677/hw4/src/transpose.cc | 119 +++++++++++-------------------------- 1 file changed, 35 insertions(+), 84 deletions(-) diff --git a/cs677/hw4/src/transpose.cc b/cs677/hw4/src/transpose.cc index 3e8e109..a0d0093 100644 --- a/cs677/hw4/src/transpose.cc +++ b/cs677/hw4/src/transpose.cc @@ -6,104 +6,55 @@ #include using namespace std; -#define TRIALS 100 - -double sendAndTime(int length); -void receive(int length); +/* + * 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; - double max_bytes_per_sec = 0.0; - int max_bps_pkt_size = 0; - char hostname[256]; + 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); - if (my_rank == 0) + for (int i = 0; i < argc; i++) { - MPI_Recv(&hostname[0], 256, MPI_CHAR, MPI_ANY_SOURCE, - MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); - cout << "Remote host name: " << hostname << endl; - } - else - { - gethostname(&hostname[0], 255); - MPI_Send(&hostname[0], 256, MPI_CHAR, 0, 42, MPI_COMM_WORLD); - } - - for (int length = 100; length < 100000; length += 100) - { - if (my_rank == 0) + if (!strncmp(argv[i], "-s", 2)) { - cout << "Testing packet length " << length << endl; - double rtt = sendAndTime(length); - double bytes_per_sec = (length * TRIALS) / rtt; - if (bytes_per_sec > max_bytes_per_sec) - { - max_bps_pkt_size = length; - max_bytes_per_sec = bytes_per_sec; - } + matrix_size = atoi(strlen(argv[i]) > 2 + ? argv[i] + 2 + : argv[++i]); } - else - { - receive(length); - } - } - - if (my_rank == 0) - { - cout << "Maximum bytes per second (round trip) was " << max_bytes_per_sec << endl; - cout << "This occurred with a packet size of " << max_bps_pkt_size << endl; - } - else - { - cout << "Slave exiting." << endl; } MPI_Finalize(); return 0; } - -double sendAndTime(int length) -{ - char buff[length]; - for (int i = 0; i < length; i++) - { - buff[i] = rand() & 0xFF; - } - - struct timeval before, after; - gettimeofday(&before, NULL); /* Start timing */ - - for (int i = 0; i < TRIALS; i++) - { - int ret = MPI_Send(&buff, length, MPI_CHAR, 0, 42, MPI_COMM_WORLD); -// cout << "MPI_Send() returned " << ret << endl; - - MPI_Recv(&buff, length, MPI_CHAR, - MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); - } - - gettimeofday(&after, NULL); /* Stop timing */ - - double time_before = before.tv_sec + before.tv_usec / 1000000.0; - double time_after = after.tv_sec + after.tv_usec / 1000000.0; - double diff = time_after - time_before; - return diff; -} - -void receive(int length) -{ - char buff[length]; - - for (int i = 0; i < TRIALS; i++) - { - MPI_Recv(&buff, length, MPI_CHAR, - MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); - - MPI_Send(&buff, length, MPI_CHAR, 0, 42, MPI_COMM_WORLD); - } -}