Skip to content

Commit 07a6e09

Browse files
authored
This would allow users to find out what builtin is. (simdjson#1227)
* This would allow users to find out what builtin is. * Trying another approach. * Added instructions. * Cleaning up the printout. * Let us be less invasive. * Adding a comment.
1 parent e4897d6 commit 07a6e09

File tree

8 files changed

+44
-8
lines changed

8 files changed

+44
-8
lines changed

benchmark/kostya/ondemand.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ using namespace simdjson::builtin;
1212
class OnDemand {
1313
public:
1414
simdjson_really_inline bool Run(const padded_string &json);
15-
1615
simdjson_really_inline const std::vector<my_point> &Result() { return container; }
1716
simdjson_really_inline size_t ItemCount() { return container.size(); }
1817

@@ -43,7 +42,6 @@ namespace sum {
4342
class OnDemand {
4443
public:
4544
simdjson_really_inline bool Run(const padded_string &json);
46-
4745
simdjson_really_inline my_point &Result() { return sum; }
4846
simdjson_really_inline size_t ItemCount() { return count; }
4947

benchmark/largerandom/ondemand.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ using namespace simdjson::builtin;
1212
class OnDemand {
1313
public:
1414
simdjson_really_inline bool Run(const padded_string &json);
15-
1615
simdjson_really_inline const std::vector<my_point> &Result() { return container; }
1716
simdjson_really_inline size_t ItemCount() { return container.size(); }
1817

@@ -40,7 +39,6 @@ namespace sum {
4039
class OnDemand {
4140
public:
4241
simdjson_really_inline bool Run(const padded_string &json);
43-
4442
simdjson_really_inline my_point &Result() { return sum; }
4543
simdjson_really_inline size_t ItemCount() { return count; }
4644

benchmark/partial_tweets/ondemand.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ namespace partial_tweets {
99
using namespace simdjson;
1010
using namespace simdjson::builtin;
1111

12+
1213
class OnDemand {
1314
public:
15+
OnDemand() {
16+
if(!displayed_implementation) {
17+
std::cout << "On Demand implementation: " << builtin_implementation()->name() << std::endl;
18+
displayed_implementation = true;
19+
}
20+
}
1421
simdjson_really_inline bool Run(const padded_string &json);
15-
1622
simdjson_really_inline const std::vector<tweet> &Result() { return tweets; }
1723
simdjson_really_inline size_t ItemCount() { return tweets.size(); }
1824

@@ -30,6 +36,7 @@ class OnDemand {
3036
ondemand::object u = std::move(user);
3137
return { u["id"], u["screen_name"] };
3238
}
39+
static inline bool displayed_implementation = false;
3340
};
3441

3542
simdjson_really_inline bool OnDemand::Run(const padded_string &json) {

doc/ondemand.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,21 @@ Good applications for the On Demand API might be:
509509
* You have a closed system on predetermined hardware. Both the generation and the consumption of JSON data is within your system. Your team controls both the software that produces the JSON and the software the parses it, your team knows and control the hardware. Thus you can fully test your system.
510510
* You are working with stable JSON APIs which have a consistent layout and JSON dialect.
511511
512+
## Checking Your CPU Selection
513+
514+
Given that the On Demand API does not offer runtime dispatching, your code is compiled against a specific CPU target. You should
515+
verify that the code is compiled against the target you expect: `haswell` (AVX2 x64 processors), `westmere` (SSE4 x64 processors), `arm64` (64-bit ARM), `fallback` (others). Under x64 processors, many programmers will want to target `haswell` whereas under ARM,
516+
most programmers will want to target `arm64`. The `fallback` is probably only good for testing purposes, not for deployment.
517+
518+
```C++
519+
std::cout << simdjson::builtin_implementation()->name() << std::endl;
520+
```
521+
522+
If you are using CMake for your C++ project, then you can pass compilation flags to your compiler during the first configuration
523+
by using the `CXXFLAGS` configuration variable:
524+
```
525+
CXXFLAGS=-march=haswell cmake -B build_haswell
526+
cmake --build build_haswell
527+
```
528+
529+
You may also use the `CMAKE_CXX_FLAGS` variable.

include/simdjson/builtin.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ namespace simdjson {
2929
* code that uses it) will use westmere.
3030
*/
3131
namespace builtin = SIMDJSON_BUILTIN_IMPLEMENTATION;
32+
/**
33+
* Function which returns a pointer to an implementation matching the "builtin" implementation.
34+
* The builtin implementation is the best statically linked simdjson implementation that can be used by the compiling
35+
* program. If you compile with g++ -march=haswell, this will return the haswell implementation.
36+
* It is handy to be able to check what builtin was used: builtin_implementation()->name().
37+
*/
38+
const implementation * builtin_implementation();
3239
} // namespace simdjson
3340

3441
#endif // SIMDJSON_BUILTIN_H

src/implementation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,10 @@ simdjson_warn_unused bool validate_utf8(const char *buf, size_t len) noexcept {
147147
return active_implementation->validate_utf8(buf, len);
148148
}
149149

150+
const implementation * builtin_implementation() {
151+
static const implementation * builtin_impl = available_implementations[STRINGIFY(SIMDJSON_BUILTIN_IMPLEMENTATION)];
152+
return builtin_impl;
153+
}
154+
150155

151156
} // namespace simdjson

tests/ondemand/ondemand_basictests.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,8 +1337,11 @@ int main(int argc, char *argv[]) {
13371337
printf("unsupported CPU\n");
13381338
}
13391339
// We want to know what we are testing.
1340-
std::cout << "Running tests against this implementation: " << simdjson::active_implementation->name();
1341-
std::cout << "(" << simdjson::active_implementation->description() << ")" << std::endl;
1340+
// Next line would be the runtime dispatched implementation but that's not necessarily what gets tested.
1341+
// std::cout << "Running tests against this implementation: " << simdjson::active_implementation->name();
1342+
// Rather, we want to display builtin_implementation()->name().
1343+
// In practice, by default, we often end up testing against fallback.
1344+
std::cout << "builtin_implementation -- " << builtin_implementation()->name() << std::endl;
13421345
std::cout << "------------------------------------------------------------" << std::endl;
13431346

13441347
std::cout << "Running basic tests." << std::endl;

0 commit comments

Comments
 (0)