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++高性能并行编程与优化 - 课件 - 17 由浅入深学习 map 容器由浅入深学习 map 容器 by 彭于斌( @archibate ) 我负责监督你鞋习 ! 我负责监督你鞋习 ! 本期看点: 用方括号 [ ] 取出 map 元素居然是错误的! 能不能在遍历的同时删除元素?安全吗? emplace , emplace_hint , try_emplace 的区别? 课程安排 1. vector 容器初体验 & 迭代器入门 (BV1qF411T7sd) 2 2. 你所不知道的 set 容器 & 迭代器分类 (BV1m34y157wb) 3. string , string_view , const char * 的爱恨纠葛 (BV1ja411M7Di) 4. 万能的 map 容器全家桶及其妙用举例 ( 本期 ) 5. 函子 functor 与 lambda 表达式知多少 6. 通过实战案例来学习 STL 算法库 7. C++ 标准输入输出流 & 字符串格式化 第一章:读取与写入 我负责监督你鞋习 ! 我负责监督你鞋习 ! map 查找元素的两个接口 • map 提供了两个查找元素的接口,一曰 [] ,二曰 at 。 • 那么他们两个又有什么区别呢?很多新手都分不清他俩,可能只认识 [] 。 读取 map 元素 • mapm; • 读取 map 中指定键值的元素有两种方法。 • val = m[“key”]; 0 码力 | 90 页 | 8.76 MB | 1 年前3
C++23: An Overview of Almost All New and Updated FeaturesLibrary 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) 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() Library 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++高性能并行编程与优化 - 课件 - 14 C++ 标准库系列课 - 你所不知道的 set 容器你所不知道的 set 容 器 by 小彭老师( @archibate ) 课件 & 代码: https://github.com/parallel101/course 上期回顾: https://www.bilibili.com/video/BV1qF411T7sd 课程安排 1. vector 容器初体验 & 迭代器入门 (BV1qF411T7sd) 2. 你所不知道的 set 容器 & 万能的 map 容器全家桶及其妙用举例 5. 函子 functor 与 lambda 表达式知多少 6. 通过实战案例来学习 STL 算法库 7. C++ 标准输入输出流 & 字符串格式化 8. traits 技术,用户自定义迭代器与算法 9. allocator ,内存管理与对象生命周期 set 和 vector 的区别 • 都是能存储一连串数据的容器 。 • 区别 1 : set 会自动给其中的 会保持插入时的顺序。 • 区别 2 : set 会把重复的元素 去除,只保留一个,即去重。 • 区别 3 : vector 中的元素在内 存中是连续的,可以高效地按 索引随机访问, set 则不行。 • 区别 4 : set 中的元素可以高 效地按值查找,而 vector 则 低效。 set 的排序: string 会按“字典序”来排 • set 会从小到大排序,对 int 来 说就是数值的大小比较。那么对0 码力 | 83 页 | 10.23 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
hazard pointer synchronous reclamationremove A from SRC 1 5 4 Safe to use pointer A SRC A hazard pointer is a single-writer multi-reader pointer. set HP to A if SRC == A clear HP 2 3 6 if HP != A HP Safe to delete A A 7 8 SAFE ACCESS retire() noexcept; }; Hazard pointers class hazard_pointer { templateT* protect(const std::atomic & src) noexcept; }; hazard_pointer make_hazard_pointer(); Hazard Pointer Synchronous Reclamation Pointers class Foo : public hazard_pointer_obj_base { /* Foo members */ }; Result read_and_use(std::atomic & src, Func fn) { // Called frequently folly::hazard_pointer h = folly::make_hazard_pointer(); 0 码力 | 31 页 | 856.38 KB | 6 月前3
C++20 STL Features: 1 Year of Development on GitHubFeatures Part 17 Comparing Integers8 Usual Arithmetic Conversions #includeusing namespace std; int main() { cout << boolalpha; short s = -1; unsigned int ui = 1729; cout Functions • GH-621 implemented by Neargye #include #include using namespace std; int main() { cout << boolalpha; short s = -1; unsigned int ui = 1729; cout #include #include #include #include using namespace std; int main() { static constexpr array skipped_extensions{".dll"sv, ".exe"sv, ".obj"sv}; 0 码力 | 45 页 | 702.09 KB | 6 月前3
Working with Asynchrony Generically: A Tour of C++ Executorsthen, when_all, sync_wait, repeat, stop_when, timeout, etc. (not all proposed yet) • A standard set of abstractions (aka, concepts) derived from the algorithms • Efficient interoperability with coroutines 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, scheduler) • A unit of lazy async work (aka, sender) • A completion handler (aka, receiver) • A small, initial set of generic async algorithms: • E.g., then, when_all, sync_wait, let_* • Utilities for integration0 码力 | 121 页 | 7.73 MB | 6 月前3
C++高性能并行编程与优化 - 课件 - Zeno 中的现代 C++ 最佳实践 make_shared(*obj) ,这就实现了拷 贝的多态。 如何批量定义 clone 函数? • 可以定义一个宏 IOBJECT_DEFINE_CLONE ,其内容是 clone 的实现。这里我们用 std::decay_t 快速获取了 this 指针所指向的类型,也就是当前所在类的类型 。 • 宏的缺点是他不遵守命名空间的规则,宏的名 字是全局可见的,不符合 随意转换任意依赖于操作为虚函数。 • 实际上 std::any 也是一个类型擦除的容器…… • 这里我们的 Animal 擦除了 speak 这个成员函数, 而 std::any 实际上是擦除了拷贝构造函数和解构函数 , std::function 则是擦除 operator() 函数。 • 你甚至可以擦除 push_back() ,让 std::string 和 std::vector 等价(留做回家作业)。 成员函数的类(比如这里 的 Phone 类只有 play 函数)就会在实例化的那行出错。 • 注意:这里的 m_inner.speak() 只是一个例子,其实不一定是成员函数,完全可以是 std::sort(m_inner.begin(), m_inner.end()) 之类的任意表达式,只要语义上通过,就可以实例化。 • (把 sort 封装成虚函数,留作回家作业) Zeno 中对 OpenVDB 0 码力 | 54 页 | 3.94 MB | 1 年前3
C++高性能并行编程与优化 - 课件 - 04 从汇编角度看编译器优化成 new/delete 的容器。 简单的代码,比什么优化手段都强。 造成 new/delete 的容器:我是说,内存分配在堆上的容器 • 存储在堆上(妨碍优化): • vector, map, set, string, function, any • unique_ptr, shared_ptr, weak_ptr • 存储在栈上(利于优化): • array, bitset, glm::vec 结论:如果发现编译器放弃了自动优化,可以 用 constexpr 函数迫使编译器进行常量折叠! 不过, constexpr 函数中无法使用非 constexpr 的容器: vector, map, set, string 等 …… constexpr :强迫编译器在编译期求值(续) 发现:会让编译变得很慢,因为这 50000 次迭代是在编译期进行的。 第 2 章:内联 调用外部函数: 。 假定指针是 16 字节对齐的: assume_aligned 如果能保证指针 a 总是对齐到 16 字节,在 GCC 编译器中这样 写: 但这样不通用,因此 C++20 引入了标准化的 std::assume_aligned : movups 变成了 movaps 对齐的读写可能 带来微乎其微的 性能提升…… 数组求和: reduction 的优化 你看懂了吗?没关系!小彭老师也没看0 码力 | 108 页 | 9.47 MB | 1 年前3
共 52 条
- 1
- 2
- 3
- 4
- 5
- 6













