Skip to content

Instantly share code, notes, and snippets.

@markalle
Created August 14, 2020 17:49
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 markalle/9f92e9facbd71136bcfb9f0e0305a1da to your computer and use it in GitHub Desktop.
Save markalle/9f92e9facbd71136bcfb9f0e0305a1da to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include "mpi.h"
double
getavg(double *a, int n) {
int i;
double sum = 0;
for (i=0; i<n; ++i) {
sum += a[i];
}
return sum/n;
}
int
main(int argc, char* argv[])
{
MPI_Datatype dtbig, dtsmall;
MPI_Init(&argc, &argv);
int i;
/*
* Make dtbig as [double, --------, double, --------, etc..]
* Make dtsmall as just one [double, --------]
*/
int nelements = 200000; /* number of doubles */
MPI_Type_vector(nelements, 1, 2, MPI_DOUBLE ,&dtbig);
MPI_Type_commit(&dtbig);
MPI_Type_create_resized(MPI_DOUBLE, 0, 2*sizeof(double), &dtsmall);
MPI_Type_commit(&dtsmall);
double *packedbuf = malloc(nelements * sizeof(double));
double *unpackedbuf = malloc(nelements * 2 * sizeof(double));
int position;
/* set unpackedbuf[] to [(double)1, (double)-1, (double)2, (double)-1, ...] */
for (i=0; i<nelements; ++i) {
unpackedbuf[2*i+0] = i+1;
unpackedbuf[2*i+1] = -1;
}
#define CHECK_UBUF() \
do { \
for (i=0; i<nelements; ++i) { \
double got = unpackedbuf[2*i+0]; \
double exp = i+1; \
if (got != exp) { \
printf("unpacked[%d] = %f, exp %f\n", 2*i, got, exp); \
MPI_Abort(MPI_COMM_WORLD, MPI_ERR_OTHER); \
} \
} \
} while (0)
double t, tpack_big[5], tunpack_big[5], tpack_small[5], tunpack_small[5];
for (i=0; i<5; ++i) {
t = MPI_Wtime();
position = 0;
MPI_Pack(unpackedbuf, 1, dtbig,
packedbuf, nelements*sizeof(double), &position, MPI_COMM_WORLD);
tpack_big[i] = MPI_Wtime() - t;
}
for (i=0; i<5; ++i) {
t = MPI_Wtime();
position = 0;
MPI_Unpack(packedbuf, nelements*sizeof(double), &position,
unpackedbuf, 1, dtbig, MPI_COMM_WORLD);
tunpack_big[i] = MPI_Wtime() - t;
}
CHECK_UBUF();
for (i=0; i<5; ++i) {
t = MPI_Wtime();
position = 0;
MPI_Pack(unpackedbuf, nelements, dtsmall,
packedbuf, nelements*sizeof(double), &position, MPI_COMM_WORLD);
tpack_small[i] = MPI_Wtime() - t;
}
for (i=0; i<5; ++i) {
t = MPI_Wtime();
position = 0;
MPI_Unpack(packedbuf, nelements*sizeof(double), &position,
unpackedbuf, nelements, dtsmall, MPI_COMM_WORLD);
tunpack_small[i] = MPI_Wtime() - t;
}
CHECK_UBUF();
printf("times:\n");
printf("pack dtbig : %6.0f %6.0f %6.0f %6.0f %6.0f (avg %.0f) usec\n",
tpack_big[0] * 1000000,
tpack_big[1] * 1000000,
tpack_big[2] * 1000000,
tpack_big[3] * 1000000,
tpack_big[4] * 1000000,
getavg(tpack_big, 5) * 1000000);
printf("unpack dtbig : %6.0f %6.0f %6.0f %6.0f %6.0f (avg %.0f) usec\n",
tunpack_big[0] * 1000000,
tunpack_big[1] * 1000000,
tunpack_big[2] * 1000000,
tunpack_big[3] * 1000000,
tunpack_big[4] * 1000000,
getavg(tunpack_big, 5) * 1000000);
printf("pack dtsmall : %6.0f %6.0f %6.0f %6.0f %6.0f (avg %.0f) usec\n",
tpack_small[0] * 1000000,
tpack_small[1] * 1000000,
tpack_small[2] * 1000000,
tpack_small[3] * 1000000,
tpack_small[4] * 1000000,
getavg(tpack_small, 5) * 1000000);
printf("unpack dtsmall: %6.0f %6.0f %6.0f %6.0f %6.0f (avg %.0f) usec\n",
tunpack_small[0] * 1000000,
tunpack_small[1] * 1000000,
tunpack_small[2] * 1000000,
tunpack_small[3] * 1000000,
tunpack_small[4] * 1000000,
getavg(tunpack_small, 5) * 1000000);
MPI_Type_free(&dtbig);
MPI_Type_free(&dtsmall);
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment