Calling a C function from Julia and passing a 2D array as a pointer of pointers as argument

mgab

The background

I'm trying to use the ccall Julia function to use code written in C. I know how to pass an array as an argument to a function that expects int *arg. For example, trying to use this C function

void sum_one(int *arr, int len)
{
  for (int i=0; i<len; i++){
    arr[i]++;
  }
}

this Julia code works

x = collect(Cint, 1:5)
ccall((:sum_one, "/path/to/mylib.so"), Void, (Ptr{Cint}, Cint), x, 5)

The problem

It doesn't seem to be so straight forward with C functions that expect a pointer to a pointer (int **arg) to be used as a 2-dimensional matrix. Say this one

void fill_matrix(int **arr, int row, int col)
{
  for (int i=0; i<row; i++){
    for (int j=0; j<col; j++){
      arr[i][j] = arr[i][j] + i + j*10;
    }
  }
}

Here, I needed to create an Array of Arrays so that the C code would accept it:

xx = [zeros(Cint, 5) for i in 1:6]
ccall((:fill_matrix, "/path/to/mylib.so"),
       Void, (Ptr{Ptr{Cint}}, Cint, Cint), xx, 6,5)

But this structure structure is not very convenient from the Julia side.

The question(s)

  • Is there any other way to pass a 2-dimensional matrix to a C function that expects an argument of the type int **arg?
  • If not, how can you transform an already existing 2-dimensional array of Julia to the array of arrays structure of C?
  • and the other way around?
meggart

I will try to answer you questions one by one:

Is there any other way to pass a 2-dimensional matrix to a C function that expects an argument of the type int **arg?

Yes. You have to add a method to julia's cconvert function so that it does the conversion from Matrix{Cint} to Ptr{Ptr{Cint}}. So you define:

Base.cconvert(::Type{Ptr{Ptr{Cint}}},xx2::Matrix{Cint})=Ref{Ptr{Cint}}([Ref(xx2,i) for i=1:size(xx2,1):length(xx2)])

(see next question for explanation) and can afterwards directly pass your matrix to ccall:

xx2=zeros(Cint,5,6)
ccall((:fill_matrix, "mylib.so"),Void, (Ptr{Ptr{Cint}}, Cint, Cint), xx2, 6,5)

However, I would suggest to be very conservative in which cconvert methods you overwrite, because other julia code might expect the original behavior.

If not, how can you transform an already existing 2-dimensional array of Julia to the array of arrays structure of C?

The following should work: You generate an Array of pointers to every column of your matrix, so in julia-0.4:

xx2=zeros(Cint,5,6)
refAr=[Ref(xx2,i) for i=1:size(xx2,1):length(xx2)]
ccall((:fill_matrix, "mylib.so"),Void, (Ptr{Ptr{Cint}}, Cint, Cint), refAr, 6,5)

Now the matrix xx2 is filled by the C function. Note that in julia v0.3 you have to replace Ref(xx2,i) with pointer(xx2,i)

and the other way around?

I don't think this is generally possible. In order to construct a julia 2D array, the data must be in a contiguous block of memory. If you are REALLY confident this is the case you can do:

p=pointer(refAr)  # This is a Ptr{Ptr{Cint}} representing the int**
aa=pointer_to_array(p,6,false)
bb=pointer_to_array(aa[1],(5,6),false)

Which returns the original matrix. Here, the last argument to pointer_to_array determines, if Julia takes the ownership of the array and the data should be freed by Julia's gc.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing a function pointer from an array of function pointers as a template argument

From Dev

(Julia) What is the difference between explicitly passing an array into a function, and calling that array from within an argument-less function?

From Dev

Passing a pointer to a char array as an argument to a function - C

From Dev

passing an 2d array pointer to a function in c++

From Dev

Passing a 2D array as Function Argument

From Dev

C array of pointers to strings, passing string from pointer

From Dev

Pointer to 2D Array Argument (C)

From Dev

standard way to solve "passing argument of function from incompatible pointer type" with function pointers parameter

From Dev

C - Trouble passing array reference to pointer argument in function

From Dev

Passing array of pointers to a function accepting 2d array

From Dev

passing function pointer in c with a pointer argument

From Dev

Passing an array of pointers to function in C

From Dev

How to explain calling a C function from an array of function pointers?

From Dev

passing 2d array of pointers to function giving error

From Dev

Calling C function with array pointer and int pointer from Swift

From Dev

c: pointer errors when passing a 2D array into a function and using helper functions

From Dev

2D array passing via pointers in C

From Dev

passing 2d array to function with in c

From Dev

Passing a pointer to struct as an argument in Julia

From Dev

Passing a pointer to an array in a function in C

From Dev

Passing pointer of an array to a function in C

From Dev

Passing an Array as Argument to Function in C

From Dev

Compiler warning when passing a 2D array as function argument

From Dev

c struct passing itself as an argument into a pointer function

From Dev

Passing a char array as an argument in C turns into a pointer

From Dev

C Warning passing argument 2 of ‘getopt’ from incompatible pointer type

From Dev

Passing a pointer as a function argument

From Dev

Function argument binding rules for passing an array by reference vs passing pointer

From Dev

Invoking function from the 2d array of function pointers

Related Related

  1. 1

    Passing a function pointer from an array of function pointers as a template argument

  2. 2

    (Julia) What is the difference between explicitly passing an array into a function, and calling that array from within an argument-less function?

  3. 3

    Passing a pointer to a char array as an argument to a function - C

  4. 4

    passing an 2d array pointer to a function in c++

  5. 5

    Passing a 2D array as Function Argument

  6. 6

    C array of pointers to strings, passing string from pointer

  7. 7

    Pointer to 2D Array Argument (C)

  8. 8

    standard way to solve "passing argument of function from incompatible pointer type" with function pointers parameter

  9. 9

    C - Trouble passing array reference to pointer argument in function

  10. 10

    Passing array of pointers to a function accepting 2d array

  11. 11

    passing function pointer in c with a pointer argument

  12. 12

    Passing an array of pointers to function in C

  13. 13

    How to explain calling a C function from an array of function pointers?

  14. 14

    passing 2d array of pointers to function giving error

  15. 15

    Calling C function with array pointer and int pointer from Swift

  16. 16

    c: pointer errors when passing a 2D array into a function and using helper functions

  17. 17

    2D array passing via pointers in C

  18. 18

    passing 2d array to function with in c

  19. 19

    Passing a pointer to struct as an argument in Julia

  20. 20

    Passing a pointer to an array in a function in C

  21. 21

    Passing pointer of an array to a function in C

  22. 22

    Passing an Array as Argument to Function in C

  23. 23

    Compiler warning when passing a 2D array as function argument

  24. 24

    c struct passing itself as an argument into a pointer function

  25. 25

    Passing a char array as an argument in C turns into a pointer

  26. 26

    C Warning passing argument 2 of ‘getopt’ from incompatible pointer type

  27. 27

    Passing a pointer as a function argument

  28. 28

    Function argument binding rules for passing an array by reference vs passing pointer

  29. 29

    Invoking function from the 2d array of function pointers

HotTag

Archive