在Julia

时间:2019-03-12 05:23:34

标签: c++ julia opencl arrayfire

我在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);
  }

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

1 个答案:

答案 0 :(得分:0)

这不是一个明确的答案,但我认为它可以显着改善功能。 af_get_device_ptr函数是一种从af_array对象获取设备指针的解决方案,编写能够从Julia调用的函数的正确方法似乎是带有af_array参数的函数(请参阅: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()
    }

在朱莉娅中,呼叫将是:


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