- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 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)
Af
和 Bf
是 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/
好的,所以我编辑了以下... 只需将以下内容放入我的 custom.css #rt-utility .rt-block {CODE HERE} 但是当我尝试改变... 与 #rt-sideslid
在表格 View 中,我有一个自定义单元格(在界面生成器中高度为 500)。在该单元格中,我有一个 Collection View ,我按 (10,10,10,10) 固定到边缘。但是在 tablev
对于我的无能,我很抱歉,但总的来说,我对 Cocoa、Swift 和面向对象编程还很陌生。我的主要来源是《Cocoa Programming for OS X》(第 5 版),以及 Apple 的充满
我正在使用 meta-tegra 为我的 NVIDIA Jetson Nano 构建自定义图像。我需要 PyTorch,但没有它的配方。我在设备上构建了 PyTorch,并将其打包到设备上的轮子中。现
在 jquery 中使用 $.POST 和 $.GET 时,有没有办法将自定义变量添加到 URL 并发送它们?我尝试了以下方法: $.ajax({type:"POST", url:"file.php?
Traefik 已经默认实现了很多中间件,可以满足大部分我们日常的需求,但是在实际工作中,用户仍然还是有自定义中间件的需求,为解决这个问题,官方推出了一个 Traefik Pilot[1] 的功
我想让我的 CustomTextInputLayout 将 Widget.MaterialComponents.TextInputLayout.OutlinedBox 作为默认样式,无需在 XML 中
我在 ~/.emacs 中有以下自定义函数: (defun xi-rgrep (term) (grep-compute-defaults) (interactive "sSearch Te
我有下表: 考虑到每个月的权重,我的目标是在 5 个月内分散 10,000 个单位。与 10,000 相邻的行是我最好的尝试(我在这上面花了几个小时)。黄色是我所追求的。 我试图用来计算的逻辑如下:计
我的表单中有一个字段,它是文件类型。当用户点击保存图标时,我想自然地将文件上传到服务器并将文件名保存在数据库中。我尝试通过回显文件名来测试它,但它似乎不起作用。另外,如何将文件名添加到数据库中?是在模
我有一个 python 脚本来发送电子邮件,它工作得很好,但问题是当我检查我的电子邮件收件箱时。 我希望该用户名是自定义用户名,而不是整个电子邮件地址。 最佳答案 发件人地址应该使用的格式是: You
我想减小 ggcorrplot 中标记的大小,并减少文本和绘图之间的空间。 library(ggcorrplot) data(mtcars) corr <- round(cor(mtcars), 1)
GTK+ noob 问题在这里: 是否可以自定义 GtkFileChooserButton 或 GtkFileChooserDialog 以删除“位置”部分(左侧)和顶部的“位置”输入框? 我实际上要
我正在尝试在主页上使用 ajax 在 magento 中使用 ajax 显示流行的产品列表,我可以为 5 或“N”个产品执行此操作,但我想要的是将分页工具栏与结果集一起添加. 这是我添加的以显示流行产
我正在尝试使用 PasswordResetForm 内置函数。 由于我想要自定义表单字段,因此我编写了自己的表单: class FpasswordForm(PasswordResetForm):
据我了解,新的 Angular 7 提供了拖放功能。我搜索了有关 DnD 的 Tree 组件,但没有找到与树相关的内容。 我在 Stackblitz 上找到的一个工作示例.对比drag'ndrop功能
我必须开发一个自定义选项卡控件并决定使用 WPF/XAML 创建它,因为我无论如何都打算学习它。完成后应该是这样的: 到目前为止,我取得了很好的进展,但还有两个问题: 只有第一个/最后一个标签项应该有
我要定制xtable用于导出到 LaTeX。我知道有些问题是关于 xtable在这里,但我找不到我要找的具体东西。 以下是我的表的外观示例: my.table <- data.frame(Specif
用ejs在这里显示日期 它给我结果 Tue Feb 02 2016 16:02:24 GMT+0530 (IST) 但是我需要表现为 19th January, 2016 如何在ejs中执行此操作?
我想问在 JavaFX 中使用自定义对象制作 ListView 的最佳方法,我想要一个每个项目如下所示的列表: 我搜了一下,发现大部分人都是用细胞工厂的方法来做的。有没有其他办法?例如使用客户 fxm
我是一名优秀的程序员,十分优秀!