aboutsummaryrefslogtreecommitdiff
path: root/src/WildWriter.hh
blob: c2e09afc6250593c5cd3a59fb836cabadd3fcc58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#ifndef __WILDWRITER_HH_
#define __WILDWRITER_HH_

#include "IO.hh"
#include "IEEEIO.hh"
#include "FlexArrayTmpl.H"

#define DISKBLOCKSIZE 8192
#define BLOCKSIZE 2*DISKBLOCKSIZE

// Does not assume contiguous data (writes in chunks)
class WildWriterB {
  IObase &file;
  int nnodes,ncells,ncomp;
  char buffer[BLOCKSIZE]; // could dynamically allocate
  FlexArray<char> compnames;
  int totalcomp;
  IObase::DataType comptype;
  FlexArray<int> compsizes;
  int bufferindex,masterindex,segsize;
public:
  WildWriterB(IObase &descriptor):file(descriptor){
  }
  ~WildWriterB(){}
  void beginDataset(int numnodes,
		    int numcells,
		    IObase::DataType datacomponenttype,
		    int numdatacomponents,
		    int *componentsizes,
		    char *componentnames,
		    char componentnameseparator);

  void beginNodes(int numnodes);
  void beginNodes();
  inline void addNode(float *coords){
    // attrib WildNode[%u] (should buffer internally)
    // but we can only chunk on Datasets :(
    // so collect data in 8k buffers
    //checkMode(WriteNodes);
    float *copyptr=(float *)buffer;
    // copy into master buffer.
    int index=bufferindex*3;
    for(int i=0;i<3;i++) copyptr[index+i]=coords[i];
    bufferindex++;
    masterindex++;
    if(bufferindex>=segsize){ // dump the buffer to disk
      int origin[2]={0,0},dims[2]={3,0};
      dims[1]=segsize;
      origin[1]=masterindex-segsize;
      file.writeChunk(dims,origin,buffer);
      bufferindex=0; // reset the bufferindex for next seg
    }
  }
  void endNodes();
  void beginCells(int numcells);
  void beginCells();
  inline void addCell(int level,int *neighborlist){
    //    checkMode(WriteCells);
    int *copyptr=(int *)buffer;
    int index=bufferindex*9;
    // copy into master buffer.
    copyptr[index+1]=level;
    for(int i=0;i<8;i++) 
      copyptr[index+i+1]=neighborlist[i];
    bufferindex++;
    masterindex++;
    if(bufferindex>=segsize){ // dump the buffer to disk
      int origin[2]={0,0},dims[2]={3,0};
      dims[1]=segsize;
      origin[1]=masterindex-segsize;
      file.writeChunk(dims,origin,buffer);
      bufferindex=0; // reset the bufferindex for next seg
    }
  }
  void endCells();
  void beginData();
  void beginData(IObase::DataType datacomponenttype,
		    int numdatacomponents,
		    int *componentsizes,
		    char *componentnames,
		    char componentnameseparator);
  inline void addData(void *data){
    //    checkMode(WriteCells);
    if(comptype==IObase::Float32){
      float *copyptr=(float *)buffer;
      float *dptr=(float*)data;
      int index = bufferindex*totalcomp*sizeof(double);
      for(int i=0;i<totalcomp;i++) copyptr[index+i]=dptr[i];
    }
    else if(comptype==IObase::Float64){
      double *copyptr=(double *)buffer;
      double *dptr = (double *)data;
      int index = bufferindex*totalcomp*sizeof(double);
      for(int i=0;i<totalcomp;i++) copyptr[index+i]=dptr[i];
    }
    bufferindex++;
    masterindex++;
    if(bufferindex>=segsize){ // dump the buffer to disk
      int origin[2]={0,0},dims[2]={0,0};
      dims[0]=totalcomp;
      dims[1]=segsize;
      origin[1]=masterindex-segsize;
      file.writeChunk(dims,origin,buffer);
      bufferindex=0; // reset the bufferindex for next seg
    }
  }
  void endData();
  void endDataset() ;
};

extern "C" {
#include "WildWriter.h"
}

#endif

/*
// Assumes Contiguous Data
class WildWriterA {
  IO &file;
public:
  WildWriter(IO &descriptor):file(descriptor){
  }
  ~WildWriter(){
  }
  // All nodes for all levels
  void writeNodes(int numnodes,float *node_coords){
    int dims[2]={3,0};
    dims[1]=numnodes;
    file.write(IObase::Float32,2,dims,node_coords);
    file.writeAttribute("WildNodes",IObase::Int,0,dims);
  }
  // Must Be Hexahedral Cells
  void writeCells(int level,int ncells,int *celllist){
    
  }
  // Assumes Node-centered data
  void writeData(char *name,IObase::DataType type,int veclen,void *data){
    
  }
};
*/