gpt4 book ai didi

c++ - 在 Julia 中编写和调用 ArrayFire 自定义 C 函数的正确方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:39:15 24 4
gpt4 key购买 nike

我在 Julia 工作,当我使用如下代码时,我需要调用一些使用 ArraFire 库的自定义 C 函数:

  void copy(const af::array &A, af::array &B,size_t length) {
// 2.Obtain the device, context, and queue used by ArrayFire
// 3.Obtain cl_mem references to af::array objects
cl_mem * d_A = A.device<cl_mem>();
cl_mem * d_B = B.device<cl_mem>();

// 4. Load, build, and use your kernels.

// Set arguments and launch your kernels
//kernel is the function build in step 4
clSetKernelArg(kernel, 0, sizeof(cl_mem), d_A);
clSetKernelArg(kernel, 1, sizeof(cl_mem), d_B);
clEnqueueNDRangeKernel(af_queue, kernel, 1, NULL, &length, NULL, 0, NULL, NULL);
// 5. Return control of af::array memory to ArrayFire
A.unlock();
B.unlock();
}

我使用了以下提供的示例作为引用:Interoperability with OpenCL

我在 Julia 中调用这个函数如下:

ccall((:copy,"path/to/dll"),Cvoid(Ref{af_array},Ref{af_array}),Af.arr,Bf.arr)

AfBf 是 ArrayFire 数组,调用按预期工作,问题是当我直接使用 B=A 只是为了测试即

  void copy(const af::array &A, af::array &B,size_t length) {        
B=A;//only to test
}

调用停止在 Julia 中有效,这让我怀疑我是否使用正确的方式编写和调用此函数。

我看到的 Julia 中包含的一些 Arrayfire 函数调用以 af_array 作为参数的函数,这些参数不同于参数 af::array。好吧,我想更改参数,然后我这样做:

void copy(const af_array &dA, af_array &dB,size_t length) {

//this to be able to use A.device and B.device
array A=array(dA);
array B=array(dB);

//steps 2 to 5 in the original code

}

它在 C 或 Julia 中不起作用,问题是如果我想使用 af_array 作为参数,我如何获得设备指针?或者当我在 Julia 中调用它们时,处理这些函数以避免出现问题的正确方法是什么?

提前致谢。

UPD


我在函数内部更改了B=A;:

  void copy(const af::array &A, af::array &B,size_t length) {        
size_t len = A.dims(0);
seq idx(0, len - 1, 1);
af::copy(B, A, idx);
}

并且有效!但是,我仍然怀疑这是不是正确的方法,因为这段代码非常简单。我将使用更复杂的代码,这些代码可能会以类似的方式停止工作。

最佳答案

这不是一个确定的答案,但我认为它显着改进了功能。 af_get_device_ptr 函数是从 af_array 对象获取设备指针的解决方案,编写能够从 Julia 调用的函数的正确方法似乎是那些带有 af_array arguments (See: calling custom C ArrayFire functions in Julia #229) ,由于集成在 ArrayFire.jl 中的函数是这样做的。这是一个简单而完整的示例,说明如何从 Julia 编写和调用函数:

在 C 中


//function for adding ArrayFire arrays   
void AFire::sumaaf(af_array* out , af_array dA, af_array dB) {

//to store the result
af_array dC;
af_copy_array(&dC, dA);

// 2. Obtain the device, context, and queue used by ArrayFire
static cl_context af_context = afcl::getContext();
static cl_device_id af_device_id = afcl::getDeviceId();
static cl_command_queue af_queue = afcl::getQueue();

dim_t _order[4];
af_get_dims(&_order[0], &_order[1], &_order[2], &_order[3], dA);
size_t order = _order[0];

int status = CL_SUCCESS;

// 3. Obtain cl_mem references to af_array objects
cl_mem *d_A = (cl_mem*)clCreateBuffer(af_context,
CL_MEM_READ_ONLY, sizeof(float) * order,
NULL, &status);
af_get_device_ptr((void**)d_A, dA);

cl_mem *d_B = (cl_mem*)clCreateBuffer(af_context,
CL_MEM_READ_ONLY, sizeof(float) * order,
NULL, &status);
af_get_device_ptr((void**)d_B, dB);

cl_mem *d_C = (cl_mem*)clCreateBuffer(af_context,
CL_MEM_WRITE_ONLY, sizeof(float) * order,
NULL, &status);
af_get_device_ptr((void**)d_C, dC);

// 4. Load, build, and use your kernels.
// For the sake of readability, we have omitted error checking.
// A simple sum kernel, uses C++11 syntax for multi-line strings.
const char * kernel_name = "sum_kernel";
const char * source = R"(
void __kernel
sum_kernel(__global float * gC, __global float * gA, __global float * gB)
{
int id = get_global_id(0);
gC[id] = gA[id]+gB[id];
}
)";
// Create the program, build the executable, and extract the entry point
// for the kernel.
cl_program program = clCreateProgramWithSource(af_context, 1, &source, NULL, &status);
status = clBuildProgram(program, 1, &af_device_id, NULL, NULL, NULL);
cl_kernel sumkernel = clCreateKernel(program, kernel_name, &status);
// Set arguments and launch your kernels
clSetKernelArg(sumkernel, 0, sizeof(cl_mem), d_C);
clSetKernelArg(sumkernel, 1, sizeof(cl_mem), d_A);
clSetKernelArg(sumkernel, 2, sizeof(cl_mem), d_B);
clEnqueueNDRangeKernel(af_queue, sumkernel, 1, NULL, &order, NULL, 0, NULL, NULL);

// 5. Return control of af::array memory to ArrayFire
af_unlock_array(dA);
af_unlock_array(dB);
af_unlock_array(dC);

//copy results to output argument
af_copy_array(out, dC);

// ... resume ArrayFire operations
// Because the device pointers, d_x and d_y, were returned to ArrayFire's
// control by the unlock function, there is no need to free them using
// clReleaseMemObject()
}

在 Julia 中调用将是:


function sumaaf(A::AFArray{Float32,1},B::AFArray{Float32,1})
out = ArrayFire.RefValue{af_array}(0);
ccall((:sumaaf,"path/to/dll")
,Cvoid,(Ptr{af_array},af_array,af_array),out,Af.arr,Bf.arr);
AFArray{Float32,1}(out[])
end

关于c++ - 在 Julia 中编写和调用 ArrayFire 自定义 C 函数的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55114657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com