Skip to content

Commit eaf4ea9

Browse files
c-p-i-ofacebook-github-bot
authored andcommitted
Back out "fbcode/gloo/examples" (#404)
Summary: Put back example files that were deleted by codemod because they didn't have a TARGET Reviewed By: fduwjj Differential Revision: D69182638
1 parent f6bb0c4 commit eaf4ea9

File tree

4 files changed

+525
-1
lines changed

4 files changed

+525
-1
lines changed

gloo/examples/example1.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <iostream>
66
#include <memory>
7+
#include <string>
8+
#include <vector>
79

810
#include "gloo/allreduce_ring.h"
911
#include "gloo/rendezvous/context.h"
@@ -55,7 +57,8 @@ int main(void) {
5557

5658
// attr.ai_family = AF_INET; // Force IPv4
5759
// attr.ai_family = AF_INET6; // Force IPv6
58-
attr.ai_family = AF_UNSPEC; // Use either (default)
60+
// Use either (default)
61+
attr.ai_family = AF_UNSPEC;
5962

6063
// A string is implicitly converted to an "attr" struct with its
6164
// hostname field populated. This will try to resolve the interface

gloo/examples/example_allreduce.cc

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*/
4+
5+
#include <array>
6+
#include <cstdio>
7+
#include <iostream>
8+
#include <memory>
9+
#include <string>
10+
#include <typeinfo>
11+
#include <vector>
12+
13+
#include "gloo/allreduce.h"
14+
15+
#include "gloo/rendezvous/context.h"
16+
#include "gloo/rendezvous/file_store.h"
17+
#include "gloo/rendezvous/prefix_store.h"
18+
#include "gloo/transport/uv/device.h"
19+
20+
// Usage:
21+
//
22+
// Open two terminals. Run the same program in both terminals, using
23+
// a different RANK in each. For example:
24+
//
25+
// A: PREFIX=test1 SIZE=2 RANK=0 example_allreduce
26+
// B: PREFIX=test1 SIZE=2 RANK=1 example_allreduce
27+
//
28+
// Expected output:
29+
//
30+
// data[0] = 18
31+
// data[1] = 18
32+
// data[2] = 18
33+
// data[3] = 18
34+
//
35+
36+
void mysum(void* c_, const void* a_, const void* b_, int n) {
37+
printf("n=%d\r\n", n);
38+
int* c = static_cast<int*>(c_);
39+
const int* a = static_cast<const int*>(a_);
40+
const int* b = static_cast<const int*>(b_);
41+
for (auto i = 0; i < n; i++) {
42+
printf("a[%d]=%d\r\n", i, a[i]);
43+
printf("b[%d]=%d\r\n", i, b[i]);
44+
c[i] = a[i] + b[i];
45+
printf("c[%d]=%d\r\n", i, c[i]);
46+
}
47+
}
48+
49+
int main(void) {
50+
if (getenv("PREFIX") == nullptr || getenv("SIZE") == nullptr ||
51+
getenv("RANK") == nullptr) {
52+
std::cerr << "Please set environment variables PREFIX, SIZE, and RANK."
53+
<< std::endl;
54+
return 1;
55+
}
56+
57+
// The following statement creates a TCP "device" for Gloo to use.
58+
// See "gloo/transport/device.h" for more information. For the
59+
// purposes of this example, it is sufficient to see the device as
60+
// a factory for every communication pair.
61+
//
62+
// The argument to gloo::transport::tcp::CreateDevice is used to
63+
// find the network interface to bind connection to. The attr struct
64+
// can be populated to specify exactly which interface should be
65+
// used, as shown below. This is useful if you have identical
66+
// multi-homed machines that all share the same network interface
67+
// name, for example.
68+
//
69+
gloo::transport::uv::attr attr;
70+
// attr.iface = "eth0";
71+
// attr.iface = "ib0";
72+
// attr.iface = "Wi-Fi";
73+
74+
// attr.ai_family = AF_INET; // Force IPv4
75+
// attr.ai_family = AF_INET6; // Force IPv6
76+
// Use either (default)
77+
attr.ai_family = AF_UNSPEC;
78+
79+
// A string is implicitly converted to an "attr" struct with its
80+
// hostname field populated. This will try to resolve the interface
81+
// to use by resolving the hostname or IP address, and finding the
82+
// corresponding network interface.
83+
//
84+
// Hostname "localhost" should resolve to 127.0.0.1, so using this
85+
// implies that all connections will be local. This can be useful
86+
// for single machine operation.
87+
//
88+
// auto dev = gloo::transport::tcp::CreateDevice("localhost");
89+
//
90+
91+
auto dev = gloo::transport::uv::CreateDevice(attr);
92+
93+
// Now that we have a device, we can connect all participating
94+
// processes. We call this process "rendezvous". It can be performed
95+
// using a shared filesystem, a Redis instance, or something else by
96+
// extending it yourself.
97+
//
98+
// See "gloo/rendezvous/store.h" for the functionality you need to
99+
// implement to create your own store for performing rendezvous.
100+
//
101+
// Below, we instantiate rendezvous using the filesystem, given that
102+
// this example uses multiple processes on a single machine.
103+
//
104+
auto fileStore = gloo::rendezvous::FileStore("/libtmp");
105+
106+
// To be able to reuse the same store over and over again and not have
107+
// interference between runs, we scope it to a unique prefix with the
108+
// PrefixStore. This wraps another store and prefixes every key before
109+
// forwarding the call to the underlying store.
110+
std::string prefix = getenv("PREFIX");
111+
auto prefixStore = gloo::rendezvous::PrefixStore(prefix, fileStore);
112+
113+
// Using this store, we can now create a Gloo context. The context
114+
// holds a reference to every communication pair involving this
115+
// process. It is used by every collective algorithm to find the
116+
// current process's rank in the collective, the collective size,
117+
// and setup of send/receive buffer pairs.
118+
const int rank = atoi(getenv("RANK"));
119+
const int size = atoi(getenv("SIZE"));
120+
auto context = std::make_shared<gloo::rendezvous::Context>(rank, size);
121+
context->connectFullMesh(prefixStore, dev);
122+
123+
// All connections are now established. We can now initialize some
124+
// test data, instantiate the collective algorithm, and run it.
125+
size_t elements = 4;
126+
std::vector<int*> inputPointers;
127+
std::vector<int*> outputPointers;
128+
for (size_t i = 0; i < elements; i++) {
129+
int* value = reinterpret_cast<int*>(malloc(sizeof(int)));
130+
*value = i * (rank + 1);
131+
inputPointers.push_back(value);
132+
int* value1 = reinterpret_cast<int*>(malloc(sizeof(int)));
133+
*value1 = 0;
134+
outputPointers.push_back(value1);
135+
}
136+
137+
// Configure AllreduceOptions struct
138+
gloo::AllreduceOptions opts_(context);
139+
opts_.setInputs(inputPointers, 1);
140+
opts_.setOutputs(outputPointers, 1);
141+
opts_.setAlgorithm(gloo::AllreduceOptions::Algorithm::RING);
142+
void (*fn)(void*, const void*, const void*, int) = &mysum;
143+
opts_.setReduceFunction(fn);
144+
gloo::allreduce(opts_);
145+
146+
// Print the result.
147+
std::cout << "Output: " << std::endl;
148+
for (int i = 0; i < outputPointers.size(); i++) {
149+
std::cout << "data[" << i << "] = " << *outputPointers[i] << std::endl;
150+
}
151+
152+
return 0;
153+
}

gloo/examples/example_reduce.cc

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*/
4+
5+
#include <cstdio>
6+
#include <iostream>
7+
#include <memory>
8+
#include <string>
9+
10+
#include "gloo/reduce.h"
11+
#include "gloo/rendezvous/context.h"
12+
#include "gloo/rendezvous/file_store.h"
13+
#include "gloo/rendezvous/prefix_store.h"
14+
#include "gloo/transport/uv/device.h"
15+
16+
// Usage:
17+
//
18+
// Open two terminals. Run the same program in both terminals, using
19+
// a different RANK in each. For example:
20+
//
21+
// A: PREFIX=test1 SIZE=2 RANK=0 example_reduce
22+
// B: PREFIX=test1 SIZE=2 RANK=1 example_reduce
23+
//
24+
// Expected output:
25+
//
26+
// data[0] = 0
27+
// data[1] = 3
28+
// data[2] = 6
29+
// data[3] = 9
30+
//
31+
32+
void mysum(void* c_, const void* a_, const void* b_, int n) {
33+
printf("n=%d\r\n", n);
34+
int* c = static_cast<int*>(c_);
35+
const int* a = static_cast<const int*>(a_);
36+
const int* b = static_cast<const int*>(b_);
37+
for (auto i = 0; i < n; i++) {
38+
printf("a[%d]=%d\r\n", i, a[i]);
39+
printf("b[%d]=%d\r\n", i, b[i]);
40+
c[i] = a[i] + b[i];
41+
printf("c[%d]=%d\r\n", i, c[i]);
42+
}
43+
}
44+
45+
int main(void) {
46+
// Unrelated to the example: perform some sanity checks.
47+
if (getenv("PREFIX") == nullptr || getenv("SIZE") == nullptr ||
48+
getenv("RANK") == nullptr) {
49+
std::cerr << "Please set environment variables PREFIX, SIZE, and RANK."
50+
<< std::endl;
51+
return 1;
52+
}
53+
54+
// The following statement creates a TCP "device" for Gloo to use.
55+
// See "gloo/transport/device.h" for more information. For the
56+
// purposes of this example, it is sufficient to see the device as
57+
// a factory for every communication pair.
58+
//
59+
// The argument to gloo::transport::tcp::CreateDevice is used to
60+
// find the network interface to bind connection to. The attr struct
61+
// can be populated to specify exactly which interface should be
62+
// used, as shown below. This is useful if you have identical
63+
// multi-homed machines that all share the same network interface
64+
// name, for example.
65+
//
66+
gloo::transport::uv::attr attr;
67+
// attr.iface = "eth0";
68+
// attr.iface = "ib0";
69+
// attr.iface = "Wi-Fi";
70+
71+
// attr.ai_family = AF_INET; // Force IPv4
72+
// attr.ai_family = AF_INET6; // Force IPv6
73+
// Use either (default)
74+
attr.ai_family = AF_UNSPEC;
75+
76+
// A string is implicitly converted to an "attr" struct with its
77+
// hostname field populated. This will try to resolve the interface
78+
// to use by resolving the hostname or IP address, and finding the
79+
// corresponding network interface.
80+
//
81+
// Hostname "localhost" should resolve to 127.0.0.1, so using this
82+
// implies that all connections will be local. This can be useful
83+
// for single machine operation.
84+
//
85+
// auto dev = gloo::transport::tcp::CreateDevice("localhost");
86+
//
87+
88+
auto dev = gloo::transport::uv::CreateDevice(attr);
89+
90+
// Now that we have a device, we can connect all participating
91+
// processes. We call this process "rendezvous". It can be performed
92+
// using a shared filesystem, a Redis instance, or something else by
93+
// extending it yourself.
94+
//
95+
// See "gloo/rendezvous/store.h" for the functionality you need to
96+
// implement to create your own store for performing rendezvous.
97+
//
98+
// Below, we instantiate rendezvous using the filesystem, given that
99+
// this example uses multiple processes on a single machine.
100+
//
101+
auto fileStore = gloo::rendezvous::FileStore("/libtmp");
102+
103+
// To be able to reuse the same store over and over again and not have
104+
// interference between runs, we scope it to a unique prefix with the
105+
// PrefixStore. This wraps another store and prefixes every key before
106+
// forwarding the call to the underlying store.
107+
std::string prefix = getenv("PREFIX");
108+
auto prefixStore = gloo::rendezvous::PrefixStore(prefix, fileStore);
109+
110+
// Using this store, we can now create a Gloo context. The context
111+
// holds a reference to every communication pair involving this
112+
// process. It is used by every collective algorithm to find the
113+
// current process's rank in the collective, the collective size,
114+
// and setup of send/receive buffer pairs.
115+
const int rank = atoi(getenv("RANK"));
116+
const int size = atoi(getenv("SIZE"));
117+
auto context = std::make_shared<gloo::rendezvous::Context>(rank, size);
118+
context->connectFullMesh(prefixStore, dev);
119+
120+
// All connections are now established. We can now initialize some
121+
// test data, instantiate the collective algorithm, and run it.
122+
int* inputPointers = reinterpret_cast<int*>(malloc(sizeof(int) * 4));
123+
int* outputPointers = reinterpret_cast<int*>(malloc(sizeof(int) * 4));
124+
gloo::ReduceOptions opts(context);
125+
opts.setInput(inputPointers, 4);
126+
opts.setOutput(outputPointers, 4);
127+
for (int i = 0; i < 4; i++) {
128+
inputPointers[i] = i * (rank + 1);
129+
outputPointers[i] = 0;
130+
}
131+
132+
void (*fn)(void*, const void*, const void*, int) = &mysum;
133+
opts.setReduceFunction(fn);
134+
135+
// A small maximum segment size triggers code paths where we'll
136+
// have a number of segments larger than the lower bound of
137+
// twice the context size.
138+
opts.setMaxSegmentSize(128);
139+
opts.setRoot(size - 1);
140+
reduce(opts);
141+
142+
// Print the result.
143+
std::cout << "Output: " << std::endl;
144+
for (int i = 0; i < 4; i++) {
145+
std::cout << "data = " << outputPointers[i] << std::endl;
146+
}
147+
148+
return 0;
149+
}

0 commit comments

Comments
 (0)