added taskAllocate()

git-svn-id: svn://anubis/gvsu@242 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-11-22 23:35:51 +00:00
parent 9723bbf212
commit de4d4a9968

View File

@ -6,104 +6,55 @@
#include <mpi.h> #include <mpi.h>
using namespace std; using namespace std;
#define TRIALS 100 /*
* taskAllocate() will divide a set of total_tasks tasks into
double sendAndTime(int length); * total_workers groups, as evenly as possible
void receive(int length); * 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 main(int argc, char * argv[])
{ {
int my_rank; int my_rank;
double max_bytes_per_sec = 0.0; int comm_size;
int max_bps_pkt_size = 0; int matrix_size = 100;
char hostname[256];
MPI_Init(&argc, &argv); MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); 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, if (!strncmp(argv[i], "-s", 2))
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)
{ {
cout << "Testing packet length " << length << endl; matrix_size = atoi(strlen(argv[i]) > 2
double rtt = sendAndTime(length); ? argv[i] + 2
double bytes_per_sec = (length * TRIALS) / rtt; : argv[++i]);
if (bytes_per_sec > max_bytes_per_sec)
{
max_bps_pkt_size = length;
max_bytes_per_sec = bytes_per_sec;
}
} }
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(); MPI_Finalize();
return 0; 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);
}
}