Skip to content

Commit cd3c648

Browse files
Replace cout usages with logger calls
1 parent 1519c84 commit cd3c648

File tree

7 files changed

+133
-96
lines changed

7 files changed

+133
-96
lines changed

gloo/allreduce_bcube.cc

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "gloo/allreduce_bcube.h"
2+
3+
#include <string_view>
4+
5+
#include "gloo/common/log.h"
6+
7+
namespace gloo {
8+
9+
template <typename T>
10+
void AllreduceBcube<T>::printElems(T* p, int count, int start) {
11+
/* Early return if log level is not high enough, to prevent expensive code
12+
* running. */
13+
if (!spdlog::should_log(spdlog::level::trace))
14+
return;
15+
16+
const std::size_t alignedStart = (start / wordsPerLine) * wordsPerLine;
17+
fmt::memory_buffer line{};
18+
19+
/* Logs/flushes the line buffer - starting a new line */
20+
auto printLine = [&]() {
21+
if (!line.size())
22+
return;
23+
std::string_view sv{line.data(), line.size()};
24+
GLOO_TRACE("{}", sv);
25+
line.clear();
26+
};
27+
28+
for (std::size_t x = alignedStart; x < start + count; ++x) {
29+
if (x % wordsPerLine == 0) {
30+
if (x != alignedStart)
31+
printLine();
32+
fmt::format_to(
33+
std::back_inserter(line), "{} {:05}: ", fmt::ptr(&p[x]), x);
34+
} else if (x % wordsPerSection == 0) {
35+
fmt::format_to(std::back_inserter(line), "- ");
36+
}
37+
38+
if (x < start)
39+
fmt::format_to(std::back_inserter(line), "..... ");
40+
else
41+
fmt::format_to(std::back_inserter(line), "{:05} ", p[x]);
42+
}
43+
printLine();
44+
}
45+
46+
template <typename T>
47+
void AllreduceBcube<T>::printStageBuffer(const std::string& msg) {
48+
if (printCheck(myRank_)) {
49+
GLOO_TRACE("rank ({}) {}:", myRank_, msg);
50+
printElems(&ptrs_[0][0], totalNumElems_);
51+
}
52+
}
53+
54+
template <typename T>
55+
void AllreduceBcube<T>::printStepBuffer(
56+
const std::string& stage,
57+
int step,
58+
int srcRank,
59+
int destRank,
60+
T* p,
61+
int count,
62+
int start) {
63+
if (printCheck(myRank_)) {
64+
GLOO_TRACE(
65+
"{}: step ({}) srcRank ({}) -> destRank ({})",
66+
stage,
67+
step,
68+
srcRank,
69+
destRank);
70+
printElems(p, count, start);
71+
}
72+
}
73+
74+
} // namespace gloo

gloo/allreduce_bcube.h

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313
#include <string.h>
1414
#include <algorithm>
1515
#include <cstring>
16-
#include <iomanip>
17-
#include <iostream>
1816
#include <memory>
1917
#include <string>
2018
#include <unordered_map>
2119
#include <vector>
2220

2321
#include "gloo/algorithm.h"
24-
#include "gloo/common/error.h"
2522
#include "gloo/context.h"
2623

2724
/**
@@ -537,48 +534,18 @@ class AllreduceBcube : public Algorithm {
537534
static bool printCheck(int /*rank*/) {
538535
return false;
539536
}
540-
/**
541-
* Prints a break given the offset of an element about to be printed
542-
* @param p Pointer to the elements
543-
* @param x The current offset to the pointer to words
544-
*/
545-
static void printBreak(T* p, int x) {
546-
if (0 == x % wordsPerLine) {
547-
std::cout << std::endl
548-
<< &p[x] << " " << std::setfill('0') << std::setw(5) << x
549-
<< ": ";
550-
} else if (0 == x % wordsPerSection) {
551-
std::cout << "- ";
552-
}
553-
}
554537
/**
555538
* Pretty prints a list of elements
556539
* @param p Pointer to the elements
557540
* @param count The number of elements to be printed
558541
* @param start The offset from which to print
559542
*/
560-
static void printElems(T* p, int count, int start = 0) {
561-
auto alignedStart = (start / wordsPerLine) * wordsPerLine;
562-
for (int x = alignedStart; x < start + count; ++x) {
563-
printBreak(p, x);
564-
if (x < start) {
565-
std::cout << "..... ";
566-
} else {
567-
std::cout << std::setfill('0') << std::setw(5) << p[x] << " ";
568-
}
569-
}
570-
}
543+
static void printElems(T* p, int count, int start);
571544
/**
572545
* Prints contents in the ptrs array at a particular stage
573546
* @param msg Custom message to be printed
574547
*/
575-
void printStageBuffer(const std::string& msg) {
576-
if (printCheck(myRank_)) {
577-
std::cout << "rank (" << myRank_ << ") " << msg << ": ";
578-
printElems(&ptrs_[0][0], totalNumElems_);
579-
std::cout << std::endl;
580-
}
581-
}
548+
void printStageBuffer(const std::string& msg);
582549

