Baunsgaard commented on a change in pull request #874: Add SystemDSContext for 
systemds operations
URL: https://github.com/apache/systemml/pull/874#discussion_r403501389
 
 

 ##########
 File path: src/main/python/systemds/context/systemds_context.py
 ##########
 @@ -0,0 +1,145 @@
+# 
------------------------------------------------------------------------------
+#  Copyright 2020 Graz University of Technology
+#
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+# 
------------------------------------------------------------------------------
+
+__all__ = ["SystemDSContext"]
+
+import os
+import subprocess
+import threading
+from typing import Optional, Sequence, Union, Dict, Tuple, Iterable
+
+import numpy as np
+from py4j.java_gateway import JavaGateway
+from py4j.protocol import Py4JNetworkError
+
+from systemds.matrix import full, seq, federated, Matrix, OperationNode
+from systemds.utils.helpers import get_module_dir
+from systemds.utils.consts import VALID_INPUT_TYPES
+
+PROCESS_LOCK: threading.Lock = threading.Lock()
+PROCESS: Optional[subprocess.Popen] = None
+ACTIVE_PROCESS_CONNECTIONS: int = 0
+
+
+class SystemDSContext(object):
+    """A context with a connection to the java instance with which we execute 
SystemDS operations.
+    If necessary this class might also start a java process which we use for 
the SystemDS operations,
+    before connecting."""
+    _java_gateway: Optional[JavaGateway]
+
+    def __init__(self):
+        global PROCESS_LOCK
+        global PROCESS
+        global ACTIVE_PROCESS_CONNECTIONS
+        # make sure that only we would start a process if necessary and no 
other thread
+        # is killing the process we would connect to
+        PROCESS_LOCK.acquire()
+        try:
+            # attempt connection to manually started java instance
+            self._java_gateway = JavaGateway(eager_load=True)
+        except Py4JNetworkError:
+            # if no java instance is running start it
+            systemds_java_path = os.path.join(get_module_dir(), 
'systemds-java')
+            cp_separator = ':'
+            if os.name == 'nt':  # nt means its Windows
+                cp_separator = ';'
+            lib_cp = os.path.join(systemds_java_path, 'lib', '*')
+            systemds_cp = os.path.join(systemds_java_path, '*')
+            classpath = cp_separator.join([lib_cp, systemds_cp])
+            process = subprocess.Popen(['java', '-cp', classpath, 
'org.tugraz.sysds.pythonapi.PythonDMLScript'],
 
 Review comment:
   :bug: should be apache further more this should make the tests fail because 
it makes the local test fail.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to