- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找一种方便的设计,以便能够在编译时大小未知的设备上使用类。只需将此类的一个实例发送到设备,为此应该调用一次 cudaMalloc 和 cudaMemcpy(理想情况下)。
类的主机版本如下所示:
Class A {
public:
A(int size) : table(size) {
// some useful initialization of table
}
double get(int i) const {
// return some processed element from table
}
private:
std::vector<int> table;
};
内核:
__global__ void kernel(const A *a){
int idx = threadIdx.x + blockDim.x * blockIdx.x;
a->get(idx); // do something useful with it
}
到目前为止,我设计类的设备版本的方式是这样的:
const int sizeMax = 1000;
Class A {
public:
A(int size) {
// size checking + some useful initialization of table
}
__host__ __device__
double get(int i) const {
//
}
private:
int table[sizeMax];
};
和客户端代码:
A a(128);
A* da;
cudaMalloc((void**)&da, sizeof(A));
cudaMemcpy(da, &a, sizeof(A), cudaMemcpyHostToDevice);
kernel<<<1, 32>>>(da);
cudaDeviceSynchronize();
cudaFree(da);
这很丑陋,因为:
是否有任何其他方法可以更简洁地实现相同的目标而不会对性能产生负面影响?明确地说,我只需要类的设备版本,第一个版本只是等效的非 CUDA 代码来说明表大小应该是动态的。
最佳答案
在我的评论中,我说:
- separate host and device storage for table, contained in the class, both of which are allocated dynamically. 2. dynamic allocation of table storage size in the constructor, rather than in your client code. This could also include resizing if necessary. 3. differentiation in class methods to use either the host copy of the data or the device copy (i.e. pointer) to the data, depending on whether the method is being executed in host or device code 4. A method to copy data from host to device or vice versa, as the class context is moved from host to device or vice versa.
这是我的想法的一个例子:
#include <stdio.h>
#include <assert.h>
#include <cuda_runtime_api.h>
#include <iostream>
template <typename T>
class gpuvec{
private:
T *h_vec = NULL;
T *d_vec = NULL;
size_t vsize = 0;
bool iscopy;
public:
__host__ __device__
T * data(){
#ifndef __CUDA_ARCH__
return h_vec;
#else
return d_vec;
#endif
}
__host__ __device__
T& operator[](size_t i) {
assert(i < vsize);
return data()[i];}
void to_device(){
assert(cudaMemcpy(d_vec, h_vec, vsize*sizeof(T), cudaMemcpyHostToDevice) == cudaSuccess);}
void to_host(){
assert(cudaMemcpy(h_vec, d_vec, vsize*sizeof(T), cudaMemcpyDeviceToHost) == cudaSuccess);}
gpuvec(gpuvec &o){
h_vec = o.h_vec;
d_vec = o.d_vec;
vsize = o.vsize;
iscopy = true;}
void copy(gpuvec &o){
free();
iscopy = false;
vsize = o.vsize;
h_vec = (T *)malloc(vsize*sizeof(T));
assert(h_vec != NULL);
assert(cudaMalloc(&d_vec, vsize*sizeof(T)) == cudaSuccess);
memcpy(h_vec, o.h_vec, vsize*sizeof(T));
assert(cudaMemcpy(d_vec, o.d_vec, vsize*sizeof(T), cudaMemcpyDeviceToDevice) == cudaSuccess);}
gpuvec(size_t ds) {
assert(ds > 0);
iscopy = false;
vsize = ds;
h_vec = (T *)malloc(vsize*sizeof(T));
assert(h_vec != NULL);
assert(cudaMalloc(&d_vec, vsize*sizeof(T)) == cudaSuccess);}
gpuvec(){
iscopy = false;
}
~gpuvec(){
if (!iscopy) free();}
void free(){
if (d_vec != NULL) cudaFree(d_vec);
d_vec = NULL;
if (h_vec != NULL) ::free(h_vec);
h_vec = NULL;}
__host__ __device__
size_t size() {
return vsize;}
};
template <typename T>
__global__ void test(gpuvec<T> d){
for (int i = 0; i < d.size(); i++){
d[i] += 1;
}
}
int main(){
size_t ds = 10;
gpuvec<int> A(ds);
A.to_device();
test<<<1,1>>>(A);
A.to_host();
for (size_t i = 0; i < ds; i++)
std::cout << A[i];
std::cout << std::endl;
gpuvec<int> B;
B.copy(A);
A.free();
B.to_device();
test<<<1,1>>>(B);
B.to_host();
for (size_t i = 0; i < ds; i++)
std::cout << B[i];
std::cout << std::endl;
B.free();
}
我敢肯定会有很多批评。这可能不符合任何关于“vector 语法”应该是什么的特定意见。此外,我确信它没有涵盖一些用例,并且可能包含彻底的缺陷。创建一个健壮的主机/设备 vector 实现可能需要与thrust一样多的工作和复杂性。主机和设备 vector 。然而,我并不是说推力 vector 是问题的直接答案。
关于c++ - Cuda C++ 设计 : reusable class with unknown compile-time size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50509600/
哪些原则、代码质量、实践、方法、语言或框架特性可帮助您在更广泛的情况下有效地重用函数、类等。所有情况都很有趣:您可以修改代码的实现和接口(interface)以启用/改进重用,或者只修改实现,或者什么
我很想知道人们如何组织他们的代码库,特别是关于可重用组件。我在下面谈论 OO 术语,但我也对您如何为其他类型的语言组织库感兴趣。 例如: 您是所有类库项目的忠实拥护者,还是更喜欢将所有内容都保存在一个
我经常编写一次性代码(在 research environment 中) - 例如探索科学属性或过程的算法或模型。许多这些“实验”是一次性的,但有时我发现我需要稍后使用。例如,我刚刚发现了我 7 年前
每个人都喜欢谈论可重用性。在我工作的地方,每当有人提出或测试一些新想法时,总是会出现可重用性的问题。 “我们希望最大限度地提高我们在这方面的投资,让它可重复使用。” “可重用性将以更少的工作带来更高的
我认为这是个愚蠢的问题,但是有没有办法将“if”语句用作可重用函数 success: function(msg){ if(msg=='o'){ $j('#ok').s
情况是这样的:我正在开发一个相对较大的 C++ 程序(在 Linux 中)。这个程序可以分为两部分。 A 部分只是一堆基于一些数据的预计算,这些数据随着时间的推移保持不变,构建了一个相当复杂的大数据结
有没有办法提取并参数化 Logback XML 配置的重复部分?我有许多不同的滚动文件附加器,除了文件名之外,配置基本相同。我将其与一堆关闭“可加性”的记录器结合使用,这样我就可以将堆栈的不同部分重定
如果我有一个在两个 Tibco 项目中使用的子流程,重用它的最佳方式是什么? 目前我正在复制两个项目中的子流程,但这会导致代码重复,难以维护。 最佳答案 或者,如果您使用的是 BusinessWork
如果我有一个在两个 Tibco 项目中使用的子流程,重用它的最佳方式是什么? 目前我正在复制两个项目中的子流程,但这会导致代码重复,难以维护。 最佳答案 或者,如果您使用的是 BusinessWork
是否可以制作一个可重用的 xml 代码,其中包含一些 javascript?像导入或包含。我的问题是我需要编写不同的 xml 文件并且几乎都是一样的,改变的只是一些属性。任何最佳实践?谢谢彼得罗 最佳
我是 asp.net 的新手,所以这可能是一个非常基本的问题,但我无法弄明白。 我在互联网上找到了一些连接到数据库的代码。我创建了一个命名空间和一些类,以便在不同的项目中使用相同的代码。 代码和我的类
刚刚开始使用 Angular: 我想创建一个共享配置单例,其中包含例如服务端点的baseURL。 Angular 中的模式是什么? 最佳答案 我建议使用类似常量 https://docs.angula
我正在学习 angularjs 并开发几个应用程序(同时,老板的命令)。 我的所有应用程序都有一些与初始化相关的常见任务,但我不知道如何使这些任务成为可重用模块(是的,也许我是个菜鸟)。我研究了很多,
阅读讨论后 How to create generic/reusable code with Scrum?我想到了我在可重用组件方面的经历。 我创建了几个在其他项目中成功重用的“技术组件”。 我还创建
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 年前。 Improve
在过去的几个小时里,我一直在尝试自己完成这项工作,并使用 Google 寻找解决方案,但无济于事。 因此,如果您能提供帮助,我将不胜感激! 基本上我有一个页面,在“预览模式”下有 3 个独立的传记,它
我正在尝试在 PDO 中创建可重用的代码。 这是我的代码。 $myClass = new main_c(); $condition = "email_address = :email_address
我有一个 JTable,它的行数不断增加(可能是无限多)。避免高内存使用的解决方案是将内容“缓存”到文件中,并根据当前滚动条光标位置从该文件中检索页面/数据 block 。另一个“额外”问题是根据列值
这是问题所在,当我使用 TableViewController 并在选定的单元格上添加行为时。该行为显示两次 我怎样才能避免这种情况? // MARK: - Table Deleget overrid
我正在构建一个网络应用程序,它有一组功能,用户可能会执行几次,但涉及足够多的异步操作,回调会变得有点失控。 $.Deffered 和 $.when 可以多次“使用”的现实替代方案是什么? 我不是在寻找
我是一名优秀的程序员,十分优秀!