583550
/**
584551
* Prints specified buffer during a step
@@ -596,14 +563,7 @@ class AllreduceBcube : public Algorithm {
596563
int destRank,
597564
T* p,
598565
int count,
599-
int start = 0) {
600-
if (printCheck(myRank_)) {
601-
std::cout << stage << ": step (" << step << ") " << "srcRank (" << srcRank
602-
<< ") -> " << "destRank (" << destRank << "): ";
603-
printElems(p, count, start);
604-
std::cout << std::endl;
605-
}
606-
}
566+
int start = 0);
607567
/**
608568
* Get all the peers of node with specified rank
609569
* @param rank Rank of the node for which peers are needed

gloo/cuda_allreduce_bcube.cc

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99
#include "gloo/cuda_allreduce_bcube.h"
1010

11+
#include "gloo/common/log.h"
1112
#include "gloo/cuda_collectives_device.h"
1213
#include "gloo/cuda_collectives_host.h"
1314
#include "gloo/cuda_private.h"
1415

1516
#include <sstream>
17+
#include <string_view>
1618
#ifndef _WIN32
1719
#include <unistd.h>
1820
#endif
@@ -226,35 +228,48 @@ bool CudaAllreduceBcube<T, W>::printCheck(int /* rank */) {
226228
return false;
227229
}
228230

