• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

sst/elements/portals4_sm/trig_cpu/application.h

00001 // Copyright 2009-2010 Sandia Corporation. Under the terms
00002 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
00003 // Government retains certain rights in this software.
00004 // 
00005 // Copyright (c) 2009-2010, Sandia Corporation
00006 // All rights reserved.
00007 // 
00008 // This file is part of the SST software package. For license
00009 // information, see the LICENSE file in the top level directory of the
00010 // distribution.
00011 
00012 
00013 #ifndef COMPONENTS_TRIG_CPU_APPLICATION_H
00014 #define COMPONENTS_TRIG_CPU_APPLICATION_H
00015 
00016 #include <vector>
00017 #include <utility>
00018 #include <boost/tuple/tuple.hpp>
00019 
00020 #include <sst/core/event.h>
00021 #include "trig_cpu.h"
00022 
00023 class application {
00024 public:
00025     virtual bool operator()(SST::Event *ev) = 0;
00026 
00027 protected:
00028     application(trig_cpu *cpu) : cpu(cpu), state(0)
00029     {
00030         my_id = cpu->getMyId();
00031         num_nodes = cpu->getNumNodes();
00032     }
00033 
00034     virtual ~application() { }
00035 
00036     /**
00037      * Returns the floor form of binary logarithm for a 32 bit integer.
00038      * -1 is returned if n is 0.
00039      */
00040     uint32_t floorLog2(uint32_t n) {
00041         uint32_t pos = 0;
00042         if (n >= 1<<16) { n >>= 16; pos += 16; }
00043         if (n >= 1<< 8) { n >>=  8; pos +=  8; }
00044         if (n >= 1<< 4) { n >>=  4; pos +=  4; }
00045         if (n >= 1<< 2) { n >>=  2; pos +=  2; }
00046         if (n >= 1<< 1) {           pos +=  1; }
00047         return ((n == 0) ? (-1) : pos);
00048     }
00049 
00050     /**
00051      * Returns pair<int, vector<int>> containing root and children for
00052      * the current process and given radix
00053      */
00054     std::pair<int, std::vector<int> >
00055     buildBinomialTree(int radix)
00056     {
00057         std::vector<int> my_children;
00058         int my_root = -1;
00059 
00060         for (int i = 1 ; i <= num_nodes ; i *= radix) {
00061             int tmp_radix = (num_nodes / i < radix) ? (num_nodes / i) + 1 : radix;
00062             my_root = (my_id / (tmp_radix * i)) * (tmp_radix * i);
00063             if (my_root != my_id) break;
00064             for (int j = 1 ; j < tmp_radix ; ++j) {
00065                 if (my_id + i * j < num_nodes) {
00066                     my_children.push_back(my_id + i * j);
00067                 }
00068             }
00069         }
00070         std::reverse(my_children.begin(), my_children.end());
00071 
00072         return std::make_pair(my_root, my_children);
00073     }
00074 
00075     std::pair<int, std::vector<int> >
00076     buildNaryTree(int radix)
00077     {
00078         std::vector<int> my_children;
00079         int my_root = (my_id - 1) / radix;
00080         for (int i = 1 ; i <= radix ; ++i) {
00081             int tmp = radix * my_id + i;
00082             if (tmp < num_nodes) my_children.push_back(tmp);
00083         }
00084 
00085         return std::make_pair(my_root, my_children);
00086     }
00087 
00088     void start_noise_section() {
00089         cpu->start_noise_section();
00090     }
00091     
00092     void end_noise_section() {
00093         cpu->start_noise_section();
00094     }
00095     
00096     trig_cpu *cpu;
00097     int state;
00098     int my_id;
00099     int num_nodes;
00100 
00101 private:
00102     application(const application& a);
00103     void operator=(application const&);
00104 };
00105 
00106 /* Yeah for Duff's Device.  Google it. */
00107 #define crBegin() switch(state) { case 0:
00108 #define crReturn() do { state=__LINE__; return false; case __LINE__:; } while (0)
00109 #define crFinish() state = 0 ;  }
00110 
00111 #endif // COMPONENTS_TRIG_CPU_APPLICATION_H

Generated on Fri Oct 22 2010 11:02:25 for SST by  doxygen 1.7.1