File: adios_query_xml_parse.c

package info (click to toggle)
adios 1.13.1-31
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 23,692 kB
  • sloc: ansic: 133,236; f90: 8,791; sh: 7,779; python: 7,648; xml: 3,793; makefile: 2,996; cpp: 2,340; java: 626; sed: 16; perl: 8
file content (429 lines) | stat: -rw-r--r-- 14,908 bytes parent folder | download | duplicates (4)
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
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
174
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * adios_query_xml_parse.c
 *
 *  Created on: Sep 30, 2014
 *      Author: Houjun Tang
 */

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <assert.h>
#include "adios_selection.h"
#include "adios_query.h"
#include "core/strutil.h"
#include <mxml.h>
#include <sys/stat.h>

#include "adios_query_xml_parse.h"

#define MAXDIM    10
#define MAXQUERY  1000

#define GET_ATTR2(n,attr,var,en)                                 \
    if (!strcasecmp (n, attr->name)) {                           \
        if (!var)                                                \
        {                                                        \
            var = attr->value;                                   \
            continue;                                            \
        }                                                        \
        else                                                     \
        {                                                        \
            printf ("xml: duplicate attribute %s on %s (ignored)",n,en); \
            continue;                                            \
        }                                                        \
    }

// Stack for storing queries
typedef struct {
    int size;
    ADIOS_QUERY *stack[MAXQUERY];
} QueryStack;

// init query stack
static void queryStackInit(QueryStack* queryStack)
{
    queryStack->size=0;
}

static void queryPush(QueryStack* queryStack, ADIOS_QUERY *q)
{
    if (queryStack->size>=MAXQUERY) {
        fprintf(stderr, "Query number exceeds MAXQUERY, exiting\n");
        abort();
    }
    queryStack->stack[queryStack->size++] = q;

}

static int queryStackSize(QueryStack* queryStack)
{
    return queryStack->size;
}

static ADIOS_QUERY * queryPop(QueryStack* queryStack)
{
    if (queryStackSize(queryStack)==0) {
        fprintf(stderr, "Error: popping empty query stack, exiting...\n");
        abort();
    }
    return queryStack->stack[--queryStack->size];
}

#define CHECK_ERROR_DATA(data, num, check) {                     \
		 uint64_t di = 0;                                        \
		 for(di = 0; di < (num); di++){                          \
				if (check)                                       \
					fprintf(stderr, "error data: %f, ", (data)[di]);      \
		 }                                                       \
}

ADIOS_QUERY_TEST_INFO * parseXml(const char *inputxml, ADIOS_FILE* f) {
	int i, j;
	FILE * fp = fopen (inputxml,"r");
	if (!fp){
		fprintf(stderr, "missing xml input file %s \n", inputxml);
		return NULL;
	}
	struct stat s;
	char * buffer = NULL;
	if (stat (inputxml, &s) == 0) {
		buffer = malloc (s.st_size + 1);
		buffer [s.st_size] = 0;
	}

	if (buffer)     {
		size_t bytes_read = fread (buffer, 1, s.st_size, fp);

		if (bytes_read != s.st_size) {
			fprintf(stderr, "error reading input xml file: %s. Expected %lld Got %lld\n"
					,inputxml, (long long int) s.st_size, (long long int)bytes_read );
			fclose(fp);
			return NULL;
		}
	}
	fclose (fp);
	mxml_node_t * root = NULL;
	root = mxmlLoadString (NULL, buffer, MXML_TEXT_CALLBACK);
	free (buffer);
	buffer = NULL;

	if (!root) {
		fprintf(stderr,  "unknown error parsing XML (probably structural)\n"
				"Did you remember to start the file with\n"
				"<?xml version=\"1.0\"?>\n");
		return NULL;
	}
	if (strcasecmp(root->value.element.name, "query") != 0) {
		root = mxmlFindElement(root, root, "query", NULL, NULL, MXML_DESCEND_FIRST);
	}

	const char *numVarS=NULL;
	const char *fromTimestepS=NULL;
	const char *numTimestepsS=NULL;
	const char *batchsizeS=NULL;

	int numQuery = 0;
	int fromTimestep = 1;
	int numTimesteps = 1;
	uint64_t batchsize= 1;
	for (i = 0; i < root->value.element.num_attrs; i++) {
		mxml_attr_t * attr = &root->value.element.attrs [i];
		GET_ATTR2("num",attr,numVarS,"query");
		GET_ATTR2("from-timestep",attr,fromTimestepS,"query");
		GET_ATTR2("num-timesteps",attr,numTimestepsS,"query");
		GET_ATTR2("batchsize",attr,batchsizeS,"query");
	}
	if ( !numVarS || !strcmp ( numVarS, "")) {
		fprintf(stderr, "missing values for num attribute \n");
		mxmlRelease(root);
		return NULL;
	}
	else {
		numQuery  = atoi(numVarS);
		fromTimestep  = atoi(fromTimestepS);
		numTimesteps = atoi(numTimestepsS);
		batchsize = strtoull(batchsizeS, NULL, 10);
	}

	mxml_node_t *outputNode     = NULL;
	const char *outputTypeS=NULL, *outputDimS=NULL, *outputStartS=NULL, *outputCountS=NULL, *outputWbIndexS=NULL;
	int outputDim;
	int outputWbIndex;
	int selType;
	char** outputCountTokens=NULL;
	char** outputStartTokens=NULL;
	ADIOS_SELECTION *outputBox;

	// Parse output selection info

	outputNode = mxmlFindElement(root, root, "output", NULL, NULL, MXML_DESCEND_FIRST);
	for (i = 0; i < outputNode->value.element.num_attrs; i++) {
		mxml_attr_t * attr = &outputNode->value.element.attrs [i];
		GET_ATTR2("type",attr,outputTypeS,"output");
		if ( strcmp(outputTypeS, "ADIOS_SELECTION_BOUNDINGBOX") == 0) {
			selType = ADIOS_SELECTION_BOUNDINGBOX;
			GET_ATTR2("dim",attr,outputDimS,"output");
			GET_ATTR2("start",attr,outputStartS,"output");
			GET_ATTR2("count",attr,outputCountS,"output");
		}
		else if ( strcmp(outputTypeS, "ADIOS_SELECTION_WRITEBLOCK") == 0) {
			selType = ADIOS_SELECTION_WRITEBLOCK;
			GET_ATTR2("index",attr,outputWbIndexS,"selection");
		}
	}
	if ( selType == ADIOS_SELECTION_BOUNDINGBOX ) {
		if ( !outputTypeS || !outputDimS || !outputStartS || !outputCountS || !strcmp (outputTypeS, "")|| !strcmp (outputDimS, "") || !strcmp (outputStartS, "") || !strcmp (outputCountS, "") ) {
			fprintf(stderr, "missing values for output attribute \n");
			mxmlRelease(root);
			return NULL;
		}
		else {
			int specifiedDim = atoi(outputDimS);
			if (specifiedDim > MAXDIM) {
				fprintf(stderr, "QueryDim exceeds 10, readjust MAXDIM to larger value, exiting...\n");
				abort();
			}

			a2s_tokenize_dimensions(outputStartS, &outputStartTokens, &outputDim);
			a2s_tokenize_dimensions(outputCountS, &outputCountTokens, &outputDim);

            if (specifiedDim != outputDim) {
                fprintf(stderr, "Specified # of dimensions (%d)  "
                        "!= number of dimensions (%d) in start/count, exiting...\n",
                        specifiedDim, outputDim);
                abort();
            }

			// Allocate arrays to give to the bounding box constructor
			uint64_t *outputStart = malloc(outputDim * sizeof(uint64_t));
			uint64_t *outputCount = malloc(outputDim * sizeof(uint64_t));

			for (j = 0; j < outputDim; j ++){
				outputStart[j] = atoi(outputStartTokens[j]);
				outputCount[j] = atoi(outputCountTokens[j]);
			}
			/* a2s_cleanup_dimensions(outputStartTokens, outputDim); */
            /* a2s_cleanup_dimensions(outputCountTokens, outputDim); */

			outputBox = adios_selection_boundingbox(outputDim, outputStart, outputCount);

			/* fprintf(stderr, "Selected output boundingbox: dim:%d start:", outputDim); */
			/* for (j = 0; j < outputDim; j ++){ */
			/* 	fprintf(stderr, " %d", outputStart[j]); */
			/* } */
			/* fprintf(stderr, "\t count:"); */
			/* for (j = 0; j < outputDim; j ++){ */
			/* 	fprintf(stderr, " %d", outputCount[j]); */
			/* } */
			/* fprintf(stderr, "\n"); */

		}

	}
	else if( selType == ADIOS_SELECTION_WRITEBLOCK ) {

		if ( !outputWbIndexS || !strcmp (outputWbIndexS, "") ) {
			fprintf(stderr, "missing values for selection attribute \n");
			mxmlRelease(root);
			return NULL;
		}
		else {
			outputWbIndex = atoi(outputWbIndexS);
			outputBox = adios_selection_writeblock(outputWbIndex);

			/* fprintf(stderr, "Selected output writeblock: %d\n", outputWbIndex); */
		}
	}


	// Iterate all combine/entry nodes in <query>
	mxml_node_t *entryNode      = NULL;
	mxml_node_t *selectionNode  = NULL;
	const char *varNameS=NULL, *opS=NULL, *constraintS=NULL;
	const char *typeS=NULL, *dimS=NULL, *startS=NULL, *countS=NULL, *wbIndexS=NULL;
	int entryIter;
	int queryDim;
	int wbIndex;
	ADIOS_SELECTION *inputSelection;
	ADIOS_QUERY *q, *q1, *q2, *qc;
	char** queryCountTokens=NULL;
	char** queryStartTokens=NULL;
	char *queryCombineOp=NULL;

	// init query stack
	QueryStack queryStack;
	queryStackInit(&queryStack);

	entryNode = mxmlFindElement(root, root, "entry", NULL, NULL, MXML_DESCEND_FIRST);
	entryIter = 0;
	for ( ; entryNode; entryNode = mxmlWalkNext (entryNode, root, MXML_NO_DESCEND))
    {
        if (entryNode->type != MXML_ELEMENT)
        {
            continue;
        }

		// check if current node is <combine>
		if ( strcmp(entryNode->value.element.name, "combine") == 0 ) {
			queryCombineOp = (&(entryNode->value.element.attrs[0]))->value;
			/* fprintf(stderr, "Found combine op %s\n", queryCombineOp); */
			// pop up two query and perform the op
			if (queryStackSize(&queryStack)<2) {
				fprintf(stderr, "Popping with less than 2 queries in query stack, exiting...\n");
				abort();
			}

			q1 = queryPop(&queryStack);
			q2 = queryPop(&queryStack);
			if (strcmp(queryCombineOp, "AND") == 0 || strcmp(queryCombineOp, "and") == 0) {
				qc = adios_query_combine(q1, ADIOS_QUERY_OP_AND, q2);
			}
			else if (strcmp(queryCombineOp, "OR") == 0 || strcmp(queryCombineOp, "or") == 0) {
				qc = adios_query_combine(q1, ADIOS_QUERY_OP_OR, q2);
			}
			queryPush(&queryStack,qc);

			//adios_query_free(q1);
			//adios_query_free(q2);

			continue;
		}

        if (entryIter >= numQuery)
            break;

		// Make sure all *S are NULL for verification
		varNameS=NULL, opS=NULL, constraintS=NULL;
		typeS=NULL, dimS=NULL, startS=NULL, countS=NULL, wbIndexS=NULL;

		for (i = 0; i < entryNode->value.element.num_attrs; i++) {
			mxml_attr_t * attr = &entryNode->value.element.attrs [i];
			GET_ATTR2("var",attr,varNameS,"entry");
			GET_ATTR2("op",attr,opS,"entry");
			GET_ATTR2("constraint",attr,constraintS,"entry");
		}
		if ( !varNameS || !opS || !constraintS || !strcmp (varNameS, "")|| !strcmp (opS, "") || !strcmp (constraintS, "") ) {
			fprintf(stderr, "missing values for entry attribute \n");
			mxmlRelease(root);
			return NULL;
		}

		// Parse selection
		selectionNode = mxmlFindElement(entryNode, entryNode, "selection", NULL, NULL, MXML_DESCEND_FIRST);

                if (selectionNode == NULL) {
                    inputSelection = NULL;
                }
                else {
                    // selection is not NULL

		        for (i = 0; i < selectionNode->value.element.num_attrs; i++) {
		        	mxml_attr_t * attr = &selectionNode->value.element.attrs [i];
		        	GET_ATTR2("type",attr,typeS,"selection");
		        	if ( strcmp(typeS, "ADIOS_SELECTION_BOUNDINGBOX") == 0) {
		        		selType = ADIOS_SELECTION_BOUNDINGBOX;
		        		GET_ATTR2("dim",attr,dimS,"selection");
		        		GET_ATTR2("start",attr,startS,"selection");
		        		GET_ATTR2("count",attr,countS,"selection");
		        	}
		        	else if ( strcmp(typeS, "ADIOS_SELECTION_WRITEBLOCK") == 0) {
		        		selType = ADIOS_SELECTION_WRITEBLOCK;
		        		GET_ATTR2("index",attr,wbIndexS,"selection");
		        	}
		        }
        		if ( selType == ADIOS_SELECTION_BOUNDINGBOX ) {
        
        			if ( !typeS || !dimS || !startS || !countS || !strcmp (typeS, "")|| !strcmp (dimS, "") || !strcmp (startS, "") || !strcmp (countS, "") ) {
        				fprintf(stderr, "missing values for selection attribute \n");
        				mxmlRelease(root);
        				return NULL;
        			}
        			else {
        		        int specifiedDim = atoi(dimS);
        		        if (specifiedDim > MAXDIM) {
        				    fprintf(stderr, "QueryDim exceeds 10, readjust MAXDIM to larger value, exiting...\n");
        					abort();
        				}
        
        				a2s_tokenize_dimensions(startS, &queryStartTokens, &queryDim);
        				a2s_tokenize_dimensions(countS, &queryCountTokens, &queryDim);

        	            if (specifiedDim != outputDim) {
        	                fprintf(stderr, "Specified # of dimensions (%d)  "
        	                        "!= number of dimensions (%d) in start/count, exiting...\n",
        	                        specifiedDim, outputDim);
        	                abort();
        	            }

        				// Allocate arrays to give to the bounding box constructor
        				uint64_t *queryStart = malloc(queryDim * sizeof(uint64_t));
        				uint64_t *queryCount = malloc(queryDim * sizeof(uint64_t));
        
        				for (j = 0; j < queryDim; j ++){
        					queryStart[j] = atoi(queryStartTokens[j]);
        					queryCount[j] = atoi(queryCountTokens[j]);
        				}
        	            /* a2s_cleanup_dimensions(outputStartTokens, outputDim); */
        	            /* a2s_cleanup_dimensions(outputCountTokens, outputDim); */
        
        				inputSelection = adios_selection_boundingbox(queryDim, queryStart, queryCount);
                                }
        
				/* fprintf(stderr, "Selected input bounding box:  dim:%d start:", queryDim); */
				/* for (j = 0; j < queryDim; j ++){ */
				/* 	fprintf(stderr, " %d", queryStart[j]); */
				/* } */
				/* fprintf(stderr, "\t count:"); */
				/* for (j = 0; j < queryDim; j ++){ */
				/* 	fprintf(stderr, " %d", queryCount[j]); */
				/* } */
				/* fprintf(stderr, "\n"); */

        		} // end of selType == ADIOS_SELECTION_BOUNDINGBOX
		        else {
			        // selType == ADIOS_SELECTION_WRITEBLOCK
                                if ( !wbIndexS || !strcmp (wbIndexS, "") ) {
			        	fprintf(stderr, "missing values for selection attribute \n");
			        	mxmlRelease(root);
			        	return NULL;
			        }
			        else {
			        	wbIndex = atoi(wbIndexS);
                                }
				
                                inputSelection = adios_selection_writeblock(wbIndex);
                        }
                }// end of selection is not NULL
        

		if( strcmp(opS, "<=") == 0 )
	                q = adios_query_create(f, inputSelection, varNameS, ADIOS_LTEQ, constraintS);
		else if( strcmp(opS, "<") == 0 )
		        q = adios_query_create(f, inputSelection, varNameS, ADIOS_LT, constraintS);
		else if( strcmp(opS, ">=") == 0 )
			q = adios_query_create(f, inputSelection, varNameS, ADIOS_GTEQ, constraintS);
		else if( strcmp(opS, ">") == 0 )
			q = adios_query_create(f, inputSelection, varNameS, ADIOS_GT, constraintS);
		else {
			fprintf(stderr, "Unsupported entry op %s\n", opS);
			return NULL;
		}

		queryPush(&queryStack,q);
		entryIter++;
		/* fprintf(stderr, "Parsed entry: var=%s op=%s constraint=%s\n", varNameS, opS, constraintS); */
	}

	ADIOS_QUERY_TEST_INFO *retval = (ADIOS_QUERY_TEST_INFO *)malloc(sizeof(ADIOS_QUERY_TEST_INFO));
	*retval = (ADIOS_QUERY_TEST_INFO){
		.query           = queryPop(&queryStack),
		.outputSelection = outputBox,
		.fromStep        = fromTimestep,
		.numSteps        = numTimesteps,
        .batchSize       = batchsize,
        .varName         = varNameS,
	};
	return retval;
}