229-
template <typename T, typename W>
230-
void CudaAllreduceBcube<T, W>::printBreak(T* p, int x) {
231-
if (0 == x % wordsPerLine) {
232-
std::cout << std::endl
233-
<< &p[x] << " " << std::setfill('0') << std::setw(5) << x << ": ";
234-
} else if (0 == x % wordsPerSection) {
235-
std::cout << "- ";
236-
}
237-
}
238-
239231
template <typename T, typename W>
240232
void CudaAllreduceBcube<T, W>::printElems(T* p, int count, int start) {
241-
auto alignedStart = (start / wordsPerLine) * wordsPerLine;
242-
for (int x = alignedStart; x < start + count; ++x) {
243-
printBreak(p, x);
244-
if (x < start) {
245-
std::cout << "..... ";
246-
} else {
247-
std::cout << std::setfill('0') << std::setw(5) << p[x] << " ";
233+
/* Early return if log level is not high enough, to prevent expensive code
234+
* running. */
235+
if (!spdlog::should_log(spdlog::level::trace))
236+
return;
237+
238+
const std::size_t alignedStart = (start / wordsPerLine) * wordsPerLine;
239+
fmt::memory_buffer line{};
240+
241+
/* Logs/flushes the line buffer - starting a new line */
242+
auto printLine = [&]() {
243+
if (!line.size())
244+
return;
245+
std::string_view sv{line.data(), line.size()};
246+
GLOO_TRACE("{}", sv);
247+
line.clear();
248+
};
249+
250+
for (std::size_t x = alignedStart; x < start + count; ++x) {
251+
if (x % wordsPerLine == 0) {
252+
if (x != alignedStart)
253+
printLine();
254+
fmt::format_to(
255+
std::back_inserter(line), "{} {:05}: ", fmt::ptr(&p[x]), x);
256+
} else if (x % wordsPerSection == 0) {
257+
fmt::format_to(std::back_inserter(line), "- ");
248258
}
259+
260+
if (x < start)
261+
fmt::format_to(std::back_inserter(line), "..... ");
262+
else
263+
fmt::format_to(std::back_inserter(line), "{:05} ", p[x]);
249264
}
265+
printLine();
250266
}
251267

252268
template <typename T, typename W>
253269
void CudaAllreduceBcube<T, W>::printStageBuffer(const std::string& msg) {
254270
if (printCheck(myRank_)) {
255-
std::cout << "rank (" << myRank_ << ") " << msg << ": ";
271+
GLOO_TRACE("rank ({}) {}:", myRank_, msg);
256272
printElems(&scratch_[0], totalNumElems_);
257-
std::cout << std::endl;
258273
}
259274
}
260275

@@ -268,10 +283,13 @@ void CudaAllreduceBcube<T, W>::printStepBuffer(
268283
int count,
269284
int start) {
270285
if (printCheck(myRank_)) {
271-
std::cout << stage << ": step (" << step << ") " << "srcRank (" << srcRank
272-
<< ") -> " << "destRank (" << destRank << "): ";
286+
GLOO_TRACE(
287+
"{}: step ({}) srcRank ({}) -> destRank ({}):",
288+
stage,
289+
step,
290+
srcRank,
291+
destRank);
273292
printElems(p, count, start);
274-
std::cout << std::endl;
275293
}
276294
}
277295

gloo/transport/buffer.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@ namespace transport {
1616
class Buffer {
1717
public:
1818
explicit Buffer(int slot, void* ptr, size_t size)
19-
: slot_(slot), ptr_(ptr), size_(size), debug_(false) {}
19+
: slot_(slot), ptr_(ptr), size_(size) {}
2020
virtual ~Buffer() = 0;
2121

22-
virtual void setDebug(bool debug) {
23-
debug_ = debug;
24-
}
25-
2622
virtual void send(size_t offset, size_t length, size_t roffset = 0) = 0;
2723

2824
// Send entire buffer by default
@@ -37,7 +33,6 @@ class Buffer {
3733
int slot_;
3834
void* ptr_;
3935
size_t size_;
40-
bool debug_;
4136
};
4237

4338
} // namespace transport

gloo/transport/ibverbs/buffer.cc

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,7 @@ void Buffer::send(size_t offset, size_t length, size_t roffset) {
189189

190190
checkErrorState();
191191

192-
if (debug_) {
193-
std::cout << "[" << getpid() << "] ";
194-
std::cout << "send " << length << " bytes";
195-
std::cout << std::endl;
196-
}
192+
GLOO_TRACE("send {} bytes", length);
197193

198194
// Increment number of sends in flight
199195
sendPending_++;
@@ -208,19 +204,11 @@ void Buffer::handleCompletion(int rank, struct ibv_wc* wc) {
208204
std::unique_lock<std::mutex> lock(m_);
209205

210206
if (wc->opcode & IBV_WC_RECV) {
211-
if (debug_) {
212-
std::cout << "[" << getpid() << "] ";
213-
std::cout << "recv " << wc->byte_len << " bytes";
214-
std::cout << std::endl;
215-
}
207+
GLOO_TRACE("recv {} bytes", wc->byte_len);
216208
recvCompletions_++;
217209
recvCv_.notify_one();
218210
} else if (wc->opcode == IBV_WC_RDMA_WRITE) {
219-
if (debug_) {
220-
std::cout << "[" << getpid() << "] ";
221-
std::cout << "send complete";
222-
std::cout << std::endl;
223-
}
211+
GLOO_TRACE("send complete");
224212
sendCompletions_++;
225213
sendPending_--;
226214
sendCv_.notify_one();

gloo/transport/tcp/buffer.cc

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
#include "gloo/transport/tcp/buffer.h"
10-
119
#include <string.h>
1210
#include <sys/syscall.h>
1311
#include <unistd.h>
14-
1512
#include <iostream>
1613

17-
#include "gloo/common/error.h"
1814
#include "gloo/common/enforce.h"
15+
#include "gloo/common/error.h"
16+
#include "gloo/common/log.h"
17+
#include "gloo/transport/tcp/buffer.h"
1918

2019
namespace gloo {
2120
namespace transport {
@@ -126,12 +125,7 @@ void Buffer::send(size_t offset, size_t length, size_t roffset) {
126125
// to support this.
127126
GLOO_ENFORCE_LE(offset + length, size_);
128127

129-
if (debug_) {
130-
std::cout << "[" << getpid() << ": " << syscall(SYS_gettid) << "] ";
131-
std::cout << "send " << length << " bytes";
132-
std::cout << " to " << pair_->peer().str();
133-
std::cout << std::endl;
134-
}
128+
GLOO_TRACE("send {} bytes to {}", length, pair_->peer().str());
135129

136130
op.preamble.nbytes = sizeof(op.preamble) + length;
137131
op.preamble.opcode = Op::SEND_BUFFER;

gloo/types.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,12 @@ inline float cpu_half2float(float16 h) {
339339
return *(float*)rp;
340340
}
341341

342+
/**
343+
* @brief Format helper for spdlog/fmt - convert to a float32 and format as
344+
* usual
345+
*/
346+
inline float format_as(float16 v) {
347+
return cpu_half2float(v);
348+
}
349+
342350
} // namespace gloo

0 commit comments

Comments
 (0)