我知道OpenCL可以控制GPU的内存架构,因此可以实现更好的优化,但是,除此之外,我们可以使用Compute Shaders进行矢量运算(加法,乘法,反演等)吗?
与实体相比,函数显然不那么冗长.但它意味着许多缺点,包括:
似乎可以递归调用函数.可能不是实体的情况吗?如果是这样,除了美学目的之外,还有什么理由使用功能吗?
我正在GPU上分配一个cl_mem缓冲区并对其进行处理,它可以正常工作,直到超过一定的大小.在这种情况下,分配本身成功,但执行或复制不成功.我确实想要使用设备的内存以便更快地操作,所以我分配如下:
buf = clCreateBuffer (cxGPUContext, CL_MEM_WRITE_ONLY, buf_size, NULL, &ciErrNum);
Run Code Online (Sandbox Code Playgroud)
现在我不明白的是尺寸限制.我正在复制大约16 MB但应该可以使用大约128 MB(参见参考资料CL_DEVICE_MAX_MEM_ALLOC_SIZE).
为什么这些数字差异如此之大?
以下是oclDeviceQuery的一些摘录:
CL_PLATFORM_NAME: NVIDIA
CL_PLATFORM_VERSION: OpenCL 1.0
OpenCL SDK Version: 4788711
CL_DEVICE_NAME: GeForce 8600 GTS
CL_DEVICE_TYPE: CL_DEVICE_TYPE_GPU
CL_DEVICE_ADDRESS_BITS: 32
CL_DEVICE_MAX_MEM_ALLOC_SIZE: 128 MByte
CL_DEVICE_GLOBAL_MEM_SIZE: 255 MByte
CL_DEVICE_LOCAL_MEM_TYPE: local
CL_DEVICE_LOCAL_MEM_SIZE: 16 KByte
CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE: 64 KByte
Run Code Online (Sandbox Code Playgroud) 下面的python代码使用PyOpenCL 用数组b中的元素之和填充数组a_plus_b(这不是我的实际目标,但它是我能找到的最简单的代码仍然显示问题).
import pyopencl as cl
import numpy as np
import numpy.linalg as la
height = 50
width = 32
b = np.arange(width,dtype=np.int32)
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
b_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b)
dest_buf = cl.Buffer(ctx, mf.WRITE_ONLY, height*4)
prg = cl.Program(ctx, """
__kernel void sum(__global const int *b, __global int *c)
{
int x = get_global_id(1);
int y;
c[x] = 0;
for(y=0;y<get_global_size(0);y++) {
c[x] += b[y];
}
}
""").build()
prg.sum(queue, (width,height), None, …Run Code Online (Sandbox Code Playgroud) 有人可以给出一个用例示例"Mediator模式"在现实世界中有用吗?
有没有办法在主机上分配内存,可以直接从GPU访问,而无需复制?
就像CUDA中的cudaHostGetDevicePointer一样.
在nodeJS终端中,我可以输入此表达式并返回'true':
> var x = true; x;
true
Run Code Online (Sandbox Code Playgroud)
如何在不更改表达式的情况下捕获变量中的返回值?
以下不起作用:
> var y = (var x = true; x)
SyntaxError: Unexpected token var
Run Code Online (Sandbox Code Playgroud) OpenCL为cl.h提供了一个只有头文件的C++包装器,称为cl.hpp.它随附一些实现,但也可以从khronos.org获得,因为它只使用OpenCL库.
它包含很多模板和内联的东西,但也有这样的东西:
namespace cl
{
class Buffer : public Memory
{
Buffer (...stuff...)
{
// Constructor implementation
}
// ...
// Other Constructors and such
// ...
Buffer& operator= (const Buffer& rhs)
{
// implementation
}
// ...
Buffer createSubBuffer (...)
{
// implementation
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题是:那些在类定义中但既不是内联也不是模板化的实现不应该违反一个定义规则吗?我在这里想念的是什么?
我得到"decleration没有声明任何[-fpermissive]错误"; 这是我的代码;
#ifndef CAMERA_H
#define CAMERA_H
#include "Vector.h"
#include <string>
using namespace std;
class Camera
{
private:
int id;
float position[3];
Vector gaze;
Vector up;
float left;
float right;
float bottom;
float top;
float near;
float far;
int type;
public:
Camera(int i,string c, string g, string u, string f, string t);
int getID() {return id; }
float* getPosition() { return position; }
Vector getGaze() { return gaze; }
Vector getUp() { return up; }
float getLeft() {return left;}
float getRight() …Run Code Online (Sandbox Code Playgroud)