Skip to content

NVSL/cpp-common

Repository files navigation

CircleCI

Common macros and functions for C++

Header only collection of common macro functionality for C++ 20.

Usage

Clone (as a submodule or otherwise):

git submodule add [email protected]:nvsl/cpp-common.git vendor/cpp-common

Add the directory as a include directory:

g++ -iquote"vendor/cpp-common/include" main.cc -o main

PMemOps: CLWB, CLFLUSHOPT, CLFLUSH and MSYNC

PMemOps provides convinient functions to flush and drain arbitrary sized address range.

Flush and drain using pmemops

#include "nvsl/pmemops.hh"

int main() {
    nvsl::PmemOps *pmemops = new nvsl::PmemOpsClwb();
    
    /* uint64_t *val = pmalloc(sizeof(uint64_t)); */
    
    // CLWB
    pmemops->flush(val, sizeof(*val));
    
    // SFENCE
    pmemops->drain();
}

Streaming writes using pmemops

PMemOps supports streaming writes (NT stores) to perform memcpy.
streaming_wr() automatically selects the right instruction depending on the number of bytes being copied.

#include "nvsl/pmemops.hh"

int main() {
    nvsl::PmemOps *pmemops = new nvsl::PmemOpsClwb();
    
    /* 
     * uint64_t *src = pmalloc(100 * sizeof(uint64_t)); 
     * uint64_t *dst = pmalloc(100 * sizeof(uint64_t)); 
     */
    
    // NT stores to do memcpy
    pmemops->streaming_wr(dst, src, 100*sizeof(uint64_t));
}

String operations

Use the files in your project:

#include "nvsl/string.hh"

split()

for (const auto tok : split("Hello! World.", " ")) {
    std::cout << tok << std::endl;
}

Prints:

Hello!
World

zip(): join vector of string using a token

std::cout << zip({"1", "2", "3"}, ", ")) << std::endl;

Prints:

1, 2, 3

S(): String constructor

Convert char*, numbers or floats to std::string:

using namespace nvsl;
char *buf = "some string";

std::string buf_str = S(buf);
std::string num = S(1);

Clock

Simple timing benchmark

void do_something() {
    nvsl::Clock clk;

    clk.tick();
    sleep(1);
    clk.tock();
    
    clk.reconcile();
    
    std::cout << "Execution summary" << clk.summarize() << std::endl;
}

Some of the utilities also have a C interface

#include "nvsl/c-common.h"

Available functions

void nvsl_fatal(const char *fmt, ...);
int nvsl_is_log_enabled(int check_lvl);
int nvsl_log(const int lvl, const char *fmt, ...);

Available files

Supported operations

Docs available at https://nvsl.github.io/cpp-common/.