Skip to content

Commit d436297

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 b67ecd8 commit d436297

File tree

6 files changed

+528
-1
lines changed

6 files changed

+528
-1
lines changed

.github/config/lint/.clang-format

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
Language: Cpp
3+
PointerAlignment: Left
4+
SkipMacroDefinitionBody: true

gloo/examples/.clang-format

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
Language: Cpp
3+
PointerAlignment: Left

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

0 commit comments

Comments
 (0)