0

How to modify different parts of a numpy array of complex numbers in parallel using python? This question seems to give an answer for numpy array with real coefficients: Is shared readonly data copied to different processes for Python multiprocessing? , but not for complex coefficients.

1 Answer 1

1

You need to view the array containing twice the number of floats floats as complex numbers:

>>> shared_array_base = multiprocessing.Array(ctypes.c_double, 3*3*2)
>>> shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
>>> shared_array = shared_array.view(np.complex128).reshape(3, 3)

The complex number format is [re0, im0, re1, im1, re2, im2, ...]:

>>> shared_array[1,1] = 1+2j
>>> shared_array.base
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  2.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.])
>>> shared_array.base.base
<multiprocessing.sharedctypes.c_double_Array_18 object at 0x7f7c1b5d1f80>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.