我正在尝试测量folly哈希图中并发插入的性能。用于这种插入的程序的简化版本显示在此处:
#include <folly/concurrency/ConcurrentHashMap.h>
#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
const int kNumMutexLocks = 2003;
std::unique_ptr<std::mutex[]> mutices(new std::mutex[kNumMutexLocks]);
__inline__ void
concurrentInsertion(unsigned int threadId, unsigned int numInsertionsPerThread,
unsigned int numInsertions, unsigned int numUniqueKeys,
folly::ConcurrentHashMap<int, int> &follyMap) {
int base = threadId * numInsertionsPerThread;
for (int i = 0; i < numInsertionsPerThread; i++) {
int idx = base + i;
if (idx >= numInsertions)
break;
int val = idx;
int key = val % numUniqueKeys;
mutices[key % …Run Code Online (Sandbox Code Playgroud) 琐事
通常,当我想用 C++ 编写多线程程序时,我会询问硬件支持的并发线程数,如下所示:
unsigned int numThreads = std::thread::hardware_concurrency();
Run Code Online (Sandbox Code Playgroud)
这将返回支持的并发总数。因此,如果我们有 2 个 CPU,每个 CPU 都可以支持 12 个线程,numThreads则等于 24。
问题
最近我曾经numactl强制一个程序只在一个 CPU 上运行。
numactl -N 1 ./a.out
Run Code Online (Sandbox Code Playgroud)
问题是std::thread::hardware_concurrency()即使我使用numactl -N 1. 但是,在这种设置下,输出nproc为 12。
numactl -N 1 nproc --> output = 12
Run Code Online (Sandbox Code Playgroud)
题
也许std::thread::hardware_concurrency()不是为了支持这样的场景而设计的。那不是我关心的。我的问题是,什么是最好的做法来获得线程支持的数字时,我想我的运行与程序numactl。
更多信息
如果您还没有处理过numactl,它可以用于运行使用 NUMA 策略的进程。例如,您可以使用它来强制您的程序仅在一个 CPU 上运行。这种情况的用法如上所示。
我正在尝试研究SASS从非常基本的 CUDA 内核生成的文件。这是内核:
__global__ void kernel(const float * x,
float * y,
const uint num_rows,
const uint num_cols) {
const uint num_elems = num_rows * num_cols;
const uint tid = blockDim.x * blockIdx.x + threadIdx.x;
for (uint idx = tid; idx < num_elems; idx += blockDim.x * gridDim.x) {
y[idx] = x[idx];
}
}
Run Code Online (Sandbox Code Playgroud)
这是SASS文件。
1 00007f26 14f69f00 MOV R1, c[0x0][0x28]
2 00007f26 14f69f10 S2R R0, SR_CTAID.X
3 00007f26 14f69f20 ULDC.64 UR4, c[0x0][0x178]
4 00007f26 14f69f30 …Run Code Online (Sandbox Code Playgroud) 在C++ 11中,可以获得string变量的散列值,如下所示:
std::size_t h1 = std::hash<std::string>{}("Some_String");
Run Code Online (Sandbox Code Playgroud)
它干净而简单.但是,我有两个问题:
我们假设输入模板参数T可能有也可能没有内部变量bar。我正在尝试编写一个结构,当我们拥有它时返回值bar,当我们没有时返回一些常量。这是我的尝试:
struct A {
static constexpr unsgined int bar = 20;
hasBar = true;
};
struct B {
hasBar = false;
};
template <typename T, typename std::enable_if<T::hasBar, int>::type>
struct getBar {
static constexpr unsigned int bar = T::bar;
};
template <typename T, typename std::enable_if<!T::hasBar, int>::type>
struct getBar {
static constexpr unsigned int bar = 0;
};
int main() {
getBar<A>::bar; // Expect 20
getBar<B>::bar; //Expect 0
}
Run Code Online (Sandbox Code Playgroud)
我无法使用 C++14 编译此代码。编译器抱怨:“模板非类型参数具有不同的类型”。
为什么我们会出现这样的错误?我该如何解决它?
我正在尝试编写一个玩具示例来使用 Facebook 的Folly 库。该程序引入如下:
#include <utility>
#include <iostream>
#include <folly/Format.h>
#include <folly/futures/Future.h>
#include <folly/executors/ThreadedExecutor.h>
#include <folly/Uri.h>
#include <folly/FBString.h>
static void print_uri(const folly::fbstring &address)
{
const folly::Uri uri(address);
const auto authority = folly::format("The authority from {} is {}", uri.fbstr(), uri.authority());
std::cout << authority << std::endl;
}
int main()
{
folly::ThreadedExecutor executor;
folly::Promise<folly::fbstring> promise;
folly::Future<folly::fbstring> future = promise.getSemiFuture().via(&executor);
folly::Future<folly::Unit> unit = std::move(future).thenValue(print_uri);
promise.setValue("https://conan.io/");
std::move(unit).get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是我不确定编译这样的程序所需的库是什么。CMakeList.txt如果有人可以共享项目文件,我将非常感激Folly。
和之间的主要区别是显而易见的[link]。++varvar++
我的问题是关于参考文献的影响。这是详细信息:我参考cuDF::multimap了以下内容:
found = map->find(key)
Run Code Online (Sandbox Code Playgroud)
当我尝试增加该引用时,使用++found效果很好。但是,使用found++将返回以下警告:
警告:返回对局部变量的引用
我了解警告的含义。有人可以解释为什么我收到此警告吗?
更多细节
也就是说,以下代码段将生成上述警告。
found = map->find(key);
while (found != map->end() && found->first != unusedKey) {
std::cout << found->second << std::endl;
found++;
}
Run Code Online (Sandbox Code Playgroud)
但是,这不会产生任何警告:
found = map->find(key);
while (found != map->end() && found->first != unusedKey) {
std::cout << found->second << std::endl;
++found;
}
Run Code Online (Sandbox Code Playgroud)