从CMake Cookbook中,我看到我们可以使用该命令add_custom_command并add_custom_target在构建时运行自定义命令。有一个玩具示例,我想提取子目录中的压缩文件并将其链接到最终的可执行文件。我们有两个CMakeLists.txt文件,以下一个位于子目录中。
find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)
set(MATH_SRCS
${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxBLAS.cpp
${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxLAPACK.cpp
${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxBLAS.hpp
${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxLAPACK.hpp
)
add_custom_target(BLAS_LAPACK_wrappers
WORKING_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}
DEPENDS
${MATH_SRCS}
COMMENT
"Intermediate BLAS_LAPACK_wrappers target"
VERBATIM
)
add_custom_command(
OUTPUT
${MATH_SRCS}
COMMAND
${CMAKE_COMMAND} -E tar xzf ${CMAKE_CURRENT_SOURCE_DIR}/wrap_BLAS_LAPACK.tar.gz
WORKING_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/wrap_BLAS_LAPACK.tar.gz
COMMENT
"Unpacking C++ wrappers for BLAS/LAPACK"
)
add_library(math "")
target_sources(math
PRIVATE
${MATH_SRCS}
)
target_include_directories(math
INTERFACE
${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK
)
# BLAS_LIBRARIES are included in LAPACK_LIBRARIES
target_link_libraries(math
PUBLIC
${LAPACK_LIBRARIES}
)
Run Code Online (Sandbox Code Playgroud)
以下CMakeLists.txt是主目录中的内容。
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
# Fortran needed …Run Code Online (Sandbox Code Playgroud) 最近,我尝试学习如何在多个GPU上使用Tensorflow来加快训练速度。我找到了有关基于Cifar10数据集的训练分类模型的官方教程。但是,我发现本教程使用队列读取图像。出于好奇,我如何通过向Session输入价值来使用多个GPU?似乎很难解决将同一数据集的不同值提供给不同GPU的问题。谢谢大家!以下代码是官方教程的一部分。
images, labels = cifar10.distorted_inputs()
batch_queue = tf.contrib.slim.prefetch_queue.prefetch_queue(
[images, labels], capacity=2 * FLAGS.num_gpus)
# Calculate the gradients for each model tower.
tower_grads = []
with tf.variable_scope(tf.get_variable_scope()):
for i in xrange(FLAGS.num_gpus):
with tf.device('/gpu:%d' % i):
with tf.name_scope('%s_%d' % (cifar10.TOWER_NAME, i)) as scope:
# Dequeues one batch for the GPU
image_batch, label_batch = batch_queue.dequeue()
# Calculate the loss for one tower of the CIFAR model. This function
# constructs the entire CIFAR model but shares the variables across
# all towers.
loss …Run Code Online (Sandbox Code Playgroud) 最近学习了如何使用CMake中的生成器表达式来做条件链接和条件定义。
但是,在我看来,我也可以使用 CMake 中的流控制语法来做同样的事情。不知道CMake中生成器表达式的优点是什么。例如,我可以使用以下生成器表达式语法。
$<STREQUAL:string1,string2>
$<EQUAL:number1,number2>
$<VERSION_EQUAL:version1,version2>
$<VERSION_GREATER:version1,version2>
$<VERSION_LESS:version1,version2>
Run Code Online (Sandbox Code Playgroud)
但我也可以使用if() elseif() endif()来达到相同的目标。非常感谢。
我尝试使用 GPU 来加速我的程序,该程序计算两个浮点数组之间的 L2 距离。为了检查计算精度,我编写了CUDA程序和CPU程序。但是我发现总误差有200多,我不明白。我在这两种情况下都使用 float 类型,我相信我应该得到相同的结果。我的代码如下。
#include <cuda_runtime.h>
#include <stdio.h>
#include <sys/time.h>
#include <math.h>
// #include <helper_functions.h>
#define VECTORDIM 3
double cpuSecond()
{
struct timeval tp;
gettimeofday(&tp, NULL);
return ((double) tp.tv_sec + (double)tp.tv_usec*1e-6);
}
void DistanceCPU(float* array1, float* array2, int narray1, int narray2, float* output)
{
float temp;
for (int i = 0; i < narray1; i++)
{
for (int j = 0; j < narray2; j++)
{
temp = 0;
for (int l = 0; l < VECTORDIM; …Run Code Online (Sandbox Code Playgroud) 我不明白 omp_get_num_threads() 和 omp_get_max_threads() 之间的区别。我复制了演示代码如下。
omp_set_nested(1);
omp_set_max_active_levels(10);
omp_set_dynamic(0);
omp_set_num_threads(2);
#pragma omp parallel
{
omp_set_num_threads(3);
#pragma omp parallel
{
omp_set_num_threads(4);
#pragma omp single
{
std::cout << omp_get_max_active_levels() << " " << omp_get_num_threads() << " "
<< omp_get_max_threads() << std::endl;
}
}
#pragma omp barrier
#pragma omp single
{
std::cout << omp_get_max_active_levels() << " " << omp_get_num_threads() << " "
<< omp_get_max_threads() << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我得到以下输出。
10 3 4
10 3 4
10 3 4
10 3 3
Run Code Online (Sandbox Code Playgroud)
我查了官方文档,但还是很困惑。
我已经检查了有关c ++ 17下文件系统链接的很多问题,但仍然无法成功建立链接。我的main.cpp文件如下。
#include <experimental/filesystem>
int main(int argc, char** argv)
{
std::string imageDirectory = "./image";;
std::vector<std::string> imagePath;
for (const auto& entry: std::filesystem::directory_iterator(imageDirectory))
{
imagePath.push_back(entry.path());
std::cout << entry.path() << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我CMakeLists.txt的如下。
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(visual_hull LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
add_library(dataIO
STATIC
dataIO.hpp
dataIO.cpp)
find_package(OpenCV REQUIRED core highgui imgproc)
target_link_libraries(dataIO ${OpenCV_LIBS})
add_executable(visual_hull main.cpp)
target_link_libraries(visual_hull PUBLIC dataIO
stdc++fs)
Run Code Online (Sandbox Code Playgroud)
错误如下。
/home/SENSETIME/zhangshunkang/Downloads/programming/c++/visual_hull/main.cpp: In function ‘int main(int, char**)’:
/home/SENSETIME/zhangshunkang/Downloads/programming/c++/visual_hull/main.cpp:15:31: error: ‘std::filesystem’ has not been declared
for (const auto& …Run Code Online (Sandbox Code Playgroud) c++ ×3
cmake ×3
c++17 ×1
cuda ×1
distributed ×1
expression ×1
openmp ×1
precision ×1
python ×1
tensorflow ×1