Skip to content

Instantly share code, notes, and snippets.

@markalle
Created July 14, 2020 06:10
Show Gist options
  • Save markalle/6d70cf8ca14761e94bce9d0240000a3e to your computer and use it in GitHub Desktop.
Save markalle/6d70cf8ca14761e94bce9d0240000a3e to your computer and use it in GitHub Desktop.
pack external32 test
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include "mpi.h"
char inbuf[1024];
char packedbuf[1024];
char unpackedbuf[1024];
void
print_buf(char *buf, int nbytes)
{
int i;
if (nbytes > 24) {
printf("\n");
}
for (i=0; i<nbytes; ++i) {
if (i != 0 && i % 24 == 0) {
printf("\n ");
} else {
printf(" ");
if (i % 4 == 0) {
printf(" ");
}
}
printf("%02x", *((unsigned char *)(buf + i)));
}
}
void
clearbufs() {
int i;
for (i=0; i<1024; ++i) {
inbuf[i] = -1;
packedbuf[i] = -1;
unpackedbuf[i] = -1;
}
}
void
pack_and_unpack(int count, MPI_Datatype dt, int nbytes_to_print)
{
//int position;
MPI_Aint position;
position = 0;
//MPI_Pack(inbuf, count, dt, packedbuf, 1024, &position, MPI_COMM_WORLD);
MPI_Pack_external("external32", inbuf, count, dt, packedbuf, 1024, &position);
position = 0;
//MPI_Unpack(packedbuf, 1024, &position, unpackedbuf, count, dt, MPI_COMM_WORLD);
MPI_Unpack_external("external32", packedbuf, 1024, &position, unpackedbuf, count, dt);
printf("in :");
print_buf(inbuf, nbytes_to_print);
printf("\n");
printf("packed :");
print_buf(packedbuf, nbytes_to_print);
printf("\n");
printf("unpacked:");
print_buf(unpackedbuf, nbytes_to_print);
printf("\n");
}
int
main(int argc, char* argv[])
{
MPI_Datatype dt;
MPI_Datatype types[2];
int blocklens[2];
MPI_Aint disps[2];
int errors = 0;
MPI_Init(&argc, &argv);
// ------------------------------------------------------------
printf("------------------------------------------------------------\n");
printf("dt = [INT,INT] count=3\n");
MPI_Type_contiguous(2, MPI_INT, &dt);
MPI_Type_commit(&dt);
clearbufs();
((int*) inbuf)[0] = 1;
((int*) inbuf)[1] = 2;
((int*) inbuf)[2] = 3;
((int*) inbuf)[3] = 4;
((int*) inbuf)[4] = 5;
((int*) inbuf)[5] = 6;
pack_and_unpack(3, dt, 24);
MPI_Type_free(&dt);
if ( ((int*) unpackedbuf)[0] != 1 ||
((int*) unpackedbuf)[1] != 2 ||
((int*) unpackedbuf)[2] != 3 ||
((int*) unpackedbuf)[3] != 4 ||
((int*) unpackedbuf)[4] != 5 ||
((int*) unpackedbuf)[5] != 6 )
{
printf("error line %d\n", __LINE__);
++errors;
}
// ------------------------------------------------------------
printf("------------------------------------------------------------\n");
printf("dt = [INT,UNSIGNED] count=2\n");
blocklens[0] = 1; disps[0] = 0; types[0] = MPI_INT;
blocklens[1] = 1; disps[1] = 4; types[1] = MPI_UNSIGNED;
MPI_Type_create_struct(2, blocklens, disps, types, &dt);
MPI_Type_commit(&dt);
clearbufs();
((int*) inbuf)[0] = 1;
((int*) inbuf)[1] = 2;
((int*) inbuf)[2] = 3;
((int*) inbuf)[3] = 4;
pack_and_unpack(2, dt, 16);
MPI_Type_free(&dt);
if ( ((int*) unpackedbuf)[0] != 1 ||
((int*) unpackedbuf)[1] != 2 ||
((int*) unpackedbuf)[2] != 3 ||
((int*) unpackedbuf)[3] != 4 )
{
printf("error line %d\n", __LINE__);
++errors;
}
// ------------------------------------------------------------
printf("------------------------------------------------------------\n");
printf("dt = [LONG] count=2\n");
clearbufs();
((long*) inbuf)[0] = 1;
((long*) inbuf)[1] = 2;
pack_and_unpack(2, MPI_LONG, 16);
if ( ((long*) unpackedbuf)[0] != 1 ||
((long*) unpackedbuf)[1] != 2 )
{
printf("error line %d\n", __LINE__);
++errors;
}
// Check the packed buf too since the external32 LONG is differently sized
// than many systems. Data should be [00 00 00 01 00 00 00 02]
if (((char *)packedbuf)[3] != 1 ||
((char *)packedbuf)[4+3] != 2)
{
printf("error line %d (packed data)\n", __LINE__);
++errors;
}
// ------------------------------------------------------------
printf("------------------------------------------------------------\n");
printf("dt = [LONG,INT,INT] count=1\n");
blocklens[0] = 1; disps[0] = 0; types[0] = MPI_LONG;
blocklens[1] = 2; disps[1] = 8; types[1] = MPI_INT;
MPI_Type_create_struct(2, blocklens, disps, types, &dt);
MPI_Type_commit(&dt);
clearbufs();
*(long*) (inbuf + 0) = 1;
*(int*) (inbuf + sizeof(long)) = 2;
*(int*) (inbuf + sizeof(long) + 4) = 3;
pack_and_unpack(1, dt, 20);
MPI_Type_free(&dt);
if ( *(long*) (unpackedbuf + 0) != 1 ||
*(int*) (unpackedbuf + sizeof(long)) != 2 ||
*(int*) (unpackedbuf + sizeof(long) + 4) != 3)
{
printf("error line %d\n", __LINE__);
++errors;
}
// ------------------------------------------------------------
printf("------------------------------------------------------------\n");
printf("dt = [INT,<empty4b>] count=2\n");
MPI_Type_create_resized(MPI_INT, 0, 2*sizeof(int), &dt);
MPI_Type_commit(&dt);
clearbufs();
((int*) inbuf)[0] = 1;
((int*) inbuf)[1] = -1;
((int*) inbuf)[2] = 2;
((int*) inbuf)[3] = -1;
pack_and_unpack(2, dt, 16);
MPI_Type_free(&dt);
if ( ((int*) unpackedbuf)[0] != 1 ||
((int*) unpackedbuf)[1] != -1 || // was initialized to -1, should remain untouched
((int*) unpackedbuf)[2] != 2 ||
((int*) unpackedbuf)[3] != -1 )
{
printf("error line %d\n", __LINE__);
++errors;
}
// ------------------------------------------------------------
printf("------------------------------------------------------------\n");
printf("dt = [INT,<empty4b>,INT] count=1\n");
MPI_Type_vector(2, 1, 2, MPI_INT, &dt);
MPI_Type_commit(&dt);
clearbufs();
((int*) inbuf)[0] = 1;
((int*) inbuf)[1] = -1;
((int*) inbuf)[2] = 2;
pack_and_unpack(1, dt, 16);
MPI_Type_free(&dt);
if ( ((int*) unpackedbuf)[0] != 1 ||
((int*) unpackedbuf)[1] != -1 || // was initialized to -1, should remain untouched
((int*) unpackedbuf)[2] != 2)
{
printf("error line %d\n", __LINE__);
++errors;
}
// ------------------------------------------------------------
printf("------------------------------------------------------------\n");
printf("dt = [INT,INT,<empty12b>,INT,INT] count=1\n");
MPI_Type_vector(2, 2, 5, MPI_INT, &dt);
MPI_Type_commit(&dt);
clearbufs();
((int*) inbuf)[0] = 1;
((int*) inbuf)[1] = 2;
((int*) inbuf)[2] = -1;
((int*) inbuf)[3] = -1;
((int*) inbuf)[4] = -1;
((int*) inbuf)[5] = 3;
((int*) inbuf)[6] = 4;
pack_and_unpack(1, dt, 28);
MPI_Type_free(&dt);
if ( ((int*) unpackedbuf)[0] != 1 ||
((int*) unpackedbuf)[1] != 2 ||
((int*) unpackedbuf)[2] != -1 || // was initialized to -1, should remain untouched
((int*) unpackedbuf)[3] != -1 || // was initialized to -1, should remain untouched
((int*) unpackedbuf)[4] != -1 || // was initialized to -1, should remain untouched
((int*) unpackedbuf)[5] != 3 ||
((int*) unpackedbuf)[6] != 4)
{
printf("error line %d\n", __LINE__);
++errors;
}
// ------------------------------------------------------------
printf("------------------------------------------------------------\n");
printf("dt = [INT,<empty4b>,INT,INT,INT] count=1\n");
blocklens[0] = 1; disps[0] = 0; types[0] = MPI_INT;
blocklens[1] = 3; disps[1] = 8; types[1] = MPI_INT;
MPI_Type_create_struct(2, blocklens, disps, types, &dt);
MPI_Type_commit(&dt);
clearbufs();
((int*) inbuf)[0] = 1;
((int*) inbuf)[1] = -1;
((int*) inbuf)[2] = 2;
((int*) inbuf)[3] = 3;
((int*) inbuf)[4] = 4;
pack_and_unpack(1, dt, 20);
MPI_Type_free(&dt);
if ( ((int*) unpackedbuf)[0] != 1 ||
((int*) unpackedbuf)[1] != -1 ||
((int*) unpackedbuf)[2] != 2 ||
((int*) unpackedbuf)[3] != 3 ||
((int*) unpackedbuf)[4] != 4 )
{
printf("error line %d\n", __LINE__);
++errors;
}
// ------------------------------------------------------------
printf("------------------------------------------------------------\n");
printf("dt = [double_complex,double_complex,double_complex] count=2\n");
MPI_Type_contiguous(3, MPI_DOUBLE_COMPLEX, &dt);
MPI_Type_commit(&dt);
clearbufs();
((double*) inbuf)[0] = 1;
((double*) inbuf)[1] = 2;
((double*) inbuf)[2] = 3;
((double*) inbuf)[3] = 4;
((double*) inbuf)[4] = 5;
((double*) inbuf)[5] = 6;
((double*) inbuf)[6] = 7;
((double*) inbuf)[7] = 8;
((double*) inbuf)[8] = 9;
((double*) inbuf)[9] = 10;
((double*) inbuf)[10] = 11;
((double*) inbuf)[11] = 12;
pack_and_unpack(2, dt, 6*8);
MPI_Type_free(&dt);
if ( ((double*) unpackedbuf)[0] != 1 ||
((double*) unpackedbuf)[1] != 2 ||
((double*) unpackedbuf)[2] != 3 ||
((double*) unpackedbuf)[3] != 4 ||
((double*) unpackedbuf)[4] != 5 ||
((double*) unpackedbuf)[5] != 6 ||
((double*) unpackedbuf)[6] != 7 ||
((double*) unpackedbuf)[7] != 8 ||
((double*) unpackedbuf)[8] != 9 ||
((double*) unpackedbuf)[9] != 10 ||
((double*) unpackedbuf)[10] != 11 ||
((double*) unpackedbuf)[11] != 12)
{
printf("error line %d\n", __LINE__);
++errors;
}
printf("------------------------------------------------------------\n");
if (errors) {
MPI_Abort(MPI_COMM_WORLD, MPI_ERR_OTHER);
} else {
printf("no failures detected\n");
}
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment