File: attributes_read.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 (108 lines) | stat: -rw-r--r-- 3,290 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
/* 
 * ADIOS is freely available under the terms of the BSD license described
 * in the COPYING file in the top level directory of this source distribution.
 *
 * Copyright (c) 2008 - 2009.  UT-BATTELLE, LLC. All rights reserved.
 */

/* ADIOS C Example: read attributes from a BP file
 *
 * This is possible only with the generic read API.
 * so the GREAD stuff and the xml file is not used here.
 *
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mpi.h"
#include "adios_read.h"

void print_attr (int rank, ADIOS_FILE *f, int attrid, enum ADIOS_DATATYPES attr_type, int attr_size, void * data)
{
    printf ("rank %d: attr: %s %s = ", rank, adios_type_to_string(attr_type), f->attr_namelist [attrid]);
    int type_size = adios_type_size (attr_type, data);
    int nelems = attr_size / type_size;
    int k;
    char *p = (char*)data;
    for (k=0; k<nelems; k++) 
    {
        if (k>0) printf(", ");
        switch (attr_type)  
        {
            case adios_integer:
                printf ("%d", *(int *)p);
                break;
            case adios_double:
                printf ("%e", *(double *)p);
                break;
            case adios_string:
                printf ("\"%s\"", (char *)p);
                break;
            case adios_string_array:
                printf ("\"%s\"", *(char **)p);
                break;
            default:
                printf ("??????\n");
        }
        p=p+type_size;
    }
    printf("\n");
}

int main (int argc, char ** argv) 
{
    int         rank, size, i, j;
    MPI_Comm    comm = MPI_COMM_WORLD;
    enum ADIOS_DATATYPES attr_type;
    enum ADIOS_READ_METHOD method = ADIOS_READ_METHOD_BP;
    int attr_size;
    void * data = NULL;

    MPI_Init (&argc, &argv);
    MPI_Comm_rank (comm, &rank);
    MPI_Comm_size (comm, &size);

    adios_read_init_method (method, comm, "verbose=3");
    ADIOS_FILE * f = adios_read_open ("attributes.bp", method, comm, ADIOS_LOCKMODE_NONE, 0.0);
    if (f == NULL)
    {
        printf ("%s\n", adios_errmsg());
        return -1;
    }

    for (i = 0; i < f->nattrs; i++)
    {

        adios_get_attr (f, f->attr_namelist[i], &attr_type, &attr_size, &data);
        print_attr (rank, f, i, attr_type, attr_size, data);
        free (data);
        data = 0;
    }

    MPI_Barrier (comm);

    if (!rank) {
        printf ("========================================================\n");
        printf ("List of variables with related attributes:\n");
        for (i = 0; i < f->nvars; i++)
        {
            ADIOS_VARINFO * v = adios_inq_var_byid (f, i);
            printf("rank %d:    %-9s  %s    has %d attributes\n", rank, adios_type_to_string(v->type), f->var_namelist[i], v->nattrs);
            for (j=0; j < v->nattrs; j++)
            {
                adios_get_attr_byid (f, v->attr_ids[j], &attr_type, &attr_size, &data);
                print_attr (rank, f, v->attr_ids[j], attr_type, attr_size, data);
                //printf("       %s\n", f->attr_namelist[ v->attr_ids[j] ]);
                free (data);
                data = 0;
            }
        }
    }
    MPI_Barrier (comm);

    adios_read_close (f);
    MPI_Barrier (comm);
    adios_read_finalize_method (ADIOS_READ_METHOD_BP);
    MPI_Finalize ();
    return 0;
}