Othello
 All Classes Files Functions Variables Macros
muloth.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include "othello.h"
7 #include "io_helper.h"
8 #include <cstdio>
9 #include <cstdlib>
10 
11 #include <cstring>
12 using namespace std;
13 const char * VERSION = GITVERSION;
14 //#pragma message "VERSION: " GITVERSION
15 
16 // split(k,plow,phigh,spl) (((plow)=(k & ((1ULL << spl)-1))), ((phigh)=(k >> spl)))
17 
23 template <typename keyType, typename IOvalueType = uint16_t>
24 class MulOth {
25  typedef uint64_t valueType;
26  uint32_t L;
27  vector<Othello<keyType> *> vOths;
28  unsigned char split;
29  template<typename VT>
30  bool addOth(unsigned int groupid, vector<keyType> &keys, vector<VT> &values) {
31  Othello<keyType> *poth;
32  poth = new Othello<keyType>(L, &keys[0], keys.size(), true, &values[0], sizeof(values[0]));
33  if (!poth->build) {
34  printf("Build Halt!\n");
35  return false;
36  }
37  vOths[groupid] = poth;
38  if ((poth -> removedKeys).size()>0) {
39  for (keyType k : poth->removedKeys) {
40  keyType fullk; helper->combgrp(fullk,groupid,k);
41  printf("Key in Grp %x : %llx is removed \n", groupid, fullk);
42  removedKeys.push_back(fullk);
43  }
44  }
45  return true;
46  }
47 public:
48  vector<keyType> removedKeys;
49  bool buildsucc;
52  MulOth( uint32_t _L, uint32_t NN) {
53  split = 0;
54  L = _L;
55  vector<keyType> keys;
56  vector<uint8_t> values;
57  for (int i = 0 ; i < NN; i ++) {
58  keys.push_back((((uint64_t) i) << 32) + i + 1);
59  values.push_back(i & 0xFF);
60  }
61  vOths.clear();
62  vOths.resize(1);
63  addOth(0,keys,values);
64  }
73  MulOth(uint32_t _L, unsigned char _split, class FileReader<keyType, IOvalueType> * _reader) {
74  helper = _reader->helper;
75  L = _L;
76  vOths.clear();
77  vOths.resize(1<<_split);
78  char buf[1024];
79  split = _split;
80  if (split ==0) {
81  keyType k; IOvalueType v;
82  vector<keyType> keys;
83  vector<IOvalueType> values;
84  while (true) {
85  if (!_reader->getNext(&k,&v)) break;
86  keys.push_back(k);
87  values.push_back(v);
88  }
89  buildsucc = addOth(0,keys,values);
90  _reader->finish();
91  return;
92  }
93 
94  if (_reader->getFileIsSorted()) {
95  uint32_t grpid = 0;
96  printf("Reading file for keys in group %2x/%2x\n", grpid,(1<<split)-1);
97  vector<keyType> keys;
98  vector<IOvalueType> values;
99  while (true) {
100  keyType k; IOvalueType v; keyType keyingroup; uint32_t groupid;
101  if (!_reader->getNext(&k,&v)) break;
102  _reader->helper->splitgrp(k,groupid,keyingroup);
103  if (groupid != grpid) {
104  if (!addOth(grpid,keys,values))
105  {_reader->finish(); return;}
106  grpid = groupid;
107  printf("Reading file for keys in group %2x/%0x\n", grpid,(1<<split)-1);
108  keys.clear();
109  values.clear();
110  }
111  keys.push_back(keyingroup);
112  values.push_back(v);
113  }
114  if (!addOth(grpid,keys,values))
115  { _reader->finish(); return;}
116  }
117  else
118  for (uint32_t grpid = 0; grpid < (1<<_split); grpid++) {
119  printf("Reading file for keys in group %2x/%2x\n", grpid,(1<<split)-1);
120  vector<keyType> keys;
121  vector<IOvalueType> values;
122  _reader->reset();
123  while (true) {
124  keyType k; IOvalueType v; keyType keyingroup; uint32_t groupid;
125  if (!_reader->getNext(&k,&v)) break;
126  _reader->helper->splitgrp(k,groupid,keyingroup);
127  if (groupid != grpid) continue;
128  keys.push_back(keyingroup);
129  values.push_back(v);
130  }
131  printf("keycount %d ", keys.size());
132  if (keys.size()>0)
133  if (!addOth(grpid,keys,values))
134  {_reader->finish(); return;}
135  }
136 
137  buildsucc = true;
138  _reader->finish();
139  }
140  MulOth(uint32_t _L, const char * fname, unsigned char _split, class IOHelper<keyType,IOvalueType> * _helper, bool fileIsSorted = false) :
141  MulOth(_L, _split, new KmerFileReader<keyType,IOvalueType>( fname, _helper, fileIsSorted))
142  {
143 
144  }
145 
149  inline IOvalueType query(const keyType &k) {
150  uint32_t grp;
151  keyType kgrp;
152  if (split ==0)
153  return vOths[0]->queryInt(k);
154  else {
155  helper->splitgrp(k,grp,kgrp);
156  return (vOths[grp]!=NULL)?vOths[grp]->queryInt(kgrp):0;
157  }
158  }
159  void printall () {
160  printf("Printall ...\n");
161  for (auto V : vOths)
162  V.printall();
163  }
178  void writeToFile(const char* fname) {
179  FILE *pFile;
180  pFile = fopen (fname, "wb");
181  unsigned char buf0x20[0x20];
182  char *p;
183  p = (char *) &(buf0x20[0]);
184  memset(buf0x20,0, sizeof(buf0x20));
185  strcpy(p+0x4,VERSION);
186  uint32_t split32 = split;
187  memcpy(buf0x20, &split32, sizeof(uint32_t));
188  fwrite(buf0x20,sizeof(buf0x20),1,pFile);
189  for (int i = 0; i <(1<<split); i++)
190  {
191  if (vOths[i]!=NULL)
192  vOths[i]->exportInfo(buf0x20);
193  else
194  memset(buf0x20,0, sizeof(buf0x20));
195  fwrite(buf0x20,sizeof(buf0x20),1,pFile);
196  }
197  for (int i = 0; i <(1<<split); i++) {
198  if (vOths[i]!=NULL)
199  vOths[i]->writeDataToBinaryFile(pFile);
200 // fwrite(&(vOths[i]->mem[0]),sizeof(vOths[i]->mem[0]), vOths[i]->mem.size(), pFile);
201  }
202 
203  uint32_t nRemovedKeys = removedKeys.size();
204  fwrite(&nRemovedKeys,sizeof(nRemovedKeys),1,pFile);
205  for (int i = 0 ; i < nRemovedKeys; i++)
206  fwrite(&removedKeys[i],sizeof(keyType),1,pFile);
207 
208  fclose(pFile);
209  }
210 
212  MulOth(const char* fname, IOHelper<keyType,IOvalueType> * _helper): helper(_helper) {
213  buildsucc = false;
214  printf("Read from binary Othello file %s\n", fname);
215  FILE *pFile;
216  pFile = fopen (fname, "rb");
217  uint32_t compversion;
218  unsigned char buf0x20[0x20];
219  fread(buf0x20,sizeof(buf0x20),1,pFile);
220  char *p;
221  p = (char *) &(buf0x20[0]);
222 #ifndef NO_VERSION_CHECK
223  if (strcmp(p+4,VERSION)) {
224  printf("Wrong version number\n");
225  fclose(pFile);
226  return;
227  }
228 #endif
229  uint32_t split32;
230  memcpy(&split32, buf0x20, sizeof(uint32_t));
231  split = split32;
232  vOths.clear();
233  for (int i = 0; i < (1<<split); i++) {
234  fread(buf0x20,sizeof(buf0x20),1,pFile);
235  Othello<keyType> *poth = NULL;
236  uint64_t *ppoth; ppoth = (uint64_t*) &buf0x20;
237  if (*ppoth)
238  poth = new Othello<keyType>(buf0x20);
239  vOths.push_back(poth);
240  }
241  for (int i = 0; i < (1<< split); i++) {
242  if (vOths[i] != NULL) {
243  vOths[i]->loadDataFromBinaryFile(pFile);
244  L= vOths[i]->L;
245  }
246  //fread(&(vOths[i]->mem[0]),sizeof(vOths[i]->mem[0]), vOths[i]->mem.size(), pFile);
247  }
248  uint32_t nRemovedKeys = removedKeys.size();
249  fread(&nRemovedKeys,sizeof(nRemovedKeys),1,pFile);
250  removedKeys.resize(nRemovedKeys);
251  for (int i = 0 ; i < nRemovedKeys; i++)
252  fread(&removedKeys[i],sizeof(keyType),1,pFile);
253 
254  fclose(pFile);
255  buildsucc = true;
256  }
257  ~MulOth() {
258  for (auto p: vOths)
259  delete p;
260  }
261 };
262 
263 
264 //MulOthello binary file descriptor
265 //0x00 : uint32_t splitbit
266 //0x04 : uint32_t signature MulOth version
267 //0x20 : OthInfo1
268 //0x40 : OthInfo2
269 //...
270 //offset1 : Oth[0].m
271 //offset2 = offset1 + Oth[0].hashupperbound = Oth[1].m
272 //...
273 
274 //OthInfo: 32 Byte
275 //0x00 : uint64_t hash1
276 //0x08 : uint64_t hash2
277 //0x10 : uint32_t mask1
278 //0x14 : uint32_t mask2
279 //0x18 : uint64_t m.offset
280 
void writeToFile(const char *fname)
write Grouped l-Othello to a file.
Definition: muloth.h:178
virtual void splitgrp(const keyType &key, uint32_t &grp, keyType &keyInGroup)=0
split a keyTypeype value into two parts: groupID/keyInGroup by the highest splitbit bits...
Describes the data structure Grouped l-Othello. It classifies keys of keyType into 2^L classes...
Definition: muloth.h:24
void loadDataFromBinaryFile(FILE *pF)
load the array from file.
Definition: othello.h:364
IOvalueType query(const keyType &k)
returns a L-bit integer query value for a key.
Definition: muloth.h:149
std::vector< std::string > split(const char *str, char deli)
split a c-style string with delimineter chara.
Definition: io_helper.h:136
vector< keyType > removedKeys
list of skipped keys for all underlying Othello.
Definition: muloth.h:48
Definition: io_helper.h:149
Describes the data structure l-Othello. It classifies keys of keyType into 2^L classes.
Definition: othello.h:25
MulOth(const char *fname, IOHelper< keyType, IOvalueType > *_helper)
construct a Grouped l-Othello from a file.
Definition: muloth.h:212
Definition: io_helper.h:43
vector< keyType > removedKeys
The list of removed keys.
Definition: othello.h:45
MulOth(uint32_t _L, unsigned char _split, class FileReader< keyType, IOvalueType > *_reader)
Construct a Grouped l-Othello from a file.
Definition: muloth.h:73
MulOth(uint32_t _L, uint32_t NN)
This generates toy data for test purpose.
Definition: muloth.h:52
bool build
true if Othello is successfully built.
Definition: othello.h:38