Skip to content

Commit 1445da0

Browse files
authored
Add sum kernel for preprocessor (LLNL#456)
* Add sum() kernel for preprocessor * Add flops example config file
1 parent 5bbfd47 commit 1445da0

File tree

7 files changed

+258
-38
lines changed

7 files changed

+258
-38
lines changed

examples/configs/flops.json

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{ "options":
2+
[
3+
{
4+
"name" : "flops.libpfm",
5+
"category": "metric",
6+
"description": "MFlops/s on Intel CPUs with libpfm",
7+
"services": [ "libpfm" ],
8+
"config":
9+
{
10+
"CALI_LIBPFM_EVENTS": "FP_ARITH:SCALAR,FP_ARITH:PACKED",
11+
"CALI_LIBPFM_ENABLE_SAMPLING": "false"
12+
},
13+
"query":
14+
[
15+
{ "level" : "local",
16+
"let" :
17+
[
18+
"flops.scalar=first(libpfm.counter.FP_ARITH:SCALAR,sum#libpfm.counter.FP_ARITH:SCALAR)",
19+
"flops.packed=first(libpfm.counter.FP_ARITH:PACKED,sum#libpfm.counter.FP_ARITH:PACKED)"
20+
],
21+
"select" :
22+
[
23+
{ "expr": "scale(flops.scalar,1e-6)", "as": "Mflops (scalar)" },
24+
{ "expr": "scale(flops.packed,1e-6)", "as": "Mops (vector)" },
25+
{ "expr": "ratio(flops.scalar,sum#time.duration,1e-6)", "as": "Mflops/s (scalar)" }
26+
]
27+
},
28+
{ "level" : "cross",
29+
"select" :
30+
[
31+
{ "expr": "sum(scale#flops.scalar)", "as": "Mflops (scalar)" },
32+
{ "expr": "sum(scale#flops.packed)", "as": "Mops (vector)" },
33+
{ "expr": "avg(ratio#flops.scalar/sum#time.duration)", "as": "Mflops/s (avg)" },
34+
{ "expr": "max(ratio#flops.scalar/sum#time.duration)", "as": "Mflops/s (max)" },
35+
{ "expr": "sum(ratio#flops.scalar/sum#time.duration)", "as": "Mflops/s (sum)" }
36+
]
37+
}
38+
]
39+
},
40+
{
41+
"name" : "flops",
42+
"category": "metric",
43+
"description": "MFlops/s with PAPI",
44+
"services": [ "papi" ],
45+
"config":
46+
{
47+
"CALI_PAPI_COUNTERS": "PAPI_DP_OPS,PAPI_SP_OPS"
48+
},
49+
"query":
50+
[
51+
{ "level" : "local",
52+
"let" :
53+
[
54+
"flops.sp=first(sum#papi.PAPI_SP_OPS,papi.PAPI_SP_OPS)",
55+
"flops.dp=first(sum#papi.PAPI_DP_OPS,papi.PAPI_DP_OPS)",
56+
"flops=sum(flops.dp,flops.sp)"
57+
],
58+
"select" :
59+
[
60+
{ "expr": "scale(flops,1e-6)", "as": "Mflops" },
61+
{ "expr": "ratio(flops,sum#time.duration,1e-6)", "as": "Mflops/s" }
62+
]
63+
},
64+
{ "level" : "cross",
65+
"select" :
66+
[
67+
{ "expr": "sum(scale#flops)", "as": "Mflops" },
68+
{ "expr": "avg(ratio#flops/sum#time.duration)", "as": "Mflops/s (avg)" },
69+
{ "expr": "max(ratio#flops/sum#time.duration)", "as": "Mflops/s (max)" },
70+
{ "expr": "sum(ratio#flops/sum#time.duration)", "as": "Mflops/s (sum)" }
71+
]
72+
}
73+
]
74+
}
75+
]
76+
}

include/caliper/common/Variant.h

+15-13
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ namespace cali
2727
/// strings and "blobs" are stored as unmanaged pointers. Users need
2828
/// to make sure these pointers are valid while any variant
2929
/// encapsulating them is being used.
30-
31-
class Variant
30+
31+
class Variant
3232
{
3333
cali_variant_t m_v;
34-
34+
3535
public:
36-
36+
3737
CONSTEXPR_UNLESS_PGI Variant()
3838
: m_v { CALI_TYPE_INV, { static_cast<uint64_t>(0) } } { }
39-
39+
4040
Variant(const cali_variant_t& v)
4141
: m_v(v) { }
42-
42+
4343
Variant(bool val)
4444
: m_v(cali_make_variant_from_bool(val)) { }
4545
Variant(int val)
@@ -61,21 +61,21 @@ class Variant
6161
m_v = cali_make_variant(type, data, size);
6262
}
6363

64-
bool empty() const {
64+
bool empty() const {
6565
return (m_v.type_and_size & CALI_VARIANT_TYPE_MASK) == CALI_TYPE_INV;
6666
};
6767
operator bool() const {
6868
return !empty();
6969
}
7070

7171
cali_variant_t c_variant() const { return m_v; }
72-
72+
7373
cali_attr_type type() const { return cali_variant_get_type(m_v); }
7474
const void* data() const { return cali_variant_get_data(&m_v); }
7575
size_t size() const { return cali_variant_get_size(m_v); }
7676

7777
void* get_ptr() const { return cali_variant_get_ptr(m_v); }
78-
78+
7979
cali_id_t to_id(bool* okptr = nullptr) const;
8080
int to_int(bool* okptr = nullptr) const {
8181
return cali_variant_to_int(m_v, okptr);
@@ -95,19 +95,21 @@ class Variant
9595
cali_attr_type to_attr_type(bool* okptr = nullptr) const {
9696
return cali_variant_to_type(m_v, okptr);
9797
}
98-
98+
9999
std::string to_string() const;
100100

101+
Variant& operator += (const Variant& val);
102+
101103
size_t pack(unsigned char* buf) const {
102104
return cali_variant_pack(m_v, buf);
103105
}
104-
106+
105107
static Variant unpack(const unsigned char* buf, size_t* inc, bool* ok = nullptr) {
106108
return Variant(cali_variant_unpack(buf, inc, ok));
107109
}
108110

109111
static Variant from_string(cali_attr_type type, const char* str, bool* ok = nullptr);
110-
112+
111113
// vector<unsigned char> data() const;
112114

113115
friend bool operator == (const Variant& lhs, const Variant& rhs);
@@ -118,7 +120,7 @@ class Variant
118120
inline bool operator == (const Variant& lhs, const Variant& rhs) {
119121
return cali_variant_eq(lhs.m_v, rhs.m_v);
120122
}
121-
123+
122124
inline bool operator < (const Variant& lhs, const Variant& rhs) {
123125
return (cali_variant_compare(lhs.m_v, rhs.m_v) < 0);
124126
}

src/common/Variant.cpp

+44-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Variant::to_string() const
4242
{
4343
size_t size = this->size();
4444
const void* ptr = data();
45-
45+
4646
std::ostringstream os;
4747

4848
std::copy(static_cast<const unsigned char*>(ptr), static_cast<const unsigned char*>(ptr) + size,
@@ -64,9 +64,9 @@ Variant::to_string() const
6464

6565
if (len && str[len-1] == 0)
6666
--len;
67-
67+
6868
ret.assign(str, len);
69-
}
69+
}
7070
break;
7171
case CALI_TYPE_ADDR:
7272
{
@@ -100,7 +100,7 @@ Variant::from_string(cali_attr_type type, const char* str, bool* okptr)
100100
{
101101
Variant ret;
102102
bool ok = false;
103-
103+
104104
switch (type) {
105105
case CALI_TYPE_INV:
106106
case CALI_TYPE_USR:
@@ -146,7 +146,7 @@ Variant::from_string(cali_attr_type type, const char* str, bool* okptr)
146146
case CALI_TYPE_BOOL:
147147
{
148148
bool b = StringConverter(str).to_bool(&ok);
149-
149+
150150
if (ok)
151151
ret = Variant(b);
152152
}
@@ -174,3 +174,42 @@ std::ostream& cali::operator << (std::ostream& os, const Variant& v)
174174
return os;
175175
}
176176

177+
Variant& Variant::operator += (const Variant& val)
178+
{
179+
cali_attr_type type = this->type();
180+
181+
if (type == val.type()) {
182+
switch (type) {
183+
case CALI_TYPE_DOUBLE:
184+
m_v.value.v_double += val.m_v.value.v_double;
185+
break;
186+
case CALI_TYPE_INT:
187+
m_v.value.v_int += val.m_v.value.v_int;
188+
break;
189+
case CALI_TYPE_UINT:
190+
m_v.value.v_uint += val.m_v.value.v_uint;
191+
break;
192+
default:
193+
break;
194+
}
195+
} else {
196+
switch (type) {
197+
case CALI_TYPE_INV:
198+
*this = val;
199+
break;
200+
case CALI_TYPE_DOUBLE:
201+
m_v.value.v_double += val.to_double();
202+
break;
203+
case CALI_TYPE_INT:
204+
m_v.value.v_int += val.to_int64();
205+
break;
206+
case CALI_TYPE_UINT:
207+
m_v.value.v_uint += val.to_uint();
208+
break;
209+
default:
210+
break;
211+
}
212+
}
213+
214+
return *this;
215+
}

src/common/test/test_variant.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,25 @@ TEST(Variant_Test, PackUnpack) {
289289
EXPECT_EQ(v_9_i64_out.to_int64(), val_9_i64);
290290
EXPECT_EQ(v_9_i64_in, v_9_i64_out);
291291
}
292+
293+
TEST(Variant_Test, PlusEq)
294+
{
295+
cali::Variant v_i(cali_make_variant_from_int64(42));
296+
cali::Variant v_d(cali_make_variant_from_double(42.42));
297+
cali::Variant v_u(cali_make_variant_from_uint(42));
298+
299+
cali::Variant v_e;
300+
301+
v_e += v_i;
302+
EXPECT_EQ(v_e.type(), CALI_TYPE_INT);
303+
EXPECT_EQ(v_e.to_int64(), 42);
304+
305+
v_e += v_u;
306+
EXPECT_EQ(v_e.type(), CALI_TYPE_INT);
307+
EXPECT_EQ(v_e.to_int64(), 84);
308+
309+
v_e = v_d;
310+
v_e += v_i;
311+
EXPECT_EQ(v_e.type(), CALI_TYPE_DOUBLE);
312+
EXPECT_EQ(v_e.to_double(), 84.42);
313+
}

src/reader/Aggregator.cpp

+1-18
Original file line numberDiff line numberDiff line change
@@ -335,24 +335,7 @@ class SumKernel : public AggregateKernel {
335335

336336
for (const Entry& e : rec) {
337337
if (e.attribute() == tgt_id || e.attribute() == sum_id) {
338-
switch (target_attr.type()) {
339-
case CALI_TYPE_DOUBLE:
340-
m_sum = Variant(m_sum.to_double() + e.value().to_double());
341-
break;
342-
case CALI_TYPE_INT:
343-
{
344-
int64_t s = m_sum.to_int64() + e.value().to_int64();
345-
m_sum = Variant(cali_make_variant_from_int64(s));
346-
}
347-
break;
348-
case CALI_TYPE_UINT:
349-
m_sum = Variant(m_sum.to_uint() + e.value().to_uint() );
350-
break;
351-
default:
352-
;
353-
// Some error?!
354-
}
355-
338+
m_sum += e.value();
356339
++m_count;
357340
break;
358341
}

src/reader/Preprocessor.cpp

+51-2
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,58 @@ class FirstKernel : public Kernel
247247

248248
};
249249

250+
class SumKernel : public Kernel
251+
{
252+
std::string m_res_attr_name;
253+
Attribute m_res_attr;
254+
255+
std::vector<std::string> m_tgt_attr_names;
256+
std::vector<Attribute> m_tgt_attrs;
257+
258+
public:
259+
260+
SumKernel(const std::string& def, const std::vector<std::string>& args)
261+
: m_res_attr_name(def),
262+
m_res_attr(Attribute::invalid),
263+
m_tgt_attr_names(args)
264+
{
265+
m_tgt_attrs.assign(args.size(), Attribute::invalid);
266+
}
267+
268+
void process(CaliperMetadataAccessInterface& db, EntryList& rec) {
269+
Variant v_sum;
270+
271+
for (size_t i = 0; i < m_tgt_attrs.size(); ++i) {
272+
Variant v_tgt = get_value(db, m_tgt_attr_names[i], m_tgt_attrs[i], rec);
273+
274+
if (v_tgt.empty())
275+
continue;
276+
277+
v_sum += v_tgt;
278+
}
279+
280+
if (!v_sum.empty()) {
281+
if (m_res_attr == Attribute::invalid)
282+
m_res_attr = db.create_attribute(m_res_attr_name, v_sum.type(),
283+
CALI_ATTR_SKIP_EVENTS |
284+
CALI_ATTR_ASVALUE);
285+
286+
rec.push_back(Entry(m_res_attr, v_sum));
287+
}
288+
}
289+
290+
static Kernel* create(const std::string& def, const std::vector<std::string>& args) {
291+
return new SumKernel(def, args);
292+
}
293+
294+
};
295+
250296
enum KernelID {
251297
ScaledRatio,
252298
Scale,
253299
Truncate,
254300
First,
301+
Sum
255302
};
256303

257304
const char* sratio_args[] = { "numerator", "denominator", "scale" };
@@ -267,6 +314,7 @@ const QuerySpec::FunctionSignature kernel_signatures[] = {
267314
{ KernelID::Scale, "scale", 2, 2, scale_args },
268315
{ KernelID::Truncate, "truncate", 1, 2, scale_args },
269316
{ KernelID::First, "first", 1, 8, first_args },
317+
{ KernelID::Sum, "sum", 1, 8, first_args },
270318

271319
QuerySpec::FunctionSignatureTerminator
272320
};
@@ -277,10 +325,11 @@ const KernelCreateFn kernel_create_fn[] = {
277325
ScaledRatioKernel::create,
278326
ScaleKernel::create,
279327
TruncateKernel::create,
280-
FirstKernel::create
328+
FirstKernel::create,
329+
SumKernel::create
281330
};
282331

283-
constexpr int MAX_KERNEL_ID = 3;
332+
constexpr int MAX_KERNEL_ID = 4;
284333

285334
}
286335

0 commit comments

Comments
 (0)