Skip to content

Instantly share code, notes, and snippets.

@jsquyres
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsquyres/78ae1841248a40fc63ab to your computer and use it in GitHub Desktop.
Save jsquyres/78ae1841248a40fc63ab to your computer and use it in GitHub Desktop.
holiday-wishes-2015.c
/* You will need a C99 compiler and an implementation of
the POSIX human interface function feel_joy(3). */
#include <mpi.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
enum wish_t {
HAPPY_HANUKKAH,
MERRY_CHRISTMAS,
HAPPY_KWANZAA,
HAPPY_NEW_YEAR,
HAPPY_FESTIVUS,
NWISHES
};
void do_blog_author(void)
{
struct tm tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 2,
.tm_mon = 0,
.tm_year = 2015 - 1900
};
time_t done;
done = mktime(&tm);
int size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
int *wishes;
wishes = calloc(size, sizeof(int));
for (int i = 0; i < size * 2; ++i) {
wishes[i] = i % NWISHES;
}
struct timeval tv;
gettimeofday(&tv, NULL);
int i = 0;
int wish;
while (tv.tv_sec < done) {
MPI_Scatter(wishes + i, 1, MPI_INT,
&wish, 1, MPI_INT,
0, MPI_COMM_WORLD);
feel_joy(wish);
i = (i + 1) % size;
gettimeofday(&tv, NULL);
}
for (int i = 0; i < size; ++i) {
wishes[i] = -1;
}
MPI_Scatter(wishes, 1, MPI_INT,
&wish, 1, MPI_INT,
0, MPI_COMM_WORLD);
free(wishes);
}
void do_blog_reader(void)
{
int wish;
do {
MPI_Scatter(NULL, 0, MPI_DATATYPE_NULL,
&wish, 1, MPI_INT,
0, MPI_COMM_WORLD);
} while (feel_joy(wish));
}
int main(int argc, char* argv[])
{
MPI_Init(&argc, &argv);
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (0 == rank) {
do_blog_author();
} else {
do_blog_reader();
}
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment