0

I am getting a Segmentation Fault, from calling a simple arrayfire constructor.

#include <arrayfire.h>
int main(){
    af_array a;
    int N  = 10;
    dim_t dims = 10;
    af_randu(&a, N, &dims, f32);
    af::array b(a);
    return 0;
}
Dinesh K.
  • 323
  • 5
  • 10

1 Answers1

2

af_randu is C-API function in ArrayFire. Ideally, there is no need for users to be directly calling these functions.

What you would need to call is af::randu(). So your call to randu would be:

af:array b = af::randu(N, f32);

The following is just to answer the question asked, the C-API for af_randu is

AFAPI af_err af_randu(af_array *out,
                      const unsigned ndims,
                      const dim_t *const dims,
                      const af_dtype type 
)

So the second argument is ndims (number of dimensions), which in your case is 1. Hence your call to af_randu is:

af_randu(&a, 1, &dims, f32);

If you were doing a matrix of lets say 10x10, then you would do

dim_t dims[] = {10, 10}
af_randu(&a, 2, dims, f32);

Full disclosure: I am a developer for ArrayFire.

shehzan
  • 331
  • 1
  • 5
  • Thanks a lot for the response. I apologize for the late response. I don't want to generate a random array. Our group is using Afnumpy for a project. We need to expose Afnumpy.d_array.arr object in C++, so that we can write custom CUDA kernels. I am able to get handle to af_array object, but when I try to create af::array object, I get a segmentation fault. – Dinesh K. Oct 03 '16 at 21:39
  • Are you getting a segmentation fault even after correcting the ndims argument as I mentioned? You should use the [array::device()][1] function to get the CUDA device pointer. The C-API for this is [af_get_device_ptr()][2]. [1]: http://arrayfire.org/docs/group__device__func__device.htm [2]: http://arrayfire.org/docs/group__device__func__mem.htm#ga58fda2d491cd27f31108e699b5aef506 – shehzan Oct 04 '16 at 18:19