ComputationalRadiationPhysics / student_project_python_bindings

The student project investigates the performance and memory handling of Python bindings for CUDA C++ code created with pybind11.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add test to verify that the python data is correctly passed to the CUDA C++ code and back

SimeonEhrig opened this issue · comments

From the Python side, we expect a 2D Numpy array containing the image data. For the cuFFT we need a 1D C array. After reconstructing the image, matplot expects a 2D Numpy array again.
The test is to check if we can correctly convert the 2D Numpy array into a 1D C array, send it to the GPU and back, and generate a 2D Numpy array from it again. Since we are working with pure C pointers, we need to make sure that the data has the right structure. For example, numpy could introduce padding into the 2D numpy array.

To write the test, I would suggest pytest.

Possible pseudo test implementation

def test_binding_input_output():
  input = imageio.imread('b.png', as_gray=True)
  output = binding(input) # CUDA C++ code

  # attention, I'm not sure, if numpy array do a element wise comparison
  assert input == output
py::array binding(py::array input){
   std::vector<float> data_1d_input = data_to_1d(input);
   std::vector<float> data_1d_output(data_1d_input.size());

   //... allocate memory
    
   cudaMemcpy(&device_input, data_1d_input.data(), sizeof(data_1d.size()), cudaMemcpyHostToDevice);
   copy_data_from_input_to_output<<<... , ...>>>(device_input, device_output);
   cudaMemcpy(data_1d_ouput.data(), &device_output, sizeof(data_1d_ouput.size()), cudaMemcpyDeviceToHost);

   py::array output = data_to_2d(data_1d_output);
   return output;
}

Solved in 3dc7086
Nice work. Look's like there are no pitfalls at copy data from Python to CUDA via pybind11.