Skip to content

Commit

Permalink
feat(test): parse as c++ source
Browse files Browse the repository at this point in the history
All included headers are fully safe to be parsed as C++, so these unit
tests can now use C++. Rename them to `.cc' files and use a few c++
keywords to enforce that the compiler is actually treating them as such.
Actual C++ cleanups to follow later, hopefully alongside a unit test
framework like gtest or catch2.

Signed-off-by: Nicholas Sielicki <nslick@amazon.com>
  • Loading branch information
Nicholas Sielicki committed Nov 7, 2024
1 parent 60ba4db commit 5243aa5
Show file tree
Hide file tree
Showing 18 changed files with 64 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/distcheck.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
- name: Install hwloc, utilities.
run: |
yum -y install hwloc-devel autoconf automake libtool gcc g++ git make
yum -y install hwloc-devel autoconf automake libtool gcc gcc-c++ git make
- name: Install CUDA
if: matrix.sdk == 'cuda'
Expand Down
21 changes: 19 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ NCCL_NET_OFI_DISTCHCK_CONFIGURE_FLAGS=
# Checks for programs
AC_PROG_CC
AM_PROG_CC_C_O
AC_PROG_CXX
m4_version_prereq([2.70], [], [AC_PROG_CC_STDC])
AC_PROG_CXX
AC_C_INLINE

dnl ac_check_enable_debug will make sure AC_PROG_CC doesn't add a
Expand All @@ -55,12 +57,18 @@ dnl between this and AC_PROG_CC.
AS_IF([test "${ax_enable_debug}" = "no"], [
dnl Enable O3 optimization
CFLAGS="${CFLAGS} -O3"
CXXFLAGS="${CXXFLAGS} -O3"
dnl use C++17, disable exceptions, and disable runtime type information
CXXFLAGS="-std=c++17 -fno-rtti -fno-exceptions"
dnl dead code elim
CFLAGS="${CFLAGS} -ffunction-sections -fdata-sections"
CXXFLAGS="${CXXFLAGS} -ffunction-sections -fdata-sections"
dnl https://maskray.me/blog/2021-05-09-fno-semantic-interposition
CFLAGS="${CFLAGS} -fno-semantic-interposition -fvisibility=hidden"
CXXFLAGS="${CXXFLAGS} -fno-semantic-interposition -fvisibility=hidden "
LDFLAGS="${LDFLAGS} -Bsymbolic"
])
CHECK_ENABLE_SANITIZER()
Expand Down Expand Up @@ -175,10 +183,15 @@ AS_IF([test "${enable_trace}" = "yes" ],
AC_DEFINE_UNQUOTED([OFI_NCCL_TRACE], [${trace}], [Defined to 1 unit test output should include TRACE level])

picky_cflags=""
picky_cxxflags=""
AC_DEFUN([ADD_PICKY_FLAGS],[
AC_LANG_PUSH([C])
AX_CHECK_COMPILE_FLAG([$1], [picky_cflags="${picky_cflags} $1"], [], [-Werror])
AC_LANG_POP()
AC_LANG_PUSH([C++])
AX_CHECK_COMPILE_FLAG([$1], [picky_cxxflags="${picky_cxxflags} $1"], [], [-Werror -x c++])
AC_LANG_POP()
])

dnl standard and normal
Expand Down Expand Up @@ -220,17 +233,21 @@ AC_ARG_ENABLE([picky-compiler],
AS_IF([test "${enable_picky_compiler}" != "no"],
[AC_MSG_NOTICE([Adding ${picky_cflags} to CFLAGS.])
CFLAGS="${CFLAGS} ${picky_cflags}"
CXXFLAGS="${CXXFLAGS} ${picky_cxxflags}"
AS_UNSET([picky_cxxflags])
AS_UNSET([picky_cflags])])

werror_flags="-Werror"
AC_ARG_ENABLE([werror],
[AS_HELP_STRING([--enable-werror], [(Developer) Enable setting -Werror. Off by default, unless building from Git tree.])])
AS_IF([test -d "${srcdir}/.git" -a -z "${enable_werror}"],
[AC_MSG_NOTICE([Found .git directory. Adding ${werror_flags} to CFLAGS.])
CFLAGS="${werror_flags} ${CFLAGS}"],
CXXFLAGS="${werror_flags} ${CXXFLAGS} "
CFLAGS="${werror_flags} ${CFLAGS} "],
[test "${enable_werror}" = "yes"],
[AC_MSG_NOTICE([Adding ${werror_flags} to CFLAGS.])
CFLAGS="${werror_flags} ${CFLAGS}"])
CXXFLAGS="${werror_flags} ${CXXFLAGS} "
CFLAGS="${werror_flags} ${CFLAGS} "])

AC_SUBST([NCCL_NET_OFI_DISTCHCK_CONFIGURE_FLAGS])

Expand Down
3 changes: 3 additions & 0 deletions m4/check_pkg_mpi.m4
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ AC_DEFUN([CHECK_PKG_MPI], [
LDFLAGS="${MPI_LDFLAGS} ${LDFLAGS}"])
MPICC=${mpi_bindir}mpicc
MPICXX=${mpi_bindir}mpicxx
AC_MSG_CHECKING([for working mpicc])
${MPICC} --help >& AS_MESSAGE_LOG_FD
AS_IF([test $? -eq 0],
[AC_MSG_RESULT([yes])],
[AC_MSG_RESULT([no])
MPICC="${CC}"
MPICXX="${CXX}"
AS_IF([test "${check_pkg_found}" = "yes"],
[AC_CHECK_HEADERS([mpi.h], [], [check_pkg_found=no])])
Expand All @@ -49,6 +51,7 @@ AC_DEFUN([CHECK_PKG_MPI], [
[$2])
AC_SUBST([MPICC])
AC_SUBST([MPICXX])
AC_SUBST([MPI_CPPFLAGS])
AC_SUBST([MPI_LDFLAGS])
AC_SUBST([MPI_LIBS])
Expand Down
9 changes: 5 additions & 4 deletions tests/functional/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ AM_CPPFLAGS += $(MPI_CPPFLAGS) $(CUDA_CPPFLAGS)
AM_LDFLAGS = $(MPI_LDFLAGS) $(CUDA_LDFLAGS)
LDADD = $(top_builddir)/src/libinternal_net_plugin.la $(MPI_LIBS) $(CUDA_LIBS)
CC = $(MPICC)
CXX = $(MPICXX)

if ENABLE_FUNC_TESTS
noinst_HEADERS = test-common.h
noinst_HEADERS = test-common.hpp

bin_PROGRAMS = nccl_connection nccl_message_transfer ring

nccl_connection_SOURCES = nccl_connection.c
nccl_message_transfer_SOURCES = nccl_message_transfer.c
ring_SOURCES = ring.c
nccl_connection_SOURCES = nccl_connection.cc
nccl_message_transfer_SOURCES = nccl_message_transfer.cc
ring_SOURCES = ring.cc
endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "config.h"

#include "test-common.h"
#include "test-common.hpp"

int main(int argc, char* argv[])
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

#include "config.h"

#include "test-common.h"
#include "test-common.hpp"

#define PROC_NAME_IDX(i) (i * MPI_MAX_PROCESSOR_NAME)

int main(int argc, char* argv[])
{
ncclResult_t res = ncclSuccess;
int rank, proc_name_len, num_ranks = 0, local_rank = 0, peer_rank = 0;
int rank, proc_name_len, num_ranks = 0, peer_rank = 0;
/* Unused when trace prints are not enabled. */
[[maybe_unused]] int local_rank = 0;
int buffer_type = NCCL_PTR_HOST;
test_nccl_properties_t props = {};

Expand Down Expand Up @@ -390,7 +392,7 @@ int main(int argc, char* argv[])
MPI_Finalize();
NCCL_OFI_INFO(NCCL_NET, "Test completed successfully for rank %d", rank);

exit:;
exit:

ncclResult_t close_res = ncclSuccess;

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/ring.c → tests/functional/ring.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "config.h"

#include "test-common.h"
#include "test-common.hpp"

#define PROC_NAME_IDX(i) (i * MPI_MAX_PROCESSOR_NAME)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#include "nccl_ofi_math.h"
#include "nccl_ofi_param.h"


template <typename T>
constexpr T PROC_NAME_IDX(T i) { return i * MPI_MAX_PROCESSOR_NAME; };

#define STR2(v) #v
#define STR(v) STR2(v)

Expand Down
18 changes: 9 additions & 9 deletions tests/unit/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/include
AM_CPPFLAGS += -isystem $(abs_top_srcdir)/3rd-party/nccl/$(DEVICE_INTERFACE)/include
AM_CPPFLAGS += -isystem $(abs_top_srcdir)/3rd-party/uthash/include
LDADD = $(top_builddir)/src/libinternal_net_plugin.la
noinst_HEADERS = test-common.h
noinst_HEADERS = test-common.hpp

noinst_PROGRAMS = \
deque \
Expand All @@ -26,18 +26,18 @@ if WANT_PLATFORM_AWS
AM_CPPFLAGS += $(CUDA_CPPFLAGS)
LDADD += $(CUDA_LIBS)
noinst_PROGRAMS += show_tuner_decisions
show_tuner_decisions_SOURCES = show_tuner_decisions.c
show_tuner_decisions_SOURCES = show_tuner_decisions.cc
show_tuner_decisions_LDADD = $(top_builddir)/src/libinternal_tuner_plugin.la
endif
endif

idpool_SOURCES = idpool.c
deque_SOURCES = deque.c
freelist_SOURCES = freelist.c
msgbuff_SOURCES = msgbuff.c
scheduler_SOURCES = scheduler.c
ep_addr_list_SOURCES = ep_addr_list.c
mr_SOURCES = mr.c
idpool_SOURCES = idpool.cc
deque_SOURCES = deque.cc
freelist_SOURCES = freelist.cc
msgbuff_SOURCES = msgbuff.cc
scheduler_SOURCES = scheduler.cc
ep_addr_list_SOURCES = ep_addr_list.cc
mr_SOURCES = mr.cc

TESTS = $(noinst_PROGRAMS)
endif
2 changes: 1 addition & 1 deletion tests/unit/deque.c → tests/unit/deque.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <stdio.h>

#include "test-common.h"
#include "test-common.hpp"
#include "nccl_ofi_deque.h"

#define test_get_front(deque, expected) \
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/ep_addr_list.c → tests/unit/ep_addr_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <stdio.h>

#include "test-common.h"
#include "test-common.hpp"
#include "nccl_ofi_ep_addr_list.h"

static void insert_addresses(nccl_ofi_ep_addr_list_t *ep_addr_list, size_t num_addr, int ep_num)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/freelist.c → tests/unit/freelist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <stdio.h>

#include "test-common.h"
#include "test-common.hpp"
#include "nccl_ofi_freelist.h"

void *simple_base;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/idpool.c → tests/unit/idpool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <stdio.h>

#include "test-common.h"
#include "test-common.hpp"
#include "nccl_ofi_idpool.h"
#include "nccl_ofi_math.h"

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/mr.c → tests/unit/mr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <stdlib.h>

#include "test-common.h"
#include "test-common.hpp"
#include "nccl_ofi_mr.h"

static inline bool test_lookup_impl(nccl_ofi_mr_cache_t *cache, void *addr, size_t size,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/msgbuff.c → tests/unit/msgbuff.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "nccl_ofi_msgbuff.h"

#include "test-common.h"
#include "test-common.hpp"

int main(int argc, char *argv[])
{
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/scheduler.c → tests/unit/scheduler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include <nccl/net.h>

#include "nccl_ofi_log.h"
#include "test-common.hpp"
#include "nccl_ofi_scheduler.h"
#include "test-common.h"

static inline int verify_xfer_info(nccl_net_ofi_xfer_info_t *xfer, nccl_net_ofi_xfer_info_t *ref_xfer, int xfer_id)
{
Expand Down
File renamed without changes.
12 changes: 9 additions & 3 deletions tests/unit/test-common.h → tests/unit/test-common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
#ifndef TEST_COMMON_H_
#define TEST_COMMON_H_

#include "config.h"

#include <stdarg.h>
#include <stdio.h>

#include "nccl_ofi.h"
#include "nccl_ofi_log.h"

static inline void logger(ncclDebugLogLevel level, unsigned long flags, const char *filefunc,
int line, const char *fmt, ...)
namespace {

void logger(ncclDebugLogLevel level, unsigned long flags, const char *filefunc,
int line, char const *const fmt, ...)
{
va_list vargs;

Expand All @@ -24,7 +28,7 @@ static inline void logger(ncclDebugLogLevel level, unsigned long flags, const ch
printf("INFO: Function: %s Line: %d: ", filefunc, line);
break;
case NCCL_LOG_TRACE:
#if OFI_NCCL_TRACE
#if defined(OFI_NCCL_TRACE) && OFI_NCCL_TRACE
printf("TRACE: Function: %s Line: %d: ", filefunc, line);
break;
#else
Expand All @@ -46,4 +50,6 @@ static inline void logger(ncclDebugLogLevel level, unsigned long flags, const ch
#pragma GCC diagnostic pop
}

}

#endif // End TEST_COMMON_H_

0 comments on commit 5243aa5

Please sign in to comment.