From ada04a42528c29ea86e48f0a19cb8723c8bc0a66 Mon Sep 17 00:00:00 2001
From: David Luevano <55825613+luevano@users.noreply.github.com>
Date: Thu, 12 Dec 2019 20:49:41 -0700
Subject: First working parallelism

---
 c_matrix.py  | 10 +++++--
 lj_matrix.py | 10 +++++--
 main.py      | 97 +++++++++++++++++++++++++++++++++++++++---------------------
 3 files changed, 80 insertions(+), 37 deletions(-)

diff --git a/c_matrix.py b/c_matrix.py
index 2bc4d4c0c..75cc4d8a2 100644
--- a/c_matrix.py
+++ b/c_matrix.py
@@ -30,7 +30,7 @@ from numpy.linalg import eig
 def c_matrix(mol_data,
              nc_data,
              max_len=25,
-             as_eig=False,
+             as_eig=True,
              bohr_radius_units=False):
     """
     Creates the Coulomb Matrix from the molecule data given.
@@ -150,13 +150,16 @@ def c_matrix(mol_data,
 
 def c_matrix_multiple(mol_data,
                       nc_data,
+                      pipe=None,
                       max_len=25,
-                      as_eig=False,
+                      as_eig=True,
                       bohr_radius_units=False):
     """
     Calculates the Coulomb Matrix of multiple molecules.
     mol_data: molecule data, matrix of atom coordinates.
     nc_data: nuclear charge data, array of atom data.
+    pipe: for multiprocessing purposes. Sends the data calculated
+        through a pipe.
     max_len: maximum amount of atoms in molecule.
     as_eig: if data should be returned as matrix or array of eigenvalues.
     bohr_radius_units: if units should be in bohr's radius units.
@@ -167,6 +170,9 @@ def c_matrix_multiple(mol_data,
     cm_data = np.array([c_matrix(mol, nc, max_len, as_eig, bohr_radius_units)
                        for mol, nc in zip(mol_data, nc_data)])
 
+    if pipe:
+        pipe.send(cm_data)
+
     toc = time.perf_counter()
     printc('\tCM calculation took {:.4f} seconds.'.format(toc - tic), 'GREEN')
 
diff --git a/lj_matrix.py b/lj_matrix.py
index 6769bc0c3..55e729c56 100644
--- a/lj_matrix.py
+++ b/lj_matrix.py
@@ -30,7 +30,7 @@ from numpy.linalg import eig
 def lj_matrix(mol_data,
               nc_data,
               max_len=25,
-              as_eig=False,
+              as_eig=True,
               bohr_radius_units=False):
     """
     Creates the Lennard-Jones Matrix from the molecule data given.
@@ -168,13 +168,16 @@ def lj_matrix(mol_data,
 
 def lj_matrix_multiple(mol_data,
                        nc_data,
+                       pipe=None,
                        max_len=25,
-                       as_eig=False,
+                       as_eig=True,
                        bohr_radius_units=False):
     """
     Calculates the Lennard-Jones Matrix of multiple molecules.
     mol_data: molecule data, matrix of atom coordinates.
     nc_data: nuclear charge data, array of atom data.
+    pipe: for multiprocessing purposes. Sends the data calculated
+        through a pipe.
     max_len: maximum amount of atoms in molecule.
     as_eig: if data should be returned as matrix or array of eigenvalues.
     bohr_radius_units: if units should be in bohr's radius units.
@@ -185,6 +188,9 @@ def lj_matrix_multiple(mol_data,
     ljm_data = np.array([lj_matrix(mol, nc, max_len, as_eig, bohr_radius_units)
                         for mol, nc in zip(mol_data, nc_data)])
 
+    if pipe:
+        pipe.send(ljm_data)
+
     toc = time.perf_counter()
     printc('\tL-JM calculation took {:.4f} seconds.'.format(toc-tic), 'GREEN')
 
diff --git a/main.py b/main.py
index 734069920..9d7d3a645 100644
--- a/main.py
+++ b/main.py
@@ -21,40 +21,71 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 """
 import time
-from misc import printc
+from multiprocessing import Process, Pipe
 # import matplotlib.pyplot as plt
+from misc import printc
 from read_qm7_data import read_qm7_data
 from c_matrix import c_matrix_multiple
 from lj_matrix import lj_matrix_multiple
-from do_ml import do_ml
-
-
-# Initialization time.
-init_time = time.perf_counter()
-
-# Data reading.
-zi_data, molecules, nuclear_charge, energy_pbe0, energy_delta =\
-    read_qm7_data()
-
-# Matrices calculation.
-cm_data = c_matrix_multiple(molecules, nuclear_charge, as_eig=True)
-ljm_data = lj_matrix_multiple(molecules, nuclear_charge, as_eig=True)
-
-# ML calculation.
-do_ml(cm_data,
-      energy_pbe0,
-      1000,
-      test_size=100,
-      sigma=1000.0,
-      desc_type='CM')
-do_ml(ljm_data,
-      energy_pbe0,
-      1000,
-      test_size=100,
-      sigma=1000.0,
-      desc_type='L-JM')
-
-# End of program
-end_time = time.perf_counter()
-printc('Program took {:.4f} seconds of runtime.'.format(end_time - init_time),
-       'CYAN')
+# from do_ml import do_ml
+
+
+def main():
+    # Initialization time.
+    init_time = time.perf_counter()
+    procs = []
+    pipes = []
+
+    # Data reading.
+    zi_data, molecules, nuclear_charge, energy_pbe0, energy_delta =\
+        read_qm7_data()
+
+    # Matrices calculation.
+    cm_recv, cm_send = Pipe()
+    pipes.append(cm_send)
+    p1 = Process(target=c_matrix_multiple,
+                 args=(molecules, nuclear_charge, cm_send))
+    procs.append(p1)
+    p1.start()
+
+    ljm_recv, ljm_send = Pipe()
+    pipes.append(ljm_send)
+    p2 = Process(target=lj_matrix_multiple,
+                 args=(molecules, nuclear_charge, ljm_send))
+    procs.append(p2)
+    p2.start()
+
+    cm_data = cm_recv.recv()
+    ljm_data = ljm_recv.recv()
+
+    for pipe, proc in zip(pipes, procs):
+        pipe.close()
+        proc.join()
+
+    print(type(cm_data), cm_data[0])
+    print(type(ljm_data), ljm_data[0])
+
+    """
+    # ML calculation.
+    do_ml(cm_data,
+        energy_pbe0,
+        1000,
+        test_size=100,
+        sigma=1000.0,
+        desc_type='CM')
+    do_ml(ljm_data,
+        energy_pbe0,
+        1000,
+        test_size=100,
+        sigma=1000.0,
+        desc_type='L-JM')
+    """
+
+    # End of program
+    end_time = time.perf_counter()
+    printc('Program took {:.4f} seconds.'.format(end_time - init_time),
+           'CYAN')
+
+
+if __name__ == '__main__':
+    main()
-- 
cgit v1.2.3-70-g09d2