用C++创建一个动态内核调度器
Creating a Dynamic Kernel Dispatcher in C++
同一函数有多个实现,一个SIMD Accelerated,一个Cuda内核,另一个在SYCL中。用户可以使用int参数选择使用哪个内核,0表示Vanilla,1表示SIMD,2表示Cuda内核,3表示SYCL。伪代码如下(我使用的是C++(-
return type function(param1, param2, int device){
switch(device):
case Vanilla:
Normal Code.(Written right over here)
case SIMD:
Calls the SIMD Kernel
case Nvidia :
Calls the Cuda Kernel
case SYCL :
Calls the SYCL kernel
}
所有内核都存在于一个名为kernels的单独文件夹中,其子文件夹为SIMD、Cuda和SYCL。现在,人们不能指望用户拥有Cuda,而是可以运行SYCL(拥有AMD GPU(等等。因此,使用cmake选项,这些文件夹将根据用户指定的条件进行编译。最终目标是创建一个可以由用户安装的库。
因此,我不想包含包含这些内核的文件,而是创建一个动态调度器中间。我怎样才能开始设计同样的东西
TIA
可能不是最好的解决方案,但我认为在安装过程中,我可以编写一个名为installation.h
的文件,如下所示-
#define __SIMD_x86_64__ 1
#define __CUDA__ 1
#define __SYCL__ 1
#define __SIMD_ARM_NEON__ 0
调度器可以有条件地包括来自各种文件夹的头,这些文件夹包含各种设备后端的内核。然后,可以使用一个分支表,根据函数使用一个调度器键来创建一个调度器。
#include "installation.h"
#include <iostream>
#include <unordered_map>
std::unordered_map <std::string, function_pointer> branch_table;
#if defined(__SIMD_x86_64__)
include "SIMD Kernels"
Add Kernels to the Map;
#endif
#if defined(__CUDA__)
include "CUDA Kernels"
Add them to the Map;
#endif
等等。我还没有测试过,可能不是最好的解决方案。但它看起来是有效的。
非常感谢@rodburns的指导。
相关文章:
- 在c++中用vector填充一个简单的动态数组
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 使用动态分配将 char* 复制到另一个字符**
- 动态分配列表 - 创建一个函数,用于删除所有包含偶数值的元素
- 如何删除在另一个函数中初始化的动态分配数组
- 你能在链表中有一个动态数组和它的内存分配吗
- 为什么其中一个斐波那契序列的动态编程实现比另一个更快
- 如何在一个属性中动态存储基元类型
- 如果您为类的一个对象动态分配内存作为参数,会发生什么
- 从另一个动态链接库项目调用静态库函数
- 指向结构中的数组的指针,其中每个字段都是一个动态数组
- 无法在 CMake 中创建动态库并将其链接到另一个动态库?
- 动态创建 std::vector 并将其传递给另一个函数的各种方法
- 如何重载运算符+= 要增加动态数组大小,请在末尾添加一个对象
- 我如何转换一个基于动态的,基于指针的int堆栈,以便将其打印成字符串
- 当一个类在构造函数处动态分配自己时,为什么会发生堆栈溢出而不是 std::bad_alloc
- 如何从另一个类动态获取整数?
- 如何正确删除一行控件并在该位置动态创建一个新控件
- 动态创建一个继承的类,使用STD :: MAP使用基类指针访问
- 处理另一个类中类动态分配的对象数组