• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/examples/tuplespace/simple_central_tuplespace_client.cpp
1
//  Copyright (c) 2013 Shuangyang Yang
2
//
3
//  SPDX-License-Identifier: BSL-1.0
4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
#include <hpx/config.hpp>
8
#if !defined(HPX_COMPUTE_DEVICE_CODE)
9
#include <hpx/hpx_init.hpp>
10

11
#include <hpx/include/actions.hpp>
12
#include <hpx/include/lcos.hpp>
13
#include <hpx/include/runtime.hpp>
14
#include <hpx/include/util.hpp>
15
#include <hpx/iostream.hpp>
16

17
#include <string>
18
#include <vector>
19

20
#include "central_tuplespace/simple_central_tuplespace.hpp"
21
#include "small_big_object.hpp"
22

23
using central_tuplespace_type = examples::server::simple_central_tuplespace;
24
using tuple_type = central_tuplespace_type::tuple_type;
25
using elem_type = central_tuplespace_type::elem_type;
26

27
void print_tuple(tuple_type const& tuple)
×
28
{
29
    if (tuple.empty())
×
30
    {
31
        hpx::cout << "()";
×
32
        return;
33
    }
34

35
    auto it = tuple.begin();
36
    hpx::cout << "(" << *it;
×
37
    for (++it; it != tuple.end(); ++it)
×
38
    {
39
        hpx::cout << ", " << *it;
×
40
    }
41
    hpx::cout << ")";
×
42
}
43

44
void simple_central_tuplespace_test(
×
45
    std::string const& tuplespace_symbol_name, tuple_type const& tuple)
46
{
47
    examples::simple_central_tuplespace central_tuplespace;
48

49
    if (!central_tuplespace.connect(tuplespace_symbol_name))
×
50
    {
51
        hpx::cerr << "locality " << hpx::get_locality_id() << ": "
×
52
                  << "FAIL to connect " << tuplespace_symbol_name << std::endl;
×
53
        return;
54
    }
55

56
    int const ret = central_tuplespace.write(hpx::launch::sync, tuple);
×
57
    hpx::cout << "locality " << hpx::get_locality_id() << ": "
×
58
              << "write_sync ";
×
59
    print_tuple(tuple);
×
60
    hpx::cout << " returns " << ret << std::endl;
×
61

62
    tuple_type partial_tuple;
×
63

64
    if (tuple.size() > 1)    // use second field
×
65
    {
66
        partial_tuple.push_back_empty().push_back(*(tuple.begin() + 1));
67
    }
68
    else
69
    {
70
        partial_tuple.push_back(*(tuple.begin()));
71
    }
72

73
    tuple_type return_tuple =
74
        central_tuplespace.read(hpx::launch::sync, partial_tuple, 0);
75
    hpx::cout << "locality " << hpx::get_locality_id() << ": "
×
76
              << "read_sync tuple with ";
×
77
    print_tuple(partial_tuple);
×
78
    hpx::cout << " returns ";
×
79
    print_tuple(return_tuple);
×
80
    hpx::cout << std::endl;
×
81

82
    return_tuple = central_tuplespace.take(hpx::launch::sync, partial_tuple, 0);
×
83
    hpx::cout << "locality " << hpx::get_locality_id() << ": "
×
84
              << "take_sync tuple with ";
×
85
    print_tuple(partial_tuple);
×
86
    hpx::cout << " (1st) returns ";
×
87
    print_tuple(return_tuple);
×
88
    hpx::cout << std::endl;
×
89

90
    return_tuple = central_tuplespace.take(hpx::launch::sync, partial_tuple, 0);
×
91
    hpx::cout << "locality " << hpx::get_locality_id() << ": "
×
92
              << "take_sync tuple with ";
×
93
    print_tuple(partial_tuple);
×
94
    hpx::cout << " (2nd) returns ";
×
95
    print_tuple(return_tuple);
×
96
    hpx::cout << std::endl << std::flush;
×
97
}
×
98

99
HPX_PLAIN_ACTION(
×
100
    simple_central_tuplespace_test, simple_central_tuplespace_test_action)
101

102
///////////////////////////////////////////////////////////////////////////////
103
int hpx_main()
×
104
{
105
    {
106
        // Find the localities connected to this application.
107
        std::vector<hpx::id_type> const localities = hpx::find_all_localities();
×
108

109
        std::string const tuplespace_symbol_name = "/tuplespace";
×
110
        examples::simple_central_tuplespace central_tuplespace;
111

112
        if (!central_tuplespace.create(
×
113
                tuplespace_symbol_name, localities.back()))
114
        {
115
            hpx::cerr << "locality " << hpx::get_locality_id() << ": "
×
116
                      << "FAIL to create " << tuplespace_symbol_name
×
117
                      << std::endl;
×
118

119
            return hpx::finalize();
120
        }
121

122
        tuple_type tuple1;
×
123
        tuple1.push_back(std::string("first"))
×
124
            .push_back(10)                     // first elem: int
×
125
            .push_back(small_object(20))       // second elem: small_object
×
126
            .push_back(big_object(30, 40));    // third elem: big_object
×
127

128
        hpx::cout << "locality " << hpx::get_locality_id() << ": "
×
129
                  << "created tuple1: ";
×
130
        print_tuple(tuple1);
×
131
        hpx::cout << std::endl;
×
132

133
        tuple_type tuple2;
×
134
        tuple2.push_back(std::string("second"))
×
135
            .push_back(std::string("string"))    // first elem: string
×
136
            .push_back(small_object(50))         // second elem: small_object
×
137
            .push_back(big_object(60, 70));      // third elem: big_object
×
138

139
        hpx::cout << "locality " << hpx::get_locality_id() << ": "
×
140
                  << "created tuple2: ";
×
141
        print_tuple(tuple2);
×
142
        hpx::cout << std::endl;
×
143

144
        std::vector<hpx::future<void>> futures;
×
145

146
        for (hpx::id_type const& node : localities)
×
147
        {
148
            // Asynchronously start a new task. The task is encapsulated in a
149
            // future, which we can query to determine if the task has
150
            // completed.
151
            using action_type = simple_central_tuplespace_test_action;
152
            futures.push_back(
153
                hpx::async<action_type>(node, tuplespace_symbol_name, tuple1));
×
154
            futures.push_back(
155
                hpx::async<action_type>(node, tuplespace_symbol_name, tuple2));
×
156
        }
157
        hpx::wait_all(futures);
158
    }
×
159

160
    // Initiate shutdown of the runtime systems on all localities.
161
    return hpx::finalize();
×
162
}
163

164
///////////////////////////////////////////////////////////////////////////////
165
int main(int argc, char* argv[])
×
166
{
167
    // We force this example to use 2 threads by default as one of the threads
168
    // will be sitting most of the time in the kernel waiting for user input.
169
    std::vector<std::string> const cfg = {"hpx.os_threads=2"};
×
170

171
    // Initialize and run HPX.
172
    hpx::init_params init_args;
×
173
    init_args.cfg = cfg;
×
174

175
    return hpx::init(argc, argv, init_args);
×
176
}
×
177
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc