From b171a73e7b956199014b4655bb908e5237b28508 Mon Sep 17 00:00:00 2001
From: "Peter W. Draper" <p.w.draper@durham.ac.uk>
Date: Thu, 11 May 2023 13:17:08 +0100
Subject: [PATCH] Add -z option to fakes to output a log that can be re-read

---
 mpiuse.c              | 49 ++++++++++++++++++++++++++++++-------------
 mpiuse.h              |  4 ++--
 swiftmpifakestepsim.c | 21 ++++++++++++-------
 3 files changed, 50 insertions(+), 24 deletions(-)

diff --git a/mpiuse.c b/mpiuse.c
index e25981d..f49baa8 100644
--- a/mpiuse.c
+++ b/mpiuse.c
@@ -174,9 +174,11 @@ void mpiuse_log_restore(const char *filename) {
  * @brief dump the logs for all ranks to a file.
  *
  * @param nranks the number of ranks.
+ * @param standard only write a standard log, this can be used as input to
+ *                 other runs.
  * @param dumpfile the file to write
  */
-void mpiuse_dump_logs(int nranks, const char *dumpfile) {
+void mpiuse_dump_logs(int nranks, int standard, const char *dumpfile) {
 
   /* Make sure output file is empty, only on one rank. */
   FILE *fd;
@@ -184,13 +186,21 @@ void mpiuse_dump_logs(int nranks, const char *dumpfile) {
     fd = fopen(dumpfile, "w");
 
     /* Header. */
-    fprintf(fd,
-            "# logticin logtic injtic endtic dtic step rank otherrank itype "
-            " isubtype tag size nr_tests tsum tmin tmax\n");
+    if (standard) {
+      fprintf(fd,
+              "# stic etic dtic step rank otherrank type itype "
+              " subtype isubtype activation tag size sum\n");
+    } else {
+      fprintf(fd,
+              "# logticin logtic injtic endtic dtic step rank otherrank itype "
+              " isubtype tag size nr_tests tsum tmin tmax\n");
+    }
     fclose(fd);
   }
   MPI_Barrier(MPI_COMM_WORLD);
 
+  const char *types[] = {"send", "recv"};
+
   /* Loop over all ranks, one by one, getting each rank to append their
    * logs. */
   for (int k = 0; k < nranks; k++) {
@@ -210,20 +220,31 @@ void mpiuse_dump_logs(int nranks, const char *dumpfile) {
        * version to match the expected injection times for this new run. */
       size_t nlogs = mpiuse_log_count;
       ticks basetics = 0;
+      long long sum = 0;
       for (size_t k = 0; k < nlogs; k++) {
         struct mpiuse_log_entry *log = &mpiuse_log[k];
         if (log->rank == myrank && log->endtic > 0) {
           if (basetics == 0) basetics = log->tic;
-          fprintf(fd,
-                  "%lld %.4f %.4f %.4f %.6f %d %d %d %d %d %d %zd %d %.4f %.6f "
-                  "%.6f\n",
-                  log->tic, clocks_from_ticks(log->tic - basetics),
-                  clocks_from_ticks(log->injtic - clocks_start_ticks),
-                  clocks_from_ticks(log->endtic - clocks_start_ticks),
-                  clocks_from_ticks(log->endtic - log->injtic), log->step,
-                  log->rank, log->otherrank, log->type, log->subtype, log->tag,
-                  log->size, log->nr_tests, clocks_from_ticks(log->tsum),
-                  clocks_from_ticks(log->tmin), clocks_from_ticks(log->tmax));
+          if (standard) {
+            fprintf(fd, "%lld %lld %lld %d %d %d %s %d %s %d %d %d %zd %lld\n",
+                    log->injtic, log->injtic, log->endtic - log->injtic,
+                    log->step, log->rank, log->otherrank,
+                    types[log->type - SEND_TYPE], log->type, "none",
+                    log->subtype, log->activation, log->tag, log->size, sum);
+            sum += log->size;
+          } else {
+            fprintf(
+                fd,
+                "%lld %.4f %.4f %.4f %.6f %d %d %d %d %d %d %zd %d %.4f %.6f "
+                "%.6f\n",
+                log->tic, clocks_from_ticks(log->tic - basetics),
+                clocks_from_ticks(log->injtic - clocks_start_ticks),
+                clocks_from_ticks(log->endtic - clocks_start_ticks),
+                clocks_from_ticks(log->endtic - log->injtic), log->step,
+                log->rank, log->otherrank, log->type, log->subtype, log->tag,
+                log->size, log->nr_tests, clocks_from_ticks(log->tsum),
+                clocks_from_ticks(log->tmin), clocks_from_ticks(log->tmax));
+          }
         }
       }
       fclose(fd);
diff --git a/mpiuse.h b/mpiuse.h
index 71d8982..7ae96d6 100644
--- a/mpiuse.h
+++ b/mpiuse.h
@@ -88,7 +88,7 @@ struct mpiuse_log_entry {
 #ifndef SEND_TYPE
 #define SEND_TYPE 25
 #define RECV_TYPE 26
-#define NO_SUBTYPE 0
+#define NO_SUBTYPE 1
 #endif
 
 /* API. */
@@ -99,7 +99,7 @@ struct mpiuse_log_entry *mpiuse_get_log(int ind);
 void mpiuse_log_restore(const char *filename);
 int mpiuse_nr_logs(void);
 int mpiuse_nr_ranks(void);
-void mpiuse_dump_logs(int nranks, const char *logfile);
+void mpiuse_dump_logs(int nranks, int standard, const char *logfile);
 
 void mpiuse_log_generate(int nr_nodes, int nr_logs, int size, int random,
                          long int seed, int uniform, const char *cdf,
diff --git a/swiftmpifakestepsim.c b/swiftmpifakestepsim.c
index 2ded9e0..e73ab83 100644
--- a/swiftmpifakestepsim.c
+++ b/swiftmpifakestepsim.c
@@ -388,7 +388,7 @@ static void pick_logs(int random) {
  * @brief usage help.
  */
 static void usage(char *argv[]) {
-  fprintf(stderr, "Usage: %s [options] nr_messages logfile.dat\n", argv[0]);
+  fprintf(stderr, "Usage: %s [vds:rgx:c:o:f:z] nr_messages logfile.dat\n", argv[0]);
   fprintf(stderr,
           " options: -v verbose, -d data check, -s size (bytes/scale), \n"
           "\t -f <1|2> randomize injection order, 1 == just sends, "
@@ -398,7 +398,8 @@ static void usage(char *argv[]) {
           "\t-r -c <file> use cdf from file, size is a scale factor., |\n"
           "\t-r -o <file> use occurence sample of values in a file, size is a "
           "scale factor.,] \n"
-          "\t-x random seed\n");
+          "\t-x random seed\n"
+          "\t-z outout log in standard format, i.e. can be used as input");
   fflush(stderr);
 }
 
@@ -424,15 +425,16 @@ int main(int argc, char *argv[]) {
   /* Handle the command-line, we expect the number of messages to exchange per
    * rank an output log and some options, the interesting ones are a size and
    * whether to use a random selections of various kinds. */
-  int size = 1024;
-  int random = 0;
-  int randomorder = 0;
-  int uniform = 1;
   char *cdf = NULL;
   char *odata = NULL;
   int opt;
+  int random = 0;
+  int randomorder = 0;
+  int size = 1024;
+  int standard = 0;
+  int uniform = 1;
   unsigned int seed = default_seed;
-  while ((opt = getopt(argc, argv, "vds:rgx:c:o:f:")) != -1) {
+  while ((opt = getopt(argc, argv, "vds:rgx:c:o:f:z")) != -1) {
     switch (opt) {
       case 'd':
         datacheck = 1;
@@ -458,6 +460,9 @@ int main(int argc, char *argv[]) {
       case 'v':
         verbose = 1;
         break;
+      case 'z':
+        standard = 1;
+        break;
       case 'x':
         seed = atol(optarg);
         break;
@@ -557,7 +562,7 @@ int main(int argc, char *argv[]) {
   MPI_Barrier(MPI_COMM_WORLD);
   fflush(stdout);
   if (myrank == 0) message("Dumping updated log");
-  mpiuse_dump_logs(nranks, logfile);
+  mpiuse_dump_logs(nranks, standard, logfile);
 
   /* Shutdown MPI. */
   res = MPI_Finalize();
-- 
GitLab