Ruby ffi::MemoryPointer equivalent in Python

15 views
Skip to first unread message

Sai Kiran

unread,
May 1, 2017, 6:12:14 AM5/1/17
to python-cffi
Hi,


        I am trying to access C Code in Python,for that I am using Ctypes built-in in Python.

       A function in dll accepts 6 input parameters,Here is my Code.


       Here ,input is a huge array which consists of Complex numbers.

       buffer_size = c_int(len(input))
       real_array = (c_float * buffer_size.value)()
       for i in range(0,len(input)-1):
           real_array[i] = input[i].real
       imag_array = (c_float * buffer_size.value)()
       for i in range(0,len(input)-1):
           imag_array[i] = input[i].imag
       sign = c_int(-1)
       RealOutput = (c_float * buffer_size.value)()
       ImagOutput =  (c_float * buffer_size.value)()


       cfft( real_array ,imag_array,RealOutput,ImagOutput,sign.value, buffer_size.value)

       
       The Inputs passed were received by dll,in dll inputs are manipulated,real values,Imaginary values are stored in pointers RealOutput,ImagOutput respectively.

       while trying to print the data at RealOutput,ImagOutput 

Output:
       pOpReal[1] is -1.#QNAN0
       pOpImag[1] is -1.#QNAN0
       pOpReal[2] is -1.#QNAN0
       pOpImag[2] is -1.#QNAN0
       pOpReal[3] is -1.#QNAN0
       pOpImag[3] is -1.#QNAN0
       pOpReal[4] is -1.#QNAN0
       pOpImag[4] is -1.#QNAN0
       pOpReal[5] is -1.#QNAN0
       pOpImag[5] is -1.#QNAN0


In Ruby:
 
require 'ffi'

       pointers = Array.new( 4 ) { FFI::MemoryPointer.new( :float, size ) }

       * input is huge array of complex numbers

       Copying Input : 
        real_input = input.map { |c| c.to_c.real }
        imag_input = input.map { |c| c.to_c.imag }
        pointers[RealInput].write_array_of_float real_input
        pointers[ImagInput].write_array_of_float imag_input

     Copying Output:

        real_output = pointers[RealOutput].get_array_of_float( 0, buffer_size)
        imag_output = pointers[ImagOutput].get_array_of_float( 0, buffer_size)

Output :
      pOpReal[1] is -0.000063
      pOpImag[1] is -0.005755
      pOpReal[2] is -0.000076
      pOpImag[2] is 0.015815
      pOpReal[3] is 0.010016
      pOpImag[3] is -0.011680
      pOpReal[4] is -0.000109
      pOpImag[4] is -0.006708
      pOpReal[5] is -0.012282

      Successfully got the output.


Finally
1)Is there anything wrong in my approach
2)Is there any way to create MemoryPointers in Python like ruby.


     Thanks in advance.


Armin Rigo

unread,
May 1, 2017, 9:39:41 AM5/1/17
to pytho...@googlegroups.com
Hi Sai,

On 1 May 2017 at 12:12, Sai Kiran <saipri...@gmail.com> wrote:
> I am trying to access C Code in Python,for that I am using Ctypes
> built-in in Python.

This is not the correct place to ask questions about ctypes: try e.g.
on stackoverflow.com.

This list is about cffi, which you may want to (but don't have to) use
instead of ctypes if you like. See the documentation at
http://cffi.readthedocs.io/ .


A bientôt,

Armin.
Reply all
Reply to author
Forward
0 new messages