Bringing Existing Code to CUDA Using constexpr and std::pmrAllocation // cpu std::vectorx(N); std::vector y(N); // … // gpu // ??? // ??? // … 15 |• Added in C++17: • std::pmr::memory_resource • std::pmr::polymorphic_allocator • std::pmr::vector std::pmr::vector • std::pmr::monotonic_buffer_resource • … std::pmr 16 |// gpu unified_memory_resource mem; std::pmr::vector x(N, &mem); std::pmr::vector y(N, &mem); // … Memory Allocation 17 |struct rce : std::pmr::memory_resource { void* do_allocate(std::size_t, std::size_t); void do_deallocate( void* p, std::size_t, std::size_t); bool do_is_equal( const std::pmr::memory_resource& 0 码力 | 51 页 | 3.68 MB | 6 月前3
C++23: An Overview of Almost All New and Updated FeaturesString Formatting Improvements Standard Library Modules std::flat_(multi)map / std::flat_(multi)set std::mdspan std::generator basic_string(_view)::contains() Construct string(_view) Operations for std::optional Stacktrace Library Changes to Ranges Library Changes to Views Library std::expected std::move_only_function<> std::spanstream std::byteswap() std::to_underlying() String Formatting Improvements Standard Library Modules std::flat_(multi)map / std::flat_(multi)set std::mdspan std::generator basic_string(_view)::contains() Construct string(_view)0 码力 | 105 页 | 759.96 KB | 6 月前3
C++高性能并行编程与优化 - 课件 - 11 现代 CMake 进阶指南make 、 MSBuild )称为本地构建系统( native buildsystem )。 • 负责从 CMakeLists.txt 生成本地构建系统构建规则文件的,称为生成器( generator )。 -G 选项:指定要用的生成器 • Linux 系统上的 CMake 默认用是 Unix Makefiles 生成器; Windows 系统默认是 Visual Studio 2019 常见误区:小彭老师,我手动添加 -std=c++17 行不行? • 请勿直接修改 CMAKE_CXX_FLAGS 来添加 -std=c++17 (你在百度 CSDN 学到的用 法)。 • 请使用 CMake 帮你封装好的 CMAKE_CXX_STANDARD (从业人员告诉你的正确用 法)。 • 为什么百度不对:你 GCC 用户手动指定了 -std=c++17 ,让 MSVC 的用户怎么办? 的用户怎么办? • 此外 CMake 已经自动根据 CMAKE_CXX_STANDARD 的默认值 11 添加 -std=c++11 选项了,你再添加个 -std=c++17 选项不就冲突了吗?所以请用 CMAKE_CXX_STANDARD 。 https://crascit.com/2015/03/28/enabling-cxx11-in-cmake/ project 的初始化: VERSION0 码力 | 166 页 | 6.54 MB | 1 年前3
C++20: An (Almost) Complete OverviewThe C++20 Synchronization Library Semaphores, efficient atomic waiting, latches, and barriers std::atomic_ref Designated Initializers Spaceship Operator <=> Range-based for Loop Initializer & Timezones std::span Feature Test Macros Immediate Functions – consteval constinit Class Enums and using Directive Text Formatting Math Constants std::source_location { return GetWelcomeHelper(); } } Consume a module: // main.cpp import cppcon; int main() { std::cout << CppCon::GetWelcome(); }9 Modules C++20 doesn’t specify if and how to modularize the 0 码力 | 85 页 | 512.18 KB | 6 月前3
Working with Asynchrony Generically: A Tour of C++ Executorsan event loop, portable access to the system execution context, nursery for spawned work4 P2300: STD::EXECUTION Proposes: • A set of concepts that represent: • A handle to a compute resource (aka, coroutines5 Example 1: Launching concurrent work6 EXAMPLE: LAUNCHING CONCURRENT WORK namespace ex = std::execution; int compute_intensive(int); int main() { unifex::static_thread_pool pool{8}; ex::scheduler ex::then(ex::schedule(sched), [] { return compute_intensive(2); }) ); auto [a, b, c] = std::this_thread::sync_wait( std::move(work) ).value(); } Launch three tasks to execute concurrently on a custom0 码力 | 121 页 | 7.73 MB | 6 月前3
C++高性能并行编程与优化 - 课件 - 02 现代 C++ 入门:RAII 内存管理https://zhuanlan.zhihu.com/p/350136757 未来: C++20 允许函数参数为自动推断( auto ) 未来: C++20 引入协程( coroutine )和生成器( generator ) 未来: C++20 标准库加入 format 支持 跑远了! • 鉴于 C++20 还没有普遍落地(例如 CMake 不支持 C++20 modules )因此我们的课程 基于 C++17 explicit 表示必须用 () 强制转换。 • 否则 show(80) 也能编译通过! • 所以,如果你不希望这种隐式转换, • 请给单参数的构造函数加上 explicit 。 • 比如 std::vector 的构造函数 vector(size_t n) 也是 explicit 的。 explicit 对多个参数也起作用! • 多个参数时, explicit 的作用体现在禁止 从一个 • void *p{}; • 与 • int x{0}; • void *p{nullptr}; • 等价,都会零初始化。但是你不写那个空括号就会 变成内存中随机的值。 • 再比如: std::cout << int{}; 会打印出 0 编译器默认生成的构造函数:初始化列表(感谢 C++11 ) • 当一个类(和他的基类)没有定义任何构造函 数,这时编译器会自动生成一个参数个数和成0 码力 | 96 页 | 16.28 MB | 1 年前3
Making Libraries Consumable for Non-C++ DevelopersModel (COM) – 1993 Foreign function interface (libffi) – 1996 Simplified Wrapper and Interface Generator (SWIG) – 1996 JVM – Java Native Interface (JNI) – 1997 .NET – Platform Invoke (P/Invoke), COM interop open_device(std::wstring_view const dev); The types char and wchar_t do not indicate encoding. The size of wchar_t: • Windows, sizeof(wchar_t) == 2 • Non-Windows, sizeof(wchar_t) == 4 std::basic_stringhttps://gchandbook.org/ Garbage collection is really “automatic memory management”. - Reference counted - C++ – std::shared_ptr - Python - Objective-C (manual or automatic – see ARC) - Swift - COM – AddRef()/Release() 0 码力 | 29 页 | 1.21 MB | 6 月前3
C++高性能并行编程与优化 - 课件 - 05 C++11 开始的多线程编程,没有类型区分,导致很容易弄错单位,混淆时间点和时间段。 • 比如 t0 * 3 ,乘法对时间点而言根本是个无意义的计算,然而 C 语言把他们看做一样的 long 类型,从而容易让程序员犯错。 C++11 引入的时间标准库: std::chrono • 利用 C++ 强类型的特点,明确区分时间点与时间段,明确区分不同的时间单位。 • 时间点例子: 2022 年 1 月 8 日 13 点 07 分 10 秒 • 时间段例子: 省略不写就是秒, std::milli 就是毫秒, std::micro 就是微秒 seconds 是 duration的类型别名 milliseconds 是 duration std::milli> 的类型别名 这里我们创建了 double_ms 作为 duration std::milli> 的别名 跨平台的 sleep : std::this_thread::sleep_for std::this_thread::sleep_for • 可以用 std::this_thread::sleep_for 替代 Unix 类操作系统专有的的 usleep 。他可 以让当前线程休眠一段时间,然后继续。 • 而且单位也可以自己指定,比如这里是 milliseconds 表示毫秒,也可以换成 microseconds 表示微秒, seconds 表示 秒, chrono 的强类型让单位选择更自由 0 码力 | 79 页 | 14.11 MB | 1 年前3
C++高性能并行编程与优化 - 课件 - 03 现代 C++ 进阶:模板元编程以省略该模板参数。自动根据调用者的参 数判断。 模板函数:特化的重载 • 有时候,一个统一的实现(比如 t * 2 )满 足不了某些特殊情况。比如 std::string 就 不能用乘法来重复,这时候我们需要用 t + t 来替代,怎么办呢? • 没关系,只需添加一个 twice(std::string) 即可,他会自动和已有的模板 twice(T) 之间相互重载。 模板函数:特化的重载(续) • 但是这样也有一个问题,那就是如果我用 twice(“hello”) 这样去调用,他不会自动隐 式转换到 std::string 并调用那个特化函数 ,而是会去调用模板函数 twice (“hello”) ,从而出错。 • 可能的解决方案: SFINAE 。 模板函数:默认参数类型 • 但是如果模板类型参数 T 没有出现在函数 的参数中,那么编译器就无法推断,就不 sumto 和 sumto 。前者保 留了调试用的打印语句,后者则完全为性 能优化而可以去掉打印语句。 • 后者其实在编译器看来就是 • if (false) std::cout << ... • 这样显然是会被他自动优化掉的。 模板的应用:编译期分支 • 更进一步,可以用 C++17 的 if constexpr 语法,保证是编译期确定的分支: • 0 码力 | 82 页 | 12.15 MB | 1 年前3
Lock-Free Atomic Shared Pointers Without a Split Reference Count? It Can Be Done!-- danielanderson.net10 Daniel Anderson -- danielanderson.net std::shared_ptr11 Daniel Anderson -- danielanderson.net std::shared_ptr (the basics) T* ptr; shared_ptrs1: control_block: atomic ref_count = 1; … control_block* ctrl; T12 Daniel Anderson -- danielanderson.net std::shared_ptr (the basics) T* ptr; shared_ptr s1: control_block: control_block* ctrl; T T* ptr; danielanderson.net std::shared_ptr (the basics) control_block: T T* ptr; control_block* ctrl; atomic ref_count = 1; … shared_ptr s2:14 Daniel Anderson -- danielanderson.net std::shared_ptr 0 码力 | 45 页 | 5.12 MB | 6 月前3
共 32 条
- 1
- 2
- 3
- 4













