HOG2
EnvUtil.h
Go to the documentation of this file.
1 //
2 // EnvUtil.h
3 // Rubik
4 //
5 // Created by Nathan Sturtevant on 4/7/13.
6 // Copyright (c) 2013 Nathan Sturtevant. All rights reserved.
7 //
8 
9 #ifndef __Rubik__EnvUtil__
10 #define __Rubik__EnvUtil__
11 
12 #include <iostream>
13 #include <vector>
14 #include <cassert>
15 #include <stdint.h>
16 #include <cstdio>
17 #include <cinttypes>
18 
19 const int openSize = 256;
20 const int numBuckets = 2;
21 
22 struct bucketInfo {
23  bool unused;
24  int bucketID;
25  int64_t numEntries;
26  int64_t bucketOffset;
27 };
28 
29 struct bucketData {
30  int64_t theSize;
31 #ifndef DISK
32  std::vector<uint8_t> data;
33 #endif
34 };
35 
36 struct bucketChanges {
37  bucketChanges() { pthread_mutex_init(&lock, NULL); }
38  bool updated;
41  std::vector<bool> changes;
42  // std::vector<bool> roundChanges;
43  std::vector<bool> nextChanges;
44  pthread_mutex_t lock;
45 };
46 
47 template <class Environment, class State>
48 void InitTwoPieceData(std::vector<bucketInfo> &data,
49  uint64_t maxBucketSize);
50 
51 template <class Environment, class State>
52 void InitBucketSize(std::vector<bucketData> &buckets,
53  uint64_t maxBucketSize);
54 
55 template <class Environment, class State>
56 void InitChanges(std::vector<bucketData> &buckets,
57  std::vector<bucketChanges> &twoPieceChanges,
58  uint64_t maxBucketSize);
59 
60 template <class Environment, class State>
61 uint64_t GetMaxBucketSize(bool print);
62 
63 //-// template defintions //-//
64 
65 template <class Environment, class State>
66 void InitTwoPieceData(std::vector<bucketInfo> &data, uint64_t maxBucketSize)
67 {
68  Environment cc;
69  State s;
70 
71  int64_t maxVal2 = cc.getMaxSinglePlayerRank2();
72  int64_t maxRank = cc.getMaxSinglePlayerRank();
73 
74  data.resize(maxVal2);
75 
76  uint64_t bucketSize = 0;
77 
78  int currBucket = 0;
79 
80  // build data sets
81  for (int64_t x = 0; x < maxRank; x++)
82  {
83  cc.unrankPlayer(x, s, 0);
84 
85  int64_t r1;
86  cc.rankPlayerFirstTwo(s, 0, r1);
87 
88  int64_t maxValOther = cc.getMaxSinglePlayerRank2(r1);
89  x+=maxValOther-1;
90 
91  if (bucketSize > maxBucketSize)
92  {
93  bucketSize = 0;
94  currBucket++;
95  }
96  data[r1].unused = false;
97  data[r1].bucketID = currBucket;
98  data[r1].numEntries = maxValOther;
99  data[r1].bucketOffset = bucketSize;
100  bucketSize += maxValOther;
101  // 64-bit align data in buckets
102  while (bucketSize%openSize != 0)
103  bucketSize++;
104  }
105 }
106 
107 template <class Environment, class State>
108 void InitBucketSize(std::vector<bucketData> &buckets,
109  uint64_t maxBucketSize)
110 {
111  Environment cc;
112  State s;
113 
114  int64_t maxRank = cc.getMaxSinglePlayerRank();
115 
116  // uint64_t bucketSize = 0;
117  buckets.resize(1);
118  buckets[0].theSize = 0;
119 
120  int currBucket = 0;
121 
122  // build data sets
123  for (int64_t x = 0; x < maxRank; x++)
124  {
125  cc.unrankPlayer(x, s, 0);
126 
127  int64_t r1, r2, r3, r4;
128  cc.rankPlayer(s, 0, r1, r2);
129  cc.rankPlayerFirstTwo(s, 0, r3);
130  cc.rankPlayerRemaining(s, 0, r4);
131  assert(r1 == r3);
132  assert(r2 == r4);
133 
134  int64_t maxValOther = cc.getMaxSinglePlayerRank2(r1);
135  x+=maxValOther-1;
136 
137  if (buckets[currBucket].theSize > maxBucketSize)
138  {
139  currBucket++;
140  buckets.resize(buckets.size()+1);
141  buckets[currBucket].theSize = 0;
142  }
143  buckets[currBucket].theSize += maxValOther;
144 
145  // 64-bit align data in buckets
146  while (buckets[currBucket].theSize%openSize != 0)
147  buckets[currBucket].theSize++;
148  }
149 }
150 
151 template <class Environment, class State>
152 void InitChanges(std::vector<bucketData> &buckets,
153  std::vector<bucketChanges> &twoPieceChanges,
154  uint64_t maxBucketSize)
155 {
156  Environment cc;
157  State s;
158 
159  int64_t maxVal2 = cc.getMaxSinglePlayerRank2();
160  int64_t maxRank = cc.getMaxSinglePlayerRank();
161 
162  twoPieceChanges.resize(maxVal2);
163 
164  // uint64_t bucketSize = 0;
165  buckets.resize(1);
166  buckets[0].theSize = 0;
167 
168  int currBucket = 0;
169 
170  // build data sets
171  for (int64_t x = 0; x < maxRank; x++)
172  {
173  cc.unrankPlayer(x, s, 0);
174 
175  int64_t r1, r2, r3, r4;
176  cc.rankPlayer(s, 0, r1, r2);
177  cc.rankPlayerFirstTwo(s, 0, r3);
178  cc.rankPlayerRemaining(s, 0, r4);
179  assert(r1 == r3);
180  assert(r2 == r4);
181 
182  int64_t maxValOther = cc.getMaxSinglePlayerRank2(r1);
183  x+=maxValOther-1;
184 
185  if (buckets[currBucket].theSize > maxBucketSize)
186  {
187  currBucket++;
188  buckets.resize(buckets.size()+1);
189  buckets[currBucket].theSize = 0;
190  }
191  twoPieceChanges[r1].lastDepthWritten = -1;
192  twoPieceChanges[r1].currDepthWritten = -1;
193  twoPieceChanges[r1].updated = false;
194  twoPieceChanges[r1].changes.resize((maxValOther+openSize-1)/openSize);
195  // twoPieceChanges[r1].roundChanges.resize((maxValOther+openSize-1)/openSize);
196  twoPieceChanges[r1].nextChanges.resize((maxValOther+openSize-1)/openSize);
197  buckets[currBucket].theSize += maxValOther;
198  // 64-bit align data in buckets
199  while (buckets[currBucket].theSize%openSize != 0)
200  buckets[currBucket].theSize++;
201  //numStatesLeft += maxValOther;
202  }
203 }
204 
205 template <class Environment, class State>
206 uint64_t GetMaxBucketSize(bool print)
207 {
208  uint64_t maxBucketSize;
209  State s;
210  Environment cc;
211  int64_t maxRank = cc.getMaxSinglePlayerRank();
212  //int64_t maxVal2 = cc.getMaxSinglePlayerRank2();
213  uint64_t statesLeft = 0;
214 
215  // count total states left to solve
216  for (int64_t x = 0; x < maxRank; x++)
217  {
218  cc.unrankPlayer(x, s, 0);
219 
220  int64_t r1;
221  cc.rankPlayerFirstTwo(s, 0, r1);
222 
223  int64_t maxValOther = cc.getMaxSinglePlayerRank2(r1);
224  x+=maxValOther-1;
225 
226  statesLeft += maxValOther;
227  }
228 
229  maxBucketSize = (statesLeft-2)/numBuckets; // leave space for extra storage needed to align things
230  // maxBucketSize *= 3;
231  if (print)
232  {
233  printf("%" PRId64 " total states. Using 2 buckets, each with about %" PRId64 " entries\n",
234  statesLeft, maxBucketSize);
235  }
236  return maxBucketSize;
237 }
238 
239 #endif /* defined(__Rubik__EnvUtil__) */
bucketChanges::updated
bool updated
Definition: EnvUtil.h:38
InitChanges
void InitChanges(std::vector< bucketData > &buckets, std::vector< bucketChanges > &twoPieceChanges, uint64_t maxBucketSize)
Definition: EnvUtil.h:152
bucketChanges::bucketChanges
bucketChanges()
Definition: EnvUtil.h:37
bucketInfo::bucketOffset
int64_t bucketOffset
Definition: EnvUtil.h:26
bucketData
Definition: EnvUtil.h:29
bucketInfo::unused
bool unused
Definition: EnvUtil.h:23
bucketData::theSize
int64_t theSize
Definition: EnvUtil.h:30
bucketInfo
Definition: EnvUtil.h:22
bucketChanges::currDepthWritten
int currDepthWritten
Definition: EnvUtil.h:39
InitBucketSize
void InitBucketSize(std::vector< bucketData > &buckets, uint64_t maxBucketSize)
Definition: EnvUtil.h:108
bucketChanges::lock
pthread_mutex_t lock
Definition: EnvUtil.h:44
bucketInfo::bucketID
int bucketID
Definition: EnvUtil.h:24
bucketChanges
Definition: EnvUtil.h:36
bucketChanges::changes
std::vector< bool > changes
Definition: EnvUtil.h:41
openSize
const int openSize
Definition: EnvUtil.h:19
bucketData::data
std::vector< uint8_t > data
Definition: EnvUtil.h:32
InitTwoPieceData
void InitTwoPieceData(std::vector< bucketInfo > &data, uint64_t maxBucketSize)
Definition: EnvUtil.h:66
GetMaxBucketSize
uint64_t GetMaxBucketSize(bool print)
Definition: EnvUtil.h:206
bucketChanges::nextChanges
std::vector< bool > nextChanges
Definition: EnvUtil.h:43
numBuckets
const int numBuckets
Definition: EnvUtil.h:20
bucketChanges::lastDepthWritten
int lastDepthWritten
Definition: EnvUtil.h:40
bucketInfo::numEntries
int64_t numEntries
Definition: EnvUtil.h:25