COMBINATORIAL_BLAS  1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FileHeader.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* Parallel Combinatorial BLAS Library (for Graph Computations) */
3 /* version 1.2 -------------------------------------------------*/
4 /* date: 10/06/2011 --------------------------------------------*/
5 /* authors: Aydin Buluc (abuluc@lbl.gov), Adam Lugowski --------*/
6 /****************************************************************/
7 /*
8  Copyright (c) 2011, Aydin Buluc
9 
10  Permission is hereby granted, free of charge, to any person obtaining a copy
11  of this software and associated documentation files (the "Software"), to deal
12  in the Software without restriction, including without limitation the rights
13  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the Software is
15  furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included in
18  all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  THE SOFTWARE.
27  */
28 
29 #ifndef _COMBBLAS_FILE_HEADER_
30 #define _COMBBLAS_FILE_HEADER_
31 
32 #include "CombBLAS.h"
33 
34 struct HeaderInfo
35 {
36  HeaderInfo():fileexists(false), headerexists(false) {};
37  bool fileexists;
39  uint64_t version;
40  uint64_t objsize;
41  uint64_t format; // 0: binary, 1: ascii
42 
43  uint64_t m;
44  uint64_t n;
45  uint64_t nnz;
46 };
47 
48 // cout's are OK because ParseHeader is run by a single processor only
49 inline HeaderInfo ParseHeader(const string & inputname, FILE * & f, int & seeklength)
50 {
51  f = fopen(inputname.c_str(), "rb");
52  HeaderInfo hinfo;
53  memset(&hinfo, 0, sizeof(hinfo));
54  if(!f)
55  {
56  cerr << "Problem reading binary input file\n";
57  f = NULL;
58  return hinfo;
59  }
60  char fourletters[5];
61  size_t result = fread(fourletters, sizeof(char), 4, f);
62  fourletters[4] = '\0';
63  if (result != 4) { cout << "Error in fread of header, only " << result << " entries read" << endl; return hinfo;}
64 
65  if(strcmp(fourletters,"HKDT") != 0)
66  {
67  rewind(f);
68  fclose(f);
69  hinfo.fileexists = true;
70  return hinfo;
71  }
72  else
73  {
74  hinfo.fileexists = true;
75  hinfo.headerexists = true;
76  }
77 
78  size_t results[6];
79  results[0] = fread(&(hinfo.version), sizeof(hinfo.version), 1, f);
80  results[1] = fread(&(hinfo.objsize), sizeof(hinfo.objsize), 1, f);
81  results[2] = fread(&(hinfo.format), sizeof(hinfo.format), 1, f);
82 
83  results[3] = fread(&(hinfo.m), sizeof(hinfo.m), 1, f);
84  results[4] = fread(&(hinfo.n), sizeof(hinfo.n), 1, f);
85  results[5] = fread(&(hinfo.nnz), sizeof(hinfo.nnz), 1, f);
86  if(accumulate(results,results+6,0) != 6)
87  {
88  cout << "The required 6 fields (version, objsize, format, m,n,nnz) are not read" << endl;
89  cout << "Only " << accumulate(results,results+6,0) << " fields are read" << endl;
90  }
91  else
92  {
93  #ifdef DEBUG
94  cout << "Version " << hinfo.version << ", object size " << hinfo.objsize << endl;
95  cout << "Rows " << hinfo.m << ", columns " << hinfo.m << ", nonzeros " << hinfo.nnz << endl;
96  #endif
97  }
98 
99  seeklength = 4 + 6 * sizeof(uint64_t);
100  return hinfo;
101 }
102 
103 #endif
104