Python Forum
How to parallelize an 2D array?
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to parallelize an 2D array?
#1
Hi guys,

I need to parallelize my code to improve it's velocity. I tried to understand how to do it reading some internet tutorials, but without success. In order to learn how it works, I wrote some basic code with my necessities, as you can see below:  

#!/usr/bin/env python3

import numpy as np
from concurrent.futures import ProcessPoolExecutor

def vet(n):
   p = np.array([0.]*n)

   for i in range(n):
      p[i] = i
   return p

def subprocess(q,func_enter):

   pool = ProcessPoolExecutor(max_workers=2)
   results = pool.map(func_enter,q)

   return results

def sum_elements(p):

   out1 = np.array([0.])

   A = p[0]
   B = p[1]
   C = p[2]
   D = p[3]

   out1 = A + B + C + D

   return out1


P = vet(8) # creation of 1D array to simulate my real input

Q = P.reshape(2,4) # 2D array like a matrix, every line has the information about one model

U = subprocess(Q,sum_elements) # Parallel call
print(U)
My desired output is an array with sum of the elements of every line of the 2D array Q. In other words, its the output of the function sum_elements like this example:

[5   22]
The sum of the results of one "line" of the array Q with another one, can be a good result too. Example:

[27]
Here's my output at this moment:

itertools.chain object at 0x7fc104ab4d30
Thanks for all the help !!
Reply


Messages In This Thread
How to parallelize an 2D array? - by Felipe - May-10-2017, 07:03 PM
RE: How to parallelize an 2D array? - by Mekire - May-10-2017, 07:08 PM
RE: How to parallelize an 2D array? - by Felipe - May-10-2017, 07:18 PM
RE: How to parallelize an 2D array? - by nilamo - May-10-2017, 09:49 PM
RE: How to parallelize an 2D array? - by Felipe - May-10-2017, 10:15 PM
RE: How to parallelize an 2D array? - by nilamo - May-11-2017, 05:08 AM
RE: How to parallelize an 2D array? - by Felipe - May-11-2017, 01:44 PM
RE: How to parallelize an 2D array? - by nilamo - May-11-2017, 04:57 PM
RE: How to parallelize an 2D array? - by Felipe - May-13-2017, 05:11 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020