Bringing Existing Code to CUDA Using constexpr and std::pmr
Allocation // 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 | 5 月前3C++23: An Overview of Almost All New and Updated Features
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) 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 | 5 月前3Lock-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 | 5 月前3C++20: An (Almost) Complete Overview
big! Lots of new features! On Friday September 4, 2020, the C++20 standard passed ISO voting, expected to be formally published by the end of 2020.4 Agenda Modules Ranges Coroutines The 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 0 码力 | 85 页 | 512.18 KB | 5 月前3Working with Asynchrony Generically: A Tour of C++ Executors
an 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 | 5 月前3C++高性能并行编程与优化 - 课件 - 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 年前3C++高性能并行编程与优化 - 课件 - 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 年前3C++20's
#includeusing namespace std::chrono; int main() { year y{2021}; std::cout << y << "\n"; month m{October}; auto result = m + months{3}; std::cout << result << "\n"; } Output: using namespace std::chrono; int main() { weekday wd{Thursday}; auto result = wd + days{4}; std::cout << result << "\n"; weekday sun1{0}; weekday sun2{7}; std::cout << sun1 << fourth Thursd ay std::cout << wdi << "\n"; } Output: Mon Sun Sun Thu[4]13 Some examples: Compound Calendrical Types #include #include using namespace std::chrono; int main() 0 码力 | 55 页 | 8.67 MB | 5 月前3C++高性能并行编程与优化 - 课件 - 15 C++ 系列课:字符与字符串
8 位整数更高效”,所以擅自把 char 魔改成无 符号的…… • 顺便一提, C++ 标准保证 char , signed char , unsigned char 是三个完全 不同的类型, std::is_same_v 分别判断他们总会得到 false ,无论 x86 还是 arm 。 • 但是奇葩的 C 语言却规定 short , int , long , long long 必须是有符号的 就像你和同学随手“拉钩”定下的约定,这是 printf 约定俗成的。 • \ 就像正式合同,有法律效力的,这是 C 语言编译器规定好的。 C++ 字符串类 第 3 章 C 语言字符串操作繁琐 封装的 std::string 应运而生 封装的 std::string 应运而生 • string 可以从 const char * 隐式构造: • string s = “hello”; • string 具有 + 、 += 、 C 语言遗 产 • void modern_cpp(std::string name); // 这个函数是现代 C++ ,便民! • void performance_geek(std::string const &name); // 有点追求性能的极客 • void performance_nerd(std::string_view name); //0 码力 | 162 页 | 40.20 MB | 1 年前3C++高性能并行编程与优化 - 课件 - 14 C++ 标准库系列课 - 你所不知道的 set 容器
iterator 包含关系:前向迭代器>双向迭代器>随机访问迭代器 这意味着如果一个 STL 模板函数(比如 std::find )要求迭代器是前向迭代器即可,那么也可 以给他随机访问迭代器,因为前向迭代器是随机访问迭代器的子集。 例如, vector 和 list 都可以调用 std::find ( set 则直接提供了 find 作为成员函数,稍后 讨论) set 和 vector 迭代器的不同点 。虽然低效,但至少可 以用了。 std::next 等价于 + • 但是这样手写三个 ++ 太麻烦了 ,而且是就地操作,会改变迭代 器本身。 • 因此标准库提供了 std::next 函 数,他的内部实现相当于这样: • 没错,他会自动判断迭代器是否 支持 + 运算,如果不支持,会 改为比较低效的调用 n 次 ++ 。 std::advance 等价于 += • 刚刚的 std::next 会返回自增后迭代器 会返回自增后迭代器 。 • 还有 std::advance 会就地自增作为引 用传入的迭代器,他同样会判断是否支 持 += 来决定要采用哪一种实现。 • 区别: advance 就地修改迭代器,没 有返回值; next 修改迭代器后返回, 不会改变原迭代器。 • advance 相当于 += , next 相当于 + 。 next 和 advance 同样支持负数 • next 的第二个参数0 码力 | 83 页 | 10.23 MB | 1 年前3
共 33 条
- 1
- 2
- 3
- 4