Advertisement
Guest User

Untitled

a guest
Dec 6th, 2015
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <omp.h>
  5. #include <sys/types.h>
  6. #include <sys/sysctl.h>
  7.  
  8. #include <boost/pool/object_pool.hpp>
  9.  
  10.  
  11. const size_t    LINE_SIZE = 64;
  12.  
  13.  
  14. struct Node
  15. {
  16.     Node *l, *r;
  17.     int i;
  18.    
  19.     Node(int i2) : l(0), r(0), i(i2)
  20.     {}
  21.     Node(Node *l2, int i2, Node *r2) : l(l2), r(r2), i(i2)
  22.     {}
  23.    
  24.     int check() const
  25.     {
  26.         if (l)
  27.             return l->check() + i - r->check();
  28.         else return i;
  29.     }
  30. };
  31.  
  32. typedef boost::object_pool<Node> NodePool;
  33.  
  34.  
  35. Node *make(int i, int d, NodePool &store)
  36. {
  37.     if (d > 0)
  38.         return store.construct( make(2*i-1, d-1, store),
  39.                                i,
  40.                                make(2*i, d-1, store)    );
  41.     return store.construct(i);
  42. }
  43.  
  44. typedef struct cpu_set {
  45.     uint32_t    count;
  46. } cpu_set_t;
  47.  
  48. static inline void
  49. CPU_ZERO(cpu_set_t *cs) { cs->count = 0; }
  50.  
  51. #define SYSCTL_CORE_COUNT   "machdep.cpu.core_count"
  52.  
  53. int sched_getaffinity(pid_t pid, size_t cpu_size, cpu_set_t *cpu_set)
  54. {
  55.     int32_t core_count = 0;
  56.     size_t  len = sizeof(core_count);
  57.     int ret = sysctlbyname(SYSCTL_CORE_COUNT, &core_count, &len, 0, 0);
  58.     if (ret) {
  59.         printf("error while get core count %d\n", ret);
  60.         return -1;
  61.     }
  62.     cpu_set->count = 0;
  63.     for (int i = 0; i < core_count; i++) {
  64.         cpu_set->count |= (1 << i);
  65.     }
  66.    
  67.     return 0;
  68. }
  69.  
  70. static inline void
  71. CPU_SET(int num, cpu_set_t *cs) { cs->count |= (1 << num); }
  72.  
  73. static inline int
  74. CPU_ISSET(int num, cpu_set_t *cs) { return (cs->count & (1 << num)); }
  75.  
  76. int GetThreadCount()
  77. {
  78.     cpu_set_t cs;
  79.     CPU_ZERO(&cs);
  80.     sched_getaffinity(0, sizeof(cs), &cs);
  81.    
  82.     int count = 0;
  83.     for (int i = 0; i < 8; i++)
  84.     {
  85.         if (CPU_ISSET(i, &cs))
  86.             count++;
  87.     }
  88.     return count;
  89. }
  90.  
  91.  
  92.  
  93.  
  94. int main(int argc, char *argv[])
  95. {
  96.     int min_depth = 4;
  97.     int max_depth = std::max(min_depth+2,
  98.                              (argc == 2 ? atoi(argv[1]) : 10));
  99.     max_depth = 20;
  100.     int stretch_depth = max_depth+1;
  101.    
  102.     // Alloc then dealloc stretchdepth tree
  103.     {
  104.         NodePool store;
  105.         Node *c = make(0, stretch_depth, store);
  106.         std::cout << "stretch tree of depth " << stretch_depth << "\t "
  107.         << "check: " << c->check() << std::endl;
  108.     }
  109.    
  110.     NodePool long_lived_store;
  111.     Node *long_lived_tree = make(0, max_depth, long_lived_store);
  112.    
  113.     // buffer to store output of each thread
  114.     char *outputstr = (char*)malloc(LINE_SIZE * (max_depth +1) * sizeof(char));
  115.    
  116. #pragma omp parallel for default(shared) num_threads(GetThreadCount()) schedule(dynamic, 1)
  117.     for (int d = min_depth; d <= max_depth; d += 2)
  118.     {
  119.         int iterations = 1 << (max_depth - d + min_depth);
  120.         int c = 0;
  121.        
  122.         for (int i = 1; i <= iterations; ++i)
  123.         {
  124.             NodePool store;
  125.             Node *a = make(i, d, store), *b = make(-i, d, store);
  126.             c += a->check() + b->check();
  127.         }
  128.        
  129.         // each thread write to separate location
  130.         sprintf(outputstr + LINE_SIZE * d, "%d\t trees of depth %d\t check: %d\n", (2 * iterations), d, c);
  131.     }
  132.    
  133.     // print all results
  134.     for (int d = min_depth; d <= max_depth; d += 2)
  135.         printf("%s", outputstr + (d * LINE_SIZE) );
  136.     free(outputstr);
  137.    
  138.     std::cout << "long lived tree of depth " << max_depth << "\t "
  139.     << "check: " << (long_lived_tree->check()) << "\n";
  140.    
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement