I have researched for hours,
- How to access data from pointer in struct from Python with ctypes?
- Using Python Ctypes to pass struct pointer to a DLL function
- Convert C array of pointers to Python array of structures
- etc,
but I have not resolved my problem.
File: test.cu
extern TEST_API int fft_coba1d(cplx in, cplx outfft, cplx outdft, cplx outfftc, unsigned int size) // int argc, char **argv
{
....
checkCudaErrors(cudaHostAlloc((void**) &in.x, nBytes, cudaHostAllocDefault));
checkCudaErrors(cudaHostAlloc((void**) &in.y, nBytes, cudaHostAllocDefault));
checkCudaErrors(cudaHostAlloc((void**) &outfft.x, nBytes, cudaHostAllocDefault));
checkCudaErrors(cudaHostAlloc((void**) &outfft.y, nBytes, cudaHostAllocDefault));
checkCudaErrors(cudaHostAlloc((void**) &outdft.x, nBytes, cudaHostAllocDefault));
checkCudaErrors(cudaHostAlloc((void**) &outdft.y, nBytes, cudaHostAllocDefault));
checkCudaErrors(cudaHostAlloc((void**) &outfftc.x, nBytes, cudaHostAllocDefault));
checkCudaErrors(cudaHostAlloc((void**) &outfftc.y, nBytes, cudaHostAllocDefault));
....
checkCudaErrors(cudaFreeHost(outfft.x));
checkCudaErrors(cudaFreeHost(outfft.y));
checkCudaErrors(cudaFreeHost(outfftc.x));
checkCudaErrors(cudaFreeHost(outfftc.y));
checkCudaErrors(cudaFreeHost(outdft.x));
checkCudaErrors(cudaFreeHost(outdft.y));
}
File: test.h
#define DLL_FILE
#define EXPORT_FUNC
#ifdef DLL_FILE
#ifdef EXPORT_FUNC
#define TEST_API __declspec(dllexport)
#else
#define TEST_API __declspec(dllimport)
#endif
#else
#define TEST_API extern
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
float* x;
float* y;
}cplx;
....
extern TEST_API int fft_coba1d(cplx in, cplx outfft, cplx outdft, cplx outfftc, unsigned int size);
#ifdef __cplusplus
}
#endif
File: test.py
import numpy as np
import ctypes
from ctypes import *
import matplotlib.pyplot as plt
class cplx(Structure):
_fields_ = [("x", POINTER(c_float)),
("y", POINTER(c_float))]
def get_cuda_fft():
dll = ctypes.CDLL('fftcoba.dll')#, mode=ctypes.RTLD_GLOBAL)
func = dll.fft_coba1d
func.argtypes = [cplx, cplx, cplx, cplx, c_uint]
func.restype = c_int
return func
__cuda_fft = get_cuda_fft()
def cuda_fft(a, b, c, d, size):
__cuda_fft(a, b, c, d, size)
if __name__ == '__main__':
size=8
size = int(size)
print size
in_ = cplx()
outfft = cplx()
outdft = cplx()
outfftc = cplx()
in_.x = (c_float * size)(np.array(size, dtype=float))
in_.y = (c_float * size)(np.array(size, dtype=float))
outfft.x = (c_float * size)(np.array(size, dtype=float))
outfft.y = (c_float * size)(np.array(size, dtype=float))
outdft.x = (c_float * size)(np.array(size, dtype=float))
outdft.y = (c_float * size)(np.array(size, dtype=float))
outfftc.x = (c_float * size)(np.array(size, dtype=float))
outfftc.y = (c_float * size)(np.array(size, dtype=float))
cuda_fft(in_, outfft, outdft, outfftc , size)
print in_[:4]
I get this error, Python Stopped Working
How to Pass Structure of array on function that linked to shared library (dll file)?
How to get result from function on shared library?