From 606ff3aef43f9ed746627840811cd863d84fb3d7 Mon Sep 17 00:00:00 2001
From: "Peter W. Draper" <p.w.draper@durham.ac.uk>
Date: Tue, 10 Mar 2020 12:56:35 +0000
Subject: [PATCH] Add check that data received is as expected

---
 swiftmpiproxies.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 53 insertions(+), 1 deletion(-)

diff --git a/swiftmpiproxies.c b/swiftmpiproxies.c
index 80568dd..4de022a 100644
--- a/swiftmpiproxies.c
+++ b/swiftmpiproxies.c
@@ -54,6 +54,42 @@ int nr_send_pcells = 0;
 struct mpiuse_log_entry **recv_pcells;
 int nr_recv_pcells = 0;
 
+/**
+ * @brief fill a data area with a pattern that can be checked for changes.
+ *
+ * @param offset use offset bit pattern 01010101 otherwise 10101010.
+ * @param size size of data in bytes.
+ * @param data the data to fill.
+ */
+static void datacheck_fill(int offset, size_t size, void *data) {
+  unsigned char fill = 170;
+  if (offset) fill = fill >> 1;
+  unsigned char *p = (unsigned char *)data;
+  for (size_t i = 0; i < size; i++) {
+    p[i] = fill;
+  }
+}
+
+/**
+ * @brief test a filled data area for our pattern.
+ *
+ * @param offset check for offset bit pattern 01010101 otherwise 10101010.
+ * @param size size of data in bytes.
+ * @param data the data to fill.
+ *
+ * @result 1 on success, 0 otherwise.
+ */
+static int datacheck_test(int offset, size_t size, void *data) {
+  unsigned char fill = 170;
+  if (offset) fill = fill >> 1;
+
+  unsigned char *p = (unsigned char *)data;
+  for (size_t i = 0; i < size; i++) {
+    if (p[i] != fill) return 0;
+  }
+  return 1;
+}
+
 /**
  * @brief Pick out the relevant logging data for our rank, i.e. all
  * activations of sends and recvs. We ignore the original completion logs,
@@ -195,6 +231,9 @@ int main(int argc, char *argv[]) {
 
     /* Start Isend of pcells. */
     log->data = calloc(log->size, 1);
+
+    /* Fill data with a pattern for checking on arrival. */
+    datacheck_fill(0, log->size, log->data);
     res = MPI_Isend(log->data, log->size, MPI_BYTE, log->otherrank,
                     basetag + proxy_tag_cells, MPI_COMM_WORLD,
                     &req_pcells_out[k]);
@@ -225,6 +264,9 @@ int main(int argc, char *argv[]) {
     int basetag = log->otherrank * proxy_tag_shift;
 
     pcells_in[pid] = calloc(pcells_size[pid], 1);
+
+    /* Fill data with a pattern for checking when overwritten. */
+    datacheck_fill(1, log->size, log->data);
     res = MPI_Irecv(pcells_in[pid], pcells_size[pid], MPI_BYTE, log->otherrank,
                     basetag + proxy_tag_cells, MPI_COMM_WORLD,
                     &req_pcells_in[pid]);
@@ -246,7 +288,17 @@ int main(int argc, char *argv[]) {
     if (res != MPI_SUCCESS || pid == MPI_UNDEFINED)
       error("MPI_Waitany failed.");
 
-    /* XXX check the data received is correct? */
+    /* Check the data received is correct. */
+    struct mpiuse_log_entry *log = send_pcells[pid];
+    if (!datacheck_test(1, log->size, log->data)) {
+      if (datacheck_test(0, log->size, log->data)) {
+        error("Received data is not modified");
+      } else {
+        error("Received data is corrupt");
+      }
+    } else {
+      message("Received data is correct");
+    }
   }
   message("All proxy cells have arrived");
 
-- 
GitLab