Commits

Rio Yokota committed 924999f

options.h -> args.h.

Comments (0)

Files changed (4)

examples/serial.cxx

+#include "args.h"
 #include "dataset.h"
-#include "options.h"
 #include "serialfmm.h"
 #ifdef VTK
 #include "vtk.h"
 #endif
 
 int main(int argc, char ** argv) {
-  Args args[1];
-  parse_cmdline_args(argc, argv, args);
-  showArgs(args);
-
+  Args ARGS(argc,argv);
   Bodies bodies, jbodies;
   Cells cells, jcells;
   Dataset DATA;
   SerialFMM FMM;
-  FMM.NCRIT = args->NCRIT;
-  FMM.NSPAWN = args->NSPAWN;
-  FMM.IMAGES = args->IMAGES;
-  FMM.THETA = args->THETA;
+  FMM.NCRIT = ARGS.NCRIT;
+  FMM.NSPAWN = ARGS.NSPAWN;
+  FMM.IMAGES = ARGS.IMAGES;
+  FMM.THETA = ARGS.THETA;
   FMM.printNow = true;
 #if AUTO
   FMM.timeKernels();
     int numBodies = int(pow(10,(it+24)/8.0));
 #else
   {
-    int numBodies = args->numBodies;
+    int numBodies = ARGS.numBodies;
 #endif // MANY
     if(FMM.printNow) std::cout << std::endl
       << "N                    : " << numBodies << std::endl;
     bodies.resize(numBodies);
-    DATA.initBodies(bodies, args->distribution);
+    DATA.initBodies(bodies, ARGS.distribution);
     FMM.setBounds(bodies);
     FMM.buildTree(bodies,cells);                                // TODO : make it work without this
     FMM.resetTimer();
     FMM.startTimer("FMM");
     FMM.buildTree(bodies,cells);
     FMM.upwardPass(cells);
-    if (!args->buildOnly) {
+    if (!ARGS.buildOnly) {
       FMM.startPAPI();
-      FMM.evaluate(cells,cells,args->mutual);
+      FMM.evaluate(cells,cells,ARGS.mutual);
       FMM.stopPAPI();
       FMM.downwardPass(cells);
     }
     FMM.eraseTimer("FMM");
     FMM.writeTime();
     FMM.resetTimer();
-    if (!args->buildOnly) {
+    if (!ARGS.buildOnly) {
       jbodies = bodies;
-      if (int(bodies.size()) > args->numTarget) bodies.resize(args->numTarget);
+      if (int(bodies.size()) > ARGS.numTarget) bodies.resize(ARGS.numTarget);
       Bodies bodies2 = bodies;
       DATA.initTarget(bodies2);
       FMM.startTimer("Direct sum");
+#ifndef options_h
+#define options_h
+#include <getopt.h>
+
+struct option long_options[] = {
+  {"numBodies",    1, 0, 'n'},
+  {"numTarget",    1, 0, 't'},
+  {"ncrit",        1, 0, 'c'},
+  {"nspawn",       1, 0, 's'},
+  {"images",       1, 0, 'i'},
+  {"theta",        1, 0, 'h'},
+  {"buildOnly",    1, 0, 'b'},
+  {"mutual",       1, 0, 'm'},
+  {"distribution", 1, 0, 'd'},
+  {0, 0, 0, 0}
+};
+
+class Args {
+public:
+  int numBodies;
+  int numTarget;
+  int NCRIT;
+  int NSPAWN;
+  int IMAGES;
+  double THETA;
+  int buildOnly;
+  int mutual;
+  const char * distribution;
+
+private:
+  void usage(char * name) {
+    fprintf(stderr,
+            "Usage: %s [options]\n"
+            "Options:\n"
+            " --numBodies : Number of bodies (%d)\n"
+            " --numTarget : Number of targets for error checking (%d)\n"
+            " --ncrit : Number of bodies per leaf cell (%d)\n"
+            " --nspawn : Threshold for splitting both cells during recursion (%d)\n"
+            " --images : Number of periodic image levels (%d)\n"
+            " --theta : Multipole acceptance criterion (%f)\n"
+            " --buildOnly [0/1] : build tree and do not evaluate force (%d)\n"
+            " --mutual [0/1] :  use mutual interaction (%d)\n"
+            " --distribution [l/c/s/p] : lattice, cube, sphere, plummer (%s)\n",
+            name,
+            numBodies,
+            numTarget,
+            NCRIT,
+            NSPAWN,
+            IMAGES,
+            THETA,
+            buildOnly,
+            mutual,
+            distribution);
+  }
+
+  const char * parse(const char * arg) {
+    switch (arg[0]) {
+    case 'l':
+      return "lattice";
+    case 'c':
+      return "cube";
+    case 's':
+      return "sphere";
+    case 'p':
+      return "plummer";
+    default:
+      fprintf(stderr, "invalid distribution %s\n", arg);
+      exit(0);
+    }
+  }
+
+public:
+  Args(int argc, char ** argv) : numBodies(1000000), numTarget(100), NCRIT(10), NSPAWN(1000), IMAGES(0),
+                                 THETA(.6), buildOnly(0), mutual(1), distribution("cube") {
+    while (1) {
+      int option_index;
+      int c = getopt_long(argc, argv, "", long_options, &option_index);
+      if (c == -1) break;
+      switch (c) {
+      case 'n':
+        numBodies = atoi(optarg);
+        break;
+      case 't':
+        numTarget = atoi(optarg);
+        break;
+      case 'c':
+        NCRIT = atoi(optarg);
+        break;
+      case 's':
+        NSPAWN = atoi(optarg);
+        break;
+      case 'i':
+        IMAGES = atoi(optarg);
+        break;
+      case 'h':
+        THETA = atof(optarg);
+        break;
+      case 'b':
+        buildOnly = atoi(optarg);
+        break;
+      case 'm':
+        mutual = atoi(optarg);
+        break;
+      case 'd':
+        distribution = parse(optarg);
+        break;
+      default:
+        usage(argv[0]);
+        exit(0);
+      }
+    }
+    printf("numBodies: %d\n", numBodies);
+    printf("numTarget: %d\n", numTarget);
+    printf("ncrit: %d\n", NCRIT);
+    printf("nspawn: %d\n", NSPAWN);
+    printf("images: %d\n", IMAGES);
+    printf("theta: %f\n", THETA);
+    printf("buildOnly: %d\n", buildOnly);
+    printf("mutual: %d\n", mutual);
+    printf("distribution: %s\n", distribution);
+  }
+};
+
+#endif

include/options.h

-#ifndef options_h
-#define options_h
-#include <getopt.h>
-
-struct Args {
-  int numBodies;
-  int numTarget;
-  int NCRIT;
-  int NSPAWN;
-  int IMAGES;
-  double THETA;
-  int buildOnly;
-  int mutual;
-  const char * distribution;
-};
-
-static struct option long_options[] = {
-  {"numBodies",    1, 0, 'n'},
-  {"numTarget",    1, 0, 't'},
-  {"ncrit",        1, 0, 'c'},
-  {"nspawn",       1, 0, 's'},
-  {"images",       1, 0, 'i'},
-  {"theta",        1, 0, 'h'},
-  {"buildOnly",    1, 0, 'b'},
-  {"mutual",       1, 0, 'm'},
-  {"distribution", 1, 0, 'd'},
-  {0, 0, 0, 0}
-};
-
-static void showArgs(Args * args) {
-  printf("numBodies: %d\n", args->numBodies);
-  printf("numTarget: %d\n", args->numTarget);
-  printf("ncrit: %d\n", args->NCRIT);
-  printf("nspawn: %d\n", args->NSPAWN);
-  printf("images: %d\n", args->IMAGES);
-  printf("theta: %f\n", args->THETA);
-  printf("buildOnly: %d\n", args->buildOnly);
-  printf("mutual: %d\n", args->mutual);
-  printf("distribution: %s\n", args->distribution);
-}
-
-static Args defaultArgs() {
-  Args args;
-  args.numBodies = 1000000;
-  args.numTarget = 100;
-  args.NCRIT = 10;
-  args.NSPAWN = 1000;
-  args.IMAGES = 0;
-  args.THETA = 0.6;
-  args.buildOnly = 0;
-  args.mutual = 1;
-  args.distribution = "cube";
-  return args;
-}
-
-static void usage(char * name) {
-  Args args = defaultArgs();
-  fprintf(stderr, 
-          "Usage: %s [options]\n"
-          "Options:\n"
-          " --numBodies : Number of bodies (%d)\n"
-          " --numTarget : Number of targets for error checking (%d)\n"
-          " --ncrit : Number of bodies per leaf cell (%d)\n"
-          " --nspawn : Threshold for splitting both cells during recursion (%d)\n"
-          " --images : Number of periodic image levels (%d)\n"
-          " --theta : Multipole acceptance criterion (%f)\n"
-          " --buildOnly [0/1] : build tree and do not evaluate force (%d)\n"
-          " --mutual [0/1] :  use mutual interaction (%d)\n"
-          " --distribution [l/c/s/p] : lattice, cube, sphere, plummer (%s)\n",
-          name,
-          args.numBodies,
-          args.numTarget,
-          args.NCRIT,
-          args.NSPAWN,
-          args.IMAGES,
-          args.THETA,
-          args.buildOnly,
-          args.mutual,
-          args.distribution);
-}
-
-static const char * parse_distribution(const char * arg) {
-  switch (arg[0]) {
-  case 'l':
-    return "lattice";
-  case 'c':
-    return "cube";
-  case 's':
-    return "sphere";
-  case 'p':
-    return "plummer";
-  default:
-    fprintf(stderr, "invalid distribution %s\n", arg);
-    return NULL;
-  }
-}
-
-static Args * parse_cmdline_args(int argc, char ** argv, Args * args) {
-  *args = defaultArgs();
-  while (1) {
-    int option_index;
-    int c = getopt_long(argc, argv, "", long_options, &option_index);
-    if (c == -1) break;
-    switch (c) {
-    case 'n':
-      args->numBodies = atoi(optarg);
-      break;
-    case 't':
-      args->numTarget = atoi(optarg);
-      break;
-    case 'c':
-      args->NCRIT = atoi(optarg);
-      break;
-    case 's':
-      args->NSPAWN = atoi(optarg);
-      break;
-    case 'i':
-      args->IMAGES = atoi(optarg);
-      break;
-    case 'h':
-      args->THETA = atof(optarg);
-      break;
-    case 'b':
-      args->buildOnly = atoi(optarg);
-      break;
-    case 'm':
-      args->mutual = atoi(optarg);
-      break;
-    case 'd':
-      args->distribution = parse_distribution(optarg);
-      if (args->distribution == NULL) return NULL;
-      break;
-    default:
-      usage(argv[0]);
-      return NULL;
-    }
-  }
-  return args;
-}
-
-#endif

include/parameters.h

-#ifndef parameters_h
-#define parameters_h
-
-// Compile-time parameters
-const int P = 3;                                                //!< Order of expansions
-const float EPS2 = .0;                                          //!< Softening parameter (squared)
-
-// Runtime parameters
-struct Parameters {
-  int NCRIT;                                                    //!< Number of bodies per leaf cell
-  int NSPAWN;                                                   //!< Threshold of NDLEAF for spawning new threads
-  int IMAGES;                                                   //!< Number of periodic image sublevels
-  float THETA;                                                  //!< Multipole acceptance criteria
-  Parameters() : NCRIT(10), NSPAWN(1000), IMAGES(0), THETA(.6) {}
-};
-
-#endif