File: H5Vol_attr.c

package info (click to toggle)
adios2 2.10.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 33,764 kB
  • sloc: cpp: 175,964; ansic: 160,510; f90: 14,630; yacc: 12,668; python: 7,275; perl: 7,126; sh: 2,825; lisp: 1,106; xml: 1,049; makefile: 579; lex: 557
file content (273 lines) | stat: -rw-r--r-- 7,724 bytes parent folder | download | duplicates (2)
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
#ifndef __H5VOL_ATTR_FUNC
#define __H5VOL_ATTR_FUNC

#include "H5Vol_def.h"

///
// attribute handlers
//
void *H5VL_adios2_attr_create(void *obj, const H5VL_loc_params_t *loc_params, const char *name,
                              hid_t type_id, hid_t space_id, hid_t acpl_id, hid_t aapl_id,
                              hid_t dxpl_id, void **req)
{
    REQUIRE_NOT_NULL_ERR(loc_params, NULL);
    REQUIRE_NOT_NULL_ERR(obj, NULL);

    H5VL_ObjDef_t *vol = (H5VL_ObjDef_t *)obj;

    H5VL_AttrDef_t *attrDef = gCreateAttrDef(name, type_id, space_id);
    return gAttrToVolObj(attrDef, vol);
}

void *H5VL_adios2_attr_open(void *obj, const H5VL_loc_params_t *loc_params, const char *name,
                            hid_t aapl_id, hid_t dxpl_id, void **req)
{
    REQUIRE_NOT_NULL_ERR(loc_params, NULL);
    REQUIRE_NOT_NULL_ERR(obj, NULL);

    H5VL_ObjDef_t *vol = (H5VL_ObjDef_t *)obj;
    adios2_attribute *attr = gLocateAttrFrom(vol, name);

    H5VL_AttrDef_t *attrDef = NULL;
    if (NULL == attr)
    {
        if ('/' == name[0])
        {
            SHOW_ERROR_MSG("H5VL_ADIOS2: Error: No such ATTRIBUTE: [%s] in file\n ", name);
            return NULL;
        }
        else
        {
            char withSlash[strlen(name) + 2];
            snprintf(withSlash, sizeof(withSlash), "/%s", name);
            withSlash[strlen(name) + 1] = '\0';
            attr = gLocateAttrFrom(vol, withSlash);
            if (NULL == attr)
            {
                SHOW_ERROR_MSG("H5VL_ADIOS2: Error: No such ATTRIBUTE: [%s] "
                               "found in file\n ",
                               withSlash);
                return NULL;
            }
            attrDef = gCreateAttrDef(withSlash, -1, -1);
        }
    }
    else
        attrDef = gCreateAttrDef(name, -1, -1);

    // H5VL_AttrDef_t *attrDef = gCreateAttrDef(name, -1, -1);
    attrDef->m_Attribute = attr;

    gLoadAttrDef(attrDef);

    return gAttrToVolObj(attrDef, vol);
}

herr_t H5VL_adios2_attr_read(void *attrObj, hid_t mem_type_id, void *buf, hid_t dxpl_id, void **req)
{
    REQUIRE_NOT_NULL_ERR(attrObj, -1);

    H5VL_ObjDef_t *vol = (H5VL_ObjDef_t *)attrObj;

    H5VL_AttrDef_t *attrDef = (H5VL_AttrDef_t *)(vol->m_ObjPtr);
    adios2_attribute *attr = attrDef->m_Attribute;

    if (NULL == attr)
        return -1;

    if ((!attrDef->m_IsScalar) && (H5Tget_class(mem_type_id) == H5T_STRING))
    {
        htri_t isVariableSize = H5Tis_variable_str(mem_type_id);
        if (!isVariableSize)
        {
            size_t strSize = H5Tget_size(mem_type_id);
            // buf is char* instead of char**, adios2 c api expects char**

            char **result = (char **)(malloc(sizeof(char *) * (int)(attrDef->m_Size)));
            size_t k = 0;
            for (k = 0; k < attrDef->m_Size; k++)
                result[k] = (char *)(malloc(strSize));

            adios2_attribute_data(result, &(attrDef->m_Size), attr);

            char *out = (char *)buf;

            if (strlen(out) > 0)
            {
                buf = result;
                return 0;
            }
            // from openPMD api.. using char*[n*max_length] to hold result
            for (k = 0; k < attrDef->m_Size; k++)
            {
                strncpy(out + k * strSize, result[k], strSize);
                out[k * strSize + strlen(result[k])] = '\0';
                free(result[k]);
            }

            free(result);
            return 0;
        }
    }

    adios2_attribute_data(buf, &(attrDef->m_Size), attr);

    return 0;
}

herr_t H5VL_adios2_attr_write(void *attr, hid_t mem_type_id, const void *buf, hid_t dxpl_id,
                              void **req)
{
    REQUIRE_NOT_NULL_ERR(attr, -1);
    H5VL_ObjDef_t *vol = (H5VL_ObjDef_t *)attr;

    H5VL_AttrDef_t *attrDef = (H5VL_AttrDef_t *)(vol->m_ObjPtr);
    attrDef->m_Data = (void *)buf;
    gADIOS2CreateAttr(vol->m_FileIO, attrDef, vol->m_Path);

    return 0;
}

void GetFromAttribute(void *attrObj, hid_t *ret_id, H5VL_attr_get_t get_type)
{
    H5VL_AttrDef_t *attrDef = (H5VL_AttrDef_t *)attrObj;

    switch (get_type)
    {
    case H5VL_ATTR_GET_SPACE: {
        *ret_id = H5Scopy(attrDef->m_SpaceID);
        break;
    }
    case H5VL_ATTR_GET_TYPE: {
        *ret_id = H5Tcopy(attrDef->m_TypeID);
        break;
    }
    default:
        break;
    }
}

herr_t H5VL_adios2_attr_get(void *obj, H5VL_attr_get_args_t *args, hid_t dxpl_id, void **req)
{
    REQUIRE_NOT_NULL_ERR(obj, -1);
    H5VL_ObjDef_t *vol = (H5VL_ObjDef_t *)obj;

    switch (args->op_type)
    {
    case H5VL_ATTR_GET_SPACE: {
        hid_t *ret_id = (hid_t *)args->args.get_space.space_id;
        GetFromAttribute((vol->m_ObjPtr), ret_id, args->op_type);
        return 0;
    }
    case H5VL_ATTR_GET_TYPE: {
        hid_t *ret_id = (hid_t *)args->args.get_type.type_id;
        GetFromAttribute((vol->m_ObjPtr), ret_id, args->op_type);
        return 0;
    }
    default:
        break;
    }

    switch (args->op_type)
    {
    case H5VL_ATTR_GET_NAME: {
        char *buf = args->args.get_name.buf;
        size_t *ret_val = (size_t *)args->args.get_name.attr_name_len;

        const H5VL_loc_params_t *loc_params = &args->args.get_name.loc_params;
        REQUIRE_NOT_NULL_ERR(loc_params, -1);

        if (H5VL_OBJECT_BY_SELF == loc_params->type)
        {
            H5VL_AttrDef_t *attrDef = (H5VL_AttrDef_t *)(vol->m_ObjPtr);
            *ret_val = strlen(attrDef->m_Name);
            if (buf)
            {
                memcpy(buf, attrDef->m_Name, *ret_val);
            }
        }
        else if (H5VL_OBJECT_BY_IDX == loc_params->type)
        {
            // The  number of attrs is from H5Oget_info(), then iterate each by
            // calling H5Aget_name_by_idx, to reach here
            *ret_val = gGetNameOfNthAttr(vol, loc_params->loc_data.loc_by_idx.n, buf);
        }
        return 0;
    }
    default:
        break;
    }

    ADIOS_VOL_NOT_SUPPORTED_ERR("UNABLE TO SUPPORT feature at attr_get\n");
    return -1;
}

herr_t H5VL_adios2_attr_close(void *attr, hid_t dxpl_id, void **req)
{
    REQUIRE_NOT_NULL_ERR(attr, -1);
    H5VL_ObjDef_t *vol = (H5VL_ObjDef_t *)attr;

    H5VL_AttrDef_t *attrDef = (H5VL_AttrDef_t *)(vol->m_ObjPtr);
    free(attrDef->m_Name);
    if (-1 != attrDef->m_SpaceID)
        H5Sclose(attrDef->m_SpaceID);
    free(attrDef);
    attrDef = NULL;

    gFreeVol(vol);
    vol = NULL;
    return 0;
}

herr_t H5VL_adios2_attr_specific(void *obj, const H5VL_loc_params_t *loc_params,
                                 H5VL_attr_specific_args_t *args, hid_t dxpl_id, void **req)
{
    REQUIRE_NOT_NULL_ERR(obj, -1);
    H5VL_ObjDef_t *vol = (H5VL_ObjDef_t *)obj;

    const char *attr_name;
    adios2_attribute *attr;

    switch (args->op_type)
    {
    case H5VL_ATTR_DELETE: {
        attr_name = (const char *)args->args.del.name;
        attr = gLocateAttrFrom(vol, attr_name);

        if (NULL != attr)
        {
            if (NULL == vol->m_Path)
                gADIOS2RemoveAttr(vol->m_FileIO, attr_name);
            else
            {
                char fullPath[strlen(vol->m_Path) + 4 + strlen(attr_name)];
                gGenerateFullPath(fullPath, vol->m_Path, attr_name);
                gADIOS2RemoveAttr(vol->m_FileIO, fullPath);
            }
            return 0;
        }
    }
    case H5VL_ATTR_EXISTS: {
        hbool_t *ret = args->args.exists.exists;

        attr_name = (const char *)args->args.exists.name;
        attr = gLocateAttrFrom(vol, attr_name);

        if (NULL == attr)
        {
            *ret = 0;
        }
        else
        {
            *ret = 1;
        }

        return 0;
    }
    default:
        break;
    }
    return 0;
}

#endif