Shared Arrays

The map function

The multiprocessing package provides a multithreaded version of the map function.

  1. Write a method compute(...) which takes as an argument an int and print it
  2. Using multiprocessing.pool, create a pool of 4 processes
  3. Use the map function so that the compute(...) method is called 10 times by the processes.

Data sharing limitations with Processing

When using the processing module, you are creating processes and not threads. By definition, processes do not share data.

  1. What is the difference between starmap(...) and map(...)
  2. Modify your code from the previous question so that compute(...) now takes an int and an array as parameters
  3. In compute(...) try to modify the array and show it is actually a copy of the original one
  4. Measure the execution time for large arrays and varying chunksize.

Data sharing and Ctypes

It is possible to have shared data structures between processes using the ctypes library. But the array must be shared at the creation of the pool, as a global variable.

  1. What is the purpose of the ctypes library ?
  2. Declare a variable shared_array at the top of your program. Add a init(array) function to your code which store the parameter in the global shared_array variable
  3. Using multiprocessing.Array(...), create an array of 100 int
  4. Modify your pool so it calls the init function using the array you just created as a parameter.
  5. Modify your previous code so that now processes use a shared array.
  6. Verify now that the array is indeed modified
  7. Measure the execution time for large arrays and varying chunksize and compare it with the previous results