// A simple MPI task farm // // To compile // // [mymachine]: mpicc -o farm farm.c // // and to run // // [mymachine]: mpirun --oversubscribe -np 8 ./farm // // where the number (here 8) gives the number of processes you want // (one of which will be the farmer, the others being workers). // // For simplicity, a "task" is just a randomly generated integer. // To "execute" a task, the worker simply sleeps for the given // number of seconds. You can experiment with the number of // tasks and the number of workers to see how the load balance // is affected. #include #include #include #include #include #define MAX_TASKS 100 #define NO_MORE_TASKS MAX_TASKS+1 void farmer (int workers); void worker (int rank); int main(int argc, char *argv[]) { int np, rank; time_t t; t = time(NULL); // seed the random number srand((int) t); // generator from outside MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &np); if (rank == 0) { farmer(np-1); } else { worker(rank); } MPI_Finalize(); } void farmer (int workers) { int i, task[MAX_TASKS], result[MAX_TASKS], temp, tag, who; MPI_Status status; for (i=0; i