forked from ornladios/ADIOS2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adios2_c_variable.cpp
667 lines (588 loc) · 24.6 KB
/
adios2_c_variable.cpp
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* adios2_c_variable.cpp
*
* Created on: Nov 10, 2017
* Author: William F Godoy godoywf@ornl.gov
*/
#include "adios2_c_variable.h"
#include "adios2/core/Variable.h"
#include "adios2/helper/adiosFunctions.h"
#include "adios2_c_internal.h"
namespace
{
adios2_shapeid adios2_ToShapeID(const adios2::ShapeID shapeIDCpp, const std::string &hint)
{
adios2_shapeid shapeID = adios2_shapeid_unknown;
switch (shapeIDCpp)
{
case (adios2::ShapeID::GlobalValue):
shapeID = adios2_shapeid_global_value;
break;
case (adios2::ShapeID::GlobalArray):
shapeID = adios2_shapeid_global_array;
break;
case (adios2::ShapeID::JoinedArray):
shapeID = adios2_shapeid_joined_array;
break;
case (adios2::ShapeID::LocalValue):
shapeID = adios2_shapeid_local_value;
break;
case (adios2::ShapeID::LocalArray):
shapeID = adios2_shapeid_local_array;
break;
default:
throw std::invalid_argument("ERROR: invalid adios2_shapeid, " + hint + "\n");
}
return shapeID;
}
} // end anonymous namespace
#ifdef __cplusplus
extern "C" {
#endif
adios2_error adios2_set_shape(adios2_variable *variable, const size_t ndims, const size_t *shape)
{
try
{
adios2::helper::CheckForNullptr(variable, "for adios2_variable, in call to "
"adios2_set_shape");
adios2::helper::CheckForNullptr(shape, "for start, in call to "
"adios2_set_shape");
adios2::core::VariableBase *variableBase =
reinterpret_cast<adios2::core::VariableBase *>(variable);
const adios2::Dims shapeV(shape, shape + ndims);
variableBase->SetShape(shapeV);
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(adios2::helper::ExceptionToError("adios2_set_shape"));
}
}
adios2::MemorySpace adios2_ToMemorySpace(const adios2_memory_space Cmem)
{
adios2::MemorySpace mem = adios2::MemorySpace::Detect;
switch (Cmem)
{
case adios2_memory_space_host:
mem = adios2::MemorySpace::Host;
break;
#ifdef ADIOS2_HAVE_GPU_SUPPORT
case adios2_memory_space_gpu:
mem = adios2::MemorySpace::GPU;
break;
#endif
default:
break;
}
return mem;
}
adios2_memory_space adios2_FromMemorySpace(const adios2::MemorySpace mem)
{
adios2_memory_space Cmem = adios2_memory_space_detect;
switch (mem)
{
case adios2::MemorySpace::Host:
Cmem = adios2_memory_space_host;
break;
#ifdef ADIOS2_HAVE_GPU_SUPPORT
case adios2::MemorySpace::GPU:
Cmem = adios2_memory_space_gpu;
break;
#endif
default:
break;
}
return Cmem;
}
adios2_error adios2_set_memory_space(adios2_variable *variable, const adios2_memory_space mem)
{
try
{
adios2::core::VariableBase *variableBase =
reinterpret_cast<adios2::core::VariableBase *>(variable);
variableBase->SetMemorySpace(adios2_ToMemorySpace(mem));
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(
adios2::helper::ExceptionToError("adios2_set_memory_space"));
}
}
adios2_error adios2_get_memory_space(adios2_memory_space *mem, adios2_variable *variable)
{
try
{
adios2::core::VariableBase *variableBase =
reinterpret_cast<adios2::core::VariableBase *>(variable);
*mem = adios2_FromMemorySpace(variableBase->m_MemSpace);
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(
adios2::helper::ExceptionToError("adios2_set_memory_space"));
}
}
adios2_error adios2_set_block_selection(adios2_variable *variable, const size_t block_id)
{
try
{
adios2::helper::CheckForNullptr(variable, "for adios2_variable, in call to "
"adios2_set_block_selection");
adios2::core::VariableBase *variableBase =
reinterpret_cast<adios2::core::VariableBase *>(variable);
variableBase->SetBlockSelection(block_id);
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(
adios2::helper::ExceptionToError("adios2_set_block_selection"));
}
}
adios2_error adios2_set_selection(adios2_variable *variable, const size_t ndims,
const size_t *start, const size_t *count)
{
try
{
adios2::helper::CheckForNullptr(variable, "for adios2_variable, in call to "
"adios2_set_selection");
adios2::helper::CheckForNullptr(count, "for count, in call to "
"adios2_set_selection");
adios2::core::VariableBase *variableBase =
reinterpret_cast<adios2::core::VariableBase *>(variable);
const adios2::Dims startV =
(start == nullptr) ? adios2::Dims() : adios2::Dims(start, start + ndims);
const adios2::Dims countV = adios2::Dims(count, count + ndims);
variableBase->SetSelection({startV, countV});
variableBase->CheckDimensions("in call to adios2_set_selection");
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(adios2::helper::ExceptionToError("adios2_set_selection"));
}
}
adios2_error adios2_set_memory_selection(adios2_variable *variable, const size_t ndims,
const size_t *memory_start, const size_t *memory_count)
{
try
{
adios2::helper::CheckForNullptr(variable, "for adios2_variable, in call to "
"adios2_set_memory_selection");
adios2::helper::CheckForNullptr(memory_start, "for start, in call to "
"adios2_set_memory_selection");
adios2::helper::CheckForNullptr(memory_count, "for count, in call to "
"adios2_set_memory_selection");
adios2::core::VariableBase *variableBase =
reinterpret_cast<adios2::core::VariableBase *>(variable);
const adios2::Dims memoryStartV(memory_start, memory_start + ndims);
const adios2::Dims memoryCountV(memory_count, memory_count + ndims);
variableBase->SetMemorySelection({memoryStartV, memoryCountV});
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(
adios2::helper::ExceptionToError("adios2_set_memory_selection"));
}
}
adios2_error adios2_set_step_selection(adios2_variable *variable, const size_t step_start,
const size_t step_count)
{
try
{
adios2::helper::CheckForNullptr(variable, "for adios2_variable, in call to "
"adios2_set_step_selection");
adios2::core::VariableBase *variableBase =
reinterpret_cast<adios2::core::VariableBase *>(variable);
variableBase->SetStepSelection(adios2::Box<size_t>{step_start, step_count});
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(adios2::helper::ExceptionToError("adios2_set_selection"));
}
}
adios2_error adios2_variable_name(char *name, size_t *size, const adios2_variable *variable)
{
try
{
adios2::helper::CheckForNullptr(
variable, "for const adios2_variable, in call to adios2_variable_name");
adios2::helper::CheckForNullptr(size, "for size_t* size, in call to adios2_variable_name");
const adios2::core::VariableBase *variableBase =
reinterpret_cast<const adios2::core::VariableBase *>(variable);
return String2CAPI(variableBase->m_Name, name, size);
}
catch (...)
{
return static_cast<adios2_error>(adios2::helper::ExceptionToError("adios2_variable_name"));
}
}
adios2_error adios2_variable_type(adios2_type *type, const adios2_variable *variable)
{
try
{
adios2::helper::CheckForNullptr(
variable, "for const adios2_variable, in call to adios2_variable_type");
const adios2::core::VariableBase *variableBase =
reinterpret_cast<const adios2::core::VariableBase *>(variable);
const adios2::DataType typeCpp = variableBase->m_Type;
if (typeCpp == adios2::helper::GetDataType<std::string>())
{
*type = adios2_type_string;
}
#define make_case(T) \
else if (typeCpp == adios2::helper::GetDataType<MapAdios2Type<T>::Type>()) \
{ \
*type = T; \
}
ADIOS2_FOREACH_C_TYPE_1ARG(make_case)
#undef make_case
else
{
*type = adios2_type_unknown;
}
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(adios2::helper::ExceptionToError("adios2_variable_type"));
}
}
adios2_error adios2_variable_type_string(char *type, size_t *size, const adios2_variable *variable)
{
try
{
adios2::helper::CheckForNullptr(variable, "for const adios2_variable, in call to "
"adios2_variable_type_string");
adios2::helper::CheckForNullptr(
size, "for size_t* length, in call to adios2_variable_type_string");
const adios2::core::VariableBase *variableBase =
reinterpret_cast<const adios2::core::VariableBase *>(variable);
return String2CAPI(ToString(variableBase->m_Type), type, size);
}
catch (...)
{
return static_cast<adios2_error>(
adios2::helper::ExceptionToError("adios2_variable_type_string"));
}
}
adios2_error adios2_variable_shapeid(adios2_shapeid *shapeid, const adios2_variable *variable)
{
try
{
adios2::helper::CheckForNullptr(variable, "for const adios2_variable, in call to "
"adios2_variable_shapeid");
const adios2::core::VariableBase *variableBase =
reinterpret_cast<const adios2::core::VariableBase *>(variable);
*shapeid = adios2_ToShapeID(variableBase->m_ShapeID, "in call to adios2_variable_shapeid");
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(
adios2::helper::ExceptionToError("adios2_variable_shapeid"));
}
}
adios2_error adios2_variable_ndims(size_t *ndims, const adios2_variable *variable)
{
try
{
adios2::helper::CheckForNullptr(variable, "for const adios2_variable, in call to "
"adios2_variable_ndims");
const adios2::core::VariableBase *variableBase =
reinterpret_cast<const adios2::core::VariableBase *>(variable);
*ndims = variableBase->m_Count.size();
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(adios2::helper::ExceptionToError("adios2_variable_ndims"));
}
}
adios2_error adios2_variable_shape_with_memory_space(size_t *shape, const adios2_variable *variable,
adios2_memory_space mem)
{
try
{
adios2::helper::CheckForNullptr(variable, "for const adios2_variable, in call to "
"adios2_variable_shape");
adios2::helper::CheckForNullptr(shape, "for size_t* shape, in call to "
"adios2_variable_shape");
const adios2::core::VariableBase *variableBase =
reinterpret_cast<const adios2::core::VariableBase *>(variable);
const adios2::DataType typeCpp = variableBase->m_Type;
if (typeCpp == adios2::DataType::Struct)
{
// not supported
}
#define declare_template_instantiation(T) \
else if (typeCpp == adios2::helper::GetDataType<T>()) \
{ \
const adios2::core::Variable<T> *variable = \
dynamic_cast<const adios2::core::Variable<T> *>(variableBase); \
adios2::Dims shapeCpp; \
if (mem == adios2_memory_space_host) \
shapeCpp = variable->Shape(adios2::EngineCurrentStep); \
else \
shapeCpp = variable->Shape(adios2::EngineCurrentStep, adios2_ToMemorySpace(mem)); \
std::copy(shapeCpp.begin(), shapeCpp.end(), shape); \
}
ADIOS2_FOREACH_STDTYPE_1ARG(declare_template_instantiation)
#undef declare_template_instantiation
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(adios2::helper::ExceptionToError("adios2_variable_shape"));
}
}
adios2_error adios2_variable_shape(size_t *shape, const adios2_variable *variable)
{
return adios2_variable_shape_with_memory_space(shape, variable, adios2_memory_space_host);
}
adios2_error adios2_variable_start(size_t *start, const adios2_variable *variable)
{
try
{
adios2::helper::CheckForNullptr(variable, "for const adios2_variable, in call to "
"adios2_variable_start");
const adios2::core::VariableBase *variableBase =
reinterpret_cast<const adios2::core::VariableBase *>(variable);
std::copy(variableBase->m_Start.begin(), variableBase->m_Start.end(), start);
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(adios2::helper::ExceptionToError("adios2_variable_shape"));
}
}
adios2_error adios2_variable_count(size_t *count, const adios2_variable *variable)
{
try
{
adios2::helper::CheckForNullptr(variable, "for const adios2_variable, in call to "
"adios2_variable_count");
adios2::helper::CheckForNullptr(count, "for const adios2_count, in call to "
"adios2_variable_count");
const adios2::core::VariableBase *variableBase =
reinterpret_cast<const adios2::core::VariableBase *>(variable);
const adios2::DataType typeCpp = variableBase->m_Type;
if (typeCpp == adios2::DataType::Struct)
{
// not supported
}
#define declare_template_instantiation(T) \
else if (typeCpp == adios2::helper::GetDataType<T>()) \
{ \
const adios2::core::Variable<T> *variable = \
dynamic_cast<const adios2::core::Variable<T> *>(variableBase); \
const adios2::Dims countCpp = variable->Count(); \
std::copy(countCpp.begin(), countCpp.end(), count); \
}
ADIOS2_FOREACH_STDTYPE_1ARG(declare_template_instantiation)
#undef declare_template_instantiation
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(adios2::helper::ExceptionToError("adios2_variable_count"));
}
}
adios2_error adios2_variable_steps_start(size_t *steps_start, const adios2_variable *variable)
{
try
{
adios2::helper::CheckForNullptr(variable, "for const adios2_variable, in call to "
"adios2_variable_steps_start");
const adios2::core::VariableBase *variableBase =
reinterpret_cast<const adios2::core::VariableBase *>(variable);
*steps_start = variableBase->m_AvailableStepsStart;
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(
adios2::helper::ExceptionToError("adios2_variable_steps_start"));
}
}
adios2_error adios2_variable_steps(size_t *steps, const adios2_variable *variable)
{
try
{
adios2::helper::CheckForNullptr(variable, "for const adios2_variable, in call to "
"adios2_variable_steps");
const adios2::core::VariableBase *variableBase =
reinterpret_cast<const adios2::core::VariableBase *>(variable);
*steps = variableBase->m_AvailableStepsCount;
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(adios2::helper::ExceptionToError("adios2_variable_steps"));
}
}
adios2_error adios2_selection_size(size_t *size, const adios2_variable *variable)
{
try
{
adios2::helper::CheckForNullptr(variable, "for adios2_variable, in call to "
"adios2_selection_size");
const adios2::core::VariableBase *variableBase =
reinterpret_cast<const adios2::core::VariableBase *>(variable);
const adios2::DataType typeCpp = variableBase->m_Type;
if (typeCpp == adios2::DataType::Struct)
{
// not supported
}
#define declare_template_instantiation(T) \
else if (typeCpp == adios2::helper::GetDataType<T>()) \
{ \
const adios2::core::Variable<T> *variable = \
dynamic_cast<const adios2::core::Variable<T> *>(variableBase); \
*size = variable->SelectionSize(); \
}
ADIOS2_FOREACH_STDTYPE_1ARG(declare_template_instantiation)
#undef declare_template_instantiation
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(adios2::helper::ExceptionToError("adios2_selection_size"));
}
}
adios2_error adios2_add_operation(size_t *operation_index, adios2_variable *variable,
adios2_operator *op, const char *key, const char *value)
{
try
{
adios2::helper::CheckForNullptr(variable, "for adios2_variable, in call to "
"adios2_add_operation");
adios2::helper::CheckForNullptr(op, "for adios2_operator, in call to "
"adios2_add_operation");
adios2::helper::CheckForNullptr(key, "for char* key, in call to "
"adios2_add_operation");
adios2::helper::CheckForNullptr(value, "for char* value, in call to "
"adios2_add_operation");
adios2::core::VariableBase *variableBase =
reinterpret_cast<adios2::core::VariableBase *>(variable);
auto *opCpp = reinterpret_cast<std::pair<std::string, adios2::Params> *>(op);
auto params = adios2::Params{{key, value}};
for (const auto &p : opCpp->second)
{
params[p.first] = p.second;
}
*operation_index = variableBase->AddOperation(opCpp->first, params);
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(adios2::helper::ExceptionToError("adios2_add_operation"));
}
}
adios2_error adios2_set_operation_parameter(adios2_variable *variable, const size_t operation_id,
const char *key, const char *value)
{
try
{
adios2::helper::CheckForNullptr(variable, "for adios2_variable, in call to "
"adios2_set_operation_parameter");
adios2::core::VariableBase *variableBase =
reinterpret_cast<adios2::core::VariableBase *>(variable);
variableBase->SetOperationParameter(operation_id, key, value);
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(
adios2::helper::ExceptionToError("adios2_set_operation_parameter"));
}
}
adios2_error adios2_remove_operations(adios2_variable *variable)
{
try
{
adios2::helper::CheckForNullptr(variable, "for adios2_variable, in call to "
"adios2_remove_operations");
adios2::core::VariableBase *variableBase =
reinterpret_cast<adios2::core::VariableBase *>(variable);
variableBase->RemoveOperations();
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(
adios2::helper::ExceptionToError("adios2_remove_operations"));
}
}
adios2_error adios2_variable_min(void *min, const adios2_variable *variable)
{
try
{
adios2::helper::CheckForNullptr(variable, "for adios2_variable, in call "
"to adios2_variable_min");
adios2::helper::CheckForNullptr(min, "for void* min, in call to adios2_variable_min");
const adios2::core::VariableBase *variableBase =
reinterpret_cast<const adios2::core::VariableBase *>(variable);
const adios2::DataType type(variableBase->m_Type);
if (type == adios2::DataType::Struct)
{
// not supported
}
#define declare_template_instantiation(T) \
else if (type == adios2::helper::GetDataType<T>()) \
{ \
T *minT = reinterpret_cast<T *>(min); \
const adios2::core::Variable<T> *variableT = \
dynamic_cast<const adios2::core::Variable<T> *>(variableBase); \
*minT = variableT->Min(adios2::EngineCurrentStep); \
}
ADIOS2_FOREACH_STDTYPE_1ARG(declare_template_instantiation)
#undef declare_template_instantiation
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(adios2::helper::ExceptionToError("adios2_variable_min"));
}
}
adios2_error adios2_variable_max(void *max, const adios2_variable *variable)
{
try
{
adios2::helper::CheckForNullptr(variable, "for adios2_variable, in call "
"to adios2_variable_max");
adios2::helper::CheckForNullptr(max, "for void* max, in call to adios2_variable_max");
const adios2::core::VariableBase *variableBase =
reinterpret_cast<const adios2::core::VariableBase *>(variable);
const adios2::DataType type(variableBase->m_Type);
if (type == adios2::DataType::Struct)
{
// not supported
}
#define declare_template_instantiation(T) \
else if (type == adios2::helper::GetDataType<T>()) \
{ \
T *maxT = reinterpret_cast<T *>(max); \
const adios2::core::Variable<T> *variableT = \
dynamic_cast<const adios2::core::Variable<T> *>(variableBase); \
*maxT = variableT->Max(adios2::EngineCurrentStep); \
}
ADIOS2_FOREACH_STDTYPE_1ARG(declare_template_instantiation)
#undef declare_template_instantiation
return adios2_error_none;
}
catch (...)
{
return static_cast<adios2_error>(adios2::helper::ExceptionToError("adios2_variable_max"));
}
}
#ifdef __cplusplus
} // end extern C
#endif