Newer
Older
/*
* Reader for John Shalf's FlexIO data for partiview.
*
* Stuart Levy, slevy@ncsa.uiuc.edu
* National Center for Supercomputing Applications,
* University of Illinois 2001.
* This file is part of partiview, released under the
* Illinois Open Source License; see the file LICENSE.partiview for details.
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
#include <stdio.h>
#include <stdlib.h>
#include <IEEEIO.hh>
#include "shmem.h"
#include <GL/glu.h>
#include "specks.h"
#include <unistd.h>
#include <getopt.h>
#include <math.h>
#include "partiviewc.h"
#include "findfile.h"
static int elements(int rank, int dims[]) {
int i, n;
for(i=0, n=1; i < rank; i++)
n *= dims[i];
return n;
}
extern "C" {
extern void strncpyt( char *dst, char *src, int dstsize );
int parti_ieee_read( struct stuff **stp, int argc, char *argv[], char *fname, void * ) {
static char Usage[] = "ieee file.amr";
if(argc < 1 || strcmp(argv[0], "ieee"))
return 0;
if(argc <= 1) {
msg(Usage);
return 1;
}
struct stuff *st = *stp;
int timestepno = st->datatime;
char *realfile;
int i = 1;
if(argc>2 && !strcmp(argv[1], "-t")) {
if((timestepno = (int) getfloat(argv[2], st->datatime)) < 0) {
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
msg("ieee -t: expected timestepnumber(0-based), not %s", argv[2]);
return 1;
}
i = 3;
}
realfile = findfile( fname, argv[i] );
if(realfile == NULL) {
msg("%s: ieee: can't find file %s", fname, argv[i]);
} else {
/* Load IEEE dataset into time slot */
specks_ieee_open( st, realfile, st->curdata, timestepno );
}
return 1;
}
void parti_ieee_init(void) {
parti_add_reader( parti_ieee_read, "ieee", NULL );
}
void specks_ieee_open( struct stuff *st, char *fname, int dataset, int starttime )
{
if(st->ndata >= MAXFILES) {
fprintf(stderr, "Oops, already read limit of %d data files, ignoring %s\n",
MAXFILES, fname);
return;
}
IObase *infile = new IEEEIO( fname, IObase::Read );
if(!infile->isValid()) {
msg("Couldn't open IEEEIO file %s", fname);
return;
}
if(starttime < 0) starttime = 0;
int ntimes = infile->nDatasets(); /* time steps */
int nd = (dataset < 0) ? st->curdata : dataset;
char *dupfname = NewN( char, strlen(fname)+1 );
strcpy(dupfname, fname);
/* Preallocate room for timesteps */
(void) specks_timespecksptr( st, nd, starttime + ntimes - 1 );
for(int i = 0; i < ntimes; i++) {
specks_discard( st, &st->anima[nd][i+starttime] ); /* free any scrap */
/* st->anima[nd][i+starttime] = NULL; */
st->datafile[nd][i+starttime] = (void *)infile;
st->fname[nd][i+starttime] = dupfname;
}
if(getenv("DBG")) fprintf(stderr, "<p%d>d%d t%d..%d <= %lx %s\n", getpid(), nd, starttime, starttime+ntimes-1, infile, fname );
}
struct specklist *
specks_ieee_read_timestep( struct stuff *st, int subsample, int dataset, int timestep )
{
int curdata = dataset;
int curtime = timestep;
int timebase;
float spacescale = st->spacescale;
int skips[64], skinx, skip;
int outnel;
if(st->ndata == 0)
return NULL;
if(curdata >= st->ndata) curdata = st->ndata-1;
if(curdata < 0) curdata = 0;
if(curtime >= st->ntimes) curtime = st->ntimes-1;
if(curtime < 0) curtime = 0;
IObase *infile = (IObase *)st->datafile[curdata][curtime];
if(infile == NULL)
return NULL;
if(!infile->isValid()) {
fprintf(stderr, "<p%d>d%d t%d infile %x invalid!\n", getpid(), dataset, timestep, infile);
fprintf(stderr, "sleeping 10 secs -- to look around, dbx -p %d\n", getpid());
sleep(10);
return NULL;
}
for(timebase = timestep;
timebase > 0 && (IObase *)st->datafile[curdata][timebase-1] == infile;
timebase--)
;
IObase::DataType dt;
int rank, dims[3];
infile->seek( curtime - timebase );
dims[1]=dims[2]=1;
infile->readInfo(dt, rank, dims);
int i, k, anel;
int nel = dims[rank-1];
if(dims[0] != 3 || rank != 2) {
fprintf(stderr, "%s<%lx> dataset %d (timestep %d): expected 3xN data organization, not %dx%d!\n",
st->fname[curdata][curtime], infile, curtime - timebase, curtime, dims[0],dims[1]);
fprintf(stderr, "sleeping 10 secs -- to look around, dbx -p %d\n", getpid());
sleep(10);
return NULL;
}
if(subsample < 1)
subsample = 1;
int sktotal = 0;
for(skinx = 0; skinx < 64; skinx++) {
skips[skinx] = subsample + (int) ((drand48() - .5)*sqrt(subsample-1));
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
if(skips[skinx] <= 0) skips[skinx] = 1;
sktotal += skips[skinx];
}
float *xyz = new float[ elements(rank,dims) ];
infile->read(xyz);
int nattr = infile->nAttributes();
if(nattr > MAXVAL) nattr = MAXVAL;
outnel = (nel / sktotal) * 64;
for(i = 0, skinx = 0; i < nel % sktotal; ) {
skip = skips[skinx++ & (64-1)];
i += skip;
outnel++;
}
struct specklist *sl = NewN( struct specklist, 1 );
memset( sl, 0, sizeof(*sl) );
sl->bytesperspeck = SMALLSPECKSIZE( nattr );
struct speck *sp = (struct speck *)NewN( char, outnel*sl->bytesperspeck );
sl->sel = NewN( SelMask, outnel );
memset( sl->sel, 0, outnel*sizeof(SelMask) );
sl->specks = sp;
sl->nspecks = outnel;
sl->scaledby = st->spacescale;
sl->coloredby = -1;
sl->sizedby = -1;
sl->subsampled = subsample;
register struct speck *tsp = sp;
float *ap = xyz;
float x0=1e8,x1=-1e8,y0=1e8,y1=-1e8,z0=1e8,z1=-1e8;
for(skinx = 0, i = 0; i < nel; ) {
tsp->p.x[0] = ap[0] * spacescale;
tsp->p.x[1] = ap[1] * spacescale;
tsp->p.x[2] = ap[2] * spacescale;
if(x0>tsp->p.x[0]) x0 = tsp->p.x[0]; if(x1<tsp->p.x[0]) x1=tsp->p.x[0];
if(y0>tsp->p.x[1]) y0 = tsp->p.x[1]; if(y1<tsp->p.x[1]) y1=tsp->p.x[1];
if(z0>tsp->p.x[2]) z0 = tsp->p.x[2]; if(z1<tsp->p.x[2]) z1=tsp->p.x[2];
tsp->size = 1;
tsp->rgba = 0xFFFFFF00;
skip = skips[skinx++ & (64-1)];
tsp = NextSpeck(tsp, sl, 1);
i += skip;
ap += 3*skip;
}
if(getenv("RANGE"))
printf("range (scaled by %g): %g..%g %g..%g %g..%g\n",
spacescale, x0,x1, y0,y1, z0,z1);
delete xyz;
float *attr = new float[nel];
for(i = 0; i < nattr && i < MAXVAL; i++) {
char aname[128];
char *myname;
float min = 1e20, max = -1e20, mean = 0;
struct valdesc *vd = &st->vdesc[curdata][i];
if(vd->nsamples > 0)
min = vd->min, max = vd->max;
infile->readAttributeInfo(i, aname, dt, anel);
myname = aname;
if(!strncmp(aname, "particle_", 9)) myname += 9;
strncpyt(st->vdesc[curdata][i].name, myname, sizeof(st->vdesc[0][0].name));
if(anel != nel) {
fprintf(stderr, "Hey, attribute %d (%s) of file %s has %d elements, not %d!\n",
i, aname, st->fname[curdata][curtime], anel, nel);
memset(attr, 0, nel*sizeof(*attr));
} else {
infile->readAttribute(i, attr);
}
struct speck *peak = NULL;
for(k = 0, skinx = 0, tsp = sp, ap = attr; k < nel; ) {
register float a = *ap;
tsp->val[i] = a;
if(min > a) min = a;
if(max < a) {
max = a;
peak = tsp;
}
mean += a;
skip = skips[skinx++ & (64-1)];
tsp = NextSpeck( tsp, sl, 1 );
ap += skip;
k += skip;
}
if((char *)tsp - (char *)sp != outnel * sl->bytesperspeck) {
fprintf(stderr, "panic: copying attr %d: subsampled into %ld, expected %d of %d\n",
i, ((char *)tsp - (char *)sp) / sl->bytesperspeck, outnel, anel);
}
if(nel > 0) {
vd->min = min;
vd->max = max;
vd->mean = (mean + vd->mean*vd->nsamples) / (outnel + vd->nsamples);
vd->nsamples += outnel;
vd->sum = vd->mean * vd->nsamples;
if(aname[0] == 'd' && peak != NULL) {
sl->interest = peak->p;
}
}
}
delete attr;
/* If we had any data in this slot before, scrap it now. */
specks_discard( st, &st->anima[curdata][curtime] );
/* Is this safe? Do we need an interlock? */
st->anima[curdata][curtime] = sl;
return sl;
}
} // end extern "C"
#endif /*USE_IEEEIO